1
0

client_crud.go 16 KB

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