entity.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. TgBotEnable bool `json:"tgBotEnable" form:"tgBotEnable"`
  26. TgBotToken string `json:"tgBotToken" form:"tgBotToken"`
  27. TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"`
  28. TgRunTime string `json:"tgRunTime" form:"tgRunTime"`
  29. TgBotBackup bool `json:"tgBotBackup" form:"tgBotBackup"`
  30. TgBotLoginNotify bool `json:"tgBotLoginNotify" form:"tgBotLoginNotify"`
  31. TgCpu int `json:"tgCpu" form:"tgCpu"`
  32. TgLang string `json:"tgLang" form:"tgLang"`
  33. TimeLocation string `json:"timeLocation" form:"timeLocation"`
  34. SecretEnable bool `json:"secretEnable" form:"secretEnable"`
  35. SubEnable bool `json:"subEnable" form:"subEnable"`
  36. SubListen string `json:"subListen" form:"subListen"`
  37. SubPort int `json:"subPort" form:"subPort"`
  38. SubPath string `json:"subPath" form:"subPath"`
  39. SubDomain string `json:"subDomain" form:"subDomain"`
  40. SubCertFile string `json:"subCertFile" form:"subCertFile"`
  41. SubKeyFile string `json:"subKeyFile" form:"subKeyFile"`
  42. SubUpdates int `json:"subUpdates" form:"subUpdates"`
  43. SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"`
  44. SubShowInfo bool `json:"subShowInfo" form:"subShowInfo"`
  45. SubURI string `json:"subURI" form:"subURI"`
  46. }
  47. func (s *AllSetting) CheckValid() error {
  48. if s.WebListen != "" {
  49. ip := net.ParseIP(s.WebListen)
  50. if ip == nil {
  51. return common.NewError("web listen is not valid ip:", s.WebListen)
  52. }
  53. }
  54. if s.SubListen != "" {
  55. ip := net.ParseIP(s.SubListen)
  56. if ip == nil {
  57. return common.NewError("Sub listen is not valid ip:", s.SubListen)
  58. }
  59. }
  60. if s.WebPort <= 0 || s.WebPort > 65535 {
  61. return common.NewError("web port is not a valid port:", s.WebPort)
  62. }
  63. if s.SubPort <= 0 || s.SubPort > 65535 {
  64. return common.NewError("Sub port is not a valid port:", s.SubPort)
  65. }
  66. if s.SubPort == s.WebPort {
  67. return common.NewError("Sub and Web could not use same port:", s.SubPort)
  68. }
  69. if s.WebCertFile != "" || s.WebKeyFile != "" {
  70. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  71. if err != nil {
  72. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  73. }
  74. }
  75. if s.SubCertFile != "" || s.SubKeyFile != "" {
  76. _, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
  77. if err != nil {
  78. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
  79. }
  80. }
  81. if !strings.HasPrefix(s.WebBasePath, "/") {
  82. s.WebBasePath = "/" + s.WebBasePath
  83. }
  84. if !strings.HasSuffix(s.WebBasePath, "/") {
  85. s.WebBasePath += "/"
  86. }
  87. if !strings.HasPrefix(s.SubPath, "/") {
  88. s.SubPath = "/" + s.SubPath
  89. }
  90. if !strings.HasSuffix(s.SubPath, "/") {
  91. s.SubPath += "/"
  92. }
  93. _, err := time.LoadLocation(s.TimeLocation)
  94. if err != nil {
  95. return common.NewError("time location not exist:", s.TimeLocation)
  96. }
  97. return nil
  98. }