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