client_crud.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. func applyShadowsocksClientMethod(clients []any, settings map[string]any) {
  179. method, _ := settings["method"].(string)
  180. is2022 := strings.HasPrefix(method, "2022-blake3-")
  181. for i := range clients {
  182. cm, ok := clients[i].(map[string]any)
  183. if !ok {
  184. continue
  185. }
  186. if is2022 {
  187. if _, hasKey := cm["method"]; hasKey {
  188. delete(cm, "method")
  189. clients[i] = cm
  190. }
  191. continue
  192. }
  193. if method == "" {
  194. continue
  195. }
  196. if existing, _ := cm["method"].(string); existing != "" {
  197. continue
  198. }
  199. cm["method"] = method
  200. clients[i] = cm
  201. }
  202. }
  203. func (s *ClientService) Update(inboundSvc *InboundService, id int, updated model.Client, inboundFilter ...int) (bool, error) {
  204. existing, err := s.GetByID(id)
  205. if err != nil {
  206. return false, err
  207. }
  208. inboundIds, err := s.GetInboundIdsForRecord(id)
  209. if err != nil {
  210. return false, err
  211. }
  212. if len(inboundFilter) > 0 {
  213. allow := make(map[int]struct{}, len(inboundFilter))
  214. for _, fid := range inboundFilter {
  215. allow[fid] = struct{}{}
  216. }
  217. filtered := inboundIds[:0:0]
  218. for _, ibId := range inboundIds {
  219. if _, ok := allow[ibId]; ok {
  220. filtered = append(filtered, ibId)
  221. }
  222. }
  223. inboundIds = filtered
  224. }
  225. if strings.TrimSpace(updated.Email) == "" {
  226. return false, common.NewError("client email is required")
  227. }
  228. if err := validateClientEmail(updated.Email); err != nil {
  229. return false, err
  230. }
  231. if err := validateClientSubID(updated.SubID); err != nil {
  232. return false, err
  233. }
  234. if updated.SubID == "" {
  235. updated.SubID = existing.SubID
  236. }
  237. if updated.SubID == "" {
  238. updated.SubID = uuid.NewString()
  239. }
  240. updated.UpdatedAt = time.Now().UnixMilli()
  241. if updated.CreatedAt == 0 {
  242. updated.CreatedAt = existing.CreatedAt
  243. }
  244. // Preserve existing credentials when the caller omits them, so a partial
  245. // update (e.g. only changing traffic/expiry) doesn't silently rotate the
  246. // client's UUID/password/auth via fillProtocolDefaults. Supplying a new
  247. // value still rotates it intentionally.
  248. if updated.ID == "" {
  249. updated.ID = existing.UUID
  250. }
  251. if updated.Password == "" {
  252. updated.Password = existing.Password
  253. }
  254. if updated.Auth == "" {
  255. updated.Auth = existing.Auth
  256. }
  257. if updated.Email != existing.Email {
  258. var collisionCount int64
  259. if err := database.GetDB().Model(&model.ClientRecord{}).
  260. Where("email = ? AND id <> ?", updated.Email, id).
  261. Count(&collisionCount).Error; err != nil {
  262. return false, err
  263. }
  264. if collisionCount > 0 {
  265. return false, common.NewError("Duplicate email:", updated.Email)
  266. }
  267. if err := database.GetDB().Model(&model.ClientRecord{}).
  268. Where("id = ?", id).
  269. Update("email", updated.Email).Error; err != nil {
  270. return false, err
  271. }
  272. }
  273. if updated.SubID != "" {
  274. var subCollision int64
  275. if err := database.GetDB().Model(&model.ClientRecord{}).
  276. Where("sub_id = ? AND id <> ?", updated.SubID, id).
  277. Count(&subCollision).Error; err != nil {
  278. return false, err
  279. }
  280. if subCollision > 0 {
  281. return false, common.NewError("Duplicate subId:", updated.SubID)
  282. }
  283. }
  284. needRestart := false
  285. for _, ibId := range inboundIds {
  286. inbound, getErr := inboundSvc.GetInbound(ibId)
  287. if getErr != nil {
  288. if errors.Is(getErr, gorm.ErrRecordNotFound) {
  289. if err := database.GetDB().
  290. Where("client_id = ? AND inbound_id = ?", id, ibId).
  291. Delete(&model.ClientInbound{}).Error; err != nil {
  292. return needRestart, err
  293. }
  294. continue
  295. }
  296. return needRestart, getErr
  297. }
  298. if existing.Email == "" {
  299. continue
  300. }
  301. if err := s.fillProtocolDefaults(&updated, inbound); err != nil {
  302. return needRestart, err
  303. }
  304. settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(updated, inbound)}})
  305. if mErr != nil {
  306. return needRestart, mErr
  307. }
  308. nr, upErr := s.UpdateInboundClient(inboundSvc, &model.Inbound{
  309. Id: ibId,
  310. Settings: string(settingsPayload),
  311. }, existing.Email)
  312. if upErr != nil {
  313. return needRestart, upErr
  314. }
  315. if nr {
  316. needRestart = true
  317. }
  318. }
  319. reverseStr := ""
  320. if updated.Reverse != nil && strings.TrimSpace(updated.Reverse.Tag) != "" {
  321. if b, mErr := json.Marshal(updated.Reverse); mErr == nil {
  322. reverseStr = string(b)
  323. }
  324. }
  325. if err := database.GetDB().Model(&model.ClientRecord{}).
  326. Where("id = ?", id).
  327. Update("reverse", reverseStr).Error; err != nil {
  328. return needRestart, err
  329. }
  330. if err := database.GetDB().Model(&model.ClientRecord{}).
  331. Where("id = ?", id).
  332. UpdateColumn("updated_at", time.Now().UnixMilli()).Error; err != nil {
  333. return needRestart, err
  334. }
  335. return needRestart, nil
  336. }
  337. func (s *ClientService) Delete(inboundSvc *InboundService, id int, keepTraffic bool) (bool, error) {
  338. existing, err := s.GetByID(id)
  339. if err != nil {
  340. return false, err
  341. }
  342. tombstoneClientEmail(existing.Email)
  343. inboundIds, err := s.GetInboundIdsForRecord(id)
  344. if err != nil {
  345. return false, err
  346. }
  347. needRestart := false
  348. for _, ibId := range inboundIds {
  349. if _, getErr := inboundSvc.GetInbound(ibId); getErr != nil {
  350. if errors.Is(getErr, gorm.ErrRecordNotFound) {
  351. continue
  352. }
  353. return needRestart, getErr
  354. }
  355. // Always delete by email — the client's stable identity. This removes
  356. // every matching entry from the inbound's settings even when the stored
  357. // credential (UUID/password/auth) drifted from the inbound JSON, or a
  358. // duplicate entry with the same email exists.
  359. if existing.Email == "" {
  360. continue
  361. }
  362. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, existing.Email, false)
  363. if delErr != nil {
  364. // The client is already absent from this inbound (data drift or a
  365. // retried delete). Skip it — deletion stays idempotent.
  366. if errors.Is(delErr, ErrClientNotInInbound) {
  367. continue
  368. }
  369. return needRestart, delErr
  370. }
  371. if nr {
  372. needRestart = true
  373. }
  374. }
  375. db := database.GetDB()
  376. if err := db.Where("client_id = ?", id).Delete(&model.ClientInbound{}).Error; err != nil {
  377. return needRestart, err
  378. }
  379. if err := db.Where("client_id = ?", id).Delete(&model.ClientExternalLink{}).Error; err != nil {
  380. return needRestart, err
  381. }
  382. if !keepTraffic && existing.Email != "" {
  383. if err := db.Where("email = ?", existing.Email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  384. return needRestart, err
  385. }
  386. if err := clearGlobalTraffic(db, existing.Email); err != nil {
  387. return needRestart, err
  388. }
  389. if err := db.Where("client_email = ?", existing.Email).Delete(&model.InboundClientIps{}).Error; err != nil {
  390. return needRestart, err
  391. }
  392. }
  393. if err := db.Delete(&model.ClientRecord{}, id).Error; err != nil {
  394. return needRestart, err
  395. }
  396. return needRestart, nil
  397. }
  398. func (s *ClientService) Attach(inboundSvc *InboundService, id int, inboundIds []int) (bool, error) {
  399. existing, err := s.GetByID(id)
  400. if err != nil {
  401. return false, err
  402. }
  403. currentIds, err := s.GetInboundIdsForRecord(id)
  404. if err != nil {
  405. return false, err
  406. }
  407. have := make(map[int]struct{}, len(currentIds))
  408. for _, x := range currentIds {
  409. have[x] = struct{}{}
  410. }
  411. clientWire := existing.ToClient()
  412. flow, ffErr := s.EffectiveFlow(nil, id)
  413. if ffErr != nil {
  414. return false, ffErr
  415. }
  416. clientWire.Flow = flow
  417. clientWire.UpdatedAt = time.Now().UnixMilli()
  418. needRestart := false
  419. for _, ibId := range inboundIds {
  420. if _, attached := have[ibId]; attached {
  421. continue
  422. }
  423. inbound, getErr := inboundSvc.GetInbound(ibId)
  424. if getErr != nil {
  425. return needRestart, getErr
  426. }
  427. copyClient := *clientWire
  428. if err := s.fillProtocolDefaults(&copyClient, inbound); err != nil {
  429. return needRestart, err
  430. }
  431. settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(copyClient, inbound)}})
  432. if mErr != nil {
  433. return needRestart, mErr
  434. }
  435. nr, addErr := s.AddInboundClient(inboundSvc, &model.Inbound{
  436. Id: ibId,
  437. Settings: string(settingsPayload),
  438. })
  439. if addErr != nil {
  440. return needRestart, addErr
  441. }
  442. if nr {
  443. needRestart = true
  444. }
  445. }
  446. return needRestart, nil
  447. }
  448. func (s *ClientService) CreateOne(inboundSvc *InboundService, inboundId int, client model.Client) (bool, error) {
  449. return s.Create(inboundSvc, &ClientCreatePayload{
  450. Client: client,
  451. InboundIds: []int{inboundId},
  452. })
  453. }
  454. func (s *ClientService) DetachByEmail(inboundSvc *InboundService, inboundId int, email string) (bool, error) {
  455. if email == "" {
  456. return false, common.NewError("client email is required")
  457. }
  458. rec, err := s.GetRecordByEmail(nil, email)
  459. if err != nil {
  460. return false, err
  461. }
  462. return s.Detach(inboundSvc, rec.Id, []int{inboundId})
  463. }
  464. func (s *ClientService) AttachByEmail(inboundSvc *InboundService, email string, inboundIds []int) (bool, error) {
  465. if email == "" {
  466. return false, common.NewError("client email is required")
  467. }
  468. rec, err := s.GetRecordByEmail(nil, email)
  469. if err != nil {
  470. return false, err
  471. }
  472. return s.Attach(inboundSvc, rec.Id, inboundIds)
  473. }
  474. func (s *ClientService) DetachByEmailMany(inboundSvc *InboundService, email string, inboundIds []int) (bool, error) {
  475. if email == "" {
  476. return false, common.NewError("client email is required")
  477. }
  478. rec, err := s.GetRecordByEmail(nil, email)
  479. if err != nil {
  480. return false, err
  481. }
  482. return s.Detach(inboundSvc, rec.Id, inboundIds)
  483. }
  484. func (s *ClientService) DeleteByEmail(inboundSvc *InboundService, email string, keepTraffic bool) (bool, error) {
  485. if email == "" {
  486. return false, common.NewError("client email is required")
  487. }
  488. rec, err := s.GetRecordByEmail(nil, email)
  489. if err == nil {
  490. return s.Delete(inboundSvc, rec.Id, keepTraffic)
  491. }
  492. if !errors.Is(err, gorm.ErrRecordNotFound) {
  493. return false, err
  494. }
  495. inboundIds, idsErr := s.findInboundIdsByClientEmail(email)
  496. if idsErr != nil {
  497. return false, idsErr
  498. }
  499. if len(inboundIds) == 0 {
  500. return false, common.NewError(fmt.Sprintf("client %q not found in any inbound or client record", email))
  501. }
  502. needRestart := false
  503. for _, ibId := range inboundIds {
  504. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, email, false)
  505. if delErr != nil {
  506. if errors.Is(delErr, ErrClientNotInInbound) {
  507. continue
  508. }
  509. return needRestart, delErr
  510. }
  511. if nr {
  512. needRestart = true
  513. }
  514. }
  515. if !keepTraffic {
  516. db := database.GetDB()
  517. if err := db.Where("email = ?", email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  518. return needRestart, err
  519. }
  520. if err := clearGlobalTraffic(db, email); err != nil {
  521. return needRestart, err
  522. }
  523. if err := db.Where("client_email = ?", email).Delete(&model.InboundClientIps{}).Error; err != nil {
  524. return needRestart, err
  525. }
  526. }
  527. return needRestart, nil
  528. }
  529. func (s *ClientService) UpdateByEmail(inboundSvc *InboundService, email string, updated model.Client, inboundFilter ...int) (bool, error) {
  530. if email == "" {
  531. return false, common.NewError("client email is required")
  532. }
  533. rec, err := s.GetRecordByEmail(nil, email)
  534. if err != nil {
  535. return false, err
  536. }
  537. return s.Update(inboundSvc, rec.Id, updated, inboundFilter...)
  538. }
  539. func (s *ClientService) Detach(inboundSvc *InboundService, id int, inboundIds []int) (bool, error) {
  540. existing, err := s.GetByID(id)
  541. if err != nil {
  542. return false, err
  543. }
  544. currentIds, err := s.GetInboundIdsForRecord(id)
  545. if err != nil {
  546. return false, err
  547. }
  548. have := make(map[int]struct{}, len(currentIds))
  549. for _, x := range currentIds {
  550. have[x] = struct{}{}
  551. }
  552. needRestart := false
  553. for _, ibId := range inboundIds {
  554. if _, attached := have[ibId]; !attached {
  555. continue
  556. }
  557. if _, getErr := inboundSvc.GetInbound(ibId); getErr != nil {
  558. return needRestart, getErr
  559. }
  560. // Detach by email — the client's stable identity (see Delete).
  561. if existing.Email == "" {
  562. continue
  563. }
  564. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, existing.Email, true)
  565. if delErr != nil {
  566. if errors.Is(delErr, ErrClientNotInInbound) {
  567. continue
  568. }
  569. return needRestart, delErr
  570. }
  571. if nr {
  572. needRestart = true
  573. }
  574. }
  575. return needRestart, nil
  576. }