entity.go 22 KB

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