setting.go 22 KB

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