setting.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. package service
  2. import (
  3. _ "embed"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "reflect"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/mhsanaei/3x-ui/v2/database"
  12. "github.com/mhsanaei/3x-ui/v2/database/model"
  13. "github.com/mhsanaei/3x-ui/v2/logger"
  14. "github.com/mhsanaei/3x-ui/v2/util/common"
  15. "github.com/mhsanaei/3x-ui/v2/util/random"
  16. "github.com/mhsanaei/3x-ui/v2/util/reflect_util"
  17. "github.com/mhsanaei/3x-ui/v2/web/entity"
  18. "github.com/mhsanaei/3x-ui/v2/xray"
  19. )
  20. //go:embed config.json
  21. var xrayTemplateConfig string
  22. var defaultValueMap = map[string]string{
  23. "xrayTemplateConfig": xrayTemplateConfig,
  24. "webListen": "",
  25. "webDomain": "",
  26. "webPort": "2053",
  27. "webCertFile": "",
  28. "webKeyFile": "",
  29. "secret": random.Seq(32),
  30. "webBasePath": "/",
  31. "sessionMaxAge": "360",
  32. "pageSize": "25",
  33. "expireDiff": "0",
  34. "trafficDiff": "0",
  35. "remarkModel": "-ieo",
  36. "timeLocation": "Local",
  37. "tgBotEnable": "false",
  38. "tgBotToken": "",
  39. "tgBotProxy": "",
  40. "tgBotAPIServer": "",
  41. "tgBotChatId": "",
  42. "tgRunTime": "@daily",
  43. "tgBotBackup": "false",
  44. "tgBotLoginNotify": "true",
  45. "tgCpu": "80",
  46. "tgLang": "en-US",
  47. "twoFactorEnable": "false",
  48. "twoFactorToken": "",
  49. "subEnable": "true",
  50. "subJsonEnable": "false",
  51. "subTitle": "",
  52. "subSupportUrl": "",
  53. "subProfileUrl": "",
  54. "subAnnounce": "",
  55. "subEnableRouting": "true",
  56. "subRoutingRules": "",
  57. "subListen": "",
  58. "subPort": "2096",
  59. "subPath": "/sub/",
  60. "subDomain": "",
  61. "subCertFile": "",
  62. "subKeyFile": "",
  63. "subUpdates": "12",
  64. "subEncrypt": "true",
  65. "subShowInfo": "true",
  66. "subURI": "",
  67. "subJsonPath": "/json/",
  68. "subJsonURI": "",
  69. "subJsonFragment": "",
  70. "subJsonNoises": "",
  71. "subJsonMux": "",
  72. "subJsonRules": "",
  73. "datepicker": "gregorian",
  74. "warp": "",
  75. "externalTrafficInformEnable": "false",
  76. "externalTrafficInformURI": "",
  77. // LDAP defaults
  78. "ldapEnable": "false",
  79. "ldapHost": "",
  80. "ldapPort": "389",
  81. "ldapUseTLS": "false",
  82. "ldapBindDN": "",
  83. "ldapPassword": "",
  84. "ldapBaseDN": "",
  85. "ldapUserFilter": "(objectClass=person)",
  86. "ldapUserAttr": "mail",
  87. "ldapVlessField": "vless_enabled",
  88. "ldapSyncCron": "@every 1m",
  89. "ldapFlagField": "",
  90. "ldapTruthyValues": "true,1,yes,on",
  91. "ldapInvertFlag": "false",
  92. "ldapInboundTags": "",
  93. "ldapAutoCreate": "false",
  94. "ldapAutoDelete": "false",
  95. "ldapDefaultTotalGB": "0",
  96. "ldapDefaultExpiryDays": "0",
  97. "ldapDefaultLimitIP": "0",
  98. }
  99. // SettingService provides business logic for application settings management.
  100. // It handles configuration storage, retrieval, and validation for all system settings.
  101. type SettingService struct{}
  102. func (s *SettingService) GetDefaultJsonConfig() (any, error) {
  103. var jsonData any
  104. err := json.Unmarshal([]byte(xrayTemplateConfig), &jsonData)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return jsonData, nil
  109. }
  110. func (s *SettingService) GetAllSetting() (*entity.AllSetting, error) {
  111. db := database.GetDB()
  112. settings := make([]*model.Setting, 0)
  113. err := db.Model(model.Setting{}).Not("key = ?", "xrayTemplateConfig").Find(&settings).Error
  114. if err != nil {
  115. return nil, err
  116. }
  117. allSetting := &entity.AllSetting{}
  118. t := reflect.TypeOf(allSetting).Elem()
  119. v := reflect.ValueOf(allSetting).Elem()
  120. fields := reflect_util.GetFields(t)
  121. setSetting := func(key, value string) (err error) {
  122. defer func() {
  123. panicErr := recover()
  124. if panicErr != nil {
  125. err = errors.New(fmt.Sprint(panicErr))
  126. }
  127. }()
  128. var found bool
  129. var field reflect.StructField
  130. for _, f := range fields {
  131. if f.Tag.Get("json") == key {
  132. field = f
  133. found = true
  134. break
  135. }
  136. }
  137. if !found {
  138. // Some settings are automatically generated, no need to return to the front end to modify the user
  139. return nil
  140. }
  141. fieldV := v.FieldByName(field.Name)
  142. switch t := fieldV.Interface().(type) {
  143. case int:
  144. n, err := strconv.ParseInt(value, 10, 64)
  145. if err != nil {
  146. return err
  147. }
  148. fieldV.SetInt(n)
  149. case string:
  150. fieldV.SetString(value)
  151. case bool:
  152. fieldV.SetBool(value == "true")
  153. default:
  154. return common.NewErrorf("unknown field %v type %v", key, t)
  155. }
  156. return
  157. }
  158. keyMap := map[string]bool{}
  159. for _, setting := range settings {
  160. err := setSetting(setting.Key, setting.Value)
  161. if err != nil {
  162. return nil, err
  163. }
  164. keyMap[setting.Key] = true
  165. }
  166. for key, value := range defaultValueMap {
  167. if keyMap[key] {
  168. continue
  169. }
  170. err := setSetting(key, value)
  171. if err != nil {
  172. return nil, err
  173. }
  174. }
  175. return allSetting, nil
  176. }
  177. func (s *SettingService) ResetSettings() error {
  178. db := database.GetDB()
  179. err := db.Where("1 = 1").Delete(model.Setting{}).Error
  180. if err != nil {
  181. return err
  182. }
  183. return db.Model(model.User{}).
  184. Where("1 = 1").Error
  185. }
  186. func (s *SettingService) getSetting(key string) (*model.Setting, error) {
  187. db := database.GetDB()
  188. setting := &model.Setting{}
  189. err := db.Model(model.Setting{}).Where("key = ?", key).First(setting).Error
  190. if err != nil {
  191. return nil, err
  192. }
  193. return setting, nil
  194. }
  195. func (s *SettingService) saveSetting(key string, value string) error {
  196. setting, err := s.getSetting(key)
  197. db := database.GetDB()
  198. if database.IsNotFound(err) {
  199. return db.Create(&model.Setting{
  200. Key: key,
  201. Value: value,
  202. }).Error
  203. } else if err != nil {
  204. return err
  205. }
  206. setting.Key = key
  207. setting.Value = value
  208. return db.Save(setting).Error
  209. }
  210. func (s *SettingService) getString(key string) (string, error) {
  211. setting, err := s.getSetting(key)
  212. if database.IsNotFound(err) {
  213. value, ok := defaultValueMap[key]
  214. if !ok {
  215. return "", common.NewErrorf("key <%v> not in defaultValueMap", key)
  216. }
  217. return value, nil
  218. } else if err != nil {
  219. return "", err
  220. }
  221. return setting.Value, nil
  222. }
  223. func (s *SettingService) setString(key string, value string) error {
  224. return s.saveSetting(key, value)
  225. }
  226. func (s *SettingService) getBool(key string) (bool, error) {
  227. str, err := s.getString(key)
  228. if err != nil {
  229. return false, err
  230. }
  231. return strconv.ParseBool(str)
  232. }
  233. func (s *SettingService) setBool(key string, value bool) error {
  234. return s.setString(key, strconv.FormatBool(value))
  235. }
  236. func (s *SettingService) getInt(key string) (int, error) {
  237. str, err := s.getString(key)
  238. if err != nil {
  239. return 0, err
  240. }
  241. return strconv.Atoi(str)
  242. }
  243. func (s *SettingService) setInt(key string, value int) error {
  244. return s.setString(key, strconv.Itoa(value))
  245. }
  246. func (s *SettingService) GetXrayConfigTemplate() (string, error) {
  247. return s.getString("xrayTemplateConfig")
  248. }
  249. func (s *SettingService) GetListen() (string, error) {
  250. return s.getString("webListen")
  251. }
  252. func (s *SettingService) SetListen(ip string) error {
  253. return s.setString("webListen", ip)
  254. }
  255. func (s *SettingService) GetWebDomain() (string, error) {
  256. return s.getString("webDomain")
  257. }
  258. func (s *SettingService) GetTgBotToken() (string, error) {
  259. return s.getString("tgBotToken")
  260. }
  261. func (s *SettingService) SetTgBotToken(token string) error {
  262. return s.setString("tgBotToken", token)
  263. }
  264. func (s *SettingService) GetTgBotProxy() (string, error) {
  265. return s.getString("tgBotProxy")
  266. }
  267. func (s *SettingService) SetTgBotProxy(token string) error {
  268. return s.setString("tgBotProxy", token)
  269. }
  270. func (s *SettingService) GetTgBotAPIServer() (string, error) {
  271. return s.getString("tgBotAPIServer")
  272. }
  273. func (s *SettingService) SetTgBotAPIServer(token string) error {
  274. return s.setString("tgBotAPIServer", token)
  275. }
  276. func (s *SettingService) GetTgBotChatId() (string, error) {
  277. return s.getString("tgBotChatId")
  278. }
  279. func (s *SettingService) SetTgBotChatId(chatIds string) error {
  280. return s.setString("tgBotChatId", chatIds)
  281. }
  282. func (s *SettingService) GetTgbotEnabled() (bool, error) {
  283. return s.getBool("tgBotEnable")
  284. }
  285. func (s *SettingService) SetTgbotEnabled(value bool) error {
  286. return s.setBool("tgBotEnable", value)
  287. }
  288. func (s *SettingService) GetTgbotRuntime() (string, error) {
  289. return s.getString("tgRunTime")
  290. }
  291. func (s *SettingService) SetTgbotRuntime(time string) error {
  292. return s.setString("tgRunTime", time)
  293. }
  294. func (s *SettingService) GetTgBotBackup() (bool, error) {
  295. return s.getBool("tgBotBackup")
  296. }
  297. func (s *SettingService) GetTgBotLoginNotify() (bool, error) {
  298. return s.getBool("tgBotLoginNotify")
  299. }
  300. func (s *SettingService) GetTgCpu() (int, error) {
  301. return s.getInt("tgCpu")
  302. }
  303. func (s *SettingService) GetTgLang() (string, error) {
  304. return s.getString("tgLang")
  305. }
  306. func (s *SettingService) GetTwoFactorEnable() (bool, error) {
  307. return s.getBool("twoFactorEnable")
  308. }
  309. func (s *SettingService) SetTwoFactorEnable(value bool) error {
  310. return s.setBool("twoFactorEnable", value)
  311. }
  312. func (s *SettingService) GetTwoFactorToken() (string, error) {
  313. return s.getString("twoFactorToken")
  314. }
  315. func (s *SettingService) SetTwoFactorToken(value string) error {
  316. return s.setString("twoFactorToken", value)
  317. }
  318. func (s *SettingService) GetPort() (int, error) {
  319. return s.getInt("webPort")
  320. }
  321. func (s *SettingService) SetPort(port int) error {
  322. return s.setInt("webPort", port)
  323. }
  324. func (s *SettingService) SetCertFile(webCertFile string) error {
  325. return s.setString("webCertFile", webCertFile)
  326. }
  327. func (s *SettingService) GetCertFile() (string, error) {
  328. return s.getString("webCertFile")
  329. }
  330. func (s *SettingService) SetKeyFile(webKeyFile string) error {
  331. return s.setString("webKeyFile", webKeyFile)
  332. }
  333. func (s *SettingService) GetKeyFile() (string, error) {
  334. return s.getString("webKeyFile")
  335. }
  336. func (s *SettingService) GetExpireDiff() (int, error) {
  337. return s.getInt("expireDiff")
  338. }
  339. func (s *SettingService) GetTrafficDiff() (int, error) {
  340. return s.getInt("trafficDiff")
  341. }
  342. func (s *SettingService) GetSessionMaxAge() (int, error) {
  343. return s.getInt("sessionMaxAge")
  344. }
  345. func (s *SettingService) GetRemarkModel() (string, error) {
  346. return s.getString("remarkModel")
  347. }
  348. func (s *SettingService) GetSecret() ([]byte, error) {
  349. secret, err := s.getString("secret")
  350. if secret == defaultValueMap["secret"] {
  351. err := s.saveSetting("secret", secret)
  352. if err != nil {
  353. logger.Warning("save secret failed:", err)
  354. }
  355. }
  356. return []byte(secret), err
  357. }
  358. func (s *SettingService) SetBasePath(basePath string) error {
  359. if !strings.HasPrefix(basePath, "/") {
  360. basePath = "/" + basePath
  361. }
  362. if !strings.HasSuffix(basePath, "/") {
  363. basePath += "/"
  364. }
  365. return s.setString("webBasePath", basePath)
  366. }
  367. func (s *SettingService) GetBasePath() (string, error) {
  368. basePath, err := s.getString("webBasePath")
  369. if err != nil {
  370. return "", err
  371. }
  372. if !strings.HasPrefix(basePath, "/") {
  373. basePath = "/" + basePath
  374. }
  375. if !strings.HasSuffix(basePath, "/") {
  376. basePath += "/"
  377. }
  378. return basePath, nil
  379. }
  380. func (s *SettingService) GetTimeLocation() (*time.Location, error) {
  381. l, err := s.getString("timeLocation")
  382. if err != nil {
  383. return nil, err
  384. }
  385. location, err := time.LoadLocation(l)
  386. if err != nil {
  387. defaultLocation := defaultValueMap["timeLocation"]
  388. logger.Errorf("location <%v> not exist, using default location: %v", l, defaultLocation)
  389. return time.LoadLocation(defaultLocation)
  390. }
  391. return location, nil
  392. }
  393. func (s *SettingService) GetSubEnable() (bool, error) {
  394. return s.getBool("subEnable")
  395. }
  396. func (s *SettingService) GetSubJsonEnable() (bool, error) {
  397. return s.getBool("subJsonEnable")
  398. }
  399. func (s *SettingService) GetSubTitle() (string, error) {
  400. return s.getString("subTitle")
  401. }
  402. func (s *SettingService) GetSubSupportUrl() (string, error) {
  403. return s.getString("subSupportUrl")
  404. }
  405. func (s *SettingService) GetSubProfileUrl() (string, error) {
  406. return s.getString("subProfileUrl")
  407. }
  408. func (s *SettingService) GetSubAnnounce() (string, error) {
  409. return s.getString("subAnnounce")
  410. }
  411. func (s *SettingService) GetSubEnableRouting() (bool, error) {
  412. return s.getBool("subEnableRouting")
  413. }
  414. func (s *SettingService) GetSubRoutingRules() (string, error) {
  415. return s.getString("subRoutingRules")
  416. }
  417. func (s *SettingService) GetSubListen() (string, error) {
  418. return s.getString("subListen")
  419. }
  420. func (s *SettingService) GetSubPort() (int, error) {
  421. return s.getInt("subPort")
  422. }
  423. func (s *SettingService) GetSubPath() (string, error) {
  424. return s.getString("subPath")
  425. }
  426. func (s *SettingService) GetSubJsonPath() (string, error) {
  427. return s.getString("subJsonPath")
  428. }
  429. func (s *SettingService) GetSubDomain() (string, error) {
  430. return s.getString("subDomain")
  431. }
  432. func (s *SettingService) SetSubCertFile(subCertFile string) error {
  433. return s.setString("subCertFile", subCertFile)
  434. }
  435. func (s *SettingService) GetSubCertFile() (string, error) {
  436. return s.getString("subCertFile")
  437. }
  438. func (s *SettingService) SetSubKeyFile(subKeyFile string) error {
  439. return s.setString("subKeyFile", subKeyFile)
  440. }
  441. func (s *SettingService) GetSubKeyFile() (string, error) {
  442. return s.getString("subKeyFile")
  443. }
  444. func (s *SettingService) GetSubUpdates() (string, error) {
  445. return s.getString("subUpdates")
  446. }
  447. func (s *SettingService) GetSubEncrypt() (bool, error) {
  448. return s.getBool("subEncrypt")
  449. }
  450. func (s *SettingService) GetSubShowInfo() (bool, error) {
  451. return s.getBool("subShowInfo")
  452. }
  453. func (s *SettingService) GetPageSize() (int, error) {
  454. return s.getInt("pageSize")
  455. }
  456. func (s *SettingService) GetSubURI() (string, error) {
  457. return s.getString("subURI")
  458. }
  459. func (s *SettingService) GetSubJsonURI() (string, error) {
  460. return s.getString("subJsonURI")
  461. }
  462. func (s *SettingService) GetSubJsonFragment() (string, error) {
  463. return s.getString("subJsonFragment")
  464. }
  465. func (s *SettingService) GetSubJsonNoises() (string, error) {
  466. return s.getString("subJsonNoises")
  467. }
  468. func (s *SettingService) GetSubJsonMux() (string, error) {
  469. return s.getString("subJsonMux")
  470. }
  471. func (s *SettingService) GetSubJsonRules() (string, error) {
  472. return s.getString("subJsonRules")
  473. }
  474. func (s *SettingService) GetDatepicker() (string, error) {
  475. return s.getString("datepicker")
  476. }
  477. func (s *SettingService) GetWarp() (string, error) {
  478. return s.getString("warp")
  479. }
  480. func (s *SettingService) SetWarp(data string) error {
  481. return s.setString("warp", data)
  482. }
  483. func (s *SettingService) GetExternalTrafficInformEnable() (bool, error) {
  484. return s.getBool("externalTrafficInformEnable")
  485. }
  486. func (s *SettingService) SetExternalTrafficInformEnable(value bool) error {
  487. return s.setBool("externalTrafficInformEnable", value)
  488. }
  489. func (s *SettingService) GetExternalTrafficInformURI() (string, error) {
  490. return s.getString("externalTrafficInformURI")
  491. }
  492. func (s *SettingService) SetExternalTrafficInformURI(InformURI string) error {
  493. return s.setString("externalTrafficInformURI", InformURI)
  494. }
  495. func (s *SettingService) GetIpLimitEnable() (bool, error) {
  496. accessLogPath, err := xray.GetAccessLogPath()
  497. if err != nil {
  498. return false, err
  499. }
  500. return (accessLogPath != "none" && accessLogPath != ""), nil
  501. }
  502. // LDAP exported getters
  503. func (s *SettingService) GetLdapEnable() (bool, error) {
  504. return s.getBool("ldapEnable")
  505. }
  506. func (s *SettingService) GetLdapHost() (string, error) {
  507. return s.getString("ldapHost")
  508. }
  509. func (s *SettingService) GetLdapPort() (int, error) {
  510. return s.getInt("ldapPort")
  511. }
  512. func (s *SettingService) GetLdapUseTLS() (bool, error) {
  513. return s.getBool("ldapUseTLS")
  514. }
  515. func (s *SettingService) GetLdapBindDN() (string, error) {
  516. return s.getString("ldapBindDN")
  517. }
  518. func (s *SettingService) GetLdapPassword() (string, error) {
  519. return s.getString("ldapPassword")
  520. }
  521. func (s *SettingService) GetLdapBaseDN() (string, error) {
  522. return s.getString("ldapBaseDN")
  523. }
  524. func (s *SettingService) GetLdapUserFilter() (string, error) {
  525. return s.getString("ldapUserFilter")
  526. }
  527. func (s *SettingService) GetLdapUserAttr() (string, error) {
  528. return s.getString("ldapUserAttr")
  529. }
  530. func (s *SettingService) GetLdapVlessField() (string, error) {
  531. return s.getString("ldapVlessField")
  532. }
  533. func (s *SettingService) GetLdapSyncCron() (string, error) {
  534. return s.getString("ldapSyncCron")
  535. }
  536. func (s *SettingService) GetLdapFlagField() (string, error) {
  537. return s.getString("ldapFlagField")
  538. }
  539. func (s *SettingService) GetLdapTruthyValues() (string, error) {
  540. return s.getString("ldapTruthyValues")
  541. }
  542. func (s *SettingService) GetLdapInvertFlag() (bool, error) {
  543. return s.getBool("ldapInvertFlag")
  544. }
  545. func (s *SettingService) GetLdapInboundTags() (string, error) {
  546. return s.getString("ldapInboundTags")
  547. }
  548. func (s *SettingService) GetLdapAutoCreate() (bool, error) {
  549. return s.getBool("ldapAutoCreate")
  550. }
  551. func (s *SettingService) GetLdapAutoDelete() (bool, error) {
  552. return s.getBool("ldapAutoDelete")
  553. }
  554. func (s *SettingService) GetLdapDefaultTotalGB() (int, error) {
  555. return s.getInt("ldapDefaultTotalGB")
  556. }
  557. func (s *SettingService) GetLdapDefaultExpiryDays() (int, error) {
  558. return s.getInt("ldapDefaultExpiryDays")
  559. }
  560. func (s *SettingService) GetLdapDefaultLimitIP() (int, error) {
  561. return s.getInt("ldapDefaultLimitIP")
  562. }
  563. func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error {
  564. if err := allSetting.CheckValid(); err != nil {
  565. return err
  566. }
  567. v := reflect.ValueOf(allSetting).Elem()
  568. t := reflect.TypeOf(allSetting).Elem()
  569. fields := reflect_util.GetFields(t)
  570. errs := make([]error, 0)
  571. for _, field := range fields {
  572. key := field.Tag.Get("json")
  573. fieldV := v.FieldByName(field.Name)
  574. value := fmt.Sprint(fieldV.Interface())
  575. err := s.saveSetting(key, value)
  576. if err != nil {
  577. errs = append(errs, err)
  578. }
  579. }
  580. return common.Combine(errs...)
  581. }
  582. func (s *SettingService) GetDefaultXrayConfig() (any, error) {
  583. var jsonData any
  584. err := json.Unmarshal([]byte(xrayTemplateConfig), &jsonData)
  585. if err != nil {
  586. return nil, err
  587. }
  588. return jsonData, nil
  589. }
  590. func (s *SettingService) GetDefaultSettings(host string) (any, error) {
  591. type settingFunc func() (any, error)
  592. settings := map[string]settingFunc{
  593. "expireDiff": func() (any, error) { return s.GetExpireDiff() },
  594. "trafficDiff": func() (any, error) { return s.GetTrafficDiff() },
  595. "pageSize": func() (any, error) { return s.GetPageSize() },
  596. "defaultCert": func() (any, error) { return s.GetCertFile() },
  597. "defaultKey": func() (any, error) { return s.GetKeyFile() },
  598. "tgBotEnable": func() (any, error) { return s.GetTgbotEnabled() },
  599. "subEnable": func() (any, error) { return s.GetSubEnable() },
  600. "subJsonEnable": func() (any, error) { return s.GetSubJsonEnable() },
  601. "subTitle": func() (any, error) { return s.GetSubTitle() },
  602. "subURI": func() (any, error) { return s.GetSubURI() },
  603. "subJsonURI": func() (any, error) { return s.GetSubJsonURI() },
  604. "remarkModel": func() (any, error) { return s.GetRemarkModel() },
  605. "datepicker": func() (any, error) { return s.GetDatepicker() },
  606. "ipLimitEnable": func() (any, error) { return s.GetIpLimitEnable() },
  607. }
  608. result := make(map[string]any)
  609. for key, fn := range settings {
  610. value, err := fn()
  611. if err != nil {
  612. return "", err
  613. }
  614. result[key] = value
  615. }
  616. subEnable := result["subEnable"].(bool)
  617. subJsonEnable := false
  618. if v, ok := result["subJsonEnable"]; ok {
  619. if b, ok2 := v.(bool); ok2 {
  620. subJsonEnable = b
  621. }
  622. }
  623. if (subEnable && result["subURI"].(string) == "") || (subJsonEnable && result["subJsonURI"].(string) == "") {
  624. subURI := ""
  625. subTitle, _ := s.GetSubTitle()
  626. subPort, _ := s.GetSubPort()
  627. subPath, _ := s.GetSubPath()
  628. subJsonPath, _ := s.GetSubJsonPath()
  629. subDomain, _ := s.GetSubDomain()
  630. subKeyFile, _ := s.GetSubKeyFile()
  631. subCertFile, _ := s.GetSubCertFile()
  632. subTLS := false
  633. if subKeyFile != "" && subCertFile != "" {
  634. subTLS = true
  635. }
  636. if subDomain == "" {
  637. subDomain = strings.Split(host, ":")[0]
  638. }
  639. if subTLS {
  640. subURI = "https://"
  641. } else {
  642. subURI = "http://"
  643. }
  644. if (subPort == 443 && subTLS) || (subPort == 80 && !subTLS) {
  645. subURI += subDomain
  646. } else {
  647. subURI += fmt.Sprintf("%s:%d", subDomain, subPort)
  648. }
  649. if subEnable && result["subURI"].(string) == "" {
  650. result["subURI"] = subURI + subPath
  651. }
  652. if result["subTitle"].(string) == "" {
  653. result["subTitle"] = subTitle
  654. }
  655. if subJsonEnable && result["subJsonURI"].(string) == "" {
  656. result["subJsonURI"] = subURI + subJsonPath
  657. }
  658. }
  659. return result, nil
  660. }