setting.go 22 KB

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