entity.go 3.7 KB

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