entity.go 5.5 KB

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