setting.go 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  1. package service
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "os"
  10. "reflect"
  11. "regexp"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/google/uuid"
  16. "gorm.io/gorm"
  17. "github.com/mhsanaei/3x-ui/v3/internal/config"
  18. "github.com/mhsanaei/3x-ui/v3/internal/database"
  19. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  20. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  21. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  22. "github.com/mhsanaei/3x-ui/v3/internal/util/netproxy"
  23. "github.com/mhsanaei/3x-ui/v3/internal/util/random"
  24. "github.com/mhsanaei/3x-ui/v3/internal/util/reflect_util"
  25. "github.com/mhsanaei/3x-ui/v3/internal/util/totp"
  26. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  27. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  28. "github.com/mhsanaei/3x-ui/v3/internal/xray/dnsconf"
  29. )
  30. //go:embed config.json
  31. var xrayTemplateConfig string
  32. const (
  33. DefaultSubClashUserAgentRegex = `(?i)(clash|mihomo)`
  34. DefaultSubJsonUserAgentRegex = ``
  35. DefaultRemarkTemplate = "{{INBOUND}}-{{EMAIL}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D"
  36. DefaultSubExpiredTemplate = "⛔ {{EMAIL}} | Expired: {{EXPIRE_DATE}}"
  37. DefaultSubTrafficDepletedTemplate = "🚫 {{EMAIL}} | Traffic Depleted | {{TRAFFIC_USED}}/{{TRAFFIC_TOTAL}}"
  38. DefaultTrustedProxyCIDRs = "127.0.0.1/32,::1/128"
  39. maxRegexLength = 2048
  40. )
  41. // Built-in profile links expose the subscription URL and require an explicit opt-in.
  42. const (
  43. SubProfileModeNone = "none"
  44. SubProfileModeBuiltin = "builtin"
  45. SubProfileModeCustom = "custom"
  46. )
  47. var defaultValueMap = map[string]string{
  48. "xrayTemplateConfig": xrayTemplateConfig,
  49. "webListen": "",
  50. "webDomain": "",
  51. "webPort": "2053",
  52. "webCertFile": "",
  53. "webKeyFile": "",
  54. "secret": random.Seq(32),
  55. "panelGuid": uuid.NewString(),
  56. "apiToken": "",
  57. // Node mTLS material (opt-in). All default empty: the CA + master client
  58. // cert are minted lazily on first use, and the node-side trust CA is pasted
  59. // in by the operator. Kept out of entity.AllSetting so private keys never
  60. // reach the settings UI/export.
  61. "nodeMtlsCaCertPem": "",
  62. "nodeMtlsCaKeyPem": "",
  63. "nodeMtlsClientCertPem": "",
  64. "nodeMtlsClientKeyPem": "",
  65. "nodeMtlsClientCertSha256": "",
  66. "nodeMtlsClientCAPem": "",
  67. "webBasePath": normalizeBasePath(getEnv("XUI_INIT_WEB_BASE_PATH", "/")),
  68. "sessionMaxAge": "360",
  69. "trustedProxyCIDRs": DefaultTrustedProxyCIDRs,
  70. "realityScanCandidates": DefaultRealityScanCandidatesCSV,
  71. "ipLimitAllowlist": "",
  72. "pageSize": "25",
  73. "expireDiff": "0",
  74. "trafficDiff": "0",
  75. "remarkTemplate": DefaultRemarkTemplate,
  76. "subShowIdentityOnAllLinks": "false",
  77. "subInfoNodeEnable": "false",
  78. "subCalendarExpireInclusive": "false",
  79. "subExpiredTemplate": DefaultSubExpiredTemplate,
  80. "subTrafficDepletedTemplate": DefaultSubTrafficDepletedTemplate,
  81. "timeLocation": "Local",
  82. "tgBotEnable": "false",
  83. "tgBotToken": "",
  84. "tgBotProxy": "",
  85. "tgBotAPIServer": "",
  86. "tgBotChatId": "",
  87. "tgRunTime": "@daily",
  88. "tgBotBackup": "false",
  89. "tgCpu": "80",
  90. "tgMemory": "80",
  91. "tgLang": "en-US",
  92. "twoFactorEnable": "false",
  93. "twoFactorToken": "",
  94. "happLinkEnable": "false",
  95. "subEnable": "true",
  96. "subJsonEnable": "false",
  97. "subJsonAutoDetect": "false",
  98. "subJsonAlwaysArray": "false",
  99. "subJsonUserAgentRegex": "",
  100. "subClashAutoDetect": "false",
  101. "subClashUserAgentRegex": "",
  102. "subTitle": "",
  103. "subSupportUrl": "",
  104. "subProfileMode": SubProfileModeNone,
  105. "subProfileUrl": "",
  106. "subAnnounce": "",
  107. "subEnableRouting": "false",
  108. "subRoutingRules": "",
  109. "subHideSettings": "false",
  110. "subHappAutoDetect": "false",
  111. "subHappProviderId": "",
  112. "subHappNewUrl": "",
  113. "subHappFallbackUrl": "",
  114. "subHappSubInfoColor": "blue",
  115. "subHappSubInfoText": "",
  116. "subHappSubInfoButtonText": "",
  117. "subHappSubInfoButtonLink": "",
  118. "subHappSubExpire": "false",
  119. "subHappSubExpireButtonLink": "",
  120. "subHappNotificationExpire": "false",
  121. "subHappNoLimit": "false",
  122. "subHappAlwaysHwid": "false",
  123. "subHappTunMode": "",
  124. "subHappTunType": "",
  125. "subHappExcludeRoutes": "",
  126. "subHappExcludeApns": "false",
  127. "subHappColorProfile": "",
  128. "subHappPingType": "",
  129. "subHappAutoConnect": "false",
  130. "subHappAutoConnectType": "lowestdelay",
  131. "subHappPerAppMode": "off",
  132. "subHappPerAppList": "",
  133. "subIncyEnableRouting": "false",
  134. "subIncyRoutingRules": "",
  135. "subListen": "",
  136. "subPort": "2096",
  137. "subPath": "/sub/",
  138. "subDomain": "",
  139. "subCertFile": "",
  140. "subKeyFile": "",
  141. "subUpdates": "12",
  142. "subEncrypt": "true",
  143. "subURI": "",
  144. "subJsonPath": "/json/",
  145. "subJsonURI": "",
  146. "subClashEnable": "false",
  147. "subClashPath": "/clash/",
  148. "subClashURI": "",
  149. "subClashEnableRouting": "false",
  150. "subClashRules": "",
  151. "subJsonMux": "",
  152. "subJsonRules": "",
  153. "subJsonRoutingRules": "",
  154. "subJsonDns": "",
  155. "subJsonFinalMask": "",
  156. "subJsonObservatory": "",
  157. "subThemeDir": "",
  158. "datepicker": "gregorian",
  159. "warp": "",
  160. "warpUpdateInterval": "0",
  161. "nord": "",
  162. "pia": "",
  163. "externalTrafficInformEnable": "false",
  164. "externalTrafficInformURI": "",
  165. "restartXrayOnClientDisable": "true",
  166. "xrayOutboundTestUrl": "https://www.google.com/generate_204",
  167. "panelOutbound": "",
  168. "devChannelEnable": "false",
  169. // LDAP defaults
  170. "ldapEnable": "false",
  171. "ldapHost": "",
  172. "ldapPort": "389",
  173. "ldapUseTLS": "false",
  174. "ldapInsecureSkipVerify": "false",
  175. "ldapBindDN": "",
  176. "ldapPassword": "",
  177. "ldapBaseDN": "",
  178. "ldapUserFilter": "(objectClass=person)",
  179. "ldapUserAttr": "mail",
  180. "ldapVlessField": "vless_enabled",
  181. "ldapSyncCron": "@every 1m",
  182. "ldapFlagField": "",
  183. "ldapTruthyValues": "true,1,yes,on",
  184. "ldapInvertFlag": "false",
  185. "ldapInboundTags": "",
  186. "ldapAutoCreate": "false",
  187. "ldapAutoDelete": "false",
  188. "ldapDefaultTotalGB": "0",
  189. "ldapDefaultExpiryDays": "0",
  190. "ldapDefaultLimitIP": "0",
  191. // Event bus — per-subscriber event filtering (empty = all disabled)
  192. "tgEnabledEvents": "login.attempt,cpu.high",
  193. "smtpEnabledEvents": "login.attempt,cpu.high",
  194. "smtpCpu": "80",
  195. "smtpMemory": "80",
  196. // Consecutive failed observatory probes before an outbound.down event fires
  197. "outboundDownThreshold": "3",
  198. // Email (SMTP) notifications
  199. "smtpEnable": "false",
  200. "smtpHost": "",
  201. "smtpPort": "587",
  202. "smtpUsername": "",
  203. "smtpPassword": "",
  204. "smtpFrom": "",
  205. "smtpFromName": "",
  206. "smtpTo": "",
  207. "smtpEncryptionType": "starttls", // no, starttls, tls
  208. // Discord bot notifications
  209. "discordBotEnable": "false",
  210. "discordBotToken": "",
  211. "discordChannelId": "",
  212. "discordAdminIds": "",
  213. "discordRunTime": "@daily",
  214. "discordBotBackup": "false",
  215. "discordCpu": "80",
  216. "discordMemory": "80",
  217. "discordLang": "en-US",
  218. "discordEnabledEvents": "login.attempt,cpu.high",
  219. }
  220. // SettingService provides business logic for application settings management.
  221. // It handles configuration storage, retrieval, and validation for all system settings.
  222. type SettingService struct{}
  223. func (s *SettingService) GetAllSetting() (*entity.AllSetting, error) {
  224. db := database.GetDB()
  225. settings := make([]*model.Setting, 0)
  226. err := db.Model(model.Setting{}).Not("key = ?", "xrayTemplateConfig").Find(&settings).Error
  227. if err != nil {
  228. return nil, err
  229. }
  230. allSetting := &entity.AllSetting{}
  231. t := reflect.TypeFor[entity.AllSetting]()
  232. v := reflect.ValueOf(allSetting).Elem()
  233. fields := reflect_util.GetFields(t)
  234. setSetting := func(key, value string) (err error) {
  235. defer func() {
  236. panicErr := recover()
  237. if panicErr != nil {
  238. err = errors.New(fmt.Sprint(panicErr))
  239. }
  240. }()
  241. var found bool
  242. var field reflect.StructField
  243. for _, f := range fields {
  244. if f.Tag.Get("json") == key {
  245. field = f
  246. found = true
  247. break
  248. }
  249. }
  250. if !found {
  251. // Some settings are automatically generated, no need to return to the front end to modify the user
  252. return nil
  253. }
  254. fieldV := v.FieldByName(field.Name)
  255. switch t := fieldV.Interface().(type) {
  256. case int:
  257. n, err := strconv.ParseInt(effectiveSettingValue(key, value), 10, 64)
  258. if err != nil {
  259. return err
  260. }
  261. fieldV.SetInt(n)
  262. case string:
  263. fieldV.SetString(value)
  264. case bool:
  265. fieldV.SetBool(effectiveSettingValue(key, value) == "true")
  266. default:
  267. return common.NewErrorf("unknown field %v type %v", key, t)
  268. }
  269. return
  270. }
  271. keyMap := map[string]bool{}
  272. for _, setting := range settings {
  273. err := setSetting(setting.Key, setting.Value)
  274. if err != nil {
  275. return nil, err
  276. }
  277. keyMap[setting.Key] = true
  278. }
  279. for key, value := range defaultValueMap {
  280. if keyMap[key] {
  281. continue
  282. }
  283. err := setSetting(key, value)
  284. if err != nil {
  285. return nil, err
  286. }
  287. }
  288. // A missing mode must still preserve URLs configured before modes existed.
  289. if !keyMap["subProfileMode"] {
  290. allSetting.SubProfileMode = ""
  291. }
  292. allSetting.SubProfileMode = effectiveSubProfileMode(allSetting.SubProfileMode, allSetting.SubProfileUrl)
  293. return allSetting, nil
  294. }
  295. func (s *SettingService) GetAllSettingView() (*entity.AllSettingView, error) {
  296. allSetting, err := s.GetAllSetting()
  297. if err != nil {
  298. return nil, err
  299. }
  300. view := &entity.AllSettingView{AllSetting: *allSetting}
  301. view.HasTgBotToken = secretConfigured(allSetting.TgBotToken)
  302. view.HasTwoFactorToken = secretConfigured(allSetting.TwoFactorToken)
  303. view.HasLdapPassword = secretConfigured(allSetting.LdapPassword)
  304. view.HasWarpSecret = secretConfigured(mustString(s.GetWarp()))
  305. view.HasNordSecret = secretConfigured(mustString(s.GetNord()))
  306. view.HasSmtpPassword = secretConfigured(allSetting.SmtpPassword)
  307. view.HasDiscordBotToken = secretConfigured(allSetting.DiscordBotToken)
  308. var apiTokenCount int64
  309. if err := database.GetDB().Model(model.ApiToken{}).Where("enabled = ?", true).Count(&apiTokenCount).Error; err == nil {
  310. view.HasApiToken = apiTokenCount > 0
  311. }
  312. view.TgBotToken = ""
  313. view.TwoFactorToken = ""
  314. view.LdapPassword = ""
  315. view.SmtpPassword = ""
  316. view.DiscordBotToken = ""
  317. return view, nil
  318. }
  319. func secretConfigured(value string) bool {
  320. return strings.TrimSpace(value) != ""
  321. }
  322. func mustString(value string, _ error) string {
  323. return value
  324. }
  325. func getEnv(key, fallback string) string {
  326. val, ok := os.LookupEnv(key)
  327. if !ok {
  328. return fallback
  329. }
  330. val = strings.TrimSpace(val)
  331. if val == "" {
  332. return fallback
  333. }
  334. return val
  335. }
  336. func (s *SettingService) ResetSettings() error {
  337. db := database.GetDB()
  338. return db.Transaction(func(tx *gorm.DB) error {
  339. if err := tx.Where("1 = 1").Delete(model.Setting{}).Error; err != nil {
  340. return err
  341. }
  342. paths := []model.Setting{
  343. {Key: "subPath", Value: "/" + random.NumLower(16) + "/"},
  344. {Key: "subJsonPath", Value: "/" + random.NumLower(16) + "/"},
  345. {Key: "subClashPath", Value: "/" + random.NumLower(16) + "/"},
  346. }
  347. return tx.Create(&paths).Error
  348. })
  349. }
  350. func (s *SettingService) getSetting(key string) (*model.Setting, error) {
  351. db := database.GetDB()
  352. setting := &model.Setting{}
  353. err := db.Model(model.Setting{}).Where("key = ?", key).First(setting).Error
  354. if err != nil {
  355. return nil, err
  356. }
  357. return setting, nil
  358. }
  359. func (s *SettingService) saveSetting(key string, value string) error {
  360. setting, err := s.getSetting(key)
  361. db := database.GetDB()
  362. if database.IsNotFound(err) {
  363. return db.Create(&model.Setting{
  364. Key: key,
  365. Value: value,
  366. }).Error
  367. } else if err != nil {
  368. return err
  369. }
  370. setting.Key = key
  371. setting.Value = value
  372. return db.Save(setting).Error
  373. }
  374. func (s *SettingService) getString(key string) (string, error) {
  375. setting, err := s.getSetting(key)
  376. if database.IsNotFound(err) {
  377. value, ok := defaultValueMap[key]
  378. if !ok {
  379. return "", common.NewErrorf("key <%v> not in defaultValueMap", key)
  380. }
  381. return value, nil
  382. } else if err != nil {
  383. return "", err
  384. }
  385. return setting.Value, nil
  386. }
  387. func (s *SettingService) setString(key string, value string) error {
  388. return s.saveSetting(key, value)
  389. }
  390. func effectiveSettingValue(key, stored string) string {
  391. if stored == "" {
  392. if def, ok := defaultValueMap[key]; ok {
  393. return def
  394. }
  395. }
  396. return stored
  397. }
  398. func (s *SettingService) getBool(key string) (bool, error) {
  399. str, err := s.getString(key)
  400. if err != nil {
  401. return false, err
  402. }
  403. return strconv.ParseBool(effectiveSettingValue(key, str))
  404. }
  405. func (s *SettingService) setBool(key string, value bool) error {
  406. return s.setString(key, strconv.FormatBool(value))
  407. }
  408. func (s *SettingService) getInt(key string) (int, error) {
  409. str, err := s.getString(key)
  410. if err != nil {
  411. return 0, err
  412. }
  413. return strconv.Atoi(effectiveSettingValue(key, str))
  414. }
  415. func (s *SettingService) setInt(key string, value int) error {
  416. return s.setString(key, strconv.Itoa(value))
  417. }
  418. func (s *SettingService) GetWarpLastUpdate() (int64, error) {
  419. setting, err := s.getSetting("warpLastUpdate")
  420. if database.IsNotFound(err) {
  421. return 0, nil
  422. }
  423. if err != nil {
  424. return 0, err
  425. }
  426. if setting.Value == "" {
  427. return 0, nil
  428. }
  429. return strconv.ParseInt(setting.Value, 10, 64)
  430. }
  431. func (s *SettingService) SetWarpLastUpdate(val int64) error {
  432. return s.saveSetting("warpLastUpdate", strconv.FormatInt(val, 10))
  433. }
  434. func (s *SettingService) SetWarpUpdateInterval(val int) error {
  435. return s.setInt("warpUpdateInterval", val)
  436. }
  437. func (s *SettingService) GetXrayConfigTemplate() (string, error) {
  438. return s.getString("xrayTemplateConfig")
  439. }
  440. func (s *SettingService) GetXrayOutboundTestUrl() (string, error) {
  441. return s.getString("xrayOutboundTestUrl")
  442. }
  443. func (s *SettingService) SetXrayOutboundTestUrl(url string) error {
  444. clean, err := SanitizeHTTPURL(url)
  445. if err != nil {
  446. return err
  447. }
  448. return s.setString("xrayOutboundTestUrl", clean)
  449. }
  450. func (s *SettingService) GetListen() (string, error) {
  451. return s.getString("webListen")
  452. }
  453. func (s *SettingService) SetListen(ip string) error {
  454. return s.setString("webListen", ip)
  455. }
  456. func (s *SettingService) GetWebDomain() (string, error) {
  457. return s.getString("webDomain")
  458. }
  459. func (s *SettingService) GetTgBotToken() (string, error) {
  460. return s.getString("tgBotToken")
  461. }
  462. func (s *SettingService) SetTgBotToken(token string) error {
  463. return s.setString("tgBotToken", token)
  464. }
  465. func (s *SettingService) GetTgBotProxy() (string, error) {
  466. return s.getString("tgBotProxy")
  467. }
  468. func (s *SettingService) SetTgBotProxy(token string) error {
  469. return s.setString("tgBotProxy", token)
  470. }
  471. // GetPanelOutbound returns the Xray outbound tag the panel's own outbound
  472. // requests (version checks, Telegram, subscription fetches) are routed through.
  473. func (s *SettingService) GetPanelOutbound() (string, error) {
  474. return s.getString("panelOutbound")
  475. }
  476. func (s *SettingService) SetPanelOutbound(tag string) error {
  477. return s.setString("panelOutbound", tag)
  478. }
  479. // PanelEgressProxyURL resolves the loopback SOCKS bridge that the generated
  480. // config exposes when a panel outbound is configured (see injectPanelEgress).
  481. // It returns "" — meaning a direct connection — when the feature is off or
  482. // the bridge is not present in the running core yet.
  483. func (s *SettingService) PanelEgressProxyURL() string {
  484. tag, err := s.GetPanelOutbound()
  485. if err != nil || tag == "" {
  486. return ""
  487. }
  488. proc := XrayProcess()
  489. if proc == nil || !proc.IsRunning() {
  490. logger.Warning("panel outbound [", tag, "] is set but Xray is not running, using a direct connection")
  491. return ""
  492. }
  493. cfg := proc.GetConfig()
  494. if cfg == nil {
  495. return ""
  496. }
  497. for i := range cfg.InboundConfigs {
  498. if cfg.InboundConfigs[i].Tag == PanelEgressInboundTag {
  499. return fmt.Sprintf("socks5://127.0.0.1:%d", cfg.InboundConfigs[i].Port)
  500. }
  501. }
  502. logger.Warning("panel outbound [", tag, "] is set but the egress bridge is not in the running config, using a direct connection")
  503. return ""
  504. }
  505. func (s *SettingService) NodeEgressProxyURL(nodeID int) string {
  506. tag := NodeEgressInboundTag(nodeID)
  507. proc := XrayProcess()
  508. if proc == nil || !proc.IsRunning() {
  509. logger.Warning("node outbound [", tag, "] is set but Xray is not running, using a direct connection")
  510. return ""
  511. }
  512. cfg := proc.GetConfig()
  513. if cfg == nil {
  514. return ""
  515. }
  516. for i := range cfg.InboundConfigs {
  517. if cfg.InboundConfigs[i].Tag == tag {
  518. return fmt.Sprintf("socks5://127.0.0.1:%d", cfg.InboundConfigs[i].Port)
  519. }
  520. }
  521. logger.Warning("node outbound [", tag, "] is set but the egress bridge is not in the running config, using a direct connection")
  522. return ""
  523. }
  524. // NewProxiedHTTPClient returns an HTTP client that routes the panel's own
  525. // outbound requests through the configured panel outbound (via the loopback
  526. // SOCKS bridge in the running Xray). When the feature is off or the bridge
  527. // is unavailable it falls back to a direct client.
  528. func (s *SettingService) NewProxiedHTTPClient(timeout time.Duration) *http.Client {
  529. proxyUrl := s.PanelEgressProxyURL()
  530. client, err := netproxy.NewHTTPClient(proxyUrl, timeout)
  531. if err != nil {
  532. logger.Warningf("Invalid panel egress proxy %q, using direct connection: %v", proxyUrl, err)
  533. return &http.Client{Timeout: timeout}
  534. }
  535. return client
  536. }
  537. func (s *SettingService) GetTgBotAPIServer() (string, error) {
  538. return s.getString("tgBotAPIServer")
  539. }
  540. func (s *SettingService) SetTgBotAPIServer(token string) error {
  541. return s.setString("tgBotAPIServer", token)
  542. }
  543. func (s *SettingService) GetTgBotChatId() (string, error) {
  544. return s.getString("tgBotChatId")
  545. }
  546. func (s *SettingService) SetTgBotChatId(chatIds string) error {
  547. return s.setString("tgBotChatId", chatIds)
  548. }
  549. func (s *SettingService) GetTgbotEnabled() (bool, error) {
  550. return s.getBool("tgBotEnable")
  551. }
  552. func (s *SettingService) SetTgbotEnabled(value bool) error {
  553. return s.setBool("tgBotEnable", value)
  554. }
  555. func (s *SettingService) GetTgbotRuntime() (string, error) {
  556. return s.getString("tgRunTime")
  557. }
  558. func (s *SettingService) SetTgbotRuntime(time string) error {
  559. return s.setString("tgRunTime", time)
  560. }
  561. func (s *SettingService) GetTgBotBackup() (bool, error) {
  562. return s.getBool("tgBotBackup")
  563. }
  564. func (s *SettingService) GetTgCpu() (int, error) {
  565. return s.getInt("tgCpu")
  566. }
  567. func (s *SettingService) GetTgMemory() (int, error) {
  568. return s.getInt("tgMemory")
  569. }
  570. func (s *SettingService) SetTgMemory(value int) error {
  571. return s.setInt("tgMemory", value)
  572. }
  573. func (s *SettingService) GetTgLang() (string, error) {
  574. return s.getString("tgLang")
  575. }
  576. func (s *SettingService) GetTwoFactorEnable() (bool, error) {
  577. return s.getBool("twoFactorEnable")
  578. }
  579. func (s *SettingService) SetTwoFactorEnable(value bool) error {
  580. return s.setBool("twoFactorEnable", value)
  581. }
  582. func (s *SettingService) GetTwoFactorToken() (string, error) {
  583. return s.getString("twoFactorToken")
  584. }
  585. func (s *SettingService) SetTwoFactorToken(value string) error {
  586. return s.setString("twoFactorToken", value)
  587. }
  588. func (s *SettingService) VerifyTwoFactorCode(code string) error {
  589. enabled, err := s.GetTwoFactorEnable()
  590. if err != nil {
  591. return err
  592. }
  593. if !enabled {
  594. return nil
  595. }
  596. token, err := s.GetTwoFactorToken()
  597. if err != nil {
  598. return err
  599. }
  600. if strings.TrimSpace(token) == "" || !totp.VerifyWithSkew(token, strings.TrimSpace(code), time.Now()) {
  601. return common.NewError("invalid two factor code")
  602. }
  603. return nil
  604. }
  605. func (s *SettingService) GetPort() (int, error) {
  606. return s.getInt("webPort")
  607. }
  608. func (s *SettingService) SetPort(port int) error {
  609. return s.setInt("webPort", port)
  610. }
  611. func (s *SettingService) SetCertFile(webCertFile string) error {
  612. return s.setString("webCertFile", webCertFile)
  613. }
  614. func (s *SettingService) GetCertFile() (string, error) {
  615. return s.getString("webCertFile")
  616. }
  617. func (s *SettingService) SetKeyFile(webKeyFile string) error {
  618. return s.setString("webKeyFile", webKeyFile)
  619. }
  620. func (s *SettingService) GetKeyFile() (string, error) {
  621. return s.getString("webKeyFile")
  622. }
  623. func (s *SettingService) GetExpireDiff() (int, error) {
  624. return s.getInt("expireDiff")
  625. }
  626. func (s *SettingService) GetTrafficDiff() (int, error) {
  627. return s.getInt("trafficDiff")
  628. }
  629. func (s *SettingService) GetSessionMaxAge() (int, error) {
  630. return s.getInt("sessionMaxAge")
  631. }
  632. // GetIpLimitAllowlist returns the operator's trusted addresses and networks,
  633. // which the IP limit neither counts nor bans.
  634. func (s *SettingService) GetIpLimitAllowlist() (string, error) {
  635. return s.getString("ipLimitAllowlist")
  636. }
  637. func (s *SettingService) GetTrustedProxyCIDRs() (string, error) {
  638. return s.getString("trustedProxyCIDRs")
  639. }
  640. func (s *SettingService) GetRealityScanCandidates() (string, error) {
  641. return s.getString("realityScanCandidates")
  642. }
  643. func (s *SettingService) GetRemarkTemplate() (string, error) {
  644. return s.getString("remarkTemplate")
  645. }
  646. func (s *SettingService) GetSubShowIdentityOnAllLinks() (bool, error) {
  647. return s.getBool("subShowIdentityOnAllLinks")
  648. }
  649. func (s *SettingService) GetSubInfoNodeEnable() (bool, error) {
  650. return s.getBool("subInfoNodeEnable")
  651. }
  652. func (s *SettingService) GetSubCalendarExpireInclusive() (bool, error) {
  653. return s.getBool("subCalendarExpireInclusive")
  654. }
  655. func (s *SettingService) GetSubExpiredTemplate() (string, error) {
  656. return s.getString("subExpiredTemplate")
  657. }
  658. func (s *SettingService) GetSubTrafficDepletedTemplate() (string, error) {
  659. return s.getString("subTrafficDepletedTemplate")
  660. }
  661. func (s *SettingService) GetSecret() ([]byte, error) {
  662. secret, err := s.getString("secret")
  663. if secret == "" || secret == defaultValueMap["secret"] {
  664. if secret == "" {
  665. secret = defaultValueMap["secret"]
  666. }
  667. saveErr := s.saveSetting("secret", secret)
  668. if saveErr != nil {
  669. logger.Warning("save secret failed:", saveErr)
  670. }
  671. }
  672. return []byte(secret), err
  673. }
  674. // GetPanelGuid returns this panel's stable self-identifier, persisting a
  675. // freshly generated UUID on first read. It is the globally stable node
  676. // identity used to attribute online clients and inbounds to the physical
  677. // node that hosts them across a chain of nodes (#4983), where per-panel
  678. // autoincrement node ids are meaningless one hop away.
  679. func (s *SettingService) GetPanelGuid() (string, error) {
  680. guid, err := s.getString("panelGuid")
  681. if err != nil {
  682. return "", err
  683. }
  684. if guid == defaultValueMap["panelGuid"] {
  685. if saveErr := s.saveSetting("panelGuid", guid); saveErr != nil {
  686. logger.Warning("save panelGuid failed:", saveErr)
  687. }
  688. }
  689. return guid, nil
  690. }
  691. func (s *SettingService) SetBasePath(basePath string) error {
  692. if !strings.HasPrefix(basePath, "/") {
  693. basePath = "/" + basePath
  694. }
  695. if !strings.HasSuffix(basePath, "/") {
  696. basePath += "/"
  697. }
  698. return s.setString("webBasePath", basePath)
  699. }
  700. func (s *SettingService) GetBasePath() (string, error) {
  701. basePath, err := s.getString("webBasePath")
  702. if err != nil {
  703. return "", err
  704. }
  705. return normalizeBasePath(basePath), nil
  706. }
  707. func (s *SettingService) GetTimeLocation() (*time.Location, error) {
  708. l, err := s.getString("timeLocation")
  709. if err != nil {
  710. return nil, err
  711. }
  712. location, err := time.LoadLocation(l)
  713. if err != nil {
  714. defaultLocation := defaultValueMap["timeLocation"]
  715. logger.Errorf("location <%v> not exist, using default location: %v", l, defaultLocation)
  716. location, err = time.LoadLocation(defaultLocation)
  717. if err != nil {
  718. logger.Errorf("failed to load default location, using UTC: %v", err)
  719. return time.UTC, nil
  720. }
  721. return location, nil
  722. }
  723. return location, nil
  724. }
  725. func (s *SettingService) GetSubEnable() (bool, error) {
  726. return s.getBool("subEnable")
  727. }
  728. func (s *SettingService) GetHappLinkEnable() (bool, error) {
  729. return s.getBool("happLinkEnable")
  730. }
  731. func (s *SettingService) GetSubJsonEnable() (bool, error) {
  732. return s.getBool("subJsonEnable")
  733. }
  734. func (s *SettingService) GetSubJsonAutoDetect() (bool, error) {
  735. return s.getBool("subJsonAutoDetect")
  736. }
  737. func (s *SettingService) GetSubJsonAlwaysArray() (bool, error) {
  738. return s.getBool("subJsonAlwaysArray")
  739. }
  740. func (s *SettingService) GetSubJsonUserAgentRegex() (string, error) {
  741. return s.getString("subJsonUserAgentRegex")
  742. }
  743. func (s *SettingService) GetSubClashAutoDetect() (bool, error) {
  744. return s.getBool("subClashAutoDetect")
  745. }
  746. func (s *SettingService) GetSubClashUserAgentRegex() (string, error) {
  747. return s.getString("subClashUserAgentRegex")
  748. }
  749. func (s *SettingService) GetSubTitle() (string, error) {
  750. return s.getString("subTitle")
  751. }
  752. func (s *SettingService) GetSubSupportUrl() (string, error) {
  753. value, err := s.getString("subSupportUrl")
  754. return common.EnsureURLScheme(value), err
  755. }
  756. func (s *SettingService) GetSubProfileUrl() (string, error) {
  757. value, err := s.getString("subProfileUrl")
  758. return common.EnsureURLScheme(value), err
  759. }
  760. func (s *SettingService) GetSubProfileMode() (string, error) {
  761. setting, err := s.getSetting("subProfileMode")
  762. if err != nil && !database.IsNotFound(err) {
  763. return SubProfileModeNone, err
  764. }
  765. if err == nil && setting.Value != "" {
  766. return effectiveSubProfileMode(setting.Value, ""), nil
  767. }
  768. profileURL, err := s.getString("subProfileUrl")
  769. if err != nil {
  770. return SubProfileModeNone, err
  771. }
  772. return effectiveSubProfileMode("", profileURL), nil
  773. }
  774. func effectiveSubProfileMode(mode, profileURL string) string {
  775. switch mode {
  776. case SubProfileModeNone, SubProfileModeBuiltin, SubProfileModeCustom:
  777. return mode
  778. case "":
  779. // Older settings have no mode; only an existing custom URL opts them in.
  780. if strings.TrimSpace(profileURL) != "" {
  781. return SubProfileModeCustom
  782. }
  783. }
  784. return SubProfileModeNone
  785. }
  786. func (s *SettingService) GetSubAnnounce() (string, error) {
  787. return s.getString("subAnnounce")
  788. }
  789. func (s *SettingService) GetSubEnableRouting() (bool, error) {
  790. return s.getBool("subEnableRouting")
  791. }
  792. func (s *SettingService) GetSubRoutingRules() (string, error) {
  793. return s.getString("subRoutingRules")
  794. }
  795. func (s *SettingService) GetSubHideSettings() (bool, error) {
  796. return s.getBool("subHideSettings")
  797. }
  798. func (s *SettingService) GetSubHappAutoDetect() (bool, error) {
  799. return s.getBool("subHappAutoDetect")
  800. }
  801. func (s *SettingService) GetSubHappProviderId() (string, error) {
  802. return s.getString("subHappProviderId")
  803. }
  804. func (s *SettingService) GetSubHappNewUrl() (string, error) {
  805. return s.getString("subHappNewUrl")
  806. }
  807. func (s *SettingService) GetSubHappFallbackUrl() (string, error) {
  808. return s.getString("subHappFallbackUrl")
  809. }
  810. func (s *SettingService) GetSubHappSubInfoColor() (string, error) {
  811. return s.getString("subHappSubInfoColor")
  812. }
  813. func (s *SettingService) GetSubHappSubInfoText() (string, error) {
  814. return s.getString("subHappSubInfoText")
  815. }
  816. func (s *SettingService) GetSubHappSubInfoButtonText() (string, error) {
  817. return s.getString("subHappSubInfoButtonText")
  818. }
  819. func (s *SettingService) GetSubHappSubInfoButtonLink() (string, error) {
  820. return s.getString("subHappSubInfoButtonLink")
  821. }
  822. func (s *SettingService) GetSubHappSubExpire() (bool, error) {
  823. return s.getBool("subHappSubExpire")
  824. }
  825. func (s *SettingService) GetSubHappSubExpireButtonLink() (string, error) {
  826. return s.getString("subHappSubExpireButtonLink")
  827. }
  828. func (s *SettingService) GetSubHappNotificationExpire() (bool, error) {
  829. return s.getBool("subHappNotificationExpire")
  830. }
  831. func (s *SettingService) GetSubHappNoLimit() (bool, error) {
  832. return s.getBool("subHappNoLimit")
  833. }
  834. func (s *SettingService) GetSubHappAlwaysHwid() (bool, error) {
  835. return s.getBool("subHappAlwaysHwid")
  836. }
  837. func (s *SettingService) GetSubHappTunMode() (string, error) {
  838. return s.getString("subHappTunMode")
  839. }
  840. func (s *SettingService) GetSubHappTunType() (string, error) {
  841. return s.getString("subHappTunType")
  842. }
  843. func (s *SettingService) GetSubHappExcludeRoutes() (string, error) {
  844. return s.getString("subHappExcludeRoutes")
  845. }
  846. func (s *SettingService) GetSubHappExcludeApns() (bool, error) {
  847. return s.getBool("subHappExcludeApns")
  848. }
  849. func (s *SettingService) GetSubHappColorProfile() (string, error) {
  850. return s.getString("subHappColorProfile")
  851. }
  852. func (s *SettingService) GetSubHappPingType() (string, error) {
  853. return s.getString("subHappPingType")
  854. }
  855. func (s *SettingService) GetSubHappAutoConnect() (bool, error) {
  856. return s.getBool("subHappAutoConnect")
  857. }
  858. func (s *SettingService) GetSubHappAutoConnectType() (string, error) {
  859. return s.getString("subHappAutoConnectType")
  860. }
  861. func (s *SettingService) GetSubHappPerAppMode() (string, error) {
  862. return s.getString("subHappPerAppMode")
  863. }
  864. func (s *SettingService) GetSubHappPerAppList() (string, error) {
  865. return s.getString("subHappPerAppList")
  866. }
  867. func (s *SettingService) GetSubIncyEnableRouting() (bool, error) {
  868. return s.getBool("subIncyEnableRouting")
  869. }
  870. func (s *SettingService) GetSubIncyRoutingRules() (string, error) {
  871. return s.getString("subIncyRoutingRules")
  872. }
  873. func (s *SettingService) GetSubListen() (string, error) {
  874. return s.getString("subListen")
  875. }
  876. func (s *SettingService) GetSubPort() (int, error) {
  877. return s.getInt("subPort")
  878. }
  879. func (s *SettingService) GetSubPath() (string, error) {
  880. return s.getString("subPath")
  881. }
  882. func (s *SettingService) GetSubJsonPath() (string, error) {
  883. return s.getString("subJsonPath")
  884. }
  885. func (s *SettingService) GetSubDomain() (string, error) {
  886. return s.getString("subDomain")
  887. }
  888. func (s *SettingService) SetSubCertFile(subCertFile string) error {
  889. return s.setString("subCertFile", subCertFile)
  890. }
  891. func (s *SettingService) GetSubCertFile() (string, error) {
  892. return s.getString("subCertFile")
  893. }
  894. func (s *SettingService) SetSubKeyFile(subKeyFile string) error {
  895. return s.setString("subKeyFile", subKeyFile)
  896. }
  897. func (s *SettingService) GetSubKeyFile() (string, error) {
  898. return s.getString("subKeyFile")
  899. }
  900. func (s *SettingService) GetSubUpdates() (string, error) {
  901. return s.getString("subUpdates")
  902. }
  903. func (s *SettingService) GetSubEncrypt() (bool, error) {
  904. return s.getBool("subEncrypt")
  905. }
  906. func (s *SettingService) GetPageSize() (int, error) {
  907. return s.getInt("pageSize")
  908. }
  909. func (s *SettingService) GetSubURI() (string, error) {
  910. return s.getString("subURI")
  911. }
  912. func (s *SettingService) GetSubJsonURI() (string, error) {
  913. return s.getString("subJsonURI")
  914. }
  915. func (s *SettingService) GetSubClashEnable() (bool, error) {
  916. return s.getBool("subClashEnable")
  917. }
  918. func (s *SettingService) GetSubClashPath() (string, error) {
  919. return s.getString("subClashPath")
  920. }
  921. func (s *SettingService) GetSubClashURI() (string, error) {
  922. return s.getString("subClashURI")
  923. }
  924. func (s *SettingService) GetSubClashEnableRouting() (bool, error) {
  925. return s.getBool("subClashEnableRouting")
  926. }
  927. func (s *SettingService) GetSubClashRules() (string, error) {
  928. return s.getString("subClashRules")
  929. }
  930. func (s *SettingService) GetSubJsonMux() (string, error) {
  931. return s.getString("subJsonMux")
  932. }
  933. func (s *SettingService) GetSubJsonRules() (string, error) {
  934. return s.getString("subJsonRules")
  935. }
  936. func (s *SettingService) GetSubJsonRoutingRules() (string, error) {
  937. return s.getString("subJsonRoutingRules")
  938. }
  939. func (s *SettingService) GetSubJsonDns() (string, error) {
  940. return s.getString("subJsonDns")
  941. }
  942. func (s *SettingService) GetSubJsonFinalMask() (string, error) {
  943. return s.getString("subJsonFinalMask")
  944. }
  945. func (s *SettingService) GetSubJsonObservatory() (string, error) {
  946. return s.getString("subJsonObservatory")
  947. }
  948. func (s *SettingService) GetSubThemeDir() (string, error) {
  949. return s.getString("subThemeDir")
  950. }
  951. func (s *SettingService) GetDatepicker() (string, error) {
  952. return s.getString("datepicker")
  953. }
  954. func (s *SettingService) GetWarp() (string, error) {
  955. return s.getString("warp")
  956. }
  957. func (s *SettingService) SetWarp(data string) error {
  958. return s.setString("warp", data)
  959. }
  960. func (s *SettingService) GetNord() (string, error) {
  961. return s.getString("nord")
  962. }
  963. func (s *SettingService) SetNord(data string) error {
  964. return s.setString("nord", data)
  965. }
  966. func (s *SettingService) GetPia() (string, error) {
  967. return s.getString("pia")
  968. }
  969. func (s *SettingService) SetPia(data string) error {
  970. return s.setString("pia", data)
  971. }
  972. func (s *SettingService) GetExternalTrafficInformEnable() (bool, error) {
  973. return s.getBool("externalTrafficInformEnable")
  974. }
  975. func (s *SettingService) SetExternalTrafficInformEnable(value bool) error {
  976. return s.setBool("externalTrafficInformEnable", value)
  977. }
  978. func (s *SettingService) GetExternalTrafficInformURI() (string, error) {
  979. return s.getString("externalTrafficInformURI")
  980. }
  981. func (s *SettingService) SetExternalTrafficInformURI(InformURI string) error {
  982. return s.setString("externalTrafficInformURI", InformURI)
  983. }
  984. func (s *SettingService) GetRestartXrayOnClientDisable() (bool, error) {
  985. return s.getBool("restartXrayOnClientDisable")
  986. }
  987. func (s *SettingService) SetRestartXrayOnClientDisable(value bool) error {
  988. return s.setBool("restartXrayOnClientDisable", value)
  989. }
  990. // GetDevChannelEnable reports whether the panel self-update tracks the rolling
  991. // per-commit dev release instead of the latest stable tag.
  992. func (s *SettingService) GetDevChannelEnable() (bool, error) {
  993. return s.getBool("devChannelEnable")
  994. }
  995. func (s *SettingService) SetDevChannelEnable(value bool) error {
  996. return s.setBool("devChannelEnable", value)
  997. }
  998. // GetIpLimitEnable reports whether the IP-limit feature is available. Always
  999. // true since the panel enforces limits via the core's online-stats API; on an
  1000. // older core the job falls back to access-log parsing and warns there when the
  1001. // log is missing, so the UI no longer hides the field behind that condition.
  1002. func (s *SettingService) GetIpLimitEnable() (bool, error) {
  1003. return true, nil
  1004. }
  1005. // GetAccessLogEnable reports whether an Xray access log is configured. Used by
  1006. // the UI for features that genuinely read the log file (the xray log viewer) —
  1007. // distinct from IP limiting, which works without it.
  1008. func (s *SettingService) GetAccessLogEnable() (bool, error) {
  1009. accessLogPath, err := xray.GetAccessLogPath()
  1010. if err != nil {
  1011. return false, err
  1012. }
  1013. return (accessLogPath != "none" && accessLogPath != ""), nil
  1014. }
  1015. // GetLdapEnable returns whether LDAP is enabled.
  1016. func (s *SettingService) GetLdapEnable() (bool, error) {
  1017. return s.getBool("ldapEnable")
  1018. }
  1019. func (s *SettingService) GetLdapHost() (string, error) {
  1020. return s.getString("ldapHost")
  1021. }
  1022. func (s *SettingService) GetLdapPort() (int, error) {
  1023. return s.getInt("ldapPort")
  1024. }
  1025. func (s *SettingService) GetLdapUseTLS() (bool, error) {
  1026. return s.getBool("ldapUseTLS")
  1027. }
  1028. func (s *SettingService) GetLdapInsecureSkipVerify() (bool, error) {
  1029. return s.getBool("ldapInsecureSkipVerify")
  1030. }
  1031. func (s *SettingService) GetLdapBindDN() (string, error) {
  1032. return s.getString("ldapBindDN")
  1033. }
  1034. func (s *SettingService) GetLdapPassword() (string, error) {
  1035. return s.getString("ldapPassword")
  1036. }
  1037. func (s *SettingService) GetLdapBaseDN() (string, error) {
  1038. return s.getString("ldapBaseDN")
  1039. }
  1040. func (s *SettingService) GetLdapUserFilter() (string, error) {
  1041. return s.getString("ldapUserFilter")
  1042. }
  1043. func (s *SettingService) GetLdapUserAttr() (string, error) {
  1044. return s.getString("ldapUserAttr")
  1045. }
  1046. func (s *SettingService) GetLdapVlessField() (string, error) {
  1047. return s.getString("ldapVlessField")
  1048. }
  1049. func (s *SettingService) GetLdapSyncCron() (string, error) {
  1050. return s.getString("ldapSyncCron")
  1051. }
  1052. func (s *SettingService) GetLdapFlagField() (string, error) {
  1053. return s.getString("ldapFlagField")
  1054. }
  1055. func (s *SettingService) GetLdapTruthyValues() (string, error) {
  1056. return s.getString("ldapTruthyValues")
  1057. }
  1058. func (s *SettingService) GetLdapInvertFlag() (bool, error) {
  1059. return s.getBool("ldapInvertFlag")
  1060. }
  1061. func (s *SettingService) GetLdapInboundTags() (string, error) {
  1062. return s.getString("ldapInboundTags")
  1063. }
  1064. func (s *SettingService) GetLdapAutoCreate() (bool, error) {
  1065. return s.getBool("ldapAutoCreate")
  1066. }
  1067. func (s *SettingService) GetLdapAutoDelete() (bool, error) {
  1068. return s.getBool("ldapAutoDelete")
  1069. }
  1070. func (s *SettingService) GetLdapDefaultTotalGB() (int, error) {
  1071. return s.getInt("ldapDefaultTotalGB")
  1072. }
  1073. func (s *SettingService) GetLdapDefaultExpiryDays() (int, error) {
  1074. return s.getInt("ldapDefaultExpiryDays")
  1075. }
  1076. func (s *SettingService) GetLdapDefaultLimitIP() (int, error) {
  1077. return s.getInt("ldapDefaultLimitIP")
  1078. }
  1079. // Event bus — per-subscriber event filtering
  1080. func (s *SettingService) GetTgEnabledEvents() (string, error) {
  1081. return s.getString("tgEnabledEvents")
  1082. }
  1083. func (s *SettingService) SetTgEnabledEvents(events string) error {
  1084. return s.setString("tgEnabledEvents", events)
  1085. }
  1086. func (s *SettingService) GetSmtpEnabledEvents() (string, error) {
  1087. return s.getString("smtpEnabledEvents")
  1088. }
  1089. func (s *SettingService) SetSmtpEnabledEvents(events string) error {
  1090. return s.setString("smtpEnabledEvents", events)
  1091. }
  1092. // Email (SMTP) settings
  1093. func (s *SettingService) GetSmtpEnable() (bool, error) {
  1094. return s.getBool("smtpEnable")
  1095. }
  1096. func (s *SettingService) SetSmtpEnable(value bool) error {
  1097. return s.setBool("smtpEnable", value)
  1098. }
  1099. func (s *SettingService) GetSmtpHost() (string, error) {
  1100. return s.getString("smtpHost")
  1101. }
  1102. func (s *SettingService) SetSmtpHost(value string) error {
  1103. return s.setString("smtpHost", value)
  1104. }
  1105. func (s *SettingService) GetSmtpPort() (int, error) {
  1106. return s.getInt("smtpPort")
  1107. }
  1108. func (s *SettingService) SetSmtpPort(value int) error {
  1109. return s.setInt("smtpPort", value)
  1110. }
  1111. func (s *SettingService) GetSmtpUsername() (string, error) {
  1112. return s.getString("smtpUsername")
  1113. }
  1114. func (s *SettingService) SetSmtpUsername(value string) error {
  1115. return s.setString("smtpUsername", value)
  1116. }
  1117. func (s *SettingService) GetSmtpFrom() (string, error) {
  1118. return s.getString("smtpFrom")
  1119. }
  1120. func (s *SettingService) SetSmtpFrom(value string) error {
  1121. return s.setString("smtpFrom", value)
  1122. }
  1123. func (s *SettingService) GetSmtpFromName() (string, error) {
  1124. return s.getString("smtpFromName")
  1125. }
  1126. func (s *SettingService) SetSmtpFromName(value string) error {
  1127. return s.setString("smtpFromName", value)
  1128. }
  1129. func (s *SettingService) GetSmtpPassword() (string, error) {
  1130. return s.getString("smtpPassword")
  1131. }
  1132. func (s *SettingService) SetSmtpPassword(value string) error {
  1133. return s.setString("smtpPassword", value)
  1134. }
  1135. func (s *SettingService) GetSmtpTo() (string, error) {
  1136. return s.getString("smtpTo")
  1137. }
  1138. func (s *SettingService) SetSmtpTo(value string) error {
  1139. return s.setString("smtpTo", value)
  1140. }
  1141. func (s *SettingService) GetSmtpEncryptionType() (string, error) {
  1142. return s.getString("smtpEncryptionType")
  1143. }
  1144. func (s *SettingService) SetSmtpEncryptionType(value string) error {
  1145. return s.setString("smtpEncryptionType", value)
  1146. }
  1147. func (s *SettingService) GetSmtpCpu() (int, error) {
  1148. return s.getInt("smtpCpu")
  1149. }
  1150. func (s *SettingService) SetSmtpCpu(value int) error {
  1151. return s.setInt("smtpCpu", value)
  1152. }
  1153. func (s *SettingService) GetSmtpMemory() (int, error) {
  1154. return s.getInt("smtpMemory")
  1155. }
  1156. func (s *SettingService) SetSmtpMemory(value int) error {
  1157. return s.setInt("smtpMemory", value)
  1158. }
  1159. // Discord bot settings
  1160. func (s *SettingService) GetDiscordBotEnable() (bool, error) {
  1161. return s.getBool("discordBotEnable")
  1162. }
  1163. func (s *SettingService) SetDiscordBotEnable(value bool) error {
  1164. return s.setBool("discordBotEnable", value)
  1165. }
  1166. func (s *SettingService) GetDiscordBotToken() (string, error) {
  1167. return s.getString("discordBotToken")
  1168. }
  1169. func (s *SettingService) SetDiscordBotToken(value string) error {
  1170. return s.setString("discordBotToken", value)
  1171. }
  1172. func (s *SettingService) GetDiscordChannelId() (string, error) {
  1173. return s.getString("discordChannelId")
  1174. }
  1175. func (s *SettingService) SetDiscordChannelId(value string) error {
  1176. return s.setString("discordChannelId", value)
  1177. }
  1178. func (s *SettingService) GetDiscordAdminIds() (string, error) {
  1179. return s.getString("discordAdminIds")
  1180. }
  1181. func (s *SettingService) SetDiscordAdminIds(value string) error {
  1182. return s.setString("discordAdminIds", value)
  1183. }
  1184. func (s *SettingService) GetDiscordEnabledEvents() (string, error) {
  1185. return s.getString("discordEnabledEvents")
  1186. }
  1187. func (s *SettingService) SetDiscordEnabledEvents(events string) error {
  1188. return s.setString("discordEnabledEvents", events)
  1189. }
  1190. func (s *SettingService) GetDiscordCpu() (int, error) {
  1191. return s.getInt("discordCpu")
  1192. }
  1193. func (s *SettingService) SetDiscordCpu(value int) error {
  1194. return s.setInt("discordCpu", value)
  1195. }
  1196. func (s *SettingService) GetDiscordMemory() (int, error) {
  1197. return s.getInt("discordMemory")
  1198. }
  1199. func (s *SettingService) SetDiscordMemory(value int) error {
  1200. return s.setInt("discordMemory", value)
  1201. }
  1202. func (s *SettingService) GetDiscordRunTime() (string, error) {
  1203. return s.getString("discordRunTime")
  1204. }
  1205. func (s *SettingService) SetDiscordRunTime(value string) error {
  1206. return s.setString("discordRunTime", value)
  1207. }
  1208. func (s *SettingService) GetDiscordBotBackup() (bool, error) {
  1209. return s.getBool("discordBotBackup")
  1210. }
  1211. func (s *SettingService) SetDiscordBotBackup(value bool) error {
  1212. return s.setBool("discordBotBackup", value)
  1213. }
  1214. func (s *SettingService) GetDiscordLang() (string, error) {
  1215. return s.getString("discordLang")
  1216. }
  1217. func (s *SettingService) SetDiscordLang(value string) error {
  1218. return s.setString("discordLang", value)
  1219. }
  1220. // GetOutboundDownThreshold returns how many consecutive failed observatory
  1221. // probes an outbound must accumulate before an outbound.down notification is
  1222. // emitted. 1 preserves the legacy "notify on the first failed probe" behaviour.
  1223. func (s *SettingService) GetOutboundDownThreshold() (int, error) {
  1224. return s.getInt("outboundDownThreshold")
  1225. }
  1226. func (s *SettingService) SetOutboundDownThreshold(value int) error {
  1227. return s.setInt("outboundDownThreshold", value)
  1228. }
  1229. // SecretClears marks redacted secrets the user explicitly emptied. Without a
  1230. // flag, a blank submitted secret means "unchanged" (the field is always served
  1231. // blank to the browser) and the stored value is preserved.
  1232. type SecretClears struct {
  1233. TgBotToken bool
  1234. LdapPassword bool
  1235. SmtpPassword bool
  1236. DiscordBotToken bool
  1237. }
  1238. func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting, clears SecretClears) error {
  1239. switch allSetting.SubProfileMode {
  1240. case "", SubProfileModeNone, SubProfileModeBuiltin, SubProfileModeCustom:
  1241. allSetting.SubProfileMode = effectiveSubProfileMode(allSetting.SubProfileMode, allSetting.SubProfileUrl)
  1242. default:
  1243. return errors.New("subscription profile mode must be none, builtin, or custom")
  1244. }
  1245. if err := s.preserveRedactedSecrets(allSetting, clears); err != nil {
  1246. return err
  1247. }
  1248. if err := validateSettingsURLs(allSetting); err != nil {
  1249. return err
  1250. }
  1251. if err := validateSubUserAgentRegexes(allSetting); err != nil {
  1252. return err
  1253. }
  1254. if err := validateSubJsonDnsSetting(allSetting); err != nil {
  1255. return err
  1256. }
  1257. if err := allSetting.CheckValid(); err != nil {
  1258. return err
  1259. }
  1260. v := reflect.ValueOf(allSetting).Elem()
  1261. t := reflect.TypeFor[entity.AllSetting]()
  1262. fields := reflect_util.GetFields(t)
  1263. db := database.GetDB()
  1264. return db.Transaction(func(tx *gorm.DB) error {
  1265. var existing []*model.Setting
  1266. if err := tx.Find(&existing).Error; err != nil {
  1267. return err
  1268. }
  1269. byKey := make(map[string]*model.Setting, len(existing))
  1270. for _, st := range existing {
  1271. byKey[st.Key] = st
  1272. }
  1273. for _, field := range fields {
  1274. key := field.Tag.Get("json")
  1275. fieldV := v.FieldByName(field.Name)
  1276. value := fmt.Sprint(fieldV.Interface())
  1277. if st, ok := byKey[key]; ok {
  1278. if st.Value == value {
  1279. continue
  1280. }
  1281. st.Value = value
  1282. if err := tx.Save(st).Error; err != nil {
  1283. return err
  1284. }
  1285. continue
  1286. }
  1287. if err := tx.Create(&model.Setting{Key: key, Value: value}).Error; err != nil {
  1288. return err
  1289. }
  1290. }
  1291. return nil
  1292. })
  1293. }
  1294. func validateSubUserAgentRegexes(allSetting *entity.AllSetting) error {
  1295. jsonPattern, err := validateSubUserAgentRegex("Xray JSON", allSetting.SubJsonUserAgentRegex, DefaultSubJsonUserAgentRegex)
  1296. if err != nil {
  1297. return err
  1298. }
  1299. clashPattern, err := validateSubUserAgentRegex("Clash/Mihomo", allSetting.SubClashUserAgentRegex, DefaultSubClashUserAgentRegex)
  1300. if err != nil {
  1301. return err
  1302. }
  1303. allSetting.SubJsonUserAgentRegex = jsonPattern
  1304. allSetting.SubClashUserAgentRegex = clashPattern
  1305. return nil
  1306. }
  1307. func validateSubUserAgentRegex(name, pattern, defaultPattern string) (string, error) {
  1308. pattern = strings.TrimSpace(pattern)
  1309. effectivePattern := pattern
  1310. if effectivePattern == "" {
  1311. effectivePattern = defaultPattern
  1312. }
  1313. if len(effectivePattern) > maxRegexLength {
  1314. return "", common.NewErrorf("%s User-Agent regex must not exceed %d characters", name, maxRegexLength)
  1315. }
  1316. if _, err := regexp.Compile(effectivePattern); err != nil {
  1317. return "", common.NewErrorf("%s User-Agent regex is invalid: %v", name, err)
  1318. }
  1319. // Return the original pattern (empty string if cleared) so the caller
  1320. // can distinguish "user explicitly set empty" from "user set a value".
  1321. // The empty value is stored in the DB and inherited as runtime default.
  1322. return pattern, nil
  1323. }
  1324. func ValidateRegex(pattern string) error {
  1325. if len(pattern) > maxRegexLength {
  1326. return common.NewErrorf("Regular expression must not exceed %d characters", maxRegexLength)
  1327. }
  1328. if _, err := regexp.Compile(pattern); err != nil {
  1329. return common.NewError("Regular expression is invalid:", err)
  1330. }
  1331. return nil
  1332. }
  1333. func (s *SettingService) preserveRedactedSecrets(allSetting *entity.AllSetting, clears SecretClears) error {
  1334. if !clears.TgBotToken && strings.TrimSpace(allSetting.TgBotToken) == "" {
  1335. value, err := s.GetTgBotToken()
  1336. if err != nil {
  1337. return err
  1338. }
  1339. allSetting.TgBotToken = value
  1340. }
  1341. if !clears.LdapPassword && strings.TrimSpace(allSetting.LdapPassword) == "" {
  1342. value, err := s.GetLdapPassword()
  1343. if err != nil {
  1344. return err
  1345. }
  1346. allSetting.LdapPassword = value
  1347. }
  1348. if allSetting.TwoFactorEnable && strings.TrimSpace(allSetting.TwoFactorToken) == "" {
  1349. value, err := s.GetTwoFactorToken()
  1350. if err != nil {
  1351. return err
  1352. }
  1353. allSetting.TwoFactorToken = value
  1354. }
  1355. if !clears.SmtpPassword && strings.TrimSpace(allSetting.SmtpPassword) == "" {
  1356. value, err := s.GetSmtpPassword()
  1357. if err != nil {
  1358. return err
  1359. }
  1360. allSetting.SmtpPassword = value
  1361. }
  1362. if !clears.DiscordBotToken && strings.TrimSpace(allSetting.DiscordBotToken) == "" {
  1363. value, err := s.GetDiscordBotToken()
  1364. if err != nil {
  1365. return err
  1366. }
  1367. allSetting.DiscordBotToken = value
  1368. }
  1369. return nil
  1370. }
  1371. func validateSettingsURLs(allSetting *entity.AllSetting) error {
  1372. if allSetting.ExternalTrafficInformURI != "" {
  1373. u, err := SanitizeHTTPURL(allSetting.ExternalTrafficInformURI)
  1374. if err != nil {
  1375. return common.NewError("external traffic inform URI is invalid:", err)
  1376. }
  1377. allSetting.ExternalTrafficInformURI = u
  1378. }
  1379. if allSetting.TgBotAPIServer != "" {
  1380. u, err := SanitizeHTTPURL(allSetting.TgBotAPIServer)
  1381. if err != nil {
  1382. return common.NewError("telegram API server URL is invalid:", err)
  1383. }
  1384. allSetting.TgBotAPIServer = u
  1385. }
  1386. // Support/profile links land in subscription headers and page data, where
  1387. // client apps resolve a scheme-less value against the panel's own domain.
  1388. // Non-http schemes (tg://, mailto:) are legitimate here, so only default
  1389. // the scheme instead of forcing SanitizeHTTPURL's http(s)-only rule.
  1390. allSetting.SubSupportUrl = common.EnsureURLScheme(allSetting.SubSupportUrl)
  1391. allSetting.SubProfileUrl = common.EnsureURLScheme(allSetting.SubProfileUrl)
  1392. for _, ptr := range []*string{
  1393. &allSetting.SubHappNewUrl,
  1394. &allSetting.SubHappFallbackUrl,
  1395. &allSetting.SubHappSubInfoButtonLink,
  1396. &allSetting.SubHappSubExpireButtonLink,
  1397. } {
  1398. if strings.TrimSpace(*ptr) != "" {
  1399. *ptr = common.EnsureURLScheme(strings.TrimSpace(*ptr))
  1400. }
  1401. }
  1402. for name, value := range map[string]*string{
  1403. "Happ routing source": &allSetting.SubRoutingRules,
  1404. "Clash/Mihomo routing source": &allSetting.SubClashRules,
  1405. "Incy routing source": &allSetting.SubIncyRoutingRules,
  1406. "JSON subscription routing source": &allSetting.SubJsonRoutingRules,
  1407. } {
  1408. if err := validateRemoteRoutingURLSetting(name, value); err != nil {
  1409. return err
  1410. }
  1411. }
  1412. return nil
  1413. }
  1414. func validateRemoteRoutingURLSetting(name string, value *string) error {
  1415. canonical, remote, err := common.ParseRemoteRoutingURL(*value)
  1416. if err != nil {
  1417. return common.NewError(name, err.Error())
  1418. }
  1419. if remote {
  1420. *value = canonical
  1421. }
  1422. return nil
  1423. }
  1424. // The same parser the sub server uses, so a value can never be saved as valid
  1425. // and then silently ignored at request time.
  1426. func validateSubJsonDnsSetting(allSetting *entity.AllSetting) error {
  1427. value := strings.TrimSpace(allSetting.SubJsonDns)
  1428. if value != "" {
  1429. if _, err := dnsconf.Parse(value); err != nil {
  1430. return common.NewError("JSON subscription DNS is invalid:", err.Error())
  1431. }
  1432. }
  1433. allSetting.SubJsonDns = value
  1434. return nil
  1435. }
  1436. func (s *SettingService) UpdateSecret(key string, value string) error {
  1437. switch key {
  1438. case "tgBotToken", "ldapPassword", "twoFactorToken":
  1439. return s.saveSetting(key, strings.TrimSpace(value))
  1440. default:
  1441. return common.NewError("secret key is not replaceable:", key)
  1442. }
  1443. }
  1444. func (s *SettingService) GetDefaultXrayConfig() (any, error) {
  1445. var jsonData any
  1446. err := json.Unmarshal([]byte(xrayTemplateConfig), &jsonData)
  1447. if err != nil {
  1448. return nil, err
  1449. }
  1450. return jsonData, nil
  1451. }
  1452. func extractHostname(host string) string {
  1453. h, _, err := net.SplitHostPort(host)
  1454. // Err is not nil means host does not contain port
  1455. if err != nil {
  1456. h = host
  1457. }
  1458. ip := net.ParseIP(h)
  1459. // If it's not an IP, return as is
  1460. if ip == nil {
  1461. return h
  1462. }
  1463. // If it's an IPv4, return as is
  1464. if ip.To4() != nil {
  1465. return h
  1466. }
  1467. // IPv6 needs bracketing
  1468. return "[" + h + "]"
  1469. }
  1470. // BuildSubURIBase is shared by GetDefaultSettings (the panel's Client
  1471. // Information page) and the subscription page so both render subscription
  1472. // URLs identically.
  1473. func (s *SettingService) BuildSubURIBase(host string) string {
  1474. subPort, _ := s.GetSubPort()
  1475. subDomain, _ := s.GetSubDomain()
  1476. subKeyFile, _ := s.GetSubKeyFile()
  1477. subCertFile, _ := s.GetSubCertFile()
  1478. subTLS := subKeyFile != "" && subCertFile != ""
  1479. if subDomain == "" {
  1480. subDomain = extractHostname(host)
  1481. }
  1482. scheme := "http"
  1483. if subTLS {
  1484. scheme = "https"
  1485. }
  1486. if (subPort == 443 && subTLS) || (subPort == 80 && !subTLS) {
  1487. return scheme + "://" + subDomain
  1488. }
  1489. return fmt.Sprintf("%s://%s:%d", scheme, subDomain, subPort)
  1490. }
  1491. func (s *SettingService) GetDefaultSettings(host string) (any, error) {
  1492. type settingFunc func() (any, error)
  1493. settings := map[string]settingFunc{
  1494. "expireDiff": func() (any, error) { return s.GetExpireDiff() },
  1495. "trafficDiff": func() (any, error) { return s.GetTrafficDiff() },
  1496. "pageSize": func() (any, error) { return s.GetPageSize() },
  1497. "defaultCert": func() (any, error) { return s.GetCertFile() },
  1498. "defaultKey": func() (any, error) { return s.GetKeyFile() },
  1499. "tgBotEnable": func() (any, error) { return s.GetTgbotEnabled() },
  1500. "subThemeDir": func() (any, error) { return s.GetSubThemeDir() },
  1501. "happLinkEnable": func() (any, error) { return s.GetHappLinkEnable() },
  1502. "subEnable": func() (any, error) { return s.GetSubEnable() },
  1503. "subJsonEnable": func() (any, error) { return s.GetSubJsonEnable() },
  1504. "subClashEnable": func() (any, error) { return s.GetSubClashEnable() },
  1505. "subTitle": func() (any, error) { return s.GetSubTitle() },
  1506. "subURI": func() (any, error) { return s.GetSubURI() },
  1507. "subJsonURI": func() (any, error) { return s.GetSubJsonURI() },
  1508. "subClashURI": func() (any, error) { return s.GetSubClashURI() },
  1509. "datepicker": func() (any, error) { return s.GetDatepicker() },
  1510. "ipLimitEnable": func() (any, error) { return s.GetIpLimitEnable() },
  1511. "accessLogEnable": func() (any, error) { return s.GetAccessLogEnable() },
  1512. "webDomain": func() (any, error) { return s.GetWebDomain() },
  1513. "subDomain": func() (any, error) { return s.GetSubDomain() },
  1514. "devChannelEnable": func() (any, error) { return s.GetDevChannelEnable() },
  1515. "isDevBuild": func() (any, error) { return config.IsDevBuild(), nil },
  1516. }
  1517. result := make(map[string]any)
  1518. for key, fn := range settings {
  1519. value, err := fn()
  1520. if err != nil {
  1521. return "", err
  1522. }
  1523. result[key] = value
  1524. }
  1525. subEnable := result["subEnable"].(bool)
  1526. subJsonEnable := false
  1527. if v, ok := result["subJsonEnable"]; ok {
  1528. if b, ok2 := v.(bool); ok2 {
  1529. subJsonEnable = b
  1530. }
  1531. }
  1532. subClashEnable := false
  1533. if v, ok := result["subClashEnable"]; ok {
  1534. if b, ok2 := v.(bool); ok2 {
  1535. subClashEnable = b
  1536. }
  1537. }
  1538. if (subEnable && result["subURI"].(string) == "") || (subJsonEnable && result["subJsonURI"].(string) == "") || (subClashEnable && result["subClashURI"].(string) == "") {
  1539. subURI := s.BuildSubURIBase(host)
  1540. subTitle, _ := s.GetSubTitle()
  1541. subPath, _ := s.GetSubPath()
  1542. subJsonPath, _ := s.GetSubJsonPath()
  1543. subClashPath, _ := s.GetSubClashPath()
  1544. if subEnable && result["subURI"].(string) == "" {
  1545. result["subURI"] = subURI + subPath
  1546. }
  1547. if result["subTitle"].(string) == "" {
  1548. result["subTitle"] = subTitle
  1549. }
  1550. if subJsonEnable && result["subJsonURI"].(string) == "" {
  1551. result["subJsonURI"] = subURI + subJsonPath
  1552. }
  1553. if subClashEnable && result["subClashURI"].(string) == "" {
  1554. result["subClashURI"] = subURI + subClashPath
  1555. }
  1556. }
  1557. return result, nil
  1558. }
  1559. var factoryDefaultSecretKeys = map[string]bool{
  1560. "tgBotToken": true,
  1561. "twoFactorToken": true,
  1562. "ldapPassword": true,
  1563. "smtpPassword": true,
  1564. "discordBotToken": true,
  1565. }
  1566. /*
  1567. GetFactoryDefaults returns the shipped default value per setting, keyed by
  1568. the AllSetting json field name. Unlike GetDefaultSettings (which reports
  1569. current effective values), this is defaultValueMap projected through the
  1570. AllSetting field set: only keys that exist as an AllSetting json tag are
  1571. returned, minus the credential fields in factoryDefaultSecretKeys. Keys
  1572. with no AllSetting field (secret, panelGuid, the node mTLS material,
  1573. xrayTemplateConfig) are excluded structurally rather than by deny-list.
  1574. */
  1575. func (s *SettingService) GetFactoryDefaults() map[string]string {
  1576. result := make(map[string]string)
  1577. for _, field := range reflect_util.GetFields(reflect.TypeFor[entity.AllSetting]()) {
  1578. key := field.Tag.Get("json")
  1579. if key == "" || factoryDefaultSecretKeys[key] {
  1580. continue
  1581. }
  1582. if value, ok := defaultValueMap[key]; ok {
  1583. result[key] = value
  1584. }
  1585. }
  1586. return result
  1587. }