1
0

entity.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/internal/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. PanelOutbound string `json:"panelOutbound" form:"panelOutbound"` // Xray outbound tag for the panel's own outbound HTTP (update checks/downloads, Telegram, geo updates, outbound-subscription fetches)
  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. RemarkTemplate string `json:"remarkTemplate" form:"remarkTemplate"` // Subscription remark template ({{VAR}} tokens) rendered per client
  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. TgCpu int `json:"tgCpu" form:"tgCpu" validate:"gte=0,lte=100"` // CPU usage threshold for alerts (percent)
  44. TgLang string `json:"tgLang" form:"tgLang"` // Telegram bot language
  45. TgEnabledEvents string `json:"tgEnabledEvents" form:"tgEnabledEvents"` // Comma-separated event types to send via Telegram
  46. // Email (SMTP) notification settings
  47. SmtpEnable bool `json:"smtpEnable" form:"smtpEnable"` // Enable email notifications
  48. SmtpHost string `json:"smtpHost" form:"smtpHost"` // SMTP server host
  49. SmtpPort int `json:"smtpPort" form:"smtpPort" validate:"gte=1,lte=65535"` // SMTP server port
  50. SmtpUsername string `json:"smtpUsername" form:"smtpUsername"` // SMTP username
  51. SmtpPassword string `json:"smtpPassword" form:"smtpPassword"` // SMTP password
  52. SmtpTo string `json:"smtpTo" form:"smtpTo"` // Comma-separated recipient emails
  53. SmtpEncryptionType string `json:"smtpEncryptionType" form:"smtpEncryptionType"` // SMTP encryption: none, starttls, tls
  54. SmtpEnabledEvents string `json:"smtpEnabledEvents" form:"smtpEnabledEvents"` // Comma-separated event types to send via email
  55. SmtpCpu int `json:"smtpCpu" form:"smtpCpu" validate:"gte=0,lte=100"` // CPU threshold for email notifications
  56. // Security settings
  57. TimeLocation string `json:"timeLocation" form:"timeLocation"` // Time zone location
  58. TwoFactorEnable bool `json:"twoFactorEnable" form:"twoFactorEnable"` // Enable two-factor authentication
  59. TwoFactorToken string `json:"twoFactorToken" form:"twoFactorToken"` // Two-factor authentication token
  60. // Subscription server settings
  61. SubEnable bool `json:"subEnable" form:"subEnable"` // Enable subscription server
  62. SubJsonEnable bool `json:"subJsonEnable" form:"subJsonEnable"` // Enable JSON subscription endpoint
  63. SubTitle string `json:"subTitle" form:"subTitle"` // Subscription title
  64. SubSupportUrl string `json:"subSupportUrl" form:"subSupportUrl"` // Subscription support URL
  65. SubProfileUrl string `json:"subProfileUrl" form:"subProfileUrl"` // Subscription profile URL
  66. SubAnnounce string `json:"subAnnounce" form:"subAnnounce"` // Subscription announce
  67. SubEnableRouting bool `json:"subEnableRouting" form:"subEnableRouting"` // Enable routing for subscription
  68. SubRoutingRules string `json:"subRoutingRules" form:"subRoutingRules"` // Subscription global routing rules (Only for Happ)
  69. SubListen string `json:"subListen" form:"subListen"` // Subscription server listen IP
  70. SubPort int `json:"subPort" form:"subPort" validate:"gte=1,lte=65535"` // Subscription server port
  71. SubPath string `json:"subPath" form:"subPath"` // Base path for subscription URLs
  72. SubDomain string `json:"subDomain" form:"subDomain"` // Domain for subscription server validation
  73. SubCertFile string `json:"subCertFile" form:"subCertFile"` // SSL certificate file for subscription server
  74. SubKeyFile string `json:"subKeyFile" form:"subKeyFile"` // SSL private key file for subscription server
  75. SubUpdates int `json:"subUpdates" form:"subUpdates" validate:"gte=0,lte=525600"` // Subscription update interval in minutes
  76. ExternalTrafficInformEnable bool `json:"externalTrafficInformEnable" form:"externalTrafficInformEnable"` // Enable external traffic reporting
  77. ExternalTrafficInformURI string `json:"externalTrafficInformURI" form:"externalTrafficInformURI"` // URI for external traffic reporting
  78. RestartXrayOnClientDisable bool `json:"restartXrayOnClientDisable" form:"restartXrayOnClientDisable"` // Restart Xray when clients are auto-disabled by expiry/traffic limit
  79. SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"` // Encrypt subscription responses
  80. SubURI string `json:"subURI" form:"subURI"` // Subscription server URI
  81. SubJsonPath string `json:"subJsonPath" form:"subJsonPath"` // Path for JSON subscription endpoint
  82. SubJsonURI string `json:"subJsonURI" form:"subJsonURI"` // JSON subscription server URI
  83. SubClashEnable bool `json:"subClashEnable" form:"subClashEnable"` // Enable Clash/Mihomo subscription endpoint
  84. SubClashPath string `json:"subClashPath" form:"subClashPath"` // Path for Clash/Mihomo subscription endpoint
  85. SubClashURI string `json:"subClashURI" form:"subClashURI"` // Clash/Mihomo subscription server URI
  86. SubClashEnableRouting bool `json:"subClashEnableRouting" form:"subClashEnableRouting"` // Enable global routing rules for Clash/Mihomo
  87. SubClashRules string `json:"subClashRules" form:"subClashRules"` // Clash/Mihomo global routing rules
  88. SubJsonMux string `json:"subJsonMux" form:"subJsonMux"` // JSON subscription mux configuration
  89. SubJsonRules string `json:"subJsonRules" form:"subJsonRules"`
  90. SubJsonFinalMask string `json:"subJsonFinalMask" form:"subJsonFinalMask"` // JSON subscription global finalmask (tcp/udp masks + quicParams)
  91. SubThemeDir string `json:"subThemeDir" form:"subThemeDir"` // Absolute path to a folder containing a custom subscription page template
  92. SubHideSettings bool `json:"subHideSettings" form:"subHideSettings"` // Hide server settings in happ subscription (Only for Happ)
  93. // LDAP settings
  94. LdapEnable bool `json:"ldapEnable" form:"ldapEnable"`
  95. LdapHost string `json:"ldapHost" form:"ldapHost"`
  96. LdapPort int `json:"ldapPort" form:"ldapPort" validate:"gte=0,lte=65535"`
  97. LdapUseTLS bool `json:"ldapUseTLS" form:"ldapUseTLS"`
  98. LdapBindDN string `json:"ldapBindDN" form:"ldapBindDN"`
  99. LdapPassword string `json:"ldapPassword" form:"ldapPassword"`
  100. LdapBaseDN string `json:"ldapBaseDN" form:"ldapBaseDN"`
  101. LdapUserFilter string `json:"ldapUserFilter" form:"ldapUserFilter"`
  102. LdapUserAttr string `json:"ldapUserAttr" form:"ldapUserAttr"` // e.g., mail or uid
  103. LdapVlessField string `json:"ldapVlessField" form:"ldapVlessField"`
  104. LdapSyncCron string `json:"ldapSyncCron" form:"ldapSyncCron"`
  105. // Generic flag configuration
  106. LdapFlagField string `json:"ldapFlagField" form:"ldapFlagField"`
  107. LdapTruthyValues string `json:"ldapTruthyValues" form:"ldapTruthyValues"`
  108. LdapInvertFlag bool `json:"ldapInvertFlag" form:"ldapInvertFlag"`
  109. LdapInboundTags string `json:"ldapInboundTags" form:"ldapInboundTags"`
  110. LdapAutoCreate bool `json:"ldapAutoCreate" form:"ldapAutoCreate"`
  111. LdapAutoDelete bool `json:"ldapAutoDelete" form:"ldapAutoDelete"`
  112. LdapDefaultTotalGB int `json:"ldapDefaultTotalGB" form:"ldapDefaultTotalGB" validate:"gte=0"`
  113. LdapDefaultExpiryDays int `json:"ldapDefaultExpiryDays" form:"ldapDefaultExpiryDays" validate:"gte=0"`
  114. LdapDefaultLimitIP int `json:"ldapDefaultLimitIP" form:"ldapDefaultLimitIP" validate:"gte=0"`
  115. // JSON subscription routing rules
  116. // WARP
  117. WarpUpdateInterval int `json:"warpUpdateInterval" form:"warpUpdateInterval" validate:"gte=0"`
  118. }
  119. // AllSettingView is the browser-safe settings read model. Secret values
  120. // are redacted from the embedded write model and represented by presence
  121. // flags so the UI can show configured/not configured state.
  122. type AllSettingView struct {
  123. AllSetting
  124. HasTgBotToken bool `json:"hasTgBotToken"`
  125. HasTwoFactorToken bool `json:"hasTwoFactorToken"`
  126. HasLdapPassword bool `json:"hasLdapPassword"`
  127. HasApiToken bool `json:"hasApiToken"`
  128. HasWarpSecret bool `json:"hasWarpSecret"`
  129. HasNordSecret bool `json:"hasNordSecret"`
  130. HasSmtpPassword bool `json:"hasSmtpPassword"`
  131. }
  132. // CheckValid validates all settings in the AllSetting struct, checking IP addresses, ports, SSL certificates, and other configuration values.
  133. func pathHasForbiddenChar(s string) bool {
  134. for _, r := range s {
  135. if r == '\\' || r == ' ' || r < 0x20 || r == 0x7f {
  136. return true
  137. }
  138. }
  139. return false
  140. }
  141. func (s *AllSetting) CheckValid() error {
  142. if s.WebListen != "" {
  143. ip := net.ParseIP(s.WebListen)
  144. if ip == nil {
  145. return common.NewError("web listen is not valid ip:", s.WebListen)
  146. }
  147. }
  148. if s.SubListen != "" {
  149. ip := net.ParseIP(s.SubListen)
  150. if ip == nil {
  151. return common.NewError("Sub listen is not valid ip:", s.SubListen)
  152. }
  153. }
  154. if s.WebPort <= 0 || s.WebPort > math.MaxUint16 {
  155. return common.NewError("web port is not a valid port:", s.WebPort)
  156. }
  157. if s.SubPort <= 0 || s.SubPort > math.MaxUint16 {
  158. return common.NewError("Sub port is not a valid port:", s.SubPort)
  159. }
  160. if (s.SubPort == s.WebPort) && (s.WebListen == s.SubListen) {
  161. return common.NewError("Sub and Web could not use same ip:port, ", s.SubListen, ":", s.SubPort, " & ", s.WebListen, ":", s.WebPort)
  162. }
  163. if s.WebCertFile != "" || s.WebKeyFile != "" {
  164. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  165. if err != nil {
  166. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  167. }
  168. }
  169. if s.SubCertFile != "" || s.SubKeyFile != "" {
  170. _, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
  171. if err != nil {
  172. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
  173. }
  174. }
  175. for _, p := range []struct {
  176. name string
  177. value string
  178. }{
  179. {"web base path", s.WebBasePath},
  180. {"subscription path", s.SubPath},
  181. {"subscription JSON path", s.SubJsonPath},
  182. {"subscription Clash path", s.SubClashPath},
  183. } {
  184. if pathHasForbiddenChar(p.value) {
  185. return common.NewError("URI path contains an invalid character:", p.name)
  186. }
  187. }
  188. if !strings.HasPrefix(s.WebBasePath, "/") {
  189. s.WebBasePath = "/" + s.WebBasePath
  190. }
  191. if !strings.HasSuffix(s.WebBasePath, "/") {
  192. s.WebBasePath += "/"
  193. }
  194. if !strings.HasPrefix(s.SubPath, "/") {
  195. s.SubPath = "/" + s.SubPath
  196. }
  197. if !strings.HasSuffix(s.SubPath, "/") {
  198. s.SubPath += "/"
  199. }
  200. if !strings.HasPrefix(s.SubJsonPath, "/") {
  201. s.SubJsonPath = "/" + s.SubJsonPath
  202. }
  203. if !strings.HasSuffix(s.SubJsonPath, "/") {
  204. s.SubJsonPath += "/"
  205. }
  206. if !strings.HasPrefix(s.SubClashPath, "/") {
  207. s.SubClashPath = "/" + s.SubClashPath
  208. }
  209. if !strings.HasSuffix(s.SubClashPath, "/") {
  210. s.SubClashPath += "/"
  211. }
  212. for cidr := range strings.SplitSeq(s.TrustedProxyCIDRs, ",") {
  213. cidr = strings.TrimSpace(cidr)
  214. if cidr == "" {
  215. continue
  216. }
  217. if ip := net.ParseIP(cidr); ip != nil {
  218. continue
  219. }
  220. if _, _, err := net.ParseCIDR(cidr); err != nil {
  221. return common.NewError("trusted proxy CIDR is not valid:", cidr)
  222. }
  223. }
  224. _, err := time.LoadLocation(s.TimeLocation)
  225. if err != nil {
  226. return common.NewError("time location not exist:", s.TimeLocation)
  227. }
  228. return nil
  229. }