entity.go 13 KB

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