entity.go 4.1 KB

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