entity.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. WebPort int `json:"webPort" form:"webPort"`
  28. WebCertFile string `json:"webCertFile" form:"webCertFile"`
  29. WebKeyFile string `json:"webKeyFile" form:"webKeyFile"`
  30. WebBasePath string `json:"webBasePath" form:"webBasePath"`
  31. SessionMaxAge int `json:"sessionMaxAge" form:"sessionMaxAge"`
  32. ExpireDiff int `json:"expireDiff" form:"expireDiff"`
  33. TrafficDiff int `json:"trafficDiff" form:"trafficDiff"`
  34. TgBotEnable bool `json:"tgBotEnable" form:"tgBotEnable"`
  35. TgBotToken string `json:"tgBotToken" form:"tgBotToken"`
  36. TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"`
  37. TgRunTime string `json:"tgRunTime" form:"tgRunTime"`
  38. TgBotBackup bool `json:"tgBotBackup" form:"tgBotBackup"`
  39. TgCpu int `json:"tgCpu" form:"tgCpu"`
  40. XrayTemplateConfig string `json:"xrayTemplateConfig" form:"xrayTemplateConfig"`
  41. TimeLocation string `json:"timeLocation" form:"timeLocation"`
  42. SecretEnable bool `json:"secretEnable" form:"secretEnable"`
  43. }
  44. func (s *AllSetting) CheckValid() error {
  45. if s.WebListen != "" {
  46. ip := net.ParseIP(s.WebListen)
  47. if ip == nil {
  48. return common.NewError("web listen is not valid ip:", s.WebListen)
  49. }
  50. }
  51. if s.WebPort <= 0 || s.WebPort > 65535 {
  52. return common.NewError("web port is not a valid port:", s.WebPort)
  53. }
  54. if s.WebCertFile != "" || s.WebKeyFile != "" {
  55. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  56. if err != nil {
  57. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  58. }
  59. }
  60. if !strings.HasPrefix(s.WebBasePath, "/") {
  61. s.WebBasePath = "/" + s.WebBasePath
  62. }
  63. if !strings.HasSuffix(s.WebBasePath, "/") {
  64. s.WebBasePath += "/"
  65. }
  66. xrayConfig := &xray.Config{}
  67. err := json.Unmarshal([]byte(s.XrayTemplateConfig), xrayConfig)
  68. if err != nil {
  69. return common.NewError("xray template config invalid:", err)
  70. }
  71. _, err = time.LoadLocation(s.TimeLocation)
  72. if err != nil {
  73. return common.NewError("time location not exist:", s.TimeLocation)
  74. }
  75. return nil
  76. }