entity.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/v3/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. TrustedProxyCIDRs string `json:"trustedProxyCIDRs" form:"trustedProxyCIDRs"` // Trusted reverse proxy IPs/CIDRs for forwarded headers
  28. // UI settings
  29. PageSize int `json:"pageSize" form:"pageSize"` // Number of items per page in lists
  30. ExpireDiff int `json:"expireDiff" form:"expireDiff"` // Expiration warning threshold in days
  31. TrafficDiff int `json:"trafficDiff" form:"trafficDiff"` // Traffic warning threshold percentage
  32. RemarkModel string `json:"remarkModel" form:"remarkModel"` // Remark model pattern for inbounds
  33. Datepicker string `json:"datepicker" form:"datepicker"` // Date picker format
  34. // Telegram bot settings
  35. TgBotEnable bool `json:"tgBotEnable" form:"tgBotEnable"` // Enable Telegram bot notifications
  36. TgBotToken string `json:"tgBotToken" form:"tgBotToken"` // Telegram bot token
  37. TgBotProxy string `json:"tgBotProxy" form:"tgBotProxy"` // Proxy URL for Telegram bot
  38. TgBotAPIServer string `json:"tgBotAPIServer" form:"tgBotAPIServer"` // Custom API server for Telegram bot
  39. TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"` // Telegram chat ID for notifications
  40. TgRunTime string `json:"tgRunTime" form:"tgRunTime"` // Cron schedule for Telegram notifications
  41. TgBotBackup bool `json:"tgBotBackup" form:"tgBotBackup"` // Enable database backup via Telegram
  42. TgBotLoginNotify bool `json:"tgBotLoginNotify" form:"tgBotLoginNotify"` // Send login notifications
  43. TgCpu int `json:"tgCpu" form:"tgCpu"` // CPU usage threshold for alerts
  44. TgLang string `json:"tgLang" form:"tgLang"` // Telegram bot language
  45. // Security settings
  46. TimeLocation string `json:"timeLocation" form:"timeLocation"` // Time zone location
  47. TwoFactorEnable bool `json:"twoFactorEnable" form:"twoFactorEnable"` // Enable two-factor authentication
  48. TwoFactorToken string `json:"twoFactorToken" form:"twoFactorToken"` // Two-factor authentication token
  49. // Subscription server settings
  50. SubEnable bool `json:"subEnable" form:"subEnable"` // Enable subscription server
  51. SubJsonEnable bool `json:"subJsonEnable" form:"subJsonEnable"` // Enable JSON subscription endpoint
  52. SubTitle string `json:"subTitle" form:"subTitle"` // Subscription title
  53. SubSupportUrl string `json:"subSupportUrl" form:"subSupportUrl"` // Subscription support URL
  54. SubProfileUrl string `json:"subProfileUrl" form:"subProfileUrl"` // Subscription profile URL
  55. SubAnnounce string `json:"subAnnounce" form:"subAnnounce"` // Subscription announce
  56. SubEnableRouting bool `json:"subEnableRouting" form:"subEnableRouting"` // Enable routing for subscription
  57. SubRoutingRules string `json:"subRoutingRules" form:"subRoutingRules"` // Subscription global routing rules (Only for Happ)
  58. SubListen string `json:"subListen" form:"subListen"` // Subscription server listen IP
  59. SubPort int `json:"subPort" form:"subPort"` // Subscription server port
  60. SubPath string `json:"subPath" form:"subPath"` // Base path for subscription URLs
  61. SubDomain string `json:"subDomain" form:"subDomain"` // Domain for subscription server validation
  62. SubCertFile string `json:"subCertFile" form:"subCertFile"` // SSL certificate file for subscription server
  63. SubKeyFile string `json:"subKeyFile" form:"subKeyFile"` // SSL private key file for subscription server
  64. SubUpdates int `json:"subUpdates" form:"subUpdates"` // Subscription update interval in minutes
  65. ExternalTrafficInformEnable bool `json:"externalTrafficInformEnable" form:"externalTrafficInformEnable"` // Enable external traffic reporting
  66. ExternalTrafficInformURI string `json:"externalTrafficInformURI" form:"externalTrafficInformURI"` // URI for external traffic reporting
  67. RestartXrayOnClientDisable bool `json:"restartXrayOnClientDisable" form:"restartXrayOnClientDisable"` // Restart Xray when clients are auto-disabled by expiry/traffic limit
  68. SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"` // Encrypt subscription responses
  69. SubShowInfo bool `json:"subShowInfo" form:"subShowInfo"` // Show client information in subscriptions
  70. SubURI string `json:"subURI" form:"subURI"` // Subscription server URI
  71. SubJsonPath string `json:"subJsonPath" form:"subJsonPath"` // Path for JSON subscription endpoint
  72. SubJsonURI string `json:"subJsonURI" form:"subJsonURI"` // JSON subscription server URI
  73. SubClashEnable bool `json:"subClashEnable" form:"subClashEnable"` // Enable Clash/Mihomo subscription endpoint
  74. SubClashPath string `json:"subClashPath" form:"subClashPath"` // Path for Clash/Mihomo subscription endpoint
  75. SubClashURI string `json:"subClashURI" form:"subClashURI"` // Clash/Mihomo subscription server URI
  76. SubJsonFragment string `json:"subJsonFragment" form:"subJsonFragment"` // JSON subscription fragment configuration
  77. SubJsonNoises string `json:"subJsonNoises" form:"subJsonNoises"` // JSON subscription noise configuration
  78. SubJsonMux string `json:"subJsonMux" form:"subJsonMux"` // JSON subscription mux configuration
  79. SubJsonRules string `json:"subJsonRules" form:"subJsonRules"`
  80. // LDAP settings
  81. LdapEnable bool `json:"ldapEnable" form:"ldapEnable"`
  82. LdapHost string `json:"ldapHost" form:"ldapHost"`
  83. LdapPort int `json:"ldapPort" form:"ldapPort"`
  84. LdapUseTLS bool `json:"ldapUseTLS" form:"ldapUseTLS"`
  85. LdapBindDN string `json:"ldapBindDN" form:"ldapBindDN"`
  86. LdapPassword string `json:"ldapPassword" form:"ldapPassword"`
  87. LdapBaseDN string `json:"ldapBaseDN" form:"ldapBaseDN"`
  88. LdapUserFilter string `json:"ldapUserFilter" form:"ldapUserFilter"`
  89. LdapUserAttr string `json:"ldapUserAttr" form:"ldapUserAttr"` // e.g., mail or uid
  90. LdapVlessField string `json:"ldapVlessField" form:"ldapVlessField"`
  91. LdapSyncCron string `json:"ldapSyncCron" form:"ldapSyncCron"`
  92. // Generic flag configuration
  93. LdapFlagField string `json:"ldapFlagField" form:"ldapFlagField"`
  94. LdapTruthyValues string `json:"ldapTruthyValues" form:"ldapTruthyValues"`
  95. LdapInvertFlag bool `json:"ldapInvertFlag" form:"ldapInvertFlag"`
  96. LdapInboundTags string `json:"ldapInboundTags" form:"ldapInboundTags"`
  97. LdapAutoCreate bool `json:"ldapAutoCreate" form:"ldapAutoCreate"`
  98. LdapAutoDelete bool `json:"ldapAutoDelete" form:"ldapAutoDelete"`
  99. LdapDefaultTotalGB int `json:"ldapDefaultTotalGB" form:"ldapDefaultTotalGB"`
  100. LdapDefaultExpiryDays int `json:"ldapDefaultExpiryDays" form:"ldapDefaultExpiryDays"`
  101. LdapDefaultLimitIP int `json:"ldapDefaultLimitIP" form:"ldapDefaultLimitIP"`
  102. // JSON subscription routing rules
  103. }
  104. // AllSettingView is the browser-safe settings read model. Secret values
  105. // are redacted from the embedded write model and represented by presence
  106. // flags so the UI can show configured/not configured state.
  107. type AllSettingView struct {
  108. AllSetting
  109. HasTgBotToken bool `json:"hasTgBotToken"`
  110. HasTwoFactorToken bool `json:"hasTwoFactorToken"`
  111. HasLdapPassword bool `json:"hasLdapPassword"`
  112. HasApiToken bool `json:"hasApiToken"`
  113. HasWarpSecret bool `json:"hasWarpSecret"`
  114. HasNordSecret bool `json:"hasNordSecret"`
  115. }
  116. // CheckValid validates all settings in the AllSetting struct, checking IP addresses, ports, SSL certificates, and other configuration values.
  117. func (s *AllSetting) CheckValid() error {
  118. if s.WebListen != "" {
  119. ip := net.ParseIP(s.WebListen)
  120. if ip == nil {
  121. return common.NewError("web listen is not valid ip:", s.WebListen)
  122. }
  123. }
  124. if s.SubListen != "" {
  125. ip := net.ParseIP(s.SubListen)
  126. if ip == nil {
  127. return common.NewError("Sub listen is not valid ip:", s.SubListen)
  128. }
  129. }
  130. if s.WebPort <= 0 || s.WebPort > math.MaxUint16 {
  131. return common.NewError("web port is not a valid port:", s.WebPort)
  132. }
  133. if s.SubPort <= 0 || s.SubPort > math.MaxUint16 {
  134. return common.NewError("Sub port is not a valid port:", s.SubPort)
  135. }
  136. if (s.SubPort == s.WebPort) && (s.WebListen == s.SubListen) {
  137. return common.NewError("Sub and Web could not use same ip:port, ", s.SubListen, ":", s.SubPort, " & ", s.WebListen, ":", s.WebPort)
  138. }
  139. if s.WebCertFile != "" || s.WebKeyFile != "" {
  140. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  141. if err != nil {
  142. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  143. }
  144. }
  145. if s.SubCertFile != "" || s.SubKeyFile != "" {
  146. _, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
  147. if err != nil {
  148. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
  149. }
  150. }
  151. if !strings.HasPrefix(s.WebBasePath, "/") {
  152. s.WebBasePath = "/" + s.WebBasePath
  153. }
  154. if !strings.HasSuffix(s.WebBasePath, "/") {
  155. s.WebBasePath += "/"
  156. }
  157. if !strings.HasPrefix(s.SubPath, "/") {
  158. s.SubPath = "/" + s.SubPath
  159. }
  160. if !strings.HasSuffix(s.SubPath, "/") {
  161. s.SubPath += "/"
  162. }
  163. if !strings.HasPrefix(s.SubJsonPath, "/") {
  164. s.SubJsonPath = "/" + s.SubJsonPath
  165. }
  166. if !strings.HasSuffix(s.SubJsonPath, "/") {
  167. s.SubJsonPath += "/"
  168. }
  169. if !strings.HasPrefix(s.SubClashPath, "/") {
  170. s.SubClashPath = "/" + s.SubClashPath
  171. }
  172. if !strings.HasSuffix(s.SubClashPath, "/") {
  173. s.SubClashPath += "/"
  174. }
  175. for _, cidr := range strings.Split(s.TrustedProxyCIDRs, ",") {
  176. cidr = strings.TrimSpace(cidr)
  177. if cidr == "" {
  178. continue
  179. }
  180. if ip := net.ParseIP(cidr); ip != nil {
  181. continue
  182. }
  183. if _, _, err := net.ParseCIDR(cidr); err != nil {
  184. return common.NewError("trusted proxy CIDR is not valid:", cidr)
  185. }
  186. }
  187. _, err := time.LoadLocation(s.TimeLocation)
  188. if err != nil {
  189. return common.NewError("time location not exist:", s.TimeLocation)
  190. }
  191. return nil
  192. }