setting.go 54 KB

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