setting.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. return time.LoadLocation(defaultLocation)
  402. }
  403. return location, nil
  404. }
  405. func (s *SettingService) GetSubEnable() (bool, error) {
  406. return s.getBool("subEnable")
  407. }
  408. func (s *SettingService) GetSubJsonEnable() (bool, error) {
  409. return s.getBool("subJsonEnable")
  410. }
  411. func (s *SettingService) GetSubTitle() (string, error) {
  412. return s.getString("subTitle")
  413. }
  414. func (s *SettingService) GetSubSupportUrl() (string, error) {
  415. return s.getString("subSupportUrl")
  416. }
  417. func (s *SettingService) GetSubProfileUrl() (string, error) {
  418. return s.getString("subProfileUrl")
  419. }
  420. func (s *SettingService) GetSubAnnounce() (string, error) {
  421. return s.getString("subAnnounce")
  422. }
  423. func (s *SettingService) GetSubEnableRouting() (bool, error) {
  424. return s.getBool("subEnableRouting")
  425. }
  426. func (s *SettingService) GetSubRoutingRules() (string, error) {
  427. return s.getString("subRoutingRules")
  428. }
  429. func (s *SettingService) GetSubListen() (string, error) {
  430. return s.getString("subListen")
  431. }
  432. func (s *SettingService) GetSubPort() (int, error) {
  433. return s.getInt("subPort")
  434. }
  435. func (s *SettingService) GetSubPath() (string, error) {
  436. return s.getString("subPath")
  437. }
  438. func (s *SettingService) GetSubJsonPath() (string, error) {
  439. return s.getString("subJsonPath")
  440. }
  441. func (s *SettingService) GetSubDomain() (string, error) {
  442. return s.getString("subDomain")
  443. }
  444. func (s *SettingService) SetSubCertFile(subCertFile string) error {
  445. return s.setString("subCertFile", subCertFile)
  446. }
  447. func (s *SettingService) GetSubCertFile() (string, error) {
  448. return s.getString("subCertFile")
  449. }
  450. func (s *SettingService) SetSubKeyFile(subKeyFile string) error {
  451. return s.setString("subKeyFile", subKeyFile)
  452. }
  453. func (s *SettingService) GetSubKeyFile() (string, error) {
  454. return s.getString("subKeyFile")
  455. }
  456. func (s *SettingService) GetSubUpdates() (string, error) {
  457. return s.getString("subUpdates")
  458. }
  459. func (s *SettingService) GetSubEncrypt() (bool, error) {
  460. return s.getBool("subEncrypt")
  461. }
  462. func (s *SettingService) GetSubShowInfo() (bool, error) {
  463. return s.getBool("subShowInfo")
  464. }
  465. func (s *SettingService) GetPageSize() (int, error) {
  466. return s.getInt("pageSize")
  467. }
  468. func (s *SettingService) GetSubURI() (string, error) {
  469. return s.getString("subURI")
  470. }
  471. func (s *SettingService) GetSubJsonURI() (string, error) {
  472. return s.getString("subJsonURI")
  473. }
  474. func (s *SettingService) GetSubClashEnable() (bool, error) {
  475. return s.getBool("subClashEnable")
  476. }
  477. func (s *SettingService) GetSubClashPath() (string, error) {
  478. return s.getString("subClashPath")
  479. }
  480. func (s *SettingService) GetSubClashURI() (string, error) {
  481. return s.getString("subClashURI")
  482. }
  483. func (s *SettingService) GetSubJsonFragment() (string, error) {
  484. return s.getString("subJsonFragment")
  485. }
  486. func (s *SettingService) GetSubJsonNoises() (string, error) {
  487. return s.getString("subJsonNoises")
  488. }
  489. func (s *SettingService) GetSubJsonMux() (string, error) {
  490. return s.getString("subJsonMux")
  491. }
  492. func (s *SettingService) GetSubJsonRules() (string, error) {
  493. return s.getString("subJsonRules")
  494. }
  495. func (s *SettingService) GetDatepicker() (string, error) {
  496. return s.getString("datepicker")
  497. }
  498. func (s *SettingService) GetWarp() (string, error) {
  499. return s.getString("warp")
  500. }
  501. func (s *SettingService) SetWarp(data string) error {
  502. return s.setString("warp", data)
  503. }
  504. func (s *SettingService) GetNord() (string, error) {
  505. return s.getString("nord")
  506. }
  507. func (s *SettingService) SetNord(data string) error {
  508. return s.setString("nord", data)
  509. }
  510. func (s *SettingService) GetExternalTrafficInformEnable() (bool, error) {
  511. return s.getBool("externalTrafficInformEnable")
  512. }
  513. func (s *SettingService) SetExternalTrafficInformEnable(value bool) error {
  514. return s.setBool("externalTrafficInformEnable", value)
  515. }
  516. func (s *SettingService) GetExternalTrafficInformURI() (string, error) {
  517. return s.getString("externalTrafficInformURI")
  518. }
  519. func (s *SettingService) SetExternalTrafficInformURI(InformURI string) error {
  520. return s.setString("externalTrafficInformURI", InformURI)
  521. }
  522. func (s *SettingService) GetIpLimitEnable() (bool, error) {
  523. accessLogPath, err := xray.GetAccessLogPath()
  524. if err != nil {
  525. return false, err
  526. }
  527. return (accessLogPath != "none" && accessLogPath != ""), nil
  528. }
  529. // GetLdapEnable returns whether LDAP is enabled.
  530. func (s *SettingService) GetLdapEnable() (bool, error) {
  531. return s.getBool("ldapEnable")
  532. }
  533. func (s *SettingService) GetLdapHost() (string, error) {
  534. return s.getString("ldapHost")
  535. }
  536. func (s *SettingService) GetLdapPort() (int, error) {
  537. return s.getInt("ldapPort")
  538. }
  539. func (s *SettingService) GetLdapUseTLS() (bool, error) {
  540. return s.getBool("ldapUseTLS")
  541. }
  542. func (s *SettingService) GetLdapBindDN() (string, error) {
  543. return s.getString("ldapBindDN")
  544. }
  545. func (s *SettingService) GetLdapPassword() (string, error) {
  546. return s.getString("ldapPassword")
  547. }
  548. func (s *SettingService) GetLdapBaseDN() (string, error) {
  549. return s.getString("ldapBaseDN")
  550. }
  551. func (s *SettingService) GetLdapUserFilter() (string, error) {
  552. return s.getString("ldapUserFilter")
  553. }
  554. func (s *SettingService) GetLdapUserAttr() (string, error) {
  555. return s.getString("ldapUserAttr")
  556. }
  557. func (s *SettingService) GetLdapVlessField() (string, error) {
  558. return s.getString("ldapVlessField")
  559. }
  560. func (s *SettingService) GetLdapSyncCron() (string, error) {
  561. return s.getString("ldapSyncCron")
  562. }
  563. func (s *SettingService) GetLdapFlagField() (string, error) {
  564. return s.getString("ldapFlagField")
  565. }
  566. func (s *SettingService) GetLdapTruthyValues() (string, error) {
  567. return s.getString("ldapTruthyValues")
  568. }
  569. func (s *SettingService) GetLdapInvertFlag() (bool, error) {
  570. return s.getBool("ldapInvertFlag")
  571. }
  572. func (s *SettingService) GetLdapInboundTags() (string, error) {
  573. return s.getString("ldapInboundTags")
  574. }
  575. func (s *SettingService) GetLdapAutoCreate() (bool, error) {
  576. return s.getBool("ldapAutoCreate")
  577. }
  578. func (s *SettingService) GetLdapAutoDelete() (bool, error) {
  579. return s.getBool("ldapAutoDelete")
  580. }
  581. func (s *SettingService) GetLdapDefaultTotalGB() (int, error) {
  582. return s.getInt("ldapDefaultTotalGB")
  583. }
  584. func (s *SettingService) GetLdapDefaultExpiryDays() (int, error) {
  585. return s.getInt("ldapDefaultExpiryDays")
  586. }
  587. func (s *SettingService) GetLdapDefaultLimitIP() (int, error) {
  588. return s.getInt("ldapDefaultLimitIP")
  589. }
  590. func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error {
  591. if err := allSetting.CheckValid(); err != nil {
  592. return err
  593. }
  594. v := reflect.ValueOf(allSetting).Elem()
  595. t := reflect.TypeFor[entity.AllSetting]()
  596. fields := reflect_util.GetFields(t)
  597. errs := make([]error, 0)
  598. for _, field := range fields {
  599. key := field.Tag.Get("json")
  600. fieldV := v.FieldByName(field.Name)
  601. value := fmt.Sprint(fieldV.Interface())
  602. err := s.saveSetting(key, value)
  603. if err != nil {
  604. errs = append(errs, err)
  605. }
  606. }
  607. return common.Combine(errs...)
  608. }
  609. func (s *SettingService) GetDefaultXrayConfig() (any, error) {
  610. var jsonData any
  611. err := json.Unmarshal([]byte(xrayTemplateConfig), &jsonData)
  612. if err != nil {
  613. return nil, err
  614. }
  615. return jsonData, nil
  616. }
  617. func extractHostname(host string) string {
  618. h, _, err := net.SplitHostPort(host)
  619. // Err is not nil means host does not contain port
  620. if err != nil {
  621. h = host
  622. }
  623. ip := net.ParseIP(h)
  624. // If it's not an IP, return as is
  625. if ip == nil {
  626. return h
  627. }
  628. // If it's an IPv4, return as is
  629. if ip.To4() != nil {
  630. return h
  631. }
  632. // IPv6 needs bracketing
  633. return "[" + h + "]"
  634. }
  635. func (s *SettingService) GetDefaultSettings(host string) (any, error) {
  636. type settingFunc func() (any, error)
  637. settings := map[string]settingFunc{
  638. "expireDiff": func() (any, error) { return s.GetExpireDiff() },
  639. "trafficDiff": func() (any, error) { return s.GetTrafficDiff() },
  640. "pageSize": func() (any, error) { return s.GetPageSize() },
  641. "defaultCert": func() (any, error) { return s.GetCertFile() },
  642. "defaultKey": func() (any, error) { return s.GetKeyFile() },
  643. "tgBotEnable": func() (any, error) { return s.GetTgbotEnabled() },
  644. "subEnable": func() (any, error) { return s.GetSubEnable() },
  645. "subJsonEnable": func() (any, error) { return s.GetSubJsonEnable() },
  646. "subClashEnable": func() (any, error) { return s.GetSubClashEnable() },
  647. "subTitle": func() (any, error) { return s.GetSubTitle() },
  648. "subURI": func() (any, error) { return s.GetSubURI() },
  649. "subJsonURI": func() (any, error) { return s.GetSubJsonURI() },
  650. "subClashURI": func() (any, error) { return s.GetSubClashURI() },
  651. "remarkModel": func() (any, error) { return s.GetRemarkModel() },
  652. "datepicker": func() (any, error) { return s.GetDatepicker() },
  653. "ipLimitEnable": func() (any, error) { return s.GetIpLimitEnable() },
  654. }
  655. result := make(map[string]any)
  656. for key, fn := range settings {
  657. value, err := fn()
  658. if err != nil {
  659. return "", err
  660. }
  661. result[key] = value
  662. }
  663. subEnable := result["subEnable"].(bool)
  664. subJsonEnable := false
  665. if v, ok := result["subJsonEnable"]; ok {
  666. if b, ok2 := v.(bool); ok2 {
  667. subJsonEnable = b
  668. }
  669. }
  670. subClashEnable := false
  671. if v, ok := result["subClashEnable"]; ok {
  672. if b, ok2 := v.(bool); ok2 {
  673. subClashEnable = b
  674. }
  675. }
  676. if (subEnable && result["subURI"].(string) == "") || (subJsonEnable && result["subJsonURI"].(string) == "") || (subClashEnable && result["subClashURI"].(string) == "") {
  677. subURI := ""
  678. subTitle, _ := s.GetSubTitle()
  679. subPort, _ := s.GetSubPort()
  680. subPath, _ := s.GetSubPath()
  681. subJsonPath, _ := s.GetSubJsonPath()
  682. subClashPath, _ := s.GetSubClashPath()
  683. subDomain, _ := s.GetSubDomain()
  684. subKeyFile, _ := s.GetSubKeyFile()
  685. subCertFile, _ := s.GetSubCertFile()
  686. subTLS := false
  687. if subKeyFile != "" && subCertFile != "" {
  688. subTLS = true
  689. }
  690. if subDomain == "" {
  691. subDomain = extractHostname(host)
  692. }
  693. if subTLS {
  694. subURI = "https://"
  695. } else {
  696. subURI = "http://"
  697. }
  698. if (subPort == 443 && subTLS) || (subPort == 80 && !subTLS) {
  699. subURI += subDomain
  700. } else {
  701. subURI += fmt.Sprintf("%s:%d", subDomain, subPort)
  702. }
  703. if subEnable && result["subURI"].(string) == "" {
  704. result["subURI"] = subURI + subPath
  705. }
  706. if result["subTitle"].(string) == "" {
  707. result["subTitle"] = subTitle
  708. }
  709. if subJsonEnable && result["subJsonURI"].(string) == "" {
  710. result["subJsonURI"] = subURI + subJsonPath
  711. }
  712. if subClashEnable && result["subClashURI"].(string) == "" {
  713. result["subClashURI"] = subURI + subClashPath
  714. }
  715. }
  716. return result, nil
  717. }