inbound_clients.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/google/uuid"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  11. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  12. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  13. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  14. "gorm.io/gorm"
  15. )
  16. type CopyClientsResult struct {
  17. Added []string `json:"added"`
  18. Skipped []string `json:"skipped"`
  19. Errors []string `json:"errors"`
  20. }
  21. // enrichClientStats parses each inbound's clients once, fills in the
  22. // UUID/SubId fields on the preloaded ClientStats, and tops up rows owned by
  23. // a sibling inbound (shared-email mode — the row is keyed on email so it
  24. // only preloads on its owning inbound).
  25. func (s *InboundService) enrichClientStats(db *gorm.DB, inbounds []*model.Inbound) {
  26. if len(inbounds) == 0 {
  27. return
  28. }
  29. clientsByInbound := s.backfillClientStats(db, inbounds)
  30. for i, inbound := range inbounds {
  31. clients := clientsByInbound[i]
  32. if len(clients) == 0 || len(inbound.ClientStats) == 0 {
  33. continue
  34. }
  35. cMap := make(map[string]model.Client, len(clients))
  36. for _, c := range clients {
  37. cMap[strings.ToLower(c.Email)] = c
  38. }
  39. for j := range inbound.ClientStats {
  40. email := strings.ToLower(inbound.ClientStats[j].Email)
  41. if c, ok := cMap[email]; ok {
  42. inbound.ClientStats[j].UUID = c.ID
  43. inbound.ClientStats[j].SubId = c.SubID
  44. }
  45. }
  46. }
  47. }
  48. // backfillClientStats tops up each inbound's preloaded ClientStats with rows
  49. // owned by a sibling inbound: client_traffics is keyed on email, so a client
  50. // attached to several inbounds has one row that only preloads on the inbound
  51. // it was created on. Returns the parsed clients per inbound for reuse.
  52. func (s *InboundService) backfillClientStats(db *gorm.DB, inbounds []*model.Inbound) [][]model.Client {
  53. clientsByInbound := make([][]model.Client, len(inbounds))
  54. seenByInbound := make([]map[string]struct{}, len(inbounds))
  55. missing := make(map[string]struct{})
  56. for i, inbound := range inbounds {
  57. clients, _ := s.GetClients(inbound)
  58. clientsByInbound[i] = clients
  59. seen := make(map[string]struct{}, len(inbound.ClientStats))
  60. for _, st := range inbound.ClientStats {
  61. if st.Email != "" {
  62. seen[strings.ToLower(st.Email)] = struct{}{}
  63. }
  64. }
  65. seenByInbound[i] = seen
  66. for _, c := range clients {
  67. if c.Email == "" {
  68. continue
  69. }
  70. if _, ok := seen[strings.ToLower(c.Email)]; !ok {
  71. missing[c.Email] = struct{}{}
  72. }
  73. }
  74. }
  75. if len(missing) > 0 {
  76. emails := make([]string, 0, len(missing))
  77. for e := range missing {
  78. emails = append(emails, e)
  79. }
  80. var extra []xray.ClientTraffic
  81. var loadErr error
  82. for _, batch := range chunkStrings(emails, sqlInChunk) {
  83. var page []xray.ClientTraffic
  84. if err := db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  85. loadErr = err
  86. break
  87. }
  88. extra = append(extra, page...)
  89. }
  90. if loadErr != nil {
  91. logger.Warning("backfillClientStats:", loadErr)
  92. } else {
  93. byEmail := make(map[string]xray.ClientTraffic, len(extra))
  94. for _, st := range extra {
  95. byEmail[strings.ToLower(st.Email)] = st
  96. }
  97. for i, inbound := range inbounds {
  98. for _, c := range clientsByInbound[i] {
  99. if c.Email == "" {
  100. continue
  101. }
  102. key := strings.ToLower(c.Email)
  103. if _, ok := seenByInbound[i][key]; ok {
  104. continue
  105. }
  106. if st, ok := byEmail[key]; ok {
  107. inbound.ClientStats = append(inbound.ClientStats, st)
  108. seenByInbound[i][key] = struct{}{}
  109. }
  110. }
  111. }
  112. }
  113. }
  114. return clientsByInbound
  115. }
  116. // emailUsedByOtherInbounds reports whether email lives in any inbound other
  117. // than exceptInboundId. Empty email returns false.
  118. func (s *InboundService) emailUsedByOtherInbounds(email string, exceptInboundId int) (bool, error) {
  119. if email == "" {
  120. return false, nil
  121. }
  122. db := database.GetDB()
  123. var count int64
  124. query := fmt.Sprintf(
  125. "SELECT COUNT(*) %s WHERE inbounds.id != ? AND LOWER(%s) = LOWER(?)",
  126. database.JSONClientsFromInbound(),
  127. database.JSONFieldText("client.value", "email"),
  128. )
  129. if err := db.Raw(query, exceptInboundId, email).Scan(&count).Error; err != nil {
  130. return false, err
  131. }
  132. return count > 0, nil
  133. }
  134. func (s *InboundService) emailsUsedByOtherInbounds(emails []string, exceptInboundId int) (map[string]bool, error) {
  135. shared := make(map[string]bool, len(emails))
  136. want := make(map[string]struct{}, len(emails))
  137. for _, e := range emails {
  138. e = strings.ToLower(strings.TrimSpace(e))
  139. if e != "" {
  140. want[e] = struct{}{}
  141. }
  142. }
  143. if len(want) == 0 {
  144. return shared, nil
  145. }
  146. db := database.GetDB()
  147. var rows []string
  148. query := fmt.Sprintf(
  149. "SELECT DISTINCT LOWER(%s) %s WHERE inbounds.id != ?",
  150. database.JSONFieldText("client.value", "email"),
  151. database.JSONClientsFromInbound(),
  152. )
  153. if err := db.Raw(query, exceptInboundId).Scan(&rows).Error; err != nil {
  154. return nil, err
  155. }
  156. for _, e := range rows {
  157. e = strings.ToLower(strings.TrimSpace(e))
  158. if _, ok := want[e]; ok {
  159. shared[e] = true
  160. }
  161. }
  162. return shared, nil
  163. }
  164. func (s *InboundService) writeBackClientSubID(sourceInboundID int, client model.Client, subID string) (bool, error) {
  165. client.SubID = subID
  166. client.UpdatedAt = time.Now().UnixMilli()
  167. if client.Email == "" {
  168. return false, common.NewError("empty client email")
  169. }
  170. settingsBytes, err := json.Marshal(map[string][]model.Client{
  171. "clients": {client},
  172. })
  173. if err != nil {
  174. return false, err
  175. }
  176. updatePayload := &model.Inbound{
  177. Id: sourceInboundID,
  178. Settings: string(settingsBytes),
  179. }
  180. return s.clientService.UpdateInboundClient(s, updatePayload, client.Email)
  181. }
  182. func (s *InboundService) generateRandomCredential(targetProtocol model.Protocol) string {
  183. switch targetProtocol {
  184. case model.VMESS, model.VLESS:
  185. return uuid.NewString()
  186. default:
  187. return strings.ReplaceAll(uuid.NewString(), "-", "")
  188. }
  189. }
  190. func (s *InboundService) buildTargetClientFromSource(source model.Client, targetInbound *model.Inbound, email string, flow string) (model.Client, error) {
  191. nowTs := time.Now().UnixMilli()
  192. target := source
  193. target.Email = email
  194. target.CreatedAt = nowTs
  195. target.UpdatedAt = nowTs
  196. target.ID = ""
  197. target.Password = ""
  198. target.Auth = ""
  199. target.Flow = ""
  200. target.Secret = ""
  201. targetProtocol := targetInbound.Protocol
  202. switch targetProtocol {
  203. case model.VMESS:
  204. target.ID = s.generateRandomCredential(targetProtocol)
  205. case model.VLESS:
  206. target.ID = s.generateRandomCredential(targetProtocol)
  207. if (flow == "xtls-rprx-vision" || flow == "xtls-rprx-vision-udp443") &&
  208. !targetInbound.DisableFlow &&
  209. inboundCanEnableTlsFlow(string(targetProtocol), targetInbound.StreamSettings, targetInbound.Settings) {
  210. target.Flow = flow
  211. }
  212. case model.Trojan, model.Shadowsocks:
  213. target.Password = s.generateRandomCredential(targetProtocol)
  214. case model.Hysteria:
  215. target.Auth = s.generateRandomCredential(targetProtocol)
  216. case model.MTProto:
  217. target.Secret = model.GenerateFakeTLSSecret(mtprotoDomainFromSettings(targetInbound.Settings))
  218. default:
  219. target.ID = s.generateRandomCredential(targetProtocol)
  220. }
  221. return target, nil
  222. }
  223. func (s *InboundService) nextAvailableCopiedEmail(originalEmail string, targetID int, occupied map[string]struct{}) string {
  224. base := fmt.Sprintf("%s_%d", originalEmail, targetID)
  225. candidate := base
  226. suffix := 0
  227. for {
  228. if _, exists := occupied[strings.ToLower(candidate)]; !exists {
  229. occupied[strings.ToLower(candidate)] = struct{}{}
  230. return candidate
  231. }
  232. suffix++
  233. candidate = fmt.Sprintf("%s_%d", base, suffix)
  234. }
  235. }
  236. func (s *InboundService) CopyInboundClients(targetInboundID int, sourceInboundID int, clientEmails []string, flow string) (*CopyClientsResult, bool, error) {
  237. result := &CopyClientsResult{
  238. Added: []string{},
  239. Skipped: []string{},
  240. Errors: []string{},
  241. }
  242. if targetInboundID == sourceInboundID {
  243. return result, false, common.NewError("source and target inbounds must be different")
  244. }
  245. targetInbound, err := s.GetInbound(targetInboundID)
  246. if err != nil {
  247. return result, false, err
  248. }
  249. sourceInbound, err := s.GetInbound(sourceInboundID)
  250. if err != nil {
  251. return result, false, err
  252. }
  253. sourceClients, err := s.GetClients(sourceInbound)
  254. if err != nil {
  255. return result, false, err
  256. }
  257. if len(sourceClients) == 0 {
  258. return result, false, nil
  259. }
  260. allowedEmails := map[string]struct{}{}
  261. if len(clientEmails) > 0 {
  262. for _, email := range clientEmails {
  263. allowedEmails[strings.ToLower(strings.TrimSpace(email))] = struct{}{}
  264. }
  265. }
  266. occupiedEmails := map[string]struct{}{}
  267. allEmails, err := s.GetAllEmails()
  268. if err != nil {
  269. return result, false, err
  270. }
  271. for _, email := range allEmails {
  272. clean := strings.Trim(email, "\"")
  273. if clean != "" {
  274. occupiedEmails[strings.ToLower(clean)] = struct{}{}
  275. }
  276. }
  277. newClients := make([]model.Client, 0)
  278. needRestart := false
  279. for _, sourceClient := range sourceClients {
  280. originalEmail := strings.TrimSpace(sourceClient.Email)
  281. if originalEmail == "" {
  282. continue
  283. }
  284. if len(allowedEmails) > 0 {
  285. if _, ok := allowedEmails[strings.ToLower(originalEmail)]; !ok {
  286. continue
  287. }
  288. }
  289. if sourceClient.SubID == "" {
  290. newSubID := uuid.NewString()
  291. subNeedRestart, subErr := s.writeBackClientSubID(sourceInbound.Id, sourceClient, newSubID)
  292. if subErr != nil {
  293. result.Errors = append(result.Errors, fmt.Sprintf("%s: failed to write source subId: %v", originalEmail, subErr))
  294. continue
  295. }
  296. if subNeedRestart {
  297. needRestart = true
  298. }
  299. sourceClient.SubID = newSubID
  300. }
  301. targetEmail := s.nextAvailableCopiedEmail(originalEmail, targetInboundID, occupiedEmails)
  302. targetClient, buildErr := s.buildTargetClientFromSource(sourceClient, targetInbound, targetEmail, flow)
  303. if buildErr != nil {
  304. result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", originalEmail, buildErr))
  305. continue
  306. }
  307. newClients = append(newClients, targetClient)
  308. result.Added = append(result.Added, targetEmail)
  309. }
  310. if len(newClients) == 0 {
  311. return result, needRestart, nil
  312. }
  313. settingsPayload, err := json.Marshal(map[string][]model.Client{
  314. "clients": newClients,
  315. })
  316. if err != nil {
  317. return result, needRestart, err
  318. }
  319. addNeedRestart, err := s.clientService.AddInboundClient(s, &model.Inbound{
  320. Id: targetInboundID,
  321. Settings: string(settingsPayload),
  322. })
  323. if err != nil {
  324. return result, needRestart, err
  325. }
  326. if addNeedRestart {
  327. needRestart = true
  328. }
  329. return result, needRestart, nil
  330. }
  331. func (s *InboundService) GetClientInboundByTrafficID(trafficId int) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  332. db := database.GetDB()
  333. var traffics []*xray.ClientTraffic
  334. err = db.Model(xray.ClientTraffic{}).Where("id = ?", trafficId).Find(&traffics).Error
  335. if err != nil {
  336. logger.Warningf("Error retrieving ClientTraffic with trafficId %d: %v", trafficId, err)
  337. return nil, nil, err
  338. }
  339. if len(traffics) == 0 {
  340. return nil, nil, nil
  341. }
  342. traffic = traffics[0]
  343. inbound, err = s.GetInbound(traffic.InboundId)
  344. if errors.Is(err, gorm.ErrRecordNotFound) {
  345. // client_traffics.inbound_id goes stale when an inbound is deleted and
  346. // recreated; fall back to the authoritative client_inbounds link by email.
  347. ids, idErr := s.clientService.GetInboundIdsForEmail(db, traffic.Email)
  348. if idErr != nil {
  349. return traffic, nil, idErr
  350. }
  351. if len(ids) > 0 {
  352. inbound, err = s.GetInbound(ids[0])
  353. }
  354. }
  355. return traffic, inbound, err
  356. }
  357. func (s *InboundService) GetClientInboundByEmail(email string) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  358. db := database.GetDB()
  359. var traffics []*xray.ClientTraffic
  360. err = db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error
  361. if err != nil {
  362. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  363. return nil, nil, err
  364. }
  365. if len(traffics) == 0 {
  366. return nil, nil, nil
  367. }
  368. traffic = traffics[0]
  369. inbound, err = s.GetInbound(traffic.InboundId)
  370. if errors.Is(err, gorm.ErrRecordNotFound) {
  371. // client_traffics.inbound_id is a legacy single-inbound pointer that goes
  372. // stale when an inbound is deleted and recreated: the email-keyed traffic
  373. // row survives but still references the missing inbound. Fall back to the
  374. // authoritative client_inbounds link so email lookups (reset, info, …) work.
  375. ids, idErr := s.clientService.GetInboundIdsForEmail(db, email)
  376. if idErr != nil {
  377. return traffic, nil, idErr
  378. }
  379. if len(ids) > 0 {
  380. inbound, err = s.GetInbound(ids[0])
  381. }
  382. }
  383. if err == nil && inbound != nil && !s.inboundHasClientEmail(inbound, email) {
  384. // The pointed-at inbound still exists but no longer carries the client —
  385. // the client was moved to another inbound (#6059). Resolve through the
  386. // client_inbounds link to the inbound that actually hosts it now.
  387. ids, idErr := s.clientService.GetInboundIdsForEmail(db, email)
  388. if idErr == nil {
  389. for _, id := range ids {
  390. if id == inbound.Id {
  391. continue
  392. }
  393. if other, oErr := s.GetInbound(id); oErr == nil && s.inboundHasClientEmail(other, email) {
  394. inbound = other
  395. break
  396. }
  397. }
  398. }
  399. }
  400. return traffic, inbound, err
  401. }
  402. func (s *InboundService) inboundHasClientEmail(inbound *model.Inbound, email string) bool {
  403. clients, err := s.GetClients(inbound)
  404. if err != nil {
  405. return false
  406. }
  407. for _, client := range clients {
  408. if client.Email == email {
  409. return true
  410. }
  411. }
  412. return false
  413. }
  414. func (s *InboundService) GetClientByEmail(clientEmail string) (*xray.ClientTraffic, *model.Client, error) {
  415. traffic, inbound, err := s.GetClientInboundByEmail(clientEmail)
  416. if err != nil {
  417. return nil, nil, err
  418. }
  419. if inbound == nil {
  420. return nil, nil, common.NewError("Inbound Not Found For Email:", clientEmail)
  421. }
  422. clients, err := s.GetClients(inbound)
  423. if err != nil {
  424. return nil, nil, err
  425. }
  426. for _, client := range clients {
  427. if client.Email == clientEmail {
  428. return traffic, &client, nil
  429. }
  430. }
  431. return nil, nil, common.NewError("Client Not Found In Inbound For Email:", clientEmail)
  432. }
  433. // EmailsByInbound returns the list of client emails currently configured on
  434. // an inbound's settings.clients[]. Used by the "delete all clients" flow on
  435. // the inbounds page, which then feeds the list into ClientService.BulkDelete.
  436. func (s *InboundService) EmailsByInbound(inboundId int) ([]string, error) {
  437. inbound, err := s.GetInbound(inboundId)
  438. if err != nil {
  439. return nil, err
  440. }
  441. clients, err := s.GetClients(inbound)
  442. if err != nil {
  443. return nil, err
  444. }
  445. emails := make([]string, 0, len(clients))
  446. for _, c := range clients {
  447. if e := strings.TrimSpace(c.Email); e != "" {
  448. emails = append(emails, e)
  449. }
  450. }
  451. return emails, nil
  452. }