client_crud.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. package service
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "time"
  9. "github.com/google/uuid"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database"
  11. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  12. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  13. "github.com/mhsanaei/3x-ui/v3/internal/util/random"
  14. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  15. "gorm.io/gorm"
  16. )
  17. func hasForbiddenClientChar(s string) bool {
  18. for _, r := range s {
  19. if r == '/' || r == '\\' || r == ' ' || r < 0x20 || r == 0x7f {
  20. return true
  21. }
  22. }
  23. return false
  24. }
  25. func validateClientEmail(email string) error {
  26. if hasForbiddenClientChar(email) {
  27. return common.NewError("client email contains an invalid character:", email)
  28. }
  29. return nil
  30. }
  31. func validateClientSubID(subID string) error {
  32. if hasForbiddenClientChar(subID) {
  33. return common.NewError("client subId contains an invalid character:", subID)
  34. }
  35. return nil
  36. }
  37. func (s *ClientService) Create(inboundSvc *InboundService, payload *ClientCreatePayload) (bool, error) {
  38. if payload == nil {
  39. return false, common.NewError("empty payload")
  40. }
  41. client := payload.Client
  42. if strings.TrimSpace(client.Email) == "" {
  43. return false, common.NewError("client email is required")
  44. }
  45. if err := validateClientEmail(client.Email); err != nil {
  46. return false, err
  47. }
  48. if err := validateClientSubID(client.SubID); err != nil {
  49. return false, err
  50. }
  51. if len(payload.InboundIds) == 0 {
  52. return false, common.NewError("at least one inbound is required")
  53. }
  54. if client.SubID == "" {
  55. client.SubID = uuid.NewString()
  56. }
  57. if !client.Enable {
  58. client.Enable = true
  59. }
  60. now := time.Now().UnixMilli()
  61. if client.CreatedAt == 0 {
  62. client.CreatedAt = now
  63. }
  64. client.UpdatedAt = now
  65. existing := &model.ClientRecord{}
  66. err := database.GetDB().Where("email = ?", client.Email).First(existing).Error
  67. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  68. return false, err
  69. }
  70. emailTaken := !errors.Is(err, gorm.ErrRecordNotFound)
  71. if emailTaken {
  72. if existing.SubID == "" || existing.SubID != client.SubID {
  73. return false, common.NewError("email already in use:", client.Email)
  74. }
  75. }
  76. if client.SubID != "" {
  77. var subTaken int64
  78. if err := database.GetDB().Model(&model.ClientRecord{}).
  79. Where("sub_id = ? AND email <> ?", client.SubID, client.Email).
  80. Count(&subTaken).Error; err != nil {
  81. return false, err
  82. }
  83. if subTaken > 0 {
  84. return false, common.NewError("subId already in use:", client.SubID)
  85. }
  86. }
  87. needRestart := false
  88. for _, ibId := range payload.InboundIds {
  89. inbound, getErr := inboundSvc.GetInbound(ibId)
  90. if getErr != nil {
  91. return needRestart, getErr
  92. }
  93. if err := s.fillProtocolDefaults(&client, inbound); err != nil {
  94. return needRestart, err
  95. }
  96. settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(client, inbound)}})
  97. if mErr != nil {
  98. return needRestart, mErr
  99. }
  100. nr, addErr := s.AddInboundClient(inboundSvc, &model.Inbound{
  101. Id: ibId,
  102. Settings: string(settingsPayload),
  103. })
  104. if addErr != nil {
  105. return needRestart, addErr
  106. }
  107. if nr {
  108. needRestart = true
  109. }
  110. }
  111. return needRestart, nil
  112. }
  113. func (s *ClientService) fillProtocolDefaults(c *model.Client, ib *model.Inbound) error {
  114. switch ib.Protocol {
  115. case model.VMESS, model.VLESS:
  116. if c.ID == "" {
  117. c.ID = uuid.NewString()
  118. }
  119. case model.Trojan:
  120. if c.Password == "" {
  121. c.Password = strings.ReplaceAll(uuid.NewString(), "-", "")
  122. }
  123. case model.Shadowsocks:
  124. method := shadowsocksMethodFromSettings(ib.Settings)
  125. if c.Password == "" || !validShadowsocksClientKey(method, c.Password) {
  126. c.Password = randomShadowsocksClientKey(method)
  127. }
  128. case model.Hysteria:
  129. if c.Auth == "" {
  130. c.Auth = strings.ReplaceAll(uuid.NewString(), "-", "")
  131. }
  132. }
  133. return nil
  134. }
  135. func clientWithInboundFlow(c model.Client, ib *model.Inbound) model.Client {
  136. if !inboundCanEnableTlsFlow(string(ib.Protocol), ib.StreamSettings, ib.Settings) {
  137. c.Flow = ""
  138. }
  139. return c
  140. }
  141. func shadowsocksMethodFromSettings(settings string) string {
  142. if settings == "" {
  143. return ""
  144. }
  145. var m map[string]any
  146. if err := json.Unmarshal([]byte(settings), &m); err != nil {
  147. return ""
  148. }
  149. method, _ := m["method"].(string)
  150. return method
  151. }
  152. func randomShadowsocksClientKey(method string) string {
  153. if n := shadowsocksKeyBytes(method); n > 0 {
  154. return random.Base64Bytes(n)
  155. }
  156. return strings.ReplaceAll(uuid.NewString(), "-", "")
  157. }
  158. func validShadowsocksClientKey(method, key string) bool {
  159. n := shadowsocksKeyBytes(method)
  160. if n == 0 {
  161. return key != ""
  162. }
  163. decoded, err := base64.StdEncoding.DecodeString(key)
  164. if err != nil {
  165. return false
  166. }
  167. return len(decoded) == n
  168. }
  169. func shadowsocksKeyBytes(method string) int {
  170. switch method {
  171. case "2022-blake3-aes-128-gcm":
  172. return 16
  173. case "2022-blake3-aes-256-gcm", "2022-blake3-chacha20-poly1305":
  174. return 32
  175. }
  176. return 0
  177. }
  178. // normalizeShadowsocksClientKeys rewrites any Shadowsocks-2022 client password
  179. // whose decoded length no longer matches settings.method, which happens after the
  180. // inbound method is switched between ciphers of different key sizes (e.g.
  181. // aes-256↔aes-128). A wrong-length uPSK makes xray reject the user, so the link
  182. // fails to connect; regenerating restores a valid key (clients must re-fetch).
  183. // Non-Shadowsocks / legacy-SS settings pass through unchanged.
  184. func normalizeShadowsocksClientKeys(settings string) (string, bool) {
  185. method := shadowsocksMethodFromSettings(settings)
  186. if shadowsocksKeyBytes(method) == 0 {
  187. return settings, false
  188. }
  189. var m map[string]any
  190. if err := json.Unmarshal([]byte(settings), &m); err != nil {
  191. return settings, false
  192. }
  193. clients, ok := m["clients"].([]any)
  194. if !ok {
  195. return settings, false
  196. }
  197. changed := false
  198. for i := range clients {
  199. c, ok := clients[i].(map[string]any)
  200. if !ok {
  201. continue
  202. }
  203. if pw, _ := c["password"].(string); validShadowsocksClientKey(method, pw) {
  204. continue
  205. }
  206. c["password"] = randomShadowsocksClientKey(method)
  207. clients[i] = c
  208. changed = true
  209. }
  210. if !changed {
  211. return settings, false
  212. }
  213. m["clients"] = clients
  214. bs, err := json.MarshalIndent(m, "", " ")
  215. if err != nil {
  216. return settings, false
  217. }
  218. return string(bs), true
  219. }
  220. func applyShadowsocksClientMethod(clients []any, settings map[string]any) {
  221. method, _ := settings["method"].(string)
  222. is2022 := strings.HasPrefix(method, "2022-blake3-")
  223. for i := range clients {
  224. cm, ok := clients[i].(map[string]any)
  225. if !ok {
  226. continue
  227. }
  228. if is2022 {
  229. if _, hasKey := cm["method"]; hasKey {
  230. delete(cm, "method")
  231. clients[i] = cm
  232. }
  233. continue
  234. }
  235. if method == "" {
  236. continue
  237. }
  238. if existing, _ := cm["method"].(string); existing != "" {
  239. continue
  240. }
  241. cm["method"] = method
  242. clients[i] = cm
  243. }
  244. }
  245. func (s *ClientService) Update(inboundSvc *InboundService, id int, updated model.Client, inboundFilter ...int) (bool, error) {
  246. existing, err := s.GetByID(id)
  247. if err != nil {
  248. return false, err
  249. }
  250. inboundIds, err := s.GetInboundIdsForRecord(id)
  251. if err != nil {
  252. return false, err
  253. }
  254. if len(inboundFilter) > 0 {
  255. allow := make(map[int]struct{}, len(inboundFilter))
  256. for _, fid := range inboundFilter {
  257. allow[fid] = struct{}{}
  258. }
  259. filtered := inboundIds[:0:0]
  260. for _, ibId := range inboundIds {
  261. if _, ok := allow[ibId]; ok {
  262. filtered = append(filtered, ibId)
  263. }
  264. }
  265. inboundIds = filtered
  266. }
  267. if strings.TrimSpace(updated.Email) == "" {
  268. return false, common.NewError("client email is required")
  269. }
  270. if err := validateClientEmail(updated.Email); err != nil {
  271. return false, err
  272. }
  273. if err := validateClientSubID(updated.SubID); err != nil {
  274. return false, err
  275. }
  276. if updated.SubID == "" {
  277. updated.SubID = existing.SubID
  278. }
  279. if updated.SubID == "" {
  280. updated.SubID = uuid.NewString()
  281. }
  282. updated.UpdatedAt = time.Now().UnixMilli()
  283. if updated.CreatedAt == 0 {
  284. updated.CreatedAt = existing.CreatedAt
  285. }
  286. // Preserve existing credentials when the caller omits them, so a partial
  287. // update (e.g. only changing traffic/expiry) doesn't silently rotate the
  288. // client's UUID/password/auth via fillProtocolDefaults. Supplying a new
  289. // value still rotates it intentionally.
  290. if updated.ID == "" {
  291. updated.ID = existing.UUID
  292. }
  293. if updated.Password == "" {
  294. updated.Password = existing.Password
  295. }
  296. if updated.Auth == "" {
  297. updated.Auth = existing.Auth
  298. }
  299. if updated.Email != existing.Email {
  300. var collisionCount int64
  301. if err := database.GetDB().Model(&model.ClientRecord{}).
  302. Where("email = ? AND id <> ?", updated.Email, id).
  303. Count(&collisionCount).Error; err != nil {
  304. return false, err
  305. }
  306. if collisionCount > 0 {
  307. return false, common.NewError("Duplicate email:", updated.Email)
  308. }
  309. if err := database.GetDB().Model(&model.ClientRecord{}).
  310. Where("id = ?", id).
  311. Update("email", updated.Email).Error; err != nil {
  312. return false, err
  313. }
  314. }
  315. if updated.SubID != "" {
  316. var subCollision int64
  317. if err := database.GetDB().Model(&model.ClientRecord{}).
  318. Where("sub_id = ? AND id <> ?", updated.SubID, id).
  319. Count(&subCollision).Error; err != nil {
  320. return false, err
  321. }
  322. if subCollision > 0 {
  323. return false, common.NewError("Duplicate subId:", updated.SubID)
  324. }
  325. }
  326. needRestart := false
  327. for _, ibId := range inboundIds {
  328. inbound, getErr := inboundSvc.GetInbound(ibId)
  329. if getErr != nil {
  330. if errors.Is(getErr, gorm.ErrRecordNotFound) {
  331. if err := database.GetDB().
  332. Where("client_id = ? AND inbound_id = ?", id, ibId).
  333. Delete(&model.ClientInbound{}).Error; err != nil {
  334. return needRestart, err
  335. }
  336. continue
  337. }
  338. return needRestart, getErr
  339. }
  340. if existing.Email == "" {
  341. continue
  342. }
  343. if err := s.fillProtocolDefaults(&updated, inbound); err != nil {
  344. return needRestart, err
  345. }
  346. settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(updated, inbound)}})
  347. if mErr != nil {
  348. return needRestart, mErr
  349. }
  350. nr, upErr := s.UpdateInboundClient(inboundSvc, &model.Inbound{
  351. Id: ibId,
  352. Settings: string(settingsPayload),
  353. }, existing.Email)
  354. if upErr != nil {
  355. return needRestart, upErr
  356. }
  357. if nr {
  358. needRestart = true
  359. }
  360. }
  361. reverseStr := ""
  362. if updated.Reverse != nil && strings.TrimSpace(updated.Reverse.Tag) != "" {
  363. if b, mErr := json.Marshal(updated.Reverse); mErr == nil {
  364. reverseStr = string(b)
  365. }
  366. }
  367. if err := database.GetDB().Model(&model.ClientRecord{}).
  368. Where("id = ?", id).
  369. Update("reverse", reverseStr).Error; err != nil {
  370. return needRestart, err
  371. }
  372. if err := database.GetDB().Model(&model.ClientRecord{}).
  373. Where("id = ?", id).
  374. UpdateColumn("updated_at", time.Now().UnixMilli()).Error; err != nil {
  375. return needRestart, err
  376. }
  377. return needRestart, nil
  378. }
  379. func (s *ClientService) Delete(inboundSvc *InboundService, id int, keepTraffic bool) (bool, error) {
  380. existing, err := s.GetByID(id)
  381. if err != nil {
  382. return false, err
  383. }
  384. tombstoneClientEmail(existing.Email)
  385. inboundIds, err := s.GetInboundIdsForRecord(id)
  386. if err != nil {
  387. return false, err
  388. }
  389. needRestart := false
  390. for _, ibId := range inboundIds {
  391. if _, getErr := inboundSvc.GetInbound(ibId); getErr != nil {
  392. if errors.Is(getErr, gorm.ErrRecordNotFound) {
  393. continue
  394. }
  395. return needRestart, getErr
  396. }
  397. // Always delete by email — the client's stable identity. This removes
  398. // every matching entry from the inbound's settings even when the stored
  399. // credential (UUID/password/auth) drifted from the inbound JSON, or a
  400. // duplicate entry with the same email exists.
  401. if existing.Email == "" {
  402. continue
  403. }
  404. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, existing.Email, false)
  405. if delErr != nil {
  406. // The client is already absent from this inbound (data drift or a
  407. // retried delete). Skip it — deletion stays idempotent.
  408. if errors.Is(delErr, ErrClientNotInInbound) {
  409. continue
  410. }
  411. return needRestart, delErr
  412. }
  413. if nr {
  414. needRestart = true
  415. }
  416. }
  417. db := database.GetDB()
  418. if err := db.Where("client_id = ?", id).Delete(&model.ClientInbound{}).Error; err != nil {
  419. return needRestart, err
  420. }
  421. if err := db.Where("client_id = ?", id).Delete(&model.ClientExternalLink{}).Error; err != nil {
  422. return needRestart, err
  423. }
  424. if !keepTraffic && existing.Email != "" {
  425. if err := db.Where("email = ?", existing.Email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  426. return needRestart, err
  427. }
  428. if err := clearGlobalTraffic(db, existing.Email); err != nil {
  429. return needRestart, err
  430. }
  431. if err := db.Where("client_email = ?", existing.Email).Delete(&model.InboundClientIps{}).Error; err != nil {
  432. return needRestart, err
  433. }
  434. }
  435. if err := db.Delete(&model.ClientRecord{}, id).Error; err != nil {
  436. return needRestart, err
  437. }
  438. return needRestart, nil
  439. }
  440. func (s *ClientService) Attach(inboundSvc *InboundService, id int, inboundIds []int) (bool, error) {
  441. existing, err := s.GetByID(id)
  442. if err != nil {
  443. return false, err
  444. }
  445. currentIds, err := s.GetInboundIdsForRecord(id)
  446. if err != nil {
  447. return false, err
  448. }
  449. have := make(map[int]struct{}, len(currentIds))
  450. for _, x := range currentIds {
  451. have[x] = struct{}{}
  452. }
  453. clientWire := existing.ToClient()
  454. flow, ffErr := s.EffectiveFlow(nil, id)
  455. if ffErr != nil {
  456. return false, ffErr
  457. }
  458. clientWire.Flow = flow
  459. clientWire.UpdatedAt = time.Now().UnixMilli()
  460. needRestart := false
  461. for _, ibId := range inboundIds {
  462. if _, attached := have[ibId]; attached {
  463. continue
  464. }
  465. inbound, getErr := inboundSvc.GetInbound(ibId)
  466. if getErr != nil {
  467. return needRestart, getErr
  468. }
  469. copyClient := *clientWire
  470. if err := s.fillProtocolDefaults(&copyClient, inbound); err != nil {
  471. return needRestart, err
  472. }
  473. settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(copyClient, inbound)}})
  474. if mErr != nil {
  475. return needRestart, mErr
  476. }
  477. nr, addErr := s.AddInboundClient(inboundSvc, &model.Inbound{
  478. Id: ibId,
  479. Settings: string(settingsPayload),
  480. })
  481. if addErr != nil {
  482. return needRestart, addErr
  483. }
  484. if nr {
  485. needRestart = true
  486. }
  487. }
  488. return needRestart, nil
  489. }
  490. func (s *ClientService) CreateOne(inboundSvc *InboundService, inboundId int, client model.Client) (bool, error) {
  491. return s.Create(inboundSvc, &ClientCreatePayload{
  492. Client: client,
  493. InboundIds: []int{inboundId},
  494. })
  495. }
  496. func (s *ClientService) DetachByEmail(inboundSvc *InboundService, inboundId int, email string) (bool, error) {
  497. if email == "" {
  498. return false, common.NewError("client email is required")
  499. }
  500. rec, err := s.GetRecordByEmail(nil, email)
  501. if err != nil {
  502. return false, err
  503. }
  504. return s.Detach(inboundSvc, rec.Id, []int{inboundId})
  505. }
  506. func (s *ClientService) AttachByEmail(inboundSvc *InboundService, email string, inboundIds []int) (bool, error) {
  507. if email == "" {
  508. return false, common.NewError("client email is required")
  509. }
  510. rec, err := s.GetRecordByEmail(nil, email)
  511. if err != nil {
  512. return false, err
  513. }
  514. return s.Attach(inboundSvc, rec.Id, inboundIds)
  515. }
  516. func (s *ClientService) DetachByEmailMany(inboundSvc *InboundService, email string, inboundIds []int) (bool, error) {
  517. if email == "" {
  518. return false, common.NewError("client email is required")
  519. }
  520. rec, err := s.GetRecordByEmail(nil, email)
  521. if err != nil {
  522. return false, err
  523. }
  524. return s.Detach(inboundSvc, rec.Id, inboundIds)
  525. }
  526. func (s *ClientService) DeleteByEmail(inboundSvc *InboundService, email string, keepTraffic bool) (bool, error) {
  527. if email == "" {
  528. return false, common.NewError("client email is required")
  529. }
  530. rec, err := s.GetRecordByEmail(nil, email)
  531. if err == nil {
  532. return s.Delete(inboundSvc, rec.Id, keepTraffic)
  533. }
  534. if !errors.Is(err, gorm.ErrRecordNotFound) {
  535. return false, err
  536. }
  537. inboundIds, idsErr := s.findInboundIdsByClientEmail(email)
  538. if idsErr != nil {
  539. return false, idsErr
  540. }
  541. if len(inboundIds) == 0 {
  542. return false, common.NewError(fmt.Sprintf("client %q not found in any inbound or client record", email))
  543. }
  544. needRestart := false
  545. for _, ibId := range inboundIds {
  546. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, email, false)
  547. if delErr != nil {
  548. if errors.Is(delErr, ErrClientNotInInbound) {
  549. continue
  550. }
  551. return needRestart, delErr
  552. }
  553. if nr {
  554. needRestart = true
  555. }
  556. }
  557. if !keepTraffic {
  558. db := database.GetDB()
  559. if err := db.Where("email = ?", email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  560. return needRestart, err
  561. }
  562. if err := clearGlobalTraffic(db, email); err != nil {
  563. return needRestart, err
  564. }
  565. if err := db.Where("client_email = ?", email).Delete(&model.InboundClientIps{}).Error; err != nil {
  566. return needRestart, err
  567. }
  568. }
  569. return needRestart, nil
  570. }
  571. func (s *ClientService) UpdateByEmail(inboundSvc *InboundService, email string, updated model.Client, inboundFilter ...int) (bool, error) {
  572. if email == "" {
  573. return false, common.NewError("client email is required")
  574. }
  575. rec, err := s.GetRecordByEmail(nil, email)
  576. if err != nil {
  577. return false, err
  578. }
  579. return s.Update(inboundSvc, rec.Id, updated, inboundFilter...)
  580. }
  581. func (s *ClientService) Detach(inboundSvc *InboundService, id int, inboundIds []int) (bool, error) {
  582. existing, err := s.GetByID(id)
  583. if err != nil {
  584. return false, err
  585. }
  586. currentIds, err := s.GetInboundIdsForRecord(id)
  587. if err != nil {
  588. return false, err
  589. }
  590. have := make(map[int]struct{}, len(currentIds))
  591. for _, x := range currentIds {
  592. have[x] = struct{}{}
  593. }
  594. needRestart := false
  595. for _, ibId := range inboundIds {
  596. if _, attached := have[ibId]; !attached {
  597. continue
  598. }
  599. if _, getErr := inboundSvc.GetInbound(ibId); getErr != nil {
  600. return needRestart, getErr
  601. }
  602. // Detach by email — the client's stable identity (see Delete).
  603. if existing.Email == "" {
  604. continue
  605. }
  606. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, existing.Email, true)
  607. if delErr != nil {
  608. if errors.Is(delErr, ErrClientNotInInbound) {
  609. continue
  610. }
  611. return needRestart, delErr
  612. }
  613. if nr {
  614. needRestart = true
  615. }
  616. }
  617. return needRestart, nil
  618. }