1
0

entity.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. SubSupportUrl string `json:"subSupportUrl" form:"subSupportUrl"` // Subscription support URL
  53. SubProfileUrl string `json:"subProfileUrl" form:"subProfileUrl"` // Subscription profile URL
  54. SubAnnounce string `json:"subAnnounce" form:"subAnnounce"` // Subscription announce
  55. SubEnableRouting bool `json:"subEnableRouting" form:"subEnableRouting"` // Enable routing for subscription
  56. SubRoutingRules string `json:"subRoutingRules" form:"subRoutingRules"` // Subscription global routing rules (Only for Happ)
  57. SubListen string `json:"subListen" form:"subListen"` // Subscription server listen IP
  58. SubPort int `json:"subPort" form:"subPort"` // Subscription server port
  59. SubPath string `json:"subPath" form:"subPath"` // Base path for subscription URLs
  60. SubDomain string `json:"subDomain" form:"subDomain"` // Domain for subscription server validation
  61. SubCertFile string `json:"subCertFile" form:"subCertFile"` // SSL certificate file for subscription server
  62. SubKeyFile string `json:"subKeyFile" form:"subKeyFile"` // SSL private key file for subscription server
  63. SubUpdates int `json:"subUpdates" form:"subUpdates"` // Subscription update interval in minutes
  64. ExternalTrafficInformEnable bool `json:"externalTrafficInformEnable" form:"externalTrafficInformEnable"` // Enable external traffic reporting
  65. ExternalTrafficInformURI string `json:"externalTrafficInformURI" form:"externalTrafficInformURI"` // URI for external traffic reporting
  66. SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"` // Encrypt subscription responses
  67. SubShowInfo bool `json:"subShowInfo" form:"subShowInfo"` // Show client information in subscriptions
  68. SubURI string `json:"subURI" form:"subURI"` // Subscription server URI
  69. SubJsonPath string `json:"subJsonPath" form:"subJsonPath"` // Path for JSON subscription endpoint
  70. SubJsonURI string `json:"subJsonURI" form:"subJsonURI"` // JSON subscription server URI
  71. SubJsonFragment string `json:"subJsonFragment" form:"subJsonFragment"` // JSON subscription fragment configuration
  72. SubJsonNoises string `json:"subJsonNoises" form:"subJsonNoises"` // JSON subscription noise configuration
  73. SubJsonMux string `json:"subJsonMux" form:"subJsonMux"` // JSON subscription mux configuration
  74. SubJsonRules string `json:"subJsonRules" form:"subJsonRules"`
  75. // LDAP settings
  76. LdapEnable bool `json:"ldapEnable" form:"ldapEnable"`
  77. LdapHost string `json:"ldapHost" form:"ldapHost"`
  78. LdapPort int `json:"ldapPort" form:"ldapPort"`
  79. LdapUseTLS bool `json:"ldapUseTLS" form:"ldapUseTLS"`
  80. LdapBindDN string `json:"ldapBindDN" form:"ldapBindDN"`
  81. LdapPassword string `json:"ldapPassword" form:"ldapPassword"`
  82. LdapBaseDN string `json:"ldapBaseDN" form:"ldapBaseDN"`
  83. LdapUserFilter string `json:"ldapUserFilter" form:"ldapUserFilter"`
  84. LdapUserAttr string `json:"ldapUserAttr" form:"ldapUserAttr"` // e.g., mail or uid
  85. LdapVlessField string `json:"ldapVlessField" form:"ldapVlessField"`
  86. LdapSyncCron string `json:"ldapSyncCron" form:"ldapSyncCron"`
  87. // Generic flag configuration
  88. LdapFlagField string `json:"ldapFlagField" form:"ldapFlagField"`
  89. LdapTruthyValues string `json:"ldapTruthyValues" form:"ldapTruthyValues"`
  90. LdapInvertFlag bool `json:"ldapInvertFlag" form:"ldapInvertFlag"`
  91. LdapInboundTags string `json:"ldapInboundTags" form:"ldapInboundTags"`
  92. LdapAutoCreate bool `json:"ldapAutoCreate" form:"ldapAutoCreate"`
  93. LdapAutoDelete bool `json:"ldapAutoDelete" form:"ldapAutoDelete"`
  94. LdapDefaultTotalGB int `json:"ldapDefaultTotalGB" form:"ldapDefaultTotalGB"`
  95. LdapDefaultExpiryDays int `json:"ldapDefaultExpiryDays" form:"ldapDefaultExpiryDays"`
  96. LdapDefaultLimitIP int `json:"ldapDefaultLimitIP" form:"ldapDefaultLimitIP"`
  97. // JSON subscription routing rules
  98. }
  99. // CheckValid validates all settings in the AllSetting struct, checking IP addresses, ports, SSL certificates, and other configuration values.
  100. func (s *AllSetting) CheckValid() error {
  101. if s.WebListen != "" {
  102. ip := net.ParseIP(s.WebListen)
  103. if ip == nil {
  104. return common.NewError("web listen is not valid ip:", s.WebListen)
  105. }
  106. }
  107. if s.SubListen != "" {
  108. ip := net.ParseIP(s.SubListen)
  109. if ip == nil {
  110. return common.NewError("Sub listen is not valid ip:", s.SubListen)
  111. }
  112. }
  113. if s.WebPort <= 0 || s.WebPort > math.MaxUint16 {
  114. return common.NewError("web port is not a valid port:", s.WebPort)
  115. }
  116. if s.SubPort <= 0 || s.SubPort > math.MaxUint16 {
  117. return common.NewError("Sub port is not a valid port:", s.SubPort)
  118. }
  119. if (s.SubPort == s.WebPort) && (s.WebListen == s.SubListen) {
  120. return common.NewError("Sub and Web could not use same ip:port, ", s.SubListen, ":", s.SubPort, " & ", s.WebListen, ":", s.WebPort)
  121. }
  122. if s.WebCertFile != "" || s.WebKeyFile != "" {
  123. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  124. if err != nil {
  125. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  126. }
  127. }
  128. if s.SubCertFile != "" || s.SubKeyFile != "" {
  129. _, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
  130. if err != nil {
  131. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
  132. }
  133. }
  134. if !strings.HasPrefix(s.WebBasePath, "/") {
  135. s.WebBasePath = "/" + s.WebBasePath
  136. }
  137. if !strings.HasSuffix(s.WebBasePath, "/") {
  138. s.WebBasePath += "/"
  139. }
  140. if !strings.HasPrefix(s.SubPath, "/") {
  141. s.SubPath = "/" + s.SubPath
  142. }
  143. if !strings.HasSuffix(s.SubPath, "/") {
  144. s.SubPath += "/"
  145. }
  146. if !strings.HasPrefix(s.SubJsonPath, "/") {
  147. s.SubJsonPath = "/" + s.SubJsonPath
  148. }
  149. if !strings.HasSuffix(s.SubJsonPath, "/") {
  150. s.SubJsonPath += "/"
  151. }
  152. _, err := time.LoadLocation(s.TimeLocation)
  153. if err != nil {
  154. return common.NewError("time location not exist:", s.TimeLocation)
  155. }
  156. return nil
  157. }