1
0

entity.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package entity
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "strings"
  6. "time"
  7. "x-ui/util/common"
  8. )
  9. type Msg struct {
  10. Success bool `json:"success"`
  11. Msg string `json:"msg"`
  12. Obj interface{} `json:"obj"`
  13. }
  14. type AllSetting struct {
  15. WebListen string `json:"webListen" form:"webListen"`
  16. WebDomain string `json:"webDomain" form:"webDomain"`
  17. WebPort int `json:"webPort" form:"webPort"`
  18. WebCertFile string `json:"webCertFile" form:"webCertFile"`
  19. WebKeyFile string `json:"webKeyFile" form:"webKeyFile"`
  20. WebBasePath string `json:"webBasePath" form:"webBasePath"`
  21. SessionMaxAge int `json:"sessionMaxAge" form:"sessionMaxAge"`
  22. PageSize int `json:"pageSize" form:"pageSize"`
  23. ExpireDiff int `json:"expireDiff" form:"expireDiff"`
  24. TrafficDiff int `json:"trafficDiff" form:"trafficDiff"`
  25. RemarkModel string `json:"remarkModel" form:"remarkModel"`
  26. TgBotEnable bool `json:"tgBotEnable" form:"tgBotEnable"`
  27. TgBotToken string `json:"tgBotToken" form:"tgBotToken"`
  28. TgBotProxy string `json:"tgBotProxy" form:"tgBotProxy"`
  29. TgBotAPIServer string `json:"tgBotAPIServer" form:"tgBotAPIServer"`
  30. TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"`
  31. TgRunTime string `json:"tgRunTime" form:"tgRunTime"`
  32. TgBotBackup bool `json:"tgBotBackup" form:"tgBotBackup"`
  33. TgBotLoginNotify bool `json:"tgBotLoginNotify" form:"tgBotLoginNotify"`
  34. TgCpu int `json:"tgCpu" form:"tgCpu"`
  35. TgLang string `json:"tgLang" form:"tgLang"`
  36. TimeLocation string `json:"timeLocation" form:"timeLocation"`
  37. SecretEnable bool `json:"secretEnable" form:"secretEnable"`
  38. SubEnable bool `json:"subEnable" form:"subEnable"`
  39. SubListen string `json:"subListen" form:"subListen"`
  40. SubPort int `json:"subPort" form:"subPort"`
  41. SubPath string `json:"subPath" form:"subPath"`
  42. SubDomain string `json:"subDomain" form:"subDomain"`
  43. SubCertFile string `json:"subCertFile" form:"subCertFile"`
  44. SubKeyFile string `json:"subKeyFile" form:"subKeyFile"`
  45. SubUpdates int `json:"subUpdates" form:"subUpdates"`
  46. ExternalTrafficInformEnable bool `json:"externalTrafficInformEnable" form:"externalTrafficInformEnable"`
  47. ExternalTrafficInformURI string `json:"externalTrafficInformURI" form:"externalTrafficInformURI"`
  48. SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"`
  49. SubShowInfo bool `json:"subShowInfo" form:"subShowInfo"`
  50. SubURI string `json:"subURI" form:"subURI"`
  51. SubJsonPath string `json:"subJsonPath" form:"subJsonPath"`
  52. SubJsonURI string `json:"subJsonURI" form:"subJsonURI"`
  53. SubJsonFragment string `json:"subJsonFragment" form:"subJsonFragment"`
  54. SubJsonNoises string `json:"subJsonNoises" form:"subJsonNoises"`
  55. SubJsonMux string `json:"subJsonMux" form:"subJsonMux"`
  56. SubJsonRules string `json:"subJsonRules" form:"subJsonRules"`
  57. Datepicker string `json:"datepicker" form:"datepicker"`
  58. }
  59. func (s *AllSetting) CheckValid() error {
  60. if s.WebListen != "" {
  61. ip := net.ParseIP(s.WebListen)
  62. if ip == nil {
  63. return common.NewError("web listen is not valid ip:", s.WebListen)
  64. }
  65. }
  66. if s.SubListen != "" {
  67. ip := net.ParseIP(s.SubListen)
  68. if ip == nil {
  69. return common.NewError("Sub listen is not valid ip:", s.SubListen)
  70. }
  71. }
  72. if s.WebPort <= 0 || s.WebPort > 65535 {
  73. return common.NewError("web port is not a valid port:", s.WebPort)
  74. }
  75. if s.SubPort <= 0 || s.SubPort > 65535 {
  76. return common.NewError("Sub port is not a valid port:", s.SubPort)
  77. }
  78. if (s.SubPort == s.WebPort) && (s.WebListen == s.SubListen) {
  79. return common.NewError("Sub and Web could not use same ip:port, ", s.SubListen, ":", s.SubPort, " & ", s.WebListen, ":", s.WebPort)
  80. }
  81. if s.WebCertFile != "" || s.WebKeyFile != "" {
  82. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  83. if err != nil {
  84. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  85. }
  86. }
  87. if s.SubCertFile != "" || s.SubKeyFile != "" {
  88. _, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
  89. if err != nil {
  90. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
  91. }
  92. }
  93. if !strings.HasPrefix(s.WebBasePath, "/") {
  94. s.WebBasePath = "/" + s.WebBasePath
  95. }
  96. if !strings.HasSuffix(s.WebBasePath, "/") {
  97. s.WebBasePath += "/"
  98. }
  99. if !strings.HasPrefix(s.SubPath, "/") {
  100. s.SubPath = "/" + s.SubPath
  101. }
  102. if !strings.HasSuffix(s.SubPath, "/") {
  103. s.SubPath += "/"
  104. }
  105. if !strings.HasPrefix(s.SubJsonPath, "/") {
  106. s.SubJsonPath = "/" + s.SubJsonPath
  107. }
  108. if !strings.HasSuffix(s.SubJsonPath, "/") {
  109. s.SubJsonPath += "/"
  110. }
  111. _, err := time.LoadLocation(s.TimeLocation)
  112. if err != nil {
  113. return common.NewError("time location not exist:", s.TimeLocation)
  114. }
  115. return nil
  116. }