entity.go 14 KB

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