entity.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. SubJsonFragment string `json:"subJsonFragment" form:"subJsonFragment"` // JSON subscription fragment configuration
  81. SubJsonNoises string `json:"subJsonNoises" form:"subJsonNoises"` // JSON subscription noise configuration
  82. SubJsonMux string `json:"subJsonMux" form:"subJsonMux"` // JSON subscription mux configuration
  83. SubJsonRules string `json:"subJsonRules" form:"subJsonRules"`
  84. // LDAP settings
  85. LdapEnable bool `json:"ldapEnable" form:"ldapEnable"`
  86. LdapHost string `json:"ldapHost" form:"ldapHost"`
  87. LdapPort int `json:"ldapPort" form:"ldapPort" validate:"gte=0,lte=65535"`
  88. LdapUseTLS bool `json:"ldapUseTLS" form:"ldapUseTLS"`
  89. LdapBindDN string `json:"ldapBindDN" form:"ldapBindDN"`
  90. LdapPassword string `json:"ldapPassword" form:"ldapPassword"`
  91. LdapBaseDN string `json:"ldapBaseDN" form:"ldapBaseDN"`
  92. LdapUserFilter string `json:"ldapUserFilter" form:"ldapUserFilter"`
  93. LdapUserAttr string `json:"ldapUserAttr" form:"ldapUserAttr"` // e.g., mail or uid
  94. LdapVlessField string `json:"ldapVlessField" form:"ldapVlessField"`
  95. LdapSyncCron string `json:"ldapSyncCron" form:"ldapSyncCron"`
  96. // Generic flag configuration
  97. LdapFlagField string `json:"ldapFlagField" form:"ldapFlagField"`
  98. LdapTruthyValues string `json:"ldapTruthyValues" form:"ldapTruthyValues"`
  99. LdapInvertFlag bool `json:"ldapInvertFlag" form:"ldapInvertFlag"`
  100. LdapInboundTags string `json:"ldapInboundTags" form:"ldapInboundTags"`
  101. LdapAutoCreate bool `json:"ldapAutoCreate" form:"ldapAutoCreate"`
  102. LdapAutoDelete bool `json:"ldapAutoDelete" form:"ldapAutoDelete"`
  103. LdapDefaultTotalGB int `json:"ldapDefaultTotalGB" form:"ldapDefaultTotalGB" validate:"gte=0"`
  104. LdapDefaultExpiryDays int `json:"ldapDefaultExpiryDays" form:"ldapDefaultExpiryDays" validate:"gte=0"`
  105. LdapDefaultLimitIP int `json:"ldapDefaultLimitIP" form:"ldapDefaultLimitIP" validate:"gte=0"`
  106. // JSON subscription routing rules
  107. }
  108. // AllSettingView is the browser-safe settings read model. Secret values
  109. // are redacted from the embedded write model and represented by presence
  110. // flags so the UI can show configured/not configured state.
  111. type AllSettingView struct {
  112. AllSetting
  113. HasTgBotToken bool `json:"hasTgBotToken"`
  114. HasTwoFactorToken bool `json:"hasTwoFactorToken"`
  115. HasLdapPassword bool `json:"hasLdapPassword"`
  116. HasApiToken bool `json:"hasApiToken"`
  117. HasWarpSecret bool `json:"hasWarpSecret"`
  118. HasNordSecret bool `json:"hasNordSecret"`
  119. }
  120. // CheckValid validates all settings in the AllSetting struct, checking IP addresses, ports, SSL certificates, and other configuration values.
  121. func pathHasForbiddenChar(s string) bool {
  122. for _, r := range s {
  123. if r == '\\' || r == ' ' || r < 0x20 || r == 0x7f {
  124. return true
  125. }
  126. }
  127. return false
  128. }
  129. func (s *AllSetting) CheckValid() error {
  130. if s.WebListen != "" {
  131. ip := net.ParseIP(s.WebListen)
  132. if ip == nil {
  133. return common.NewError("web listen is not valid ip:", s.WebListen)
  134. }
  135. }
  136. if s.SubListen != "" {
  137. ip := net.ParseIP(s.SubListen)
  138. if ip == nil {
  139. return common.NewError("Sub listen is not valid ip:", s.SubListen)
  140. }
  141. }
  142. if s.WebPort <= 0 || s.WebPort > math.MaxUint16 {
  143. return common.NewError("web port is not a valid port:", s.WebPort)
  144. }
  145. if s.SubPort <= 0 || s.SubPort > math.MaxUint16 {
  146. return common.NewError("Sub port is not a valid port:", s.SubPort)
  147. }
  148. if (s.SubPort == s.WebPort) && (s.WebListen == s.SubListen) {
  149. return common.NewError("Sub and Web could not use same ip:port, ", s.SubListen, ":", s.SubPort, " & ", s.WebListen, ":", s.WebPort)
  150. }
  151. if s.WebCertFile != "" || s.WebKeyFile != "" {
  152. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  153. if err != nil {
  154. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  155. }
  156. }
  157. if s.SubCertFile != "" || s.SubKeyFile != "" {
  158. _, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
  159. if err != nil {
  160. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
  161. }
  162. }
  163. for _, p := range []struct {
  164. name string
  165. value string
  166. }{
  167. {"web base path", s.WebBasePath},
  168. {"subscription path", s.SubPath},
  169. {"subscription JSON path", s.SubJsonPath},
  170. {"subscription Clash path", s.SubClashPath},
  171. } {
  172. if pathHasForbiddenChar(p.value) {
  173. return common.NewError("URI path contains an invalid character:", p.name)
  174. }
  175. }
  176. if !strings.HasPrefix(s.WebBasePath, "/") {
  177. s.WebBasePath = "/" + s.WebBasePath
  178. }
  179. if !strings.HasSuffix(s.WebBasePath, "/") {
  180. s.WebBasePath += "/"
  181. }
  182. if !strings.HasPrefix(s.SubPath, "/") {
  183. s.SubPath = "/" + s.SubPath
  184. }
  185. if !strings.HasSuffix(s.SubPath, "/") {
  186. s.SubPath += "/"
  187. }
  188. if !strings.HasPrefix(s.SubJsonPath, "/") {
  189. s.SubJsonPath = "/" + s.SubJsonPath
  190. }
  191. if !strings.HasSuffix(s.SubJsonPath, "/") {
  192. s.SubJsonPath += "/"
  193. }
  194. if !strings.HasPrefix(s.SubClashPath, "/") {
  195. s.SubClashPath = "/" + s.SubClashPath
  196. }
  197. if !strings.HasSuffix(s.SubClashPath, "/") {
  198. s.SubClashPath += "/"
  199. }
  200. for cidr := range strings.SplitSeq(s.TrustedProxyCIDRs, ",") {
  201. cidr = strings.TrimSpace(cidr)
  202. if cidr == "" {
  203. continue
  204. }
  205. if ip := net.ParseIP(cidr); ip != nil {
  206. continue
  207. }
  208. if _, _, err := net.ParseCIDR(cidr); err != nil {
  209. return common.NewError("trusted proxy CIDR is not valid:", cidr)
  210. }
  211. }
  212. _, err := time.LoadLocation(s.TimeLocation)
  213. if err != nil {
  214. return common.NewError("time location not exist:", s.TimeLocation)
  215. }
  216. return nil
  217. }