entity.go 12 KB

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