1
0

entity.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package entity
  2. import (
  3. "crypto/tls"
  4. "math"
  5. "net"
  6. "net/mail"
  7. "strings"
  8. "time"
  9. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  10. )
  11. type Msg struct {
  12. Success bool `json:"success"`
  13. Msg string `json:"msg"`
  14. Obj any `json:"obj"`
  15. }
  16. type AllSetting struct {
  17. WebListen string `json:"webListen" form:"webListen"`
  18. WebDomain string `json:"webDomain" form:"webDomain"`
  19. WebPort int `json:"webPort" form:"webPort" validate:"gte=1,lte=65535"`
  20. WebCertFile string `json:"webCertFile" form:"webCertFile"`
  21. WebKeyFile string `json:"webKeyFile" form:"webKeyFile"`
  22. WebBasePath string `json:"webBasePath" form:"webBasePath"`
  23. SessionMaxAge int `json:"sessionMaxAge" form:"sessionMaxAge" validate:"gte=1,lte=525600"`
  24. TrustedProxyCIDRs string `json:"trustedProxyCIDRs" form:"trustedProxyCIDRs"`
  25. IpLimitAllowlist string `json:"ipLimitAllowlist" form:"ipLimitAllowlist"`
  26. PanelOutbound string `json:"panelOutbound" form:"panelOutbound"`
  27. PageSize int `json:"pageSize" form:"pageSize" validate:"gte=0,lte=1000"`
  28. ExpireDiff int `json:"expireDiff" form:"expireDiff" validate:"gte=0"`
  29. TrafficDiff int `json:"trafficDiff" form:"trafficDiff" validate:"gte=0,lte=100"`
  30. RemarkTemplate string `json:"remarkTemplate" form:"remarkTemplate"`
  31. SubShowIdentityOnAllLinks bool `json:"subShowIdentityOnAllLinks" form:"subShowIdentityOnAllLinks"`
  32. Datepicker string `json:"datepicker" form:"datepicker"`
  33. TgBotEnable bool `json:"tgBotEnable" form:"tgBotEnable"`
  34. TgBotToken string `json:"tgBotToken" form:"tgBotToken"`
  35. TgBotProxy string `json:"tgBotProxy" form:"tgBotProxy"`
  36. TgBotAPIServer string `json:"tgBotAPIServer" form:"tgBotAPIServer"`
  37. TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"`
  38. TgRunTime string `json:"tgRunTime" form:"tgRunTime"`
  39. TgBotBackup bool `json:"tgBotBackup" form:"tgBotBackup"`
  40. TgCpu int `json:"tgCpu" form:"tgCpu" validate:"gte=0,lte=100"`
  41. TgMemory int `json:"tgMemory" form:"tgMemory" validate:"gte=0,lte=100"`
  42. TgLang string `json:"tgLang" form:"tgLang"`
  43. TgEnabledEvents string `json:"tgEnabledEvents" form:"tgEnabledEvents"`
  44. SmtpEnable bool `json:"smtpEnable" form:"smtpEnable"`
  45. SmtpHost string `json:"smtpHost" form:"smtpHost"`
  46. SmtpPort int `json:"smtpPort" form:"smtpPort" validate:"gte=1,lte=65535"`
  47. SmtpUsername string `json:"smtpUsername" form:"smtpUsername"`
  48. SmtpPassword string `json:"smtpPassword" form:"smtpPassword"`
  49. SmtpFrom string `json:"smtpFrom" form:"smtpFrom"`
  50. SmtpFromName string `json:"smtpFromName" form:"smtpFromName"`
  51. SmtpTo string `json:"smtpTo" form:"smtpTo"`
  52. SmtpEncryptionType string `json:"smtpEncryptionType" form:"smtpEncryptionType"`
  53. SmtpEnabledEvents string `json:"smtpEnabledEvents" form:"smtpEnabledEvents"`
  54. SmtpCpu int `json:"smtpCpu" form:"smtpCpu" validate:"gte=0,lte=100"`
  55. SmtpMemory int `json:"smtpMemory" form:"smtpMemory" validate:"gte=0,lte=100"`
  56. OutboundDownThreshold int `json:"outboundDownThreshold" form:"outboundDownThreshold" validate:"gte=1,lte=100"`
  57. TimeLocation string `json:"timeLocation" form:"timeLocation"`
  58. TwoFactorEnable bool `json:"twoFactorEnable" form:"twoFactorEnable"`
  59. TwoFactorToken string `json:"twoFactorToken" form:"twoFactorToken"`
  60. SubEnable bool `json:"subEnable" form:"subEnable"`
  61. SubJsonEnable bool `json:"subJsonEnable" form:"subJsonEnable"`
  62. SubJsonAutoDetect bool `json:"subJsonAutoDetect" form:"subJsonAutoDetect"`
  63. SubJsonAlwaysArray bool `json:"subJsonAlwaysArray" form:"subJsonAlwaysArray"`
  64. SubJsonUserAgentRegex string `json:"subJsonUserAgentRegex" form:"subJsonUserAgentRegex"`
  65. SubClashAutoDetect bool `json:"subClashAutoDetect" form:"subClashAutoDetect"`
  66. SubClashUserAgentRegex string `json:"subClashUserAgentRegex" form:"subClashUserAgentRegex"`
  67. SubTitle string `json:"subTitle" form:"subTitle"`
  68. SubSupportUrl string `json:"subSupportUrl" form:"subSupportUrl"`
  69. SubProfileUrl string `json:"subProfileUrl" form:"subProfileUrl"`
  70. SubAnnounce string `json:"subAnnounce" form:"subAnnounce"`
  71. SubEnableRouting bool `json:"subEnableRouting" form:"subEnableRouting"`
  72. SubRoutingRules string `json:"subRoutingRules" form:"subRoutingRules"`
  73. SubIncyEnableRouting bool `json:"subIncyEnableRouting" form:"subIncyEnableRouting"`
  74. SubIncyRoutingRules string `json:"subIncyRoutingRules" form:"subIncyRoutingRules"`
  75. SubListen string `json:"subListen" form:"subListen"`
  76. SubPort int `json:"subPort" form:"subPort" validate:"gte=1,lte=65535"`
  77. SubPath string `json:"subPath" form:"subPath"`
  78. SubDomain string `json:"subDomain" form:"subDomain"`
  79. SubCertFile string `json:"subCertFile" form:"subCertFile"`
  80. SubKeyFile string `json:"subKeyFile" form:"subKeyFile"`
  81. SubUpdates int `json:"subUpdates" form:"subUpdates" validate:"gte=0,lte=525600"`
  82. ExternalTrafficInformEnable bool `json:"externalTrafficInformEnable" form:"externalTrafficInformEnable"`
  83. ExternalTrafficInformURI string `json:"externalTrafficInformURI" form:"externalTrafficInformURI"`
  84. RestartXrayOnClientDisable bool `json:"restartXrayOnClientDisable" form:"restartXrayOnClientDisable"`
  85. SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"`
  86. SubURI string `json:"subURI" form:"subURI"`
  87. SubJsonPath string `json:"subJsonPath" form:"subJsonPath"`
  88. SubJsonURI string `json:"subJsonURI" form:"subJsonURI"`
  89. SubClashEnable bool `json:"subClashEnable" form:"subClashEnable"`
  90. SubClashPath string `json:"subClashPath" form:"subClashPath"`
  91. SubClashURI string `json:"subClashURI" form:"subClashURI"`
  92. SubClashEnableRouting bool `json:"subClashEnableRouting" form:"subClashEnableRouting"`
  93. SubClashRules string `json:"subClashRules" form:"subClashRules"`
  94. SubJsonMux string `json:"subJsonMux" form:"subJsonMux"`
  95. SubJsonRules string `json:"subJsonRules" form:"subJsonRules"`
  96. SubJsonFinalMask string `json:"subJsonFinalMask" form:"subJsonFinalMask"`
  97. SubThemeDir string `json:"subThemeDir" form:"subThemeDir"`
  98. SubHideSettings bool `json:"subHideSettings" form:"subHideSettings"`
  99. LdapEnable bool `json:"ldapEnable" form:"ldapEnable"`
  100. LdapHost string `json:"ldapHost" form:"ldapHost"`
  101. LdapPort int `json:"ldapPort" form:"ldapPort" validate:"gte=0,lte=65535"`
  102. LdapUseTLS bool `json:"ldapUseTLS" form:"ldapUseTLS"`
  103. LdapInsecureSkipVerify bool `json:"ldapInsecureSkipVerify" form:"ldapInsecureSkipVerify"`
  104. LdapBindDN string `json:"ldapBindDN" form:"ldapBindDN"`
  105. LdapPassword string `json:"ldapPassword" form:"ldapPassword"`
  106. LdapBaseDN string `json:"ldapBaseDN" form:"ldapBaseDN"`
  107. LdapUserFilter string `json:"ldapUserFilter" form:"ldapUserFilter"`
  108. LdapUserAttr string `json:"ldapUserAttr" form:"ldapUserAttr"`
  109. LdapVlessField string `json:"ldapVlessField" form:"ldapVlessField"`
  110. LdapSyncCron string `json:"ldapSyncCron" form:"ldapSyncCron"`
  111. LdapFlagField string `json:"ldapFlagField" form:"ldapFlagField"`
  112. LdapTruthyValues string `json:"ldapTruthyValues" form:"ldapTruthyValues"`
  113. LdapInvertFlag bool `json:"ldapInvertFlag" form:"ldapInvertFlag"`
  114. LdapInboundTags string `json:"ldapInboundTags" form:"ldapInboundTags"`
  115. LdapAutoCreate bool `json:"ldapAutoCreate" form:"ldapAutoCreate"`
  116. LdapAutoDelete bool `json:"ldapAutoDelete" form:"ldapAutoDelete"`
  117. LdapDefaultTotalGB int `json:"ldapDefaultTotalGB" form:"ldapDefaultTotalGB" validate:"gte=0"`
  118. LdapDefaultExpiryDays int `json:"ldapDefaultExpiryDays" form:"ldapDefaultExpiryDays" validate:"gte=0"`
  119. LdapDefaultLimitIP int `json:"ldapDefaultLimitIP" form:"ldapDefaultLimitIP" validate:"gte=0"`
  120. WarpUpdateInterval int `json:"warpUpdateInterval" form:"warpUpdateInterval" validate:"gte=0"`
  121. }
  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. func pathHasForbiddenChar(s string) bool {
  133. for _, r := range s {
  134. if r == '\\' || r == ' ' || r < 0x20 || r == 0x7f {
  135. return true
  136. }
  137. }
  138. return false
  139. }
  140. // checkIPOrCIDRList rejects the first comma-separated entry that is neither a
  141. // bare address nor a CIDR, naming it with the caller's message.
  142. func checkIPOrCIDRList(list, message string) error {
  143. for entry := range strings.SplitSeq(list, ",") {
  144. entry = strings.TrimSpace(entry)
  145. if entry == "" {
  146. continue
  147. }
  148. if ip := net.ParseIP(entry); ip != nil {
  149. continue
  150. }
  151. if _, _, err := net.ParseCIDR(entry); err != nil {
  152. return common.NewError(message, entry)
  153. }
  154. }
  155. return nil
  156. }
  157. func (s *AllSetting) CheckValid() error {
  158. if s.WebListen != "" {
  159. ip := net.ParseIP(s.WebListen)
  160. if ip == nil {
  161. return common.NewError("web listen is not valid ip:", s.WebListen)
  162. }
  163. }
  164. if s.SubListen != "" {
  165. ip := net.ParseIP(s.SubListen)
  166. if ip == nil {
  167. return common.NewError("Sub listen is not valid ip:", s.SubListen)
  168. }
  169. }
  170. if s.WebPort <= 0 || s.WebPort > math.MaxUint16 {
  171. return common.NewError("web port is not a valid port:", s.WebPort)
  172. }
  173. if s.SubPort <= 0 || s.SubPort > math.MaxUint16 {
  174. return common.NewError("Sub port is not a valid port:", s.SubPort)
  175. }
  176. if (s.SubPort == s.WebPort) && listenAddressesConflict(s.WebListen, s.SubListen) {
  177. return common.NewError("Sub and Web could not use same ip:port, ", s.SubListen, ":", s.SubPort, " & ", s.WebListen, ":", s.WebPort)
  178. }
  179. if s.WebCertFile != "" || s.WebKeyFile != "" {
  180. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  181. if err != nil {
  182. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  183. }
  184. }
  185. if s.SubCertFile != "" || s.SubKeyFile != "" {
  186. _, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
  187. if err != nil {
  188. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
  189. }
  190. }
  191. for _, p := range []struct {
  192. name string
  193. value string
  194. }{
  195. {"web base path", s.WebBasePath},
  196. {"subscription path", s.SubPath},
  197. {"subscription JSON path", s.SubJsonPath},
  198. {"subscription Clash path", s.SubClashPath},
  199. } {
  200. if pathHasForbiddenChar(p.value) {
  201. return common.NewError("URI path contains an invalid character:", p.name)
  202. }
  203. }
  204. if !strings.HasPrefix(s.WebBasePath, "/") {
  205. s.WebBasePath = "/" + s.WebBasePath
  206. }
  207. if !strings.HasSuffix(s.WebBasePath, "/") {
  208. s.WebBasePath += "/"
  209. }
  210. if !strings.HasPrefix(s.SubPath, "/") {
  211. s.SubPath = "/" + s.SubPath
  212. }
  213. if !strings.HasSuffix(s.SubPath, "/") {
  214. s.SubPath += "/"
  215. }
  216. if !strings.HasPrefix(s.SubJsonPath, "/") {
  217. s.SubJsonPath = "/" + s.SubJsonPath
  218. }
  219. if !strings.HasSuffix(s.SubJsonPath, "/") {
  220. s.SubJsonPath += "/"
  221. }
  222. if !strings.HasPrefix(s.SubClashPath, "/") {
  223. s.SubClashPath = "/" + s.SubClashPath
  224. }
  225. if !strings.HasSuffix(s.SubClashPath, "/") {
  226. s.SubClashPath += "/"
  227. }
  228. if err := checkIPOrCIDRList(s.TrustedProxyCIDRs, "trusted proxy CIDR is not valid:"); err != nil {
  229. return err
  230. }
  231. // Rejected here rather than skipped at scan time: a typo in an allowlist
  232. // entry silently leaves the address unprotected until a trusted network gets banned.
  233. if err := checkIPOrCIDRList(s.IpLimitAllowlist, "IP limit allowlist entry is not valid:"); err != nil {
  234. return err
  235. }
  236. _, err := time.LoadLocation(s.TimeLocation)
  237. if err != nil {
  238. return common.NewError("time location not exist:", s.TimeLocation)
  239. }
  240. if s.SmtpFrom != "" {
  241. if _, err := mail.ParseAddress(s.SmtpFrom); err != nil {
  242. return common.NewError("SMTP from address is not valid:", s.SmtpFrom)
  243. }
  244. }
  245. return nil
  246. }
  247. // listenAddressesConflict reports whether two listen addresses on the same port
  248. // would collide at bind time. A wildcard listen ("", "0.0.0.0", "::") overlaps
  249. // every address, so it conflicts with anything on that port; two specific
  250. // addresses conflict only when identical.
  251. func listenAddressesConflict(a, b string) bool {
  252. if a == b {
  253. return true
  254. }
  255. return isWildcardListen(a) || isWildcardListen(b)
  256. }
  257. func isWildcardListen(listen string) bool {
  258. if listen == "" {
  259. return true
  260. }
  261. if ip := net.ParseIP(listen); ip != nil {
  262. return ip.IsUnspecified()
  263. }
  264. return false
  265. }
  266. type HostGroup struct {
  267. GroupId string `json:"groupId"`
  268. InboundIds []int `json:"inboundIds" validate:"required,min=1"`
  269. Hosts []string `json:"hosts" validate:"omitempty"`
  270. SortOrder int `json:"sortOrder"`
  271. Remark string `json:"remark" validate:"required,max=256"`
  272. ServerDescription string `json:"serverDescription" validate:"omitempty,max=64"`
  273. IsDisabled bool `json:"isDisabled"`
  274. IsHidden bool `json:"isHidden"`
  275. Tags []string `json:"tags"`
  276. Port int `json:"port" validate:"gte=0,lte=65535"`
  277. Security string `json:"security" validate:"omitempty,oneof=same tls none reality"`
  278. Sni string `json:"sni"`
  279. HostHeader string `json:"hostHeader"`
  280. Path string `json:"path"`
  281. Alpn []string `json:"alpn"`
  282. Fingerprint string `json:"fingerprint"`
  283. OverrideSniFromAddress bool `json:"overrideSniFromAddress"`
  284. KeepSniBlank bool `json:"keepSniBlank"`
  285. PinnedPeerCertSha256 []string `json:"pinnedPeerCertSha256"`
  286. VerifyPeerCertByName string `json:"verifyPeerCertByName"`
  287. AllowInsecure bool `json:"allowInsecure"`
  288. EchConfigList string `json:"echConfigList"`
  289. MuxParams string `json:"muxParams"`
  290. SockoptParams string `json:"sockoptParams"`
  291. FinalMask string `json:"finalMask"`
  292. VlessRoute string `json:"vlessRoute"`
  293. ExcludeFromSubTypes []string `json:"excludeFromSubTypes"`
  294. NodeGuids []string `json:"nodeGuids"`
  295. MihomoIpVersion string `json:"mihomoIpVersion" validate:"omitempty,oneof=dual ipv4 ipv6 ipv4-prefer ipv6-prefer"`
  296. MihomoX25519 bool `json:"mihomoX25519"`
  297. ShuffleHost bool `json:"shuffleHost"`
  298. }