entity.go 2.5 KB

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