1
0

entity.go 14 KB

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