entity.go 15 KB

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