1
0

entity.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. package entity
  2. import (
  3. "crypto/tls"
  4. "math"
  5. "net"
  6. "net/mail"
  7. "net/netip"
  8. "strings"
  9. "time"
  10. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  11. )
  12. type Msg struct {
  13. Success bool `json:"success"`
  14. Msg string `json:"msg"`
  15. Obj any `json:"obj"`
  16. }
  17. type AllSetting struct {
  18. WebListen string `json:"webListen" form:"webListen"`
  19. WebDomain string `json:"webDomain" form:"webDomain"`
  20. WebPort int `json:"webPort" form:"webPort" validate:"gte=1,lte=65535"`
  21. WebCertFile string `json:"webCertFile" form:"webCertFile"`
  22. WebKeyFile string `json:"webKeyFile" form:"webKeyFile"`
  23. WebBasePath string `json:"webBasePath" form:"webBasePath"`
  24. SessionMaxAge int `json:"sessionMaxAge" form:"sessionMaxAge" validate:"gte=1,lte=525600"`
  25. TrustedProxyCIDRs string `json:"trustedProxyCIDRs" form:"trustedProxyCIDRs"`
  26. RealityScanCandidates string `json:"realityScanCandidates" form:"realityScanCandidates"`
  27. IpLimitAllowlist string `json:"ipLimitAllowlist" form:"ipLimitAllowlist"`
  28. PanelOutbound string `json:"panelOutbound" form:"panelOutbound"`
  29. PageSize int `json:"pageSize" form:"pageSize" validate:"gte=0,lte=1000"`
  30. ExpireDiff int `json:"expireDiff" form:"expireDiff" validate:"gte=0"`
  31. TrafficDiff int `json:"trafficDiff" form:"trafficDiff" validate:"gte=0,lte=100"`
  32. RemarkTemplate string `json:"remarkTemplate" form:"remarkTemplate"`
  33. SubShowIdentityOnAllLinks bool `json:"subShowIdentityOnAllLinks" form:"subShowIdentityOnAllLinks"`
  34. SubInfoNodeEnable bool `json:"subInfoNodeEnable" form:"subInfoNodeEnable"`
  35. SubCalendarExpireInclusive bool `json:"subCalendarExpireInclusive" form:"subCalendarExpireInclusive"`
  36. SubExpiredTemplate string `json:"subExpiredTemplate" form:"subExpiredTemplate"`
  37. SubTrafficDepletedTemplate string `json:"subTrafficDepletedTemplate" form:"subTrafficDepletedTemplate"`
  38. Datepicker string `json:"datepicker" form:"datepicker"`
  39. TgBotEnable bool `json:"tgBotEnable" form:"tgBotEnable"`
  40. TgBotToken string `json:"tgBotToken" form:"tgBotToken"`
  41. TgBotProxy string `json:"tgBotProxy" form:"tgBotProxy"`
  42. TgBotAPIServer string `json:"tgBotAPIServer" form:"tgBotAPIServer"`
  43. TgBotChatId string `json:"tgBotChatId" form:"tgBotChatId"`
  44. TgRunTime string `json:"tgRunTime" form:"tgRunTime"`
  45. TgBotBackup bool `json:"tgBotBackup" form:"tgBotBackup"`
  46. TgCpu int `json:"tgCpu" form:"tgCpu" validate:"gte=0,lte=100"`
  47. TgMemory int `json:"tgMemory" form:"tgMemory" validate:"gte=0,lte=100"`
  48. TgLang string `json:"tgLang" form:"tgLang"`
  49. TgEnabledEvents string `json:"tgEnabledEvents" form:"tgEnabledEvents"`
  50. SmtpEnable bool `json:"smtpEnable" form:"smtpEnable"`
  51. SmtpHost string `json:"smtpHost" form:"smtpHost"`
  52. SmtpPort int `json:"smtpPort" form:"smtpPort" validate:"gte=1,lte=65535"`
  53. SmtpUsername string `json:"smtpUsername" form:"smtpUsername"`
  54. SmtpPassword string `json:"smtpPassword" form:"smtpPassword"`
  55. SmtpFrom string `json:"smtpFrom" form:"smtpFrom"`
  56. SmtpFromName string `json:"smtpFromName" form:"smtpFromName"`
  57. SmtpTo string `json:"smtpTo" form:"smtpTo"`
  58. SmtpEncryptionType string `json:"smtpEncryptionType" form:"smtpEncryptionType"`
  59. SmtpEnabledEvents string `json:"smtpEnabledEvents" form:"smtpEnabledEvents"`
  60. SmtpCpu int `json:"smtpCpu" form:"smtpCpu" validate:"gte=0,lte=100"`
  61. SmtpMemory int `json:"smtpMemory" form:"smtpMemory" validate:"gte=0,lte=100"`
  62. DiscordBotEnable bool `json:"discordBotEnable" form:"discordBotEnable"`
  63. DiscordBotToken string `json:"discordBotToken" form:"discordBotToken"`
  64. DiscordChannelId string `json:"discordChannelId" form:"discordChannelId"`
  65. DiscordAdminIds string `json:"discordAdminIds" form:"discordAdminIds"`
  66. DiscordRunTime string `json:"discordRunTime" form:"discordRunTime"`
  67. DiscordBotBackup bool `json:"discordBotBackup" form:"discordBotBackup"`
  68. DiscordCpu int `json:"discordCpu" form:"discordCpu" validate:"gte=0,lte=100"`
  69. DiscordMemory int `json:"discordMemory" form:"discordMemory" validate:"gte=0,lte=100"`
  70. DiscordLang string `json:"discordLang" form:"discordLang"`
  71. DiscordEnabledEvents string `json:"discordEnabledEvents" form:"discordEnabledEvents"`
  72. OutboundDownThreshold int `json:"outboundDownThreshold" form:"outboundDownThreshold" validate:"gte=1,lte=100"`
  73. TimeLocation string `json:"timeLocation" form:"timeLocation"`
  74. TwoFactorEnable bool `json:"twoFactorEnable" form:"twoFactorEnable"`
  75. TwoFactorToken string `json:"twoFactorToken" form:"twoFactorToken"`
  76. HappLinkEnable bool `json:"happLinkEnable" form:"happLinkEnable"`
  77. SubEnable bool `json:"subEnable" form:"subEnable"`
  78. SubJsonEnable bool `json:"subJsonEnable" form:"subJsonEnable"`
  79. SubJsonAutoDetect bool `json:"subJsonAutoDetect" form:"subJsonAutoDetect"`
  80. SubJsonAlwaysArray bool `json:"subJsonAlwaysArray" form:"subJsonAlwaysArray"`
  81. SubJsonUserAgentRegex string `json:"subJsonUserAgentRegex" form:"subJsonUserAgentRegex"`
  82. SubClashAutoDetect bool `json:"subClashAutoDetect" form:"subClashAutoDetect"`
  83. SubClashUserAgentRegex string `json:"subClashUserAgentRegex" form:"subClashUserAgentRegex"`
  84. SubTitle string `json:"subTitle" form:"subTitle"`
  85. SubSupportUrl string `json:"subSupportUrl" form:"subSupportUrl"`
  86. SubProfileMode string `json:"subProfileMode" form:"subProfileMode"`
  87. SubProfileUrl string `json:"subProfileUrl" form:"subProfileUrl"`
  88. SubAnnounce string `json:"subAnnounce" form:"subAnnounce"`
  89. SubEnableRouting bool `json:"subEnableRouting" form:"subEnableRouting"`
  90. SubRoutingRules string `json:"subRoutingRules" form:"subRoutingRules"`
  91. SubIncyEnableRouting bool `json:"subIncyEnableRouting" form:"subIncyEnableRouting"`
  92. SubIncyRoutingRules string `json:"subIncyRoutingRules" form:"subIncyRoutingRules"`
  93. SubListen string `json:"subListen" form:"subListen"`
  94. SubPort int `json:"subPort" form:"subPort" validate:"gte=1,lte=65535"`
  95. SubPath string `json:"subPath" form:"subPath"`
  96. SubDomain string `json:"subDomain" form:"subDomain"`
  97. SubCertFile string `json:"subCertFile" form:"subCertFile"`
  98. SubKeyFile string `json:"subKeyFile" form:"subKeyFile"`
  99. ExternalSubUserAgent string `json:"externalSubUserAgent" form:"externalSubUserAgent"`
  100. SubUpdates int `json:"subUpdates" form:"subUpdates" validate:"gte=0,lte=525600"`
  101. ExternalTrafficInformEnable bool `json:"externalTrafficInformEnable" form:"externalTrafficInformEnable"`
  102. ExternalTrafficInformURI string `json:"externalTrafficInformURI" form:"externalTrafficInformURI"`
  103. RestartXrayOnClientDisable bool `json:"restartXrayOnClientDisable" form:"restartXrayOnClientDisable"`
  104. SubEncrypt bool `json:"subEncrypt" form:"subEncrypt"`
  105. SubURI string `json:"subURI" form:"subURI"`
  106. SubJsonPath string `json:"subJsonPath" form:"subJsonPath"`
  107. SubJsonURI string `json:"subJsonURI" form:"subJsonURI"`
  108. SubClashEnable bool `json:"subClashEnable" form:"subClashEnable"`
  109. SubClashPath string `json:"subClashPath" form:"subClashPath"`
  110. SubClashURI string `json:"subClashURI" form:"subClashURI"`
  111. SubClashEnableRouting bool `json:"subClashEnableRouting" form:"subClashEnableRouting"`
  112. SubClashRules string `json:"subClashRules" form:"subClashRules"`
  113. SubJsonMux string `json:"subJsonMux" form:"subJsonMux"`
  114. SubJsonRules string `json:"subJsonRules" form:"subJsonRules"`
  115. SubJsonRoutingRules string `json:"subJsonRoutingRules" form:"subJsonRoutingRules"`
  116. SubJsonDns string `json:"subJsonDns" form:"subJsonDns"`
  117. SubJsonFinalMask string `json:"subJsonFinalMask" form:"subJsonFinalMask"`
  118. SubJsonObservatory string `json:"subJsonObservatory" form:"subJsonObservatory"`
  119. SubThemeDir string `json:"subThemeDir" form:"subThemeDir"`
  120. SubHideSettings bool `json:"subHideSettings" form:"subHideSettings"`
  121. // Happ client customization settings (app-management / routing / UX).
  122. SubHappAutoDetect bool `json:"subHappAutoDetect" form:"subHappAutoDetect"`
  123. SubHappProviderId string `json:"subHappProviderId" form:"subHappProviderId"`
  124. SubHappNewUrl string `json:"subHappNewUrl" form:"subHappNewUrl"`
  125. SubHappFallbackUrl string `json:"subHappFallbackUrl" form:"subHappFallbackUrl"`
  126. SubHappSubInfoColor string `json:"subHappSubInfoColor" form:"subHappSubInfoColor"`
  127. SubHappSubInfoText string `json:"subHappSubInfoText" form:"subHappSubInfoText"`
  128. SubHappSubInfoButtonText string `json:"subHappSubInfoButtonText" form:"subHappSubInfoButtonText"`
  129. SubHappSubInfoButtonLink string `json:"subHappSubInfoButtonLink" form:"subHappSubInfoButtonLink"`
  130. SubHappSubExpire bool `json:"subHappSubExpire" form:"subHappSubExpire"`
  131. SubHappSubExpireButtonLink string `json:"subHappSubExpireButtonLink" form:"subHappSubExpireButtonLink"`
  132. SubHappNotificationExpire bool `json:"subHappNotificationExpire" form:"subHappNotificationExpire"`
  133. SubHappNoLimit bool `json:"subHappNoLimit" form:"subHappNoLimit"`
  134. SubHappAlwaysHwid bool `json:"subHappAlwaysHwid" form:"subHappAlwaysHwid"`
  135. SubHappTunMode string `json:"subHappTunMode" form:"subHappTunMode"`
  136. SubHappTunType string `json:"subHappTunType" form:"subHappTunType"`
  137. SubHappExcludeRoutes string `json:"subHappExcludeRoutes" form:"subHappExcludeRoutes"`
  138. SubHappExcludeApns bool `json:"subHappExcludeApns" form:"subHappExcludeApns"`
  139. SubHappColorProfile string `json:"subHappColorProfile" form:"subHappColorProfile"`
  140. SubHappPingType string `json:"subHappPingType" form:"subHappPingType"`
  141. SubHappAutoConnect bool `json:"subHappAutoConnect" form:"subHappAutoConnect"`
  142. SubHappAutoConnectType string `json:"subHappAutoConnectType" form:"subHappAutoConnectType"`
  143. SubHappPerAppMode string `json:"subHappPerAppMode" form:"subHappPerAppMode"`
  144. SubHappPerAppList string `json:"subHappPerAppList" form:"subHappPerAppList"`
  145. SubHappLocalProxyAuth string `json:"subHappLocalProxyAuth" form:"subHappLocalProxyAuth"`
  146. // Incy client customization settings (app-management). A "" value omits
  147. // the header so the subscriber's own app setting is left alone.
  148. SubIncyAppAutoDetect bool `json:"subIncyAppAutoDetect" form:"subIncyAppAutoDetect"`
  149. SubIncyProfileDescription string `json:"subIncyProfileDescription" form:"subIncyProfileDescription"`
  150. SubIncySortOrder string `json:"subIncySortOrder" form:"subIncySortOrder"`
  151. SubIncySupportEmail string `json:"subIncySupportEmail" form:"subIncySupportEmail"`
  152. SubIncyAnnounceUrl string `json:"subIncyAnnounceUrl" form:"subIncyAnnounceUrl"`
  153. SubIncyPremiumUrl string `json:"subIncyPremiumUrl" form:"subIncyPremiumUrl"`
  154. SubIncyBannerText string `json:"subIncyBannerText" form:"subIncyBannerText"`
  155. SubIncyBannerButtonText string `json:"subIncyBannerButtonText" form:"subIncyBannerButtonText"`
  156. SubIncyBannerButtonUrl string `json:"subIncyBannerButtonUrl" form:"subIncyBannerButtonUrl"`
  157. SubIncyBannerBgColor string `json:"subIncyBannerBgColor" form:"subIncyBannerBgColor"`
  158. SubIncyBannerButtonColor string `json:"subIncyBannerButtonColor" form:"subIncyBannerButtonColor"`
  159. SubIncyHideUrl string `json:"subIncyHideUrl" form:"subIncyHideUrl"`
  160. SubIncyHideCheck string `json:"subIncyHideCheck" form:"subIncyHideCheck"`
  161. SubIncyNoLimitEnabled string `json:"subIncyNoLimitEnabled" form:"subIncyNoLimitEnabled"`
  162. SubIncyPerAppEnable string `json:"subIncyPerAppEnable" form:"subIncyPerAppEnable"`
  163. SubIncyPerAppMode string `json:"subIncyPerAppMode" form:"subIncyPerAppMode"`
  164. SubIncyPerAppList string `json:"subIncyPerAppList" form:"subIncyPerAppList"`
  165. SubIncyFragmentationEnable string `json:"subIncyFragmentationEnable" form:"subIncyFragmentationEnable"`
  166. SubIncyFragmentLength string `json:"subIncyFragmentLength" form:"subIncyFragmentLength"`
  167. SubIncyFragmentInterval string `json:"subIncyFragmentInterval" form:"subIncyFragmentInterval"`
  168. SubIncyFragmentPackets string `json:"subIncyFragmentPackets" form:"subIncyFragmentPackets"`
  169. SubIncyNoisesEnable string `json:"subIncyNoisesEnable" form:"subIncyNoisesEnable"`
  170. SubIncyNoisesType string `json:"subIncyNoisesType" form:"subIncyNoisesType"`
  171. SubIncyNoisesPacket string `json:"subIncyNoisesPacket" form:"subIncyNoisesPacket"`
  172. SubIncyNoisesDelay string `json:"subIncyNoisesDelay" form:"subIncyNoisesDelay"`
  173. SubIncyResolveEnable string `json:"subIncyResolveEnable" form:"subIncyResolveEnable"`
  174. SubIncyResolveDnsDomain string `json:"subIncyResolveDnsDomain" form:"subIncyResolveDnsDomain"`
  175. SubIncyResolveDnsIp string `json:"subIncyResolveDnsIp" form:"subIncyResolveDnsIp"`
  176. LdapEnable bool `json:"ldapEnable" form:"ldapEnable"`
  177. LdapHost string `json:"ldapHost" form:"ldapHost"`
  178. LdapPort int `json:"ldapPort" form:"ldapPort" validate:"gte=0,lte=65535"`
  179. LdapUseTLS bool `json:"ldapUseTLS" form:"ldapUseTLS"`
  180. LdapInsecureSkipVerify bool `json:"ldapInsecureSkipVerify" form:"ldapInsecureSkipVerify"`
  181. LdapBindDN string `json:"ldapBindDN" form:"ldapBindDN"`
  182. LdapPassword string `json:"ldapPassword" form:"ldapPassword"`
  183. LdapBaseDN string `json:"ldapBaseDN" form:"ldapBaseDN"`
  184. LdapUserFilter string `json:"ldapUserFilter" form:"ldapUserFilter"`
  185. LdapUserAttr string `json:"ldapUserAttr" form:"ldapUserAttr"`
  186. LdapVlessField string `json:"ldapVlessField" form:"ldapVlessField"`
  187. LdapSyncCron string `json:"ldapSyncCron" form:"ldapSyncCron"`
  188. LdapFlagField string `json:"ldapFlagField" form:"ldapFlagField"`
  189. LdapTruthyValues string `json:"ldapTruthyValues" form:"ldapTruthyValues"`
  190. LdapInvertFlag bool `json:"ldapInvertFlag" form:"ldapInvertFlag"`
  191. LdapInboundTags string `json:"ldapInboundTags" form:"ldapInboundTags"`
  192. LdapAutoCreate bool `json:"ldapAutoCreate" form:"ldapAutoCreate"`
  193. LdapAutoDelete bool `json:"ldapAutoDelete" form:"ldapAutoDelete"`
  194. LdapDefaultTotalGB int `json:"ldapDefaultTotalGB" form:"ldapDefaultTotalGB" validate:"gte=0"`
  195. LdapDefaultExpiryDays int `json:"ldapDefaultExpiryDays" form:"ldapDefaultExpiryDays" validate:"gte=0"`
  196. LdapDefaultLimitIP int `json:"ldapDefaultLimitIP" form:"ldapDefaultLimitIP" validate:"gte=0"`
  197. WarpUpdateInterval int `json:"warpUpdateInterval" form:"warpUpdateInterval" validate:"gte=0"`
  198. }
  199. type AllSettingView struct {
  200. AllSetting
  201. HasTgBotToken bool `json:"hasTgBotToken"`
  202. HasTwoFactorToken bool `json:"hasTwoFactorToken"`
  203. HasLdapPassword bool `json:"hasLdapPassword"`
  204. HasApiToken bool `json:"hasApiToken"`
  205. HasWarpSecret bool `json:"hasWarpSecret"`
  206. HasNordSecret bool `json:"hasNordSecret"`
  207. HasSmtpPassword bool `json:"hasSmtpPassword"`
  208. HasDiscordBotToken bool `json:"hasDiscordBotToken"`
  209. }
  210. func pathHasForbiddenChar(s string) bool {
  211. for _, r := range s {
  212. if r == '\\' || r == ' ' || r < 0x20 || r == 0x7f {
  213. return true
  214. }
  215. }
  216. return false
  217. }
  218. // CheckNetipAddrOrPrefixList mirrors parseIpLimitAllowlist exactly: net and netip
  219. // disagree (net accepts "/024", netip does not), so save and scan must share rules.
  220. func CheckNetipAddrOrPrefixList(list, message string) error {
  221. for entry := range strings.SplitSeq(list, ",") {
  222. entry = strings.TrimSpace(entry)
  223. if entry == "" {
  224. continue
  225. }
  226. if _, err := netip.ParseAddr(entry); err == nil {
  227. continue
  228. }
  229. if _, err := netip.ParsePrefix(entry); err != nil {
  230. return common.NewError(message, entry)
  231. }
  232. }
  233. return nil
  234. }
  235. // checkIPOrCIDRList rejects the first comma-separated entry that is neither a
  236. // bare address nor a CIDR, naming it with the caller's message.
  237. func checkIPOrCIDRList(list, message string) error {
  238. for entry := range strings.SplitSeq(list, ",") {
  239. entry = strings.TrimSpace(entry)
  240. if entry == "" {
  241. continue
  242. }
  243. if ip := net.ParseIP(entry); ip != nil {
  244. continue
  245. }
  246. if _, _, err := net.ParseCIDR(entry); err != nil {
  247. return common.NewError(message, entry)
  248. }
  249. }
  250. return nil
  251. }
  252. func (s *AllSetting) CheckValid() error {
  253. if s.WebListen != "" {
  254. ip := net.ParseIP(s.WebListen)
  255. if ip == nil {
  256. return common.NewError("web listen is not valid ip:", s.WebListen)
  257. }
  258. }
  259. if s.SubListen != "" {
  260. ip := net.ParseIP(s.SubListen)
  261. if ip == nil {
  262. return common.NewError("Sub listen is not valid ip:", s.SubListen)
  263. }
  264. }
  265. if s.WebPort <= 0 || s.WebPort > math.MaxUint16 {
  266. return common.NewError("web port is not a valid port:", s.WebPort)
  267. }
  268. if s.SubPort <= 0 || s.SubPort > math.MaxUint16 {
  269. return common.NewError("Sub port is not a valid port:", s.SubPort)
  270. }
  271. if (s.SubPort == s.WebPort) && listenAddressesConflict(s.WebListen, s.SubListen) {
  272. return common.NewError("Sub and Web could not use same ip:port, ", s.SubListen, ":", s.SubPort, " & ", s.WebListen, ":", s.WebPort)
  273. }
  274. if s.WebCertFile != "" || s.WebKeyFile != "" {
  275. _, err := tls.LoadX509KeyPair(s.WebCertFile, s.WebKeyFile)
  276. if err != nil {
  277. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.WebCertFile, s.WebKeyFile, err)
  278. }
  279. }
  280. if s.SubCertFile != "" || s.SubKeyFile != "" {
  281. _, err := tls.LoadX509KeyPair(s.SubCertFile, s.SubKeyFile)
  282. if err != nil {
  283. return common.NewErrorf("cert file <%v> or key file <%v> invalid: %v", s.SubCertFile, s.SubKeyFile, err)
  284. }
  285. }
  286. for _, p := range []struct {
  287. name string
  288. value string
  289. }{
  290. {"web base path", s.WebBasePath},
  291. {"subscription path", s.SubPath},
  292. {"subscription JSON path", s.SubJsonPath},
  293. {"subscription Clash path", s.SubClashPath},
  294. } {
  295. if pathHasForbiddenChar(p.value) {
  296. return common.NewError("URI path contains an invalid character:", p.name)
  297. }
  298. }
  299. if !strings.HasPrefix(s.WebBasePath, "/") {
  300. s.WebBasePath = "/" + s.WebBasePath
  301. }
  302. if !strings.HasSuffix(s.WebBasePath, "/") {
  303. s.WebBasePath += "/"
  304. }
  305. if !strings.HasPrefix(s.SubPath, "/") {
  306. s.SubPath = "/" + s.SubPath
  307. }
  308. if !strings.HasSuffix(s.SubPath, "/") {
  309. s.SubPath += "/"
  310. }
  311. if !strings.HasPrefix(s.SubJsonPath, "/") {
  312. s.SubJsonPath = "/" + s.SubJsonPath
  313. }
  314. if !strings.HasSuffix(s.SubJsonPath, "/") {
  315. s.SubJsonPath += "/"
  316. }
  317. if !strings.HasPrefix(s.SubClashPath, "/") {
  318. s.SubClashPath = "/" + s.SubClashPath
  319. }
  320. if !strings.HasSuffix(s.SubClashPath, "/") {
  321. s.SubClashPath += "/"
  322. }
  323. if err := checkIPOrCIDRList(s.TrustedProxyCIDRs, "trusted proxy CIDR is not valid:"); err != nil {
  324. return err
  325. }
  326. // Rejected here rather than skipped at scan time: a typo in an allowlist
  327. // entry silently leaves the address unprotected until a trusted network gets banned.
  328. if err := CheckNetipAddrOrPrefixList(s.IpLimitAllowlist, "IP limit allowlist entry is not valid:"); err != nil {
  329. return err
  330. }
  331. _, err := time.LoadLocation(s.TimeLocation)
  332. if err != nil {
  333. return common.NewError("time location not exist:", s.TimeLocation)
  334. }
  335. if s.SmtpFrom != "" {
  336. if _, err := mail.ParseAddress(s.SmtpFrom); err != nil {
  337. return common.NewError("SMTP from address is not valid:", s.SmtpFrom)
  338. }
  339. }
  340. return nil
  341. }
  342. // listenAddressesConflict reports whether two listen addresses on the same port
  343. // would collide at bind time. A wildcard listen ("", "0.0.0.0", "::") overlaps
  344. // every address, so it conflicts with anything on that port; two specific
  345. // addresses conflict only when identical.
  346. func listenAddressesConflict(a, b string) bool {
  347. if a == b {
  348. return true
  349. }
  350. return isWildcardListen(a) || isWildcardListen(b)
  351. }
  352. func isWildcardListen(listen string) bool {
  353. if listen == "" {
  354. return true
  355. }
  356. if ip := net.ParseIP(listen); ip != nil {
  357. return ip.IsUnspecified()
  358. }
  359. return false
  360. }
  361. type HostGroup struct {
  362. GroupId string `json:"groupId"`
  363. InboundIds []int `json:"inboundIds" validate:"required,min=1"`
  364. Hosts []string `json:"hosts" validate:"omitempty"`
  365. SortOrder int `json:"sortOrder"`
  366. Remark string `json:"remark" validate:"required,max=256"`
  367. ServerDescription string `json:"serverDescription" validate:"omitempty,max=64"`
  368. IsDisabled bool `json:"isDisabled"`
  369. IsHidden bool `json:"isHidden"`
  370. Tags []string `json:"tags"`
  371. Port int `json:"port" validate:"gte=0,lte=65535"`
  372. Security string `json:"security" validate:"omitempty,oneof=same tls none reality"`
  373. Sni string `json:"sni"`
  374. HostHeader string `json:"hostHeader"`
  375. Path string `json:"path"`
  376. Alpn []string `json:"alpn"`
  377. Fingerprint string `json:"fingerprint"`
  378. CipherSuites string `json:"cipherSuites"`
  379. OverrideSniFromAddress bool `json:"overrideSniFromAddress"`
  380. KeepSniBlank bool `json:"keepSniBlank"`
  381. PinnedPeerCertSha256 []string `json:"pinnedPeerCertSha256"`
  382. VerifyPeerCertByName string `json:"verifyPeerCertByName"`
  383. AllowInsecure bool `json:"allowInsecure"`
  384. EchConfigList string `json:"echConfigList"`
  385. MuxParams string `json:"muxParams"`
  386. SockoptParams string `json:"sockoptParams"`
  387. FinalMask string `json:"finalMask"`
  388. VlessRoute string `json:"vlessRoute"`
  389. ExcludeFromSubTypes []string `json:"excludeFromSubTypes"`
  390. NodeGuids []string `json:"nodeGuids"`
  391. MihomoIpVersion string `json:"mihomoIpVersion" validate:"omitempty,oneof=dual ipv4 ipv6 ipv4-prefer ipv6-prefer"`
  392. MihomoX25519 bool `json:"mihomoX25519"`
  393. ShuffleHost bool `json:"shuffleHost"`
  394. }