setting.go 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. package service
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "os"
  10. "reflect"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "github.com/google/uuid"
  15. "github.com/xlzd/gotp"
  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/web/entity"
  26. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  27. )
  28. //go:embed config.json
  29. var xrayTemplateConfig string
  30. var defaultValueMap = map[string]string{
  31. "xrayTemplateConfig": xrayTemplateConfig,
  32. "webListen": "",
  33. "webDomain": "",
  34. "webPort": "2053",
  35. "webCertFile": "",
  36. "webKeyFile": "",
  37. "secret": random.Seq(32),
  38. "panelGuid": uuid.NewString(),
  39. "apiToken": "",
  40. // Node mTLS material (opt-in). All default empty: the CA + master client
  41. // cert are minted lazily on first use, and the node-side trust CA is pasted
  42. // in by the operator. Kept out of entity.AllSetting so private keys never
  43. // reach the settings UI/export.
  44. "nodeMtlsCaCertPem": "",
  45. "nodeMtlsCaKeyPem": "",
  46. "nodeMtlsClientCertPem": "",
  47. "nodeMtlsClientKeyPem": "",
  48. "nodeMtlsClientCAPem": "",
  49. "webBasePath": normalizeBasePath(getEnv("XUI_INIT_WEB_BASE_PATH", "/")),
  50. "sessionMaxAge": "360",
  51. "trustedProxyCIDRs": "127.0.0.1/32,::1/128",
  52. "pageSize": "25",
  53. "expireDiff": "0",
  54. "trafficDiff": "0",
  55. "remarkTemplate": "{{INBOUND}}-{{EMAIL}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D",
  56. "timeLocation": "Local",
  57. "tgBotEnable": "false",
  58. "tgBotToken": "",
  59. "tgBotProxy": "",
  60. "tgBotAPIServer": "",
  61. "tgBotChatId": "",
  62. "tgRunTime": "@daily",
  63. "tgBotBackup": "false",
  64. "tgCpu": "80",
  65. "tgMemory": "80",
  66. "tgLang": "en-US",
  67. "twoFactorEnable": "false",
  68. "twoFactorToken": "",
  69. "subEnable": "true",
  70. "subJsonEnable": "false",
  71. "subTitle": "",
  72. "subSupportUrl": "",
  73. "subProfileUrl": "",
  74. "subAnnounce": "",
  75. "subEnableRouting": "false",
  76. "subRoutingRules": "",
  77. "subHideSettings": "false",
  78. "subIncyEnableRouting": "false",
  79. "subIncyRoutingRules": "",
  80. "subListen": "",
  81. "subPort": "2096",
  82. "subPath": "/sub/",
  83. "subDomain": "",
  84. "subCertFile": "",
  85. "subKeyFile": "",
  86. "subUpdates": "12",
  87. "subEncrypt": "true",
  88. "subURI": "",
  89. "subJsonPath": "/json/",
  90. "subJsonURI": "",
  91. "subClashEnable": "false",
  92. "subClashPath": "/clash/",
  93. "subClashURI": "",
  94. "subClashEnableRouting": "false",
  95. "subClashRules": "",
  96. "subJsonMux": "",
  97. "subJsonRules": "",
  98. "subJsonFinalMask": "",
  99. "subThemeDir": "",
  100. "datepicker": "gregorian",
  101. "warp": "",
  102. "warpUpdateInterval": "0",
  103. "nord": "",
  104. "externalTrafficInformEnable": "false",
  105. "externalTrafficInformURI": "",
  106. "restartXrayOnClientDisable": "true",
  107. "xrayOutboundTestUrl": "https://www.google.com/generate_204",
  108. "panelOutbound": "",
  109. "devChannelEnable": "false",
  110. // LDAP defaults
  111. "ldapEnable": "false",
  112. "ldapHost": "",
  113. "ldapPort": "389",
  114. "ldapUseTLS": "false",
  115. "ldapBindDN": "",
  116. "ldapPassword": "",
  117. "ldapBaseDN": "",
  118. "ldapUserFilter": "(objectClass=person)",
  119. "ldapUserAttr": "mail",
  120. "ldapVlessField": "vless_enabled",
  121. "ldapSyncCron": "@every 1m",
  122. "ldapFlagField": "",
  123. "ldapTruthyValues": "true,1,yes,on",
  124. "ldapInvertFlag": "false",
  125. "ldapInboundTags": "",
  126. "ldapAutoCreate": "false",
  127. "ldapAutoDelete": "false",
  128. "ldapDefaultTotalGB": "0",
  129. "ldapDefaultExpiryDays": "0",
  130. "ldapDefaultLimitIP": "0",
  131. // Event bus — per-subscriber event filtering (empty = all disabled)
  132. "tgEnabledEvents": "login.attempt,cpu.high",
  133. "smtpEnabledEvents": "login.attempt,cpu.high",
  134. "smtpCpu": "80",
  135. "smtpMemory": "80",
  136. // Email (SMTP) notifications
  137. "smtpEnable": "false",
  138. "smtpHost": "",
  139. "smtpPort": "587",
  140. "smtpUsername": "",
  141. "smtpPassword": "",
  142. "smtpTo": "",
  143. "smtpEncryptionType": "starttls", // no, starttls, tls
  144. }
  145. // SettingService provides business logic for application settings management.
  146. // It handles configuration storage, retrieval, and validation for all system settings.
  147. type SettingService struct{}
  148. func (s *SettingService) GetDefaultJSONConfig() (any, error) {
  149. var jsonData any
  150. err := json.Unmarshal([]byte(xrayTemplateConfig), &jsonData)
  151. if err != nil {
  152. return nil, err
  153. }
  154. return jsonData, nil
  155. }
  156. func (s *SettingService) GetAllSetting() (*entity.AllSetting, error) {
  157. db := database.GetDB()
  158. settings := make([]*model.Setting, 0)
  159. err := db.Model(model.Setting{}).Not("key = ?", "xrayTemplateConfig").Find(&settings).Error
  160. if err != nil {
  161. return nil, err
  162. }
  163. allSetting := &entity.AllSetting{}
  164. t := reflect.TypeFor[entity.AllSetting]()
  165. v := reflect.ValueOf(allSetting).Elem()
  166. fields := reflect_util.GetFields(t)
  167. setSetting := func(key, value string) (err error) {
  168. defer func() {
  169. panicErr := recover()
  170. if panicErr != nil {
  171. err = errors.New(fmt.Sprint(panicErr))
  172. }
  173. }()
  174. var found bool
  175. var field reflect.StructField
  176. for _, f := range fields {
  177. if f.Tag.Get("json") == key {
  178. field = f
  179. found = true
  180. break
  181. }
  182. }
  183. if !found {
  184. // Some settings are automatically generated, no need to return to the front end to modify the user
  185. return nil
  186. }
  187. fieldV := v.FieldByName(field.Name)
  188. switch t := fieldV.Interface().(type) {
  189. case int:
  190. n, err := strconv.ParseInt(effectiveSettingValue(key, value), 10, 64)
  191. if err != nil {
  192. return err
  193. }
  194. fieldV.SetInt(n)
  195. case string:
  196. fieldV.SetString(value)
  197. case bool:
  198. fieldV.SetBool(effectiveSettingValue(key, value) == "true")
  199. default:
  200. return common.NewErrorf("unknown field %v type %v", key, t)
  201. }
  202. return
  203. }
  204. keyMap := map[string]bool{}
  205. for _, setting := range settings {
  206. err := setSetting(setting.Key, setting.Value)
  207. if err != nil {
  208. return nil, err
  209. }
  210. keyMap[setting.Key] = true
  211. }
  212. for key, value := range defaultValueMap {
  213. if keyMap[key] {
  214. continue
  215. }
  216. err := setSetting(key, value)
  217. if err != nil {
  218. return nil, err
  219. }
  220. }
  221. return allSetting, nil
  222. }
  223. func (s *SettingService) GetAllSettingView() (*entity.AllSettingView, error) {
  224. allSetting, err := s.GetAllSetting()
  225. if err != nil {
  226. return nil, err
  227. }
  228. view := &entity.AllSettingView{AllSetting: *allSetting}
  229. view.HasTgBotToken = secretConfigured(allSetting.TgBotToken)
  230. view.HasTwoFactorToken = secretConfigured(allSetting.TwoFactorToken)
  231. view.HasLdapPassword = secretConfigured(allSetting.LdapPassword)
  232. view.HasWarpSecret = secretConfigured(mustString(s.GetWarp()))
  233. view.HasNordSecret = secretConfigured(mustString(s.GetNord()))
  234. view.HasSmtpPassword = secretConfigured(allSetting.SmtpPassword)
  235. var apiTokenCount int64
  236. if err := database.GetDB().Model(model.ApiToken{}).Where("enabled = ?", true).Count(&apiTokenCount).Error; err == nil {
  237. view.HasApiToken = apiTokenCount > 0
  238. }
  239. view.TgBotToken = ""
  240. view.TwoFactorToken = ""
  241. view.LdapPassword = ""
  242. view.SmtpPassword = ""
  243. return view, nil
  244. }
  245. func secretConfigured(value string) bool {
  246. return strings.TrimSpace(value) != ""
  247. }
  248. func mustString(value string, _ error) string {
  249. return value
  250. }
  251. func getEnv(key, fallback string) string {
  252. val, ok := os.LookupEnv(key)
  253. if !ok {
  254. return fallback
  255. }
  256. val = strings.TrimSpace(val)
  257. if val == "" {
  258. return fallback
  259. }
  260. return val
  261. }
  262. func (s *SettingService) ResetSettings() error {
  263. db := database.GetDB()
  264. err := db.Where("1 = 1").Delete(model.Setting{}).Error
  265. if err != nil {
  266. return err
  267. }
  268. return db.Model(model.User{}).
  269. Where("1 = 1").Error
  270. }
  271. func (s *SettingService) getSetting(key string) (*model.Setting, error) {
  272. db := database.GetDB()
  273. setting := &model.Setting{}
  274. err := db.Model(model.Setting{}).Where("key = ?", key).First(setting).Error
  275. if err != nil {
  276. return nil, err
  277. }
  278. return setting, nil
  279. }
  280. func (s *SettingService) saveSetting(key string, value string) error {
  281. setting, err := s.getSetting(key)
  282. db := database.GetDB()
  283. if database.IsNotFound(err) {
  284. return db.Create(&model.Setting{
  285. Key: key,
  286. Value: value,
  287. }).Error
  288. } else if err != nil {
  289. return err
  290. }
  291. setting.Key = key
  292. setting.Value = value
  293. return db.Save(setting).Error
  294. }
  295. func (s *SettingService) getString(key string) (string, error) {
  296. setting, err := s.getSetting(key)
  297. if database.IsNotFound(err) {
  298. value, ok := defaultValueMap[key]
  299. if !ok {
  300. return "", common.NewErrorf("key <%v> not in defaultValueMap", key)
  301. }
  302. return value, nil
  303. } else if err != nil {
  304. return "", err
  305. }
  306. return setting.Value, nil
  307. }
  308. func (s *SettingService) setString(key string, value string) error {
  309. return s.saveSetting(key, value)
  310. }
  311. func effectiveSettingValue(key, stored string) string {
  312. if stored == "" {
  313. if def, ok := defaultValueMap[key]; ok {
  314. return def
  315. }
  316. }
  317. return stored
  318. }
  319. func (s *SettingService) getBool(key string) (bool, error) {
  320. str, err := s.getString(key)
  321. if err != nil {
  322. return false, err
  323. }
  324. return strconv.ParseBool(effectiveSettingValue(key, str))
  325. }
  326. func (s *SettingService) setBool(key string, value bool) error {
  327. return s.setString(key, strconv.FormatBool(value))
  328. }
  329. func (s *SettingService) getInt(key string) (int, error) {
  330. str, err := s.getString(key)
  331. if err != nil {
  332. return 0, err
  333. }
  334. return strconv.Atoi(effectiveSettingValue(key, str))
  335. }
  336. func (s *SettingService) setInt(key string, value int) error {
  337. return s.setString(key, strconv.Itoa(value))
  338. }
  339. func (s *SettingService) GetWarpLastUpdate() (int64, error) {
  340. val, err := s.getString("warpLastUpdate")
  341. if err != nil || val == "" {
  342. return 0, err
  343. }
  344. return strconv.ParseInt(val, 10, 64)
  345. }
  346. func (s *SettingService) SetWarpLastUpdate(val int64) error {
  347. return s.saveSetting("warpLastUpdate", strconv.FormatInt(val, 10))
  348. }
  349. func (s *SettingService) SetWarpUpdateInterval(val int) error {
  350. return s.setInt("warpUpdateInterval", val)
  351. }
  352. func (s *SettingService) GetXrayConfigTemplate() (string, error) {
  353. return s.getString("xrayTemplateConfig")
  354. }
  355. func (s *SettingService) GetXrayOutboundTestUrl() (string, error) {
  356. return s.getString("xrayOutboundTestUrl")
  357. }
  358. func (s *SettingService) SetXrayOutboundTestUrl(url string) error {
  359. clean, err := SanitizeHTTPURL(url)
  360. if err != nil {
  361. return err
  362. }
  363. return s.setString("xrayOutboundTestUrl", clean)
  364. }
  365. func (s *SettingService) GetListen() (string, error) {
  366. return s.getString("webListen")
  367. }
  368. func (s *SettingService) SetListen(ip string) error {
  369. return s.setString("webListen", ip)
  370. }
  371. func (s *SettingService) GetWebDomain() (string, error) {
  372. return s.getString("webDomain")
  373. }
  374. func (s *SettingService) GetTgBotToken() (string, error) {
  375. return s.getString("tgBotToken")
  376. }
  377. func (s *SettingService) SetTgBotToken(token string) error {
  378. return s.setString("tgBotToken", token)
  379. }
  380. func (s *SettingService) GetTgBotProxy() (string, error) {
  381. return s.getString("tgBotProxy")
  382. }
  383. func (s *SettingService) SetTgBotProxy(token string) error {
  384. return s.setString("tgBotProxy", token)
  385. }
  386. // GetPanelOutbound returns the Xray outbound tag the panel's own outbound
  387. // requests (version checks, Telegram, subscription fetches) are routed through.
  388. func (s *SettingService) GetPanelOutbound() (string, error) {
  389. return s.getString("panelOutbound")
  390. }
  391. func (s *SettingService) SetPanelOutbound(tag string) error {
  392. return s.setString("panelOutbound", tag)
  393. }
  394. // PanelEgressProxyURL resolves the loopback SOCKS bridge that the generated
  395. // config exposes when a panel outbound is configured (see injectPanelEgress).
  396. // It returns "" — meaning a direct connection — when the feature is off or
  397. // the bridge is not present in the running core yet.
  398. func (s *SettingService) PanelEgressProxyURL() string {
  399. tag, err := s.GetPanelOutbound()
  400. if err != nil || tag == "" {
  401. return ""
  402. }
  403. proc := XrayProcess()
  404. if proc == nil || !proc.IsRunning() {
  405. logger.Warning("panel outbound [", tag, "] is set but Xray is not running, using a direct connection")
  406. return ""
  407. }
  408. cfg := proc.GetConfig()
  409. if cfg == nil {
  410. return ""
  411. }
  412. for i := range cfg.InboundConfigs {
  413. if cfg.InboundConfigs[i].Tag == PanelEgressInboundTag {
  414. return fmt.Sprintf("socks5://127.0.0.1:%d", cfg.InboundConfigs[i].Port)
  415. }
  416. }
  417. logger.Warning("panel outbound [", tag, "] is set but the egress bridge is not in the running config, using a direct connection")
  418. return ""
  419. }
  420. func (s *SettingService) NodeEgressProxyURL(nodeID int) string {
  421. tag := NodeEgressInboundTag(nodeID)
  422. proc := XrayProcess()
  423. if proc == nil || !proc.IsRunning() {
  424. logger.Warning("node outbound [", tag, "] is set but Xray is not running, using a direct connection")
  425. return ""
  426. }
  427. cfg := proc.GetConfig()
  428. if cfg == nil {
  429. return ""
  430. }
  431. for i := range cfg.InboundConfigs {
  432. if cfg.InboundConfigs[i].Tag == tag {
  433. return fmt.Sprintf("socks5://127.0.0.1:%d", cfg.InboundConfigs[i].Port)
  434. }
  435. }
  436. logger.Warning("node outbound [", tag, "] is set but the egress bridge is not in the running config, using a direct connection")
  437. return ""
  438. }
  439. // NewProxiedHTTPClient returns an HTTP client that routes the panel's own
  440. // outbound requests through the configured panel outbound (via the loopback
  441. // SOCKS bridge in the running Xray). When the feature is off or the bridge
  442. // is unavailable it falls back to a direct client.
  443. func (s *SettingService) NewProxiedHTTPClient(timeout time.Duration) *http.Client {
  444. proxyUrl := s.PanelEgressProxyURL()
  445. client, err := netproxy.NewHTTPClient(proxyUrl, timeout)
  446. if err != nil {
  447. logger.Warningf("Invalid panel egress proxy %q, using direct connection: %v", proxyUrl, err)
  448. return &http.Client{Timeout: timeout}
  449. }
  450. return client
  451. }
  452. func (s *SettingService) GetTgBotAPIServer() (string, error) {
  453. return s.getString("tgBotAPIServer")
  454. }
  455. func (s *SettingService) SetTgBotAPIServer(token string) error {
  456. return s.setString("tgBotAPIServer", token)
  457. }
  458. func (s *SettingService) GetTgBotChatId() (string, error) {
  459. return s.getString("tgBotChatId")
  460. }
  461. func (s *SettingService) SetTgBotChatId(chatIds string) error {
  462. return s.setString("tgBotChatId", chatIds)
  463. }
  464. func (s *SettingService) GetTgbotEnabled() (bool, error) {
  465. return s.getBool("tgBotEnable")
  466. }
  467. func (s *SettingService) SetTgbotEnabled(value bool) error {
  468. return s.setBool("tgBotEnable", value)
  469. }
  470. func (s *SettingService) GetTgbotRuntime() (string, error) {
  471. return s.getString("tgRunTime")
  472. }
  473. func (s *SettingService) SetTgbotRuntime(time string) error {
  474. return s.setString("tgRunTime", time)
  475. }
  476. func (s *SettingService) GetTgBotBackup() (bool, error) {
  477. return s.getBool("tgBotBackup")
  478. }
  479. func (s *SettingService) GetTgCpu() (int, error) {
  480. return s.getInt("tgCpu")
  481. }
  482. func (s *SettingService) GetTgMemory() (int, error) {
  483. return s.getInt("tgMemory")
  484. }
  485. func (s *SettingService) SetTgMemory(value int) error {
  486. return s.setInt("tgMemory", value)
  487. }
  488. func (s *SettingService) GetTgLang() (string, error) {
  489. return s.getString("tgLang")
  490. }
  491. func (s *SettingService) GetTwoFactorEnable() (bool, error) {
  492. return s.getBool("twoFactorEnable")
  493. }
  494. func (s *SettingService) SetTwoFactorEnable(value bool) error {
  495. return s.setBool("twoFactorEnable", value)
  496. }
  497. func (s *SettingService) GetTwoFactorToken() (string, error) {
  498. return s.getString("twoFactorToken")
  499. }
  500. func (s *SettingService) SetTwoFactorToken(value string) error {
  501. return s.setString("twoFactorToken", value)
  502. }
  503. func (s *SettingService) VerifyTwoFactorCode(code string) error {
  504. enabled, err := s.GetTwoFactorEnable()
  505. if err != nil {
  506. return err
  507. }
  508. if !enabled {
  509. return nil
  510. }
  511. token, err := s.GetTwoFactorToken()
  512. if err != nil {
  513. return err
  514. }
  515. if strings.TrimSpace(token) == "" || !gotp.NewDefaultTOTP(token).Verify(strings.TrimSpace(code), time.Now().Unix()) {
  516. return common.NewError("invalid two factor code")
  517. }
  518. return nil
  519. }
  520. func (s *SettingService) GetPort() (int, error) {
  521. return s.getInt("webPort")
  522. }
  523. func (s *SettingService) SetPort(port int) error {
  524. return s.setInt("webPort", port)
  525. }
  526. func (s *SettingService) SetCertFile(webCertFile string) error {
  527. return s.setString("webCertFile", webCertFile)
  528. }
  529. func (s *SettingService) GetCertFile() (string, error) {
  530. return s.getString("webCertFile")
  531. }
  532. func (s *SettingService) SetKeyFile(webKeyFile string) error {
  533. return s.setString("webKeyFile", webKeyFile)
  534. }
  535. func (s *SettingService) GetKeyFile() (string, error) {
  536. return s.getString("webKeyFile")
  537. }
  538. func (s *SettingService) GetExpireDiff() (int, error) {
  539. return s.getInt("expireDiff")
  540. }
  541. func (s *SettingService) GetTrafficDiff() (int, error) {
  542. return s.getInt("trafficDiff")
  543. }
  544. func (s *SettingService) GetSessionMaxAge() (int, error) {
  545. return s.getInt("sessionMaxAge")
  546. }
  547. func (s *SettingService) GetTrustedProxyCIDRs() (string, error) {
  548. return s.getString("trustedProxyCIDRs")
  549. }
  550. func (s *SettingService) GetRemarkTemplate() (string, error) {
  551. return s.getString("remarkTemplate")
  552. }
  553. func (s *SettingService) GetSecret() ([]byte, error) {
  554. secret, err := s.getString("secret")
  555. if secret == defaultValueMap["secret"] {
  556. err := s.saveSetting("secret", secret)
  557. if err != nil {
  558. logger.Warning("save secret failed:", err)
  559. }
  560. }
  561. return []byte(secret), err
  562. }
  563. // GetPanelGuid returns this panel's stable self-identifier, persisting a
  564. // freshly generated UUID on first read. It is the globally stable node
  565. // identity used to attribute online clients and inbounds to the physical
  566. // node that hosts them across a chain of nodes (#4983), where per-panel
  567. // autoincrement node ids are meaningless one hop away.
  568. func (s *SettingService) GetPanelGuid() (string, error) {
  569. guid, err := s.getString("panelGuid")
  570. if err != nil {
  571. return "", err
  572. }
  573. if guid == defaultValueMap["panelGuid"] {
  574. if saveErr := s.saveSetting("panelGuid", guid); saveErr != nil {
  575. logger.Warning("save panelGuid failed:", saveErr)
  576. }
  577. }
  578. return guid, nil
  579. }
  580. func (s *SettingService) SetBasePath(basePath string) error {
  581. if !strings.HasPrefix(basePath, "/") {
  582. basePath = "/" + basePath
  583. }
  584. if !strings.HasSuffix(basePath, "/") {
  585. basePath += "/"
  586. }
  587. return s.setString("webBasePath", basePath)
  588. }
  589. func (s *SettingService) GetBasePath() (string, error) {
  590. basePath, err := s.getString("webBasePath")
  591. if err != nil {
  592. return "", err
  593. }
  594. return normalizeBasePath(basePath), nil
  595. }
  596. func (s *SettingService) GetTimeLocation() (*time.Location, error) {
  597. l, err := s.getString("timeLocation")
  598. if err != nil {
  599. return nil, err
  600. }
  601. location, err := time.LoadLocation(l)
  602. if err != nil {
  603. defaultLocation := defaultValueMap["timeLocation"]
  604. logger.Errorf("location <%v> not exist, using default location: %v", l, defaultLocation)
  605. location, err = time.LoadLocation(defaultLocation)
  606. if err != nil {
  607. logger.Errorf("failed to load default location, using UTC: %v", err)
  608. return time.UTC, nil
  609. }
  610. return location, nil
  611. }
  612. return location, nil
  613. }
  614. func (s *SettingService) GetSubEnable() (bool, error) {
  615. return s.getBool("subEnable")
  616. }
  617. func (s *SettingService) GetSubJsonEnable() (bool, error) {
  618. return s.getBool("subJsonEnable")
  619. }
  620. func (s *SettingService) GetSubTitle() (string, error) {
  621. return s.getString("subTitle")
  622. }
  623. func (s *SettingService) GetSubSupportUrl() (string, error) {
  624. return s.getString("subSupportUrl")
  625. }
  626. func (s *SettingService) GetSubProfileUrl() (string, error) {
  627. return s.getString("subProfileUrl")
  628. }
  629. func (s *SettingService) GetSubAnnounce() (string, error) {
  630. return s.getString("subAnnounce")
  631. }
  632. func (s *SettingService) GetSubEnableRouting() (bool, error) {
  633. return s.getBool("subEnableRouting")
  634. }
  635. func (s *SettingService) GetSubRoutingRules() (string, error) {
  636. return s.getString("subRoutingRules")
  637. }
  638. func (s *SettingService) GetSubHideSettings() (bool, error) {
  639. return s.getBool("subHideSettings")
  640. }
  641. func (s *SettingService) GetSubIncyEnableRouting() (bool, error) {
  642. return s.getBool("subIncyEnableRouting")
  643. }
  644. func (s *SettingService) GetSubIncyRoutingRules() (string, error) {
  645. return s.getString("subIncyRoutingRules")
  646. }
  647. func (s *SettingService) GetSubListen() (string, error) {
  648. return s.getString("subListen")
  649. }
  650. func (s *SettingService) GetSubPort() (int, error) {
  651. return s.getInt("subPort")
  652. }
  653. func (s *SettingService) GetSubPath() (string, error) {
  654. return s.getString("subPath")
  655. }
  656. func (s *SettingService) GetSubJsonPath() (string, error) {
  657. return s.getString("subJsonPath")
  658. }
  659. func (s *SettingService) GetSubDomain() (string, error) {
  660. return s.getString("subDomain")
  661. }
  662. func (s *SettingService) SetSubCertFile(subCertFile string) error {
  663. return s.setString("subCertFile", subCertFile)
  664. }
  665. func (s *SettingService) GetSubCertFile() (string, error) {
  666. return s.getString("subCertFile")
  667. }
  668. func (s *SettingService) SetSubKeyFile(subKeyFile string) error {
  669. return s.setString("subKeyFile", subKeyFile)
  670. }
  671. func (s *SettingService) GetSubKeyFile() (string, error) {
  672. return s.getString("subKeyFile")
  673. }
  674. func (s *SettingService) GetSubUpdates() (string, error) {
  675. return s.getString("subUpdates")
  676. }
  677. func (s *SettingService) GetSubEncrypt() (bool, error) {
  678. return s.getBool("subEncrypt")
  679. }
  680. func (s *SettingService) GetPageSize() (int, error) {
  681. return s.getInt("pageSize")
  682. }
  683. func (s *SettingService) GetSubURI() (string, error) {
  684. return s.getString("subURI")
  685. }
  686. func (s *SettingService) GetSubJsonURI() (string, error) {
  687. return s.getString("subJsonURI")
  688. }
  689. func (s *SettingService) GetSubClashEnable() (bool, error) {
  690. return s.getBool("subClashEnable")
  691. }
  692. func (s *SettingService) GetSubClashPath() (string, error) {
  693. return s.getString("subClashPath")
  694. }
  695. func (s *SettingService) GetSubClashURI() (string, error) {
  696. return s.getString("subClashURI")
  697. }
  698. func (s *SettingService) GetSubClashEnableRouting() (bool, error) {
  699. return s.getBool("subClashEnableRouting")
  700. }
  701. func (s *SettingService) GetSubClashRules() (string, error) {
  702. return s.getString("subClashRules")
  703. }
  704. func (s *SettingService) GetSubJsonMux() (string, error) {
  705. return s.getString("subJsonMux")
  706. }
  707. func (s *SettingService) GetSubJsonRules() (string, error) {
  708. return s.getString("subJsonRules")
  709. }
  710. func (s *SettingService) GetSubJsonFinalMask() (string, error) {
  711. return s.getString("subJsonFinalMask")
  712. }
  713. func (s *SettingService) GetSubThemeDir() (string, error) {
  714. return s.getString("subThemeDir")
  715. }
  716. func (s *SettingService) GetDatepicker() (string, error) {
  717. return s.getString("datepicker")
  718. }
  719. func (s *SettingService) GetWarp() (string, error) {
  720. return s.getString("warp")
  721. }
  722. func (s *SettingService) SetWarp(data string) error {
  723. return s.setString("warp", data)
  724. }
  725. func (s *SettingService) GetNord() (string, error) {
  726. return s.getString("nord")
  727. }
  728. func (s *SettingService) SetNord(data string) error {
  729. return s.setString("nord", data)
  730. }
  731. func (s *SettingService) GetExternalTrafficInformEnable() (bool, error) {
  732. return s.getBool("externalTrafficInformEnable")
  733. }
  734. func (s *SettingService) SetExternalTrafficInformEnable(value bool) error {
  735. return s.setBool("externalTrafficInformEnable", value)
  736. }
  737. func (s *SettingService) GetExternalTrafficInformURI() (string, error) {
  738. return s.getString("externalTrafficInformURI")
  739. }
  740. func (s *SettingService) SetExternalTrafficInformURI(InformURI string) error {
  741. return s.setString("externalTrafficInformURI", InformURI)
  742. }
  743. func (s *SettingService) GetRestartXrayOnClientDisable() (bool, error) {
  744. return s.getBool("restartXrayOnClientDisable")
  745. }
  746. func (s *SettingService) SetRestartXrayOnClientDisable(value bool) error {
  747. return s.setBool("restartXrayOnClientDisable", value)
  748. }
  749. // GetDevChannelEnable reports whether the panel self-update tracks the rolling
  750. // per-commit dev release instead of the latest stable tag.
  751. func (s *SettingService) GetDevChannelEnable() (bool, error) {
  752. return s.getBool("devChannelEnable")
  753. }
  754. func (s *SettingService) SetDevChannelEnable(value bool) error {
  755. return s.setBool("devChannelEnable", value)
  756. }
  757. // GetIpLimitEnable reports whether the IP-limit feature is available. Always
  758. // true since the panel enforces limits via the core's online-stats API; on an
  759. // older core the job falls back to access-log parsing and warns there when the
  760. // log is missing, so the UI no longer hides the field behind that condition.
  761. func (s *SettingService) GetIpLimitEnable() (bool, error) {
  762. return true, nil
  763. }
  764. // GetAccessLogEnable reports whether an Xray access log is configured. Used by
  765. // the UI for features that genuinely read the log file (the xray log viewer) —
  766. // distinct from IP limiting, which works without it.
  767. func (s *SettingService) GetAccessLogEnable() (bool, error) {
  768. accessLogPath, err := xray.GetAccessLogPath()
  769. if err != nil {
  770. return false, err
  771. }
  772. return (accessLogPath != "none" && accessLogPath != ""), nil
  773. }
  774. // GetLdapEnable returns whether LDAP is enabled.
  775. func (s *SettingService) GetLdapEnable() (bool, error) {
  776. return s.getBool("ldapEnable")
  777. }
  778. func (s *SettingService) GetLdapHost() (string, error) {
  779. return s.getString("ldapHost")
  780. }
  781. func (s *SettingService) GetLdapPort() (int, error) {
  782. return s.getInt("ldapPort")
  783. }
  784. func (s *SettingService) GetLdapUseTLS() (bool, error) {
  785. return s.getBool("ldapUseTLS")
  786. }
  787. func (s *SettingService) GetLdapBindDN() (string, error) {
  788. return s.getString("ldapBindDN")
  789. }
  790. func (s *SettingService) GetLdapPassword() (string, error) {
  791. return s.getString("ldapPassword")
  792. }
  793. func (s *SettingService) GetLdapBaseDN() (string, error) {
  794. return s.getString("ldapBaseDN")
  795. }
  796. func (s *SettingService) GetLdapUserFilter() (string, error) {
  797. return s.getString("ldapUserFilter")
  798. }
  799. func (s *SettingService) GetLdapUserAttr() (string, error) {
  800. return s.getString("ldapUserAttr")
  801. }
  802. func (s *SettingService) GetLdapVlessField() (string, error) {
  803. return s.getString("ldapVlessField")
  804. }
  805. func (s *SettingService) GetLdapSyncCron() (string, error) {
  806. return s.getString("ldapSyncCron")
  807. }
  808. func (s *SettingService) GetLdapFlagField() (string, error) {
  809. return s.getString("ldapFlagField")
  810. }
  811. func (s *SettingService) GetLdapTruthyValues() (string, error) {
  812. return s.getString("ldapTruthyValues")
  813. }
  814. func (s *SettingService) GetLdapInvertFlag() (bool, error) {
  815. return s.getBool("ldapInvertFlag")
  816. }
  817. func (s *SettingService) GetLdapInboundTags() (string, error) {
  818. return s.getString("ldapInboundTags")
  819. }
  820. func (s *SettingService) GetLdapAutoCreate() (bool, error) {
  821. return s.getBool("ldapAutoCreate")
  822. }
  823. func (s *SettingService) GetLdapAutoDelete() (bool, error) {
  824. return s.getBool("ldapAutoDelete")
  825. }
  826. func (s *SettingService) GetLdapDefaultTotalGB() (int, error) {
  827. return s.getInt("ldapDefaultTotalGB")
  828. }
  829. func (s *SettingService) GetLdapDefaultExpiryDays() (int, error) {
  830. return s.getInt("ldapDefaultExpiryDays")
  831. }
  832. func (s *SettingService) GetLdapDefaultLimitIP() (int, error) {
  833. return s.getInt("ldapDefaultLimitIP")
  834. }
  835. // Event bus — per-subscriber event filtering
  836. func (s *SettingService) GetTgEnabledEvents() (string, error) {
  837. return s.getString("tgEnabledEvents")
  838. }
  839. func (s *SettingService) SetTgEnabledEvents(events string) error {
  840. return s.setString("tgEnabledEvents", events)
  841. }
  842. func (s *SettingService) GetSmtpEnabledEvents() (string, error) {
  843. return s.getString("smtpEnabledEvents")
  844. }
  845. func (s *SettingService) SetSmtpEnabledEvents(events string) error {
  846. return s.setString("smtpEnabledEvents", events)
  847. }
  848. // Email (SMTP) settings
  849. func (s *SettingService) GetSmtpEnable() (bool, error) {
  850. return s.getBool("smtpEnable")
  851. }
  852. func (s *SettingService) SetSmtpEnable(value bool) error {
  853. return s.setBool("smtpEnable", value)
  854. }
  855. func (s *SettingService) GetSmtpHost() (string, error) {
  856. return s.getString("smtpHost")
  857. }
  858. func (s *SettingService) SetSmtpHost(value string) error {
  859. return s.setString("smtpHost", value)
  860. }
  861. func (s *SettingService) GetSmtpPort() (int, error) {
  862. return s.getInt("smtpPort")
  863. }
  864. func (s *SettingService) SetSmtpPort(value int) error {
  865. return s.setInt("smtpPort", value)
  866. }
  867. func (s *SettingService) GetSmtpUsername() (string, error) {
  868. return s.getString("smtpUsername")
  869. }
  870. func (s *SettingService) SetSmtpUsername(value string) error {
  871. return s.setString("smtpUsername", value)
  872. }
  873. func (s *SettingService) GetSmtpPassword() (string, error) {
  874. return s.getString("smtpPassword")
  875. }
  876. func (s *SettingService) SetSmtpPassword(value string) error {
  877. return s.setString("smtpPassword", value)
  878. }
  879. func (s *SettingService) GetSmtpTo() (string, error) {
  880. return s.getString("smtpTo")
  881. }
  882. func (s *SettingService) SetSmtpTo(value string) error {
  883. return s.setString("smtpTo", value)
  884. }
  885. func (s *SettingService) GetSmtpEncryptionType() (string, error) {
  886. return s.getString("smtpEncryptionType")
  887. }
  888. func (s *SettingService) SetSmtpEncryptionType(value string) error {
  889. return s.setString("smtpEncryptionType", value)
  890. }
  891. func (s *SettingService) GetSmtpCpu() (int, error) {
  892. return s.getInt("smtpCpu")
  893. }
  894. func (s *SettingService) SetSmtpCpu(value int) error {
  895. return s.setInt("smtpCpu", value)
  896. }
  897. func (s *SettingService) GetSmtpMemory() (int, error) {
  898. return s.getInt("smtpMemory")
  899. }
  900. func (s *SettingService) SetSmtpMemory(value int) error {
  901. return s.setInt("smtpMemory", value)
  902. }
  903. func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error {
  904. if err := s.preserveRedactedSecrets(allSetting); err != nil {
  905. return err
  906. }
  907. if err := validateSettingsURLs(allSetting); err != nil {
  908. return err
  909. }
  910. if err := allSetting.CheckValid(); err != nil {
  911. return err
  912. }
  913. v := reflect.ValueOf(allSetting).Elem()
  914. t := reflect.TypeFor[entity.AllSetting]()
  915. fields := reflect_util.GetFields(t)
  916. db := database.GetDB()
  917. return db.Transaction(func(tx *gorm.DB) error {
  918. var existing []*model.Setting
  919. if err := tx.Find(&existing).Error; err != nil {
  920. return err
  921. }
  922. byKey := make(map[string]*model.Setting, len(existing))
  923. for _, st := range existing {
  924. byKey[st.Key] = st
  925. }
  926. for _, field := range fields {
  927. key := field.Tag.Get("json")
  928. fieldV := v.FieldByName(field.Name)
  929. value := fmt.Sprint(fieldV.Interface())
  930. if st, ok := byKey[key]; ok {
  931. if st.Value == value {
  932. continue
  933. }
  934. st.Value = value
  935. if err := tx.Save(st).Error; err != nil {
  936. return err
  937. }
  938. continue
  939. }
  940. if err := tx.Create(&model.Setting{Key: key, Value: value}).Error; err != nil {
  941. return err
  942. }
  943. }
  944. return nil
  945. })
  946. }
  947. func (s *SettingService) preserveRedactedSecrets(allSetting *entity.AllSetting) error {
  948. if strings.TrimSpace(allSetting.TgBotToken) == "" {
  949. value, err := s.GetTgBotToken()
  950. if err != nil {
  951. return err
  952. }
  953. allSetting.TgBotToken = value
  954. }
  955. if strings.TrimSpace(allSetting.LdapPassword) == "" {
  956. value, err := s.GetLdapPassword()
  957. if err != nil {
  958. return err
  959. }
  960. allSetting.LdapPassword = value
  961. }
  962. if allSetting.TwoFactorEnable && strings.TrimSpace(allSetting.TwoFactorToken) == "" {
  963. value, err := s.GetTwoFactorToken()
  964. if err != nil {
  965. return err
  966. }
  967. allSetting.TwoFactorToken = value
  968. }
  969. if strings.TrimSpace(allSetting.SmtpPassword) == "" {
  970. value, err := s.GetSmtpPassword()
  971. if err != nil {
  972. return err
  973. }
  974. allSetting.SmtpPassword = value
  975. }
  976. return nil
  977. }
  978. func validateSettingsURLs(allSetting *entity.AllSetting) error {
  979. if allSetting.ExternalTrafficInformURI != "" {
  980. u, err := SanitizeHTTPURL(allSetting.ExternalTrafficInformURI)
  981. if err != nil {
  982. return common.NewError("external traffic inform URI is invalid:", err)
  983. }
  984. allSetting.ExternalTrafficInformURI = u
  985. }
  986. if allSetting.TgBotAPIServer != "" {
  987. u, err := SanitizeHTTPURL(allSetting.TgBotAPIServer)
  988. if err != nil {
  989. return common.NewError("telegram API server URL is invalid:", err)
  990. }
  991. allSetting.TgBotAPIServer = u
  992. }
  993. return nil
  994. }
  995. func (s *SettingService) UpdateSecret(key string, value string) error {
  996. switch key {
  997. case "tgBotToken", "ldapPassword", "twoFactorToken":
  998. return s.saveSetting(key, strings.TrimSpace(value))
  999. default:
  1000. return common.NewError("secret key is not replaceable:", key)
  1001. }
  1002. }
  1003. func (s *SettingService) GetDefaultXrayConfig() (any, error) {
  1004. var jsonData any
  1005. err := json.Unmarshal([]byte(xrayTemplateConfig), &jsonData)
  1006. if err != nil {
  1007. return nil, err
  1008. }
  1009. return jsonData, nil
  1010. }
  1011. func extractHostname(host string) string {
  1012. h, _, err := net.SplitHostPort(host)
  1013. // Err is not nil means host does not contain port
  1014. if err != nil {
  1015. h = host
  1016. }
  1017. ip := net.ParseIP(h)
  1018. // If it's not an IP, return as is
  1019. if ip == nil {
  1020. return h
  1021. }
  1022. // If it's an IPv4, return as is
  1023. if ip.To4() != nil {
  1024. return h
  1025. }
  1026. // IPv6 needs bracketing
  1027. return "[" + h + "]"
  1028. }
  1029. // BuildSubURIBase is shared by GetDefaultSettings (the panel's Client
  1030. // Information page) and the subscription page so both render subscription
  1031. // URLs identically.
  1032. func (s *SettingService) BuildSubURIBase(host string) string {
  1033. subPort, _ := s.GetSubPort()
  1034. subDomain, _ := s.GetSubDomain()
  1035. subKeyFile, _ := s.GetSubKeyFile()
  1036. subCertFile, _ := s.GetSubCertFile()
  1037. subTLS := subKeyFile != "" && subCertFile != ""
  1038. if subDomain == "" {
  1039. subDomain = extractHostname(host)
  1040. }
  1041. scheme := "http"
  1042. if subTLS {
  1043. scheme = "https"
  1044. }
  1045. if (subPort == 443 && subTLS) || (subPort == 80 && !subTLS) {
  1046. return scheme + "://" + subDomain
  1047. }
  1048. return fmt.Sprintf("%s://%s:%d", scheme, subDomain, subPort)
  1049. }
  1050. func (s *SettingService) GetDefaultSettings(host string) (any, error) {
  1051. type settingFunc func() (any, error)
  1052. settings := map[string]settingFunc{
  1053. "expireDiff": func() (any, error) { return s.GetExpireDiff() },
  1054. "trafficDiff": func() (any, error) { return s.GetTrafficDiff() },
  1055. "pageSize": func() (any, error) { return s.GetPageSize() },
  1056. "defaultCert": func() (any, error) { return s.GetCertFile() },
  1057. "defaultKey": func() (any, error) { return s.GetKeyFile() },
  1058. "tgBotEnable": func() (any, error) { return s.GetTgbotEnabled() },
  1059. "subThemeDir": func() (any, error) { return s.GetSubThemeDir() },
  1060. "subEnable": func() (any, error) { return s.GetSubEnable() },
  1061. "subJsonEnable": func() (any, error) { return s.GetSubJsonEnable() },
  1062. "subClashEnable": func() (any, error) { return s.GetSubClashEnable() },
  1063. "subTitle": func() (any, error) { return s.GetSubTitle() },
  1064. "subURI": func() (any, error) { return s.GetSubURI() },
  1065. "subJsonURI": func() (any, error) { return s.GetSubJsonURI() },
  1066. "subClashURI": func() (any, error) { return s.GetSubClashURI() },
  1067. "datepicker": func() (any, error) { return s.GetDatepicker() },
  1068. "ipLimitEnable": func() (any, error) { return s.GetIpLimitEnable() },
  1069. "accessLogEnable": func() (any, error) { return s.GetAccessLogEnable() },
  1070. "webDomain": func() (any, error) { return s.GetWebDomain() },
  1071. "subDomain": func() (any, error) { return s.GetSubDomain() },
  1072. "devChannelEnable": func() (any, error) { return s.GetDevChannelEnable() },
  1073. "isDevBuild": func() (any, error) { return config.IsDevBuild(), nil },
  1074. }
  1075. result := make(map[string]any)
  1076. for key, fn := range settings {
  1077. value, err := fn()
  1078. if err != nil {
  1079. return "", err
  1080. }
  1081. result[key] = value
  1082. }
  1083. subEnable := result["subEnable"].(bool)
  1084. subJsonEnable := false
  1085. if v, ok := result["subJsonEnable"]; ok {
  1086. if b, ok2 := v.(bool); ok2 {
  1087. subJsonEnable = b
  1088. }
  1089. }
  1090. subClashEnable := false
  1091. if v, ok := result["subClashEnable"]; ok {
  1092. if b, ok2 := v.(bool); ok2 {
  1093. subClashEnable = b
  1094. }
  1095. }
  1096. if (subEnable && result["subURI"].(string) == "") || (subJsonEnable && result["subJsonURI"].(string) == "") || (subClashEnable && result["subClashURI"].(string) == "") {
  1097. subURI := s.BuildSubURIBase(host)
  1098. subTitle, _ := s.GetSubTitle()
  1099. subPath, _ := s.GetSubPath()
  1100. subJsonPath, _ := s.GetSubJsonPath()
  1101. subClashPath, _ := s.GetSubClashPath()
  1102. if subEnable && result["subURI"].(string) == "" {
  1103. result["subURI"] = subURI + subPath
  1104. }
  1105. if result["subTitle"].(string) == "" {
  1106. result["subTitle"] = subTitle
  1107. }
  1108. if subJsonEnable && result["subJsonURI"].(string) == "" {
  1109. result["subJsonURI"] = subURI + subJsonPath
  1110. }
  1111. if subClashEnable && result["subClashURI"].(string) == "" {
  1112. result["subClashURI"] = subURI + subClashPath
  1113. }
  1114. }
  1115. return result, nil
  1116. }