1
0

client_crud.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 !keepTraffic && existing.Email != "" {
  380. if err := db.Where("email = ?", existing.Email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  381. return needRestart, err
  382. }
  383. if err := clearGlobalTraffic(db, existing.Email); err != nil {
  384. return needRestart, err
  385. }
  386. if err := db.Where("client_email = ?", existing.Email).Delete(&model.InboundClientIps{}).Error; err != nil {
  387. return needRestart, err
  388. }
  389. }
  390. if err := db.Delete(&model.ClientRecord{}, id).Error; err != nil {
  391. return needRestart, err
  392. }
  393. return needRestart, nil
  394. }
  395. func (s *ClientService) Attach(inboundSvc *InboundService, id int, inboundIds []int) (bool, error) {
  396. existing, err := s.GetByID(id)
  397. if err != nil {
  398. return false, err
  399. }
  400. currentIds, err := s.GetInboundIdsForRecord(id)
  401. if err != nil {
  402. return false, err
  403. }
  404. have := make(map[int]struct{}, len(currentIds))
  405. for _, x := range currentIds {
  406. have[x] = struct{}{}
  407. }
  408. clientWire := existing.ToClient()
  409. flow, ffErr := s.EffectiveFlow(nil, id)
  410. if ffErr != nil {
  411. return false, ffErr
  412. }
  413. clientWire.Flow = flow
  414. clientWire.UpdatedAt = time.Now().UnixMilli()
  415. needRestart := false
  416. for _, ibId := range inboundIds {
  417. if _, attached := have[ibId]; attached {
  418. continue
  419. }
  420. inbound, getErr := inboundSvc.GetInbound(ibId)
  421. if getErr != nil {
  422. return needRestart, getErr
  423. }
  424. copyClient := *clientWire
  425. if err := s.fillProtocolDefaults(&copyClient, inbound); err != nil {
  426. return needRestart, err
  427. }
  428. settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(copyClient, inbound)}})
  429. if mErr != nil {
  430. return needRestart, mErr
  431. }
  432. nr, addErr := s.AddInboundClient(inboundSvc, &model.Inbound{
  433. Id: ibId,
  434. Settings: string(settingsPayload),
  435. })
  436. if addErr != nil {
  437. return needRestart, addErr
  438. }
  439. if nr {
  440. needRestart = true
  441. }
  442. }
  443. return needRestart, nil
  444. }
  445. func (s *ClientService) CreateOne(inboundSvc *InboundService, inboundId int, client model.Client) (bool, error) {
  446. return s.Create(inboundSvc, &ClientCreatePayload{
  447. Client: client,
  448. InboundIds: []int{inboundId},
  449. })
  450. }
  451. func (s *ClientService) DetachByEmail(inboundSvc *InboundService, inboundId int, email string) (bool, error) {
  452. if email == "" {
  453. return false, common.NewError("client email is required")
  454. }
  455. rec, err := s.GetRecordByEmail(nil, email)
  456. if err != nil {
  457. return false, err
  458. }
  459. return s.Detach(inboundSvc, rec.Id, []int{inboundId})
  460. }
  461. func (s *ClientService) AttachByEmail(inboundSvc *InboundService, email string, inboundIds []int) (bool, error) {
  462. if email == "" {
  463. return false, common.NewError("client email is required")
  464. }
  465. rec, err := s.GetRecordByEmail(nil, email)
  466. if err != nil {
  467. return false, err
  468. }
  469. return s.Attach(inboundSvc, rec.Id, inboundIds)
  470. }
  471. func (s *ClientService) DetachByEmailMany(inboundSvc *InboundService, email string, inboundIds []int) (bool, error) {
  472. if email == "" {
  473. return false, common.NewError("client email is required")
  474. }
  475. rec, err := s.GetRecordByEmail(nil, email)
  476. if err != nil {
  477. return false, err
  478. }
  479. return s.Detach(inboundSvc, rec.Id, inboundIds)
  480. }
  481. func (s *ClientService) DeleteByEmail(inboundSvc *InboundService, email string, keepTraffic bool) (bool, error) {
  482. if email == "" {
  483. return false, common.NewError("client email is required")
  484. }
  485. rec, err := s.GetRecordByEmail(nil, email)
  486. if err == nil {
  487. return s.Delete(inboundSvc, rec.Id, keepTraffic)
  488. }
  489. if !errors.Is(err, gorm.ErrRecordNotFound) {
  490. return false, err
  491. }
  492. inboundIds, idsErr := s.findInboundIdsByClientEmail(email)
  493. if idsErr != nil {
  494. return false, idsErr
  495. }
  496. if len(inboundIds) == 0 {
  497. return false, common.NewError(fmt.Sprintf("client %q not found in any inbound or client record", email))
  498. }
  499. needRestart := false
  500. for _, ibId := range inboundIds {
  501. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, email, false)
  502. if delErr != nil {
  503. if errors.Is(delErr, ErrClientNotInInbound) {
  504. continue
  505. }
  506. return needRestart, delErr
  507. }
  508. if nr {
  509. needRestart = true
  510. }
  511. }
  512. if !keepTraffic {
  513. db := database.GetDB()
  514. if err := db.Where("email = ?", email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  515. return needRestart, err
  516. }
  517. if err := clearGlobalTraffic(db, email); err != nil {
  518. return needRestart, err
  519. }
  520. if err := db.Where("client_email = ?", email).Delete(&model.InboundClientIps{}).Error; err != nil {
  521. return needRestart, err
  522. }
  523. }
  524. return needRestart, nil
  525. }
  526. func (s *ClientService) UpdateByEmail(inboundSvc *InboundService, email string, updated model.Client, inboundFilter ...int) (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 false, err
  533. }
  534. return s.Update(inboundSvc, rec.Id, updated, inboundFilter...)
  535. }
  536. func (s *ClientService) Detach(inboundSvc *InboundService, id int, inboundIds []int) (bool, error) {
  537. existing, err := s.GetByID(id)
  538. if err != nil {
  539. return false, err
  540. }
  541. currentIds, err := s.GetInboundIdsForRecord(id)
  542. if err != nil {
  543. return false, err
  544. }
  545. have := make(map[int]struct{}, len(currentIds))
  546. for _, x := range currentIds {
  547. have[x] = struct{}{}
  548. }
  549. needRestart := false
  550. for _, ibId := range inboundIds {
  551. if _, attached := have[ibId]; !attached {
  552. continue
  553. }
  554. if _, getErr := inboundSvc.GetInbound(ibId); getErr != nil {
  555. return needRestart, getErr
  556. }
  557. // Detach by email — the client's stable identity (see Delete).
  558. if existing.Email == "" {
  559. continue
  560. }
  561. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, existing.Email, true)
  562. if delErr != nil {
  563. if errors.Is(delErr, ErrClientNotInInbound) {
  564. continue
  565. }
  566. return needRestart, delErr
  567. }
  568. if nr {
  569. needRestart = true
  570. }
  571. }
  572. return needRestart, nil
  573. }