1
0

setting.go 24 KB

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