1
0

setting.go 21 KB

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