entity.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Package entity defines data structures and entities used by the web layer of the 3x-ui panel.
  2. package entity
  3. import (
  4. "crypto/tls"
  5. "math"
  6. "net"
  7. "strings"
  8. "time"
  9. "github.com/mhsanaei/3x-ui/v2/util/common"
  10. )
  11. // Msg represents a standard API response message with success status, message text, and optional data object.
  12. type Msg struct {
  13. Success bool `json:"success"` // Indicates if the operation was successful
  14. Msg string `json:"msg"` // Response message text
  15. Obj any `json:"obj"` // Optional data object
  16. }
  17. // AllSetting contains all configuration settings for the 3x-ui panel including web server, Telegram bot, and subscription settings.
  18. type AllSetting struct {
  19. // Web server settings
  20. WebListen string `json:"webListen" form:"webListen"` // Web server listen IP address
  21. WebDomain string `json:"webDomain" form:"webDomain"` // Web server domain for domain validation
  22. WebPort int `json:"webPort" form:"webPort"` // Web server port number
  23. WebCertFile string `json:"webCertFile" form:"webCertFile"` // Path to SSL certificate file for web server
  24. WebKeyFile string `json:"webKeyFile" form:"webKeyFile"` // Path to SSL private key file for web server
  25. WebBasePath string `json:"webBasePath" form:"webBasePath"` // Base path for web panel URLs
  26. SessionMaxAge int `json:"sessionMaxAge" form:"sessionMaxAge"` // Session maximum age in minutes
  27. // UI settings
  28. PageSize int `json:"pageSize" form:"pageSize"` // Number of items per page in lists
  29. ExpireDiff int `json:"expireDiff" form:"expireDiff"` // Expiration warning threshold in days
  30. TrafficDiff int `json:"trafficDiff" form:"trafficDiff"` // Traffic warning threshold percentage
  31. RemarkModel string `json:"remarkModel" form:"remarkModel"` // Remark model pattern for inbounds
  32. Datepicker string `json:"datepicker" form:"datepicker"` // Date picker format
  33. // Telegram bot settings
  34. TgBotEnable bool `json:"tgBotEnable" form:"tgBotEnable"` // Enable Telegram bot notifications
  35. TgBotToken string `json:"tgBotToken" form:"tgBotToken"` // Telegram bot token
  36. TgBotProxy string `json:"tgBotProxy" form:"tgBotProxy"` // Proxy URL for Telegram bot
  37. TgBotAPIServer string `json:"tgBotAPIServer" form:"tgBotAPIServer"` // Custom API server for Telegram bot
  38. TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"` // Telegram chat ID for notifications
  39. TgRunTime string `json:"tgRunTime" form:"tgRunTime"` // Cron schedule for Telegram notifications
  40. TgBotBackup bool `json:"tgBotBackup" form:"tgBotBackup"` // Enable database backup via Telegram
  41. TgBotLoginNotify bool `json:"tgBotLoginNotify" form:"tgBotLoginNotify"` // Send login notifications
  42. TgCpu int `json:"tgCpu" form:"tgCpu"` // CPU usage threshold for alerts
  43. TgLang string `json:"tgLang" form:"tgLang"` // Telegram bot language
  44. // Security settings
  45. TimeLocation string `json:"timeLocation" form:"timeLocation"` // Time zone location
  46. TwoFactorEnable bool `json:"twoFactorEnable" form:"twoFactorEnable"` // Enable two-factor authentication
  47. TwoFactorToken string `json:"twoFactorToken" form:"twoFactorToken"` // Two-factor authentication token
  48. // Subscription server settings
  49. SubEnable bool `json:"subEnable" form:"subEnable"` // Enable subscription server
  50. SubJsonEnable bool `json:"subJsonEnable" form:"subJsonEnable"` // Enable JSON subscription endpoint
  51. SubTitle string `json:"subTitle" form:"subTitle"` // Subscription title
  52. SubListen string `json:"subListen" form:"subListen"` // Subscription server listen IP
  53. SubPort int `json:"subPort" form:"subPort"` // Subscription server port
  54. SubPath string `json:"subPath" form:"subPath"` // Base path for subscription URLs
  55. SubDomain string `json:"subDomain" form:"subDomain"` // Domain for subscription server validation
  56. SubCertFile string `json:"subCertFile" form:"subCertFile"` // SSL certificate file for subscription server
  57. SubKeyFile string `json:"subKeyFile" form:"subKeyFile"` // SSL private key file for subscription server
  58. SubUpdates int `json:"subUpdates" form:"subUpdates"` // Subscription update interval in minutes
  59. ExternalTrafficInformEnable bool `json:"externalTrafficInformEnable" form:"externalTrafficInformEnable"` // Enable external traffic reporting
  60. ExternalTrafficInformURI string `json:"externalTrafficInformURI" form:"externalTrafficInformURI"` // URI for external traffic reporting
  61. SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"` // Encrypt subscription responses
  62. SubShowInfo bool `json:"subShowInfo" form:"subShowInfo"` // Show client information in subscriptions
  63. SubURI string `json:"subURI" form:"subURI"` // Subscription server URI
  64. SubJsonPath string `json:"subJsonPath" form:"subJsonPath"` // Path for JSON subscription endpoint
  65. SubJsonURI string `json:"subJsonURI" form:"subJsonURI"` // JSON subscription server URI
  66. SubJsonFragment string `json:"subJsonFragment" form:"subJsonFragment"` // JSON subscription fragment configuration
  67. SubJsonNoises string `json:"subJsonNoises" form:"subJsonNoises"` // JSON subscription noise configuration
  68. SubJsonMux string `json:"subJsonMux" form:"subJsonMux"` // JSON subscription mux configuration
  69. SubJsonRules string `json:"subJsonRules" form:"subJsonRules"` // JSON subscription routing rules
  70. }
  71. // CheckValid validates all settings in the AllSetting struct, checking IP addresses, ports, SSL certificates, and other configuration values.
  72. func (s *AllSetting) CheckValid() error {
  73. if s.WebListen != "" {
  74. ip := net.ParseIP(s.WebListen)
  75. if ip == nil {
  76. return common.NewError("web listen is not valid ip:", s.WebListen)
  77. }
  78. }
  79. if s.SubListen != "" {
  80. ip := net.ParseIP(s.SubListen)
  81. if ip == nil {
  82. return common.NewError("Sub listen is not valid ip:", s.SubListen)
  83. }
  84. }
  85. if s.WebPort <= 0 || s.WebPort > math.MaxUint16 {
  86. return common.NewError("web port is not a valid port:", s.WebPort)
  87. }
  88. if s.SubPort <= 0 || s.SubPort > math.MaxUint16 {
  89. return common.NewError("Sub port is not a valid port:", s.SubPort)
  90. }
  91. if (s.SubPort == s.WebPort) && (s.WebListen == s.SubListen) {
  92. return common.NewError("Sub and Web could not use same ip:port, ", s.SubListen, ":", s.SubPort, " & ", s.WebListen, ":", s.WebPort)
  93. }
  94. if s.WebCertFile != "" || s.WebKeyFile != "" {
  95. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  96. if err != nil {
  97. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  98. }
  99. }
  100. if s.SubCertFile != "" || s.SubKeyFile != "" {
  101. _, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
  102. if err != nil {
  103. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
  104. }
  105. }
  106. if !strings.HasPrefix(s.WebBasePath, "/") {
  107. s.WebBasePath = "/" + s.WebBasePath
  108. }
  109. if !strings.HasSuffix(s.WebBasePath, "/") {
  110. s.WebBasePath += "/"
  111. }
  112. if !strings.HasPrefix(s.SubPath, "/") {
  113. s.SubPath = "/" + s.SubPath
  114. }
  115. if !strings.HasSuffix(s.SubPath, "/") {
  116. s.SubPath += "/"
  117. }
  118. if !strings.HasPrefix(s.SubJsonPath, "/") {
  119. s.SubJsonPath = "/" + s.SubJsonPath
  120. }
  121. if !strings.HasSuffix(s.SubJsonPath, "/") {
  122. s.SubJsonPath += "/"
  123. }
  124. _, err := time.LoadLocation(s.TimeLocation)
  125. if err != nil {
  126. return common.NewError("time location not exist:", s.TimeLocation)
  127. }
  128. return nil
  129. }