inbound_clients.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. targetProtocol := targetInbound.Protocol
  201. switch targetProtocol {
  202. case model.VMESS:
  203. target.ID = s.generateRandomCredential(targetProtocol)
  204. case model.VLESS:
  205. target.ID = s.generateRandomCredential(targetProtocol)
  206. if (flow == "xtls-rprx-vision" || flow == "xtls-rprx-vision-udp443") &&
  207. inboundCanEnableTlsFlow(string(targetProtocol), targetInbound.StreamSettings, targetInbound.Settings) {
  208. target.Flow = flow
  209. }
  210. case model.Trojan, model.Shadowsocks:
  211. target.Password = s.generateRandomCredential(targetProtocol)
  212. case model.Hysteria:
  213. target.Auth = s.generateRandomCredential(targetProtocol)
  214. default:
  215. target.ID = s.generateRandomCredential(targetProtocol)
  216. }
  217. return target, nil
  218. }
  219. func (s *InboundService) nextAvailableCopiedEmail(originalEmail string, targetID int, occupied map[string]struct{}) string {
  220. base := fmt.Sprintf("%s_%d", originalEmail, targetID)
  221. candidate := base
  222. suffix := 0
  223. for {
  224. if _, exists := occupied[strings.ToLower(candidate)]; !exists {
  225. occupied[strings.ToLower(candidate)] = struct{}{}
  226. return candidate
  227. }
  228. suffix++
  229. candidate = fmt.Sprintf("%s_%d", base, suffix)
  230. }
  231. }
  232. func (s *InboundService) CopyInboundClients(targetInboundID int, sourceInboundID int, clientEmails []string, flow string) (*CopyClientsResult, bool, error) {
  233. result := &CopyClientsResult{
  234. Added: []string{},
  235. Skipped: []string{},
  236. Errors: []string{},
  237. }
  238. if targetInboundID == sourceInboundID {
  239. return result, false, common.NewError("source and target inbounds must be different")
  240. }
  241. targetInbound, err := s.GetInbound(targetInboundID)
  242. if err != nil {
  243. return result, false, err
  244. }
  245. sourceInbound, err := s.GetInbound(sourceInboundID)
  246. if err != nil {
  247. return result, false, err
  248. }
  249. sourceClients, err := s.GetClients(sourceInbound)
  250. if err != nil {
  251. return result, false, err
  252. }
  253. if len(sourceClients) == 0 {
  254. return result, false, nil
  255. }
  256. allowedEmails := map[string]struct{}{}
  257. if len(clientEmails) > 0 {
  258. for _, email := range clientEmails {
  259. allowedEmails[strings.ToLower(strings.TrimSpace(email))] = struct{}{}
  260. }
  261. }
  262. occupiedEmails := map[string]struct{}{}
  263. allEmails, err := s.GetAllEmails()
  264. if err != nil {
  265. return result, false, err
  266. }
  267. for _, email := range allEmails {
  268. clean := strings.Trim(email, "\"")
  269. if clean != "" {
  270. occupiedEmails[strings.ToLower(clean)] = struct{}{}
  271. }
  272. }
  273. newClients := make([]model.Client, 0)
  274. needRestart := false
  275. for _, sourceClient := range sourceClients {
  276. originalEmail := strings.TrimSpace(sourceClient.Email)
  277. if originalEmail == "" {
  278. continue
  279. }
  280. if len(allowedEmails) > 0 {
  281. if _, ok := allowedEmails[strings.ToLower(originalEmail)]; !ok {
  282. continue
  283. }
  284. }
  285. if sourceClient.SubID == "" {
  286. newSubID := uuid.NewString()
  287. subNeedRestart, subErr := s.writeBackClientSubID(sourceInbound.Id, sourceClient, newSubID)
  288. if subErr != nil {
  289. result.Errors = append(result.Errors, fmt.Sprintf("%s: failed to write source subId: %v", originalEmail, subErr))
  290. continue
  291. }
  292. if subNeedRestart {
  293. needRestart = true
  294. }
  295. sourceClient.SubID = newSubID
  296. }
  297. targetEmail := s.nextAvailableCopiedEmail(originalEmail, targetInboundID, occupiedEmails)
  298. targetClient, buildErr := s.buildTargetClientFromSource(sourceClient, targetInbound, targetEmail, flow)
  299. if buildErr != nil {
  300. result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", originalEmail, buildErr))
  301. continue
  302. }
  303. newClients = append(newClients, targetClient)
  304. result.Added = append(result.Added, targetEmail)
  305. }
  306. if len(newClients) == 0 {
  307. return result, needRestart, nil
  308. }
  309. settingsPayload, err := json.Marshal(map[string][]model.Client{
  310. "clients": newClients,
  311. })
  312. if err != nil {
  313. return result, needRestart, err
  314. }
  315. addNeedRestart, err := s.clientService.AddInboundClient(s, &model.Inbound{
  316. Id: targetInboundID,
  317. Settings: string(settingsPayload),
  318. })
  319. if err != nil {
  320. return result, needRestart, err
  321. }
  322. if addNeedRestart {
  323. needRestart = true
  324. }
  325. return result, needRestart, nil
  326. }
  327. func (s *InboundService) GetClientInboundByTrafficID(trafficId int) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  328. db := database.GetDB()
  329. var traffics []*xray.ClientTraffic
  330. err = db.Model(xray.ClientTraffic{}).Where("id = ?", trafficId).Find(&traffics).Error
  331. if err != nil {
  332. logger.Warningf("Error retrieving ClientTraffic with trafficId %d: %v", trafficId, err)
  333. return nil, nil, err
  334. }
  335. if len(traffics) == 0 {
  336. return nil, nil, nil
  337. }
  338. traffic = traffics[0]
  339. inbound, err = s.GetInbound(traffic.InboundId)
  340. if errors.Is(err, gorm.ErrRecordNotFound) {
  341. // client_traffics.inbound_id goes stale when an inbound is deleted and
  342. // recreated; fall back to the authoritative client_inbounds link by email.
  343. ids, idErr := s.clientService.GetInboundIdsForEmail(db, traffic.Email)
  344. if idErr != nil {
  345. return traffic, nil, idErr
  346. }
  347. if len(ids) > 0 {
  348. inbound, err = s.GetInbound(ids[0])
  349. }
  350. }
  351. return traffic, inbound, err
  352. }
  353. func (s *InboundService) GetClientInboundByEmail(email string) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  354. db := database.GetDB()
  355. var traffics []*xray.ClientTraffic
  356. err = db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error
  357. if err != nil {
  358. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  359. return nil, nil, err
  360. }
  361. if len(traffics) == 0 {
  362. return nil, nil, nil
  363. }
  364. traffic = traffics[0]
  365. inbound, err = s.GetInbound(traffic.InboundId)
  366. if errors.Is(err, gorm.ErrRecordNotFound) {
  367. // client_traffics.inbound_id is a legacy single-inbound pointer that goes
  368. // stale when an inbound is deleted and recreated: the email-keyed traffic
  369. // row survives but still references the missing inbound. Fall back to the
  370. // authoritative client_inbounds link so email lookups (reset, info, …) work.
  371. ids, idErr := s.clientService.GetInboundIdsForEmail(db, email)
  372. if idErr != nil {
  373. return traffic, nil, idErr
  374. }
  375. if len(ids) > 0 {
  376. inbound, err = s.GetInbound(ids[0])
  377. }
  378. }
  379. return traffic, inbound, err
  380. }
  381. func (s *InboundService) GetClientByEmail(clientEmail string) (*xray.ClientTraffic, *model.Client, error) {
  382. traffic, inbound, err := s.GetClientInboundByEmail(clientEmail)
  383. if err != nil {
  384. return nil, nil, err
  385. }
  386. if inbound == nil {
  387. return nil, nil, common.NewError("Inbound Not Found For Email:", clientEmail)
  388. }
  389. clients, err := s.GetClients(inbound)
  390. if err != nil {
  391. return nil, nil, err
  392. }
  393. for _, client := range clients {
  394. if client.Email == clientEmail {
  395. return traffic, &client, nil
  396. }
  397. }
  398. return nil, nil, common.NewError("Client Not Found In Inbound For Email:", clientEmail)
  399. }
  400. // EmailsByInbound returns the list of client emails currently configured on
  401. // an inbound's settings.clients[]. Used by the "delete all clients" flow on
  402. // the inbounds page, which then feeds the list into ClientService.BulkDelete.
  403. func (s *InboundService) EmailsByInbound(inboundId int) ([]string, error) {
  404. inbound, err := s.GetInbound(inboundId)
  405. if err != nil {
  406. return nil, err
  407. }
  408. clients, err := s.GetClients(inbound)
  409. if err != nil {
  410. return nil, err
  411. }
  412. emails := make([]string, 0, len(clients))
  413. for _, c := range clients {
  414. if e := strings.TrimSpace(c.Email); e != "" {
  415. emails = append(emails, e)
  416. }
  417. }
  418. return emails, nil
  419. }