1
0

entity.go 13 KB

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