inbound_traffic.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  10. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  11. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  12. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  13. "gorm.io/gorm"
  14. "gorm.io/gorm/clause"
  15. )
  16. func (s *InboundService) AddTraffic(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (needRestart bool, clientsDisabled bool, err error) {
  17. var disabledNodeIDs []int
  18. err = submitTrafficWrite(func() error {
  19. var inner error
  20. needRestart, clientsDisabled, disabledNodeIDs, inner = s.addTrafficLocked(inboundTraffics, clientTraffics)
  21. return inner
  22. })
  23. if err == nil && len(disabledNodeIDs) > 0 {
  24. s.restartRemoteNodesOnDisable(disabledNodeIDs)
  25. }
  26. return
  27. }
  28. func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (bool, bool, []int, error) {
  29. var err error
  30. db := database.GetDB()
  31. tx := db.Begin()
  32. defer func() {
  33. if err != nil {
  34. tx.Rollback()
  35. } else {
  36. tx.Commit()
  37. }
  38. }()
  39. err = s.addInboundTraffic(tx, inboundTraffics)
  40. if err != nil {
  41. return false, false, nil, err
  42. }
  43. err = s.addClientTraffic(tx, clientTraffics)
  44. if err != nil {
  45. return false, false, nil, err
  46. }
  47. needRestart0, count, err := s.autoRenewClients(tx)
  48. if err != nil {
  49. logger.Warning("Error in renew clients:", err)
  50. } else if count > 0 {
  51. logger.Debugf("%v clients renewed", count)
  52. }
  53. disabledClientsCount := int64(0)
  54. needRestart1, count, disabledNodeIDs, err := s.disableInvalidClients(tx)
  55. if err != nil {
  56. logger.Warning("Error in disabling invalid clients:", err)
  57. } else if count > 0 {
  58. logger.Debugf("%v clients disabled", count)
  59. disabledClientsCount = count
  60. }
  61. needRestart2, count, err := s.disableInvalidInbounds(tx)
  62. if err != nil {
  63. logger.Warning("Error in disabling invalid inbounds:", err)
  64. } else if count > 0 {
  65. logger.Debugf("%v inbounds disabled", count)
  66. }
  67. return needRestart0 || needRestart1 || needRestart2, disabledClientsCount > 0, disabledNodeIDs, nil
  68. }
  69. func (s *InboundService) addInboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
  70. if len(traffics) == 0 {
  71. return nil
  72. }
  73. var err error
  74. for _, traffic := range traffics {
  75. if traffic.IsInbound {
  76. err = tx.Model(&model.Inbound{}).Where("tag = ? AND node_id IS NULL", traffic.Tag).
  77. Updates(map[string]any{
  78. "up": gorm.Expr("up + ?", traffic.Up),
  79. "down": gorm.Expr("down + ?", traffic.Down),
  80. }).Error
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. }
  86. return nil
  87. }
  88. func (s *InboundService) addClientTraffic(tx *gorm.DB, traffics []*xray.ClientTraffic) (err error) {
  89. if len(traffics) == 0 {
  90. return nil
  91. }
  92. emails := make([]string, 0, len(traffics))
  93. for _, traffic := range traffics {
  94. emails = append(emails, traffic.Email)
  95. }
  96. dbClientTraffics := make([]*xray.ClientTraffic, 0, len(traffics))
  97. // Match purely by email. client_traffics is email-keyed (one shared row per
  98. // email regardless of how many inbounds the client is attached to), and these
  99. // emails come from the local xray's report, so they always belong to a client
  100. // attached to a local inbound. The old `inbound_id NOT IN (node inbounds)`
  101. // filter dropped the local traffic of a client attached to both a node and the
  102. // mother inbound whenever the node inbound happened to be attached first — its
  103. // shared row then carried the node inbound's id (AddClientStat uses OnConflict
  104. // DoNothing and never refreshes it), so the local poll skipped it entirely.
  105. err = tx.Model(xray.ClientTraffic{}).
  106. Where("email IN (?)", emails).
  107. Find(&dbClientTraffics).Error
  108. if err != nil {
  109. return err
  110. }
  111. // Avoid empty slice error
  112. if len(dbClientTraffics) == 0 {
  113. return nil
  114. }
  115. dbClientTraffics, err = s.adjustTraffics(tx, dbClientTraffics)
  116. if err != nil {
  117. return err
  118. }
  119. // Index by email for O(N) merge — the previous nested loop was O(N²)
  120. // and dominated each cron tick on inbounds with thousands of active
  121. // clients (7500 × 7500 = 56M string comparisons every 10 seconds).
  122. trafficByEmail := make(map[string]*xray.ClientTraffic, len(traffics))
  123. for i := range traffics {
  124. if traffics[i] != nil {
  125. trafficByEmail[traffics[i].Email] = traffics[i]
  126. }
  127. }
  128. now := time.Now().UnixMilli()
  129. for dbTraffic_index := range dbClientTraffics {
  130. t, ok := trafficByEmail[dbClientTraffics[dbTraffic_index].Email]
  131. if !ok {
  132. continue
  133. }
  134. dbClientTraffics[dbTraffic_index].Up += t.Up
  135. dbClientTraffics[dbTraffic_index].Down += t.Down
  136. if t.Up+t.Down > 0 {
  137. dbClientTraffics[dbTraffic_index].LastOnline = now
  138. }
  139. }
  140. err = tx.Save(dbClientTraffics).Error
  141. if err != nil {
  142. logger.Warning("AddClientTraffic update data ", err)
  143. }
  144. return nil
  145. }
  146. func (s *InboundService) adjustTraffics(tx *gorm.DB, dbClientTraffics []*xray.ClientTraffic) ([]*xray.ClientTraffic, error) {
  147. now := time.Now().UnixMilli()
  148. // "Start After First Use" stores a negative expiry (the duration). On the
  149. // first traffic tick it becomes an absolute deadline of now+duration. Compute
  150. // it once per email so every inbound the client is attached to lands on the
  151. // same value (recomputing per inbound would skip all but the first one).
  152. newExpiryByEmail := make(map[string]int64, len(dbClientTraffics))
  153. for traffic_index := range dbClientTraffics {
  154. if dbClientTraffics[traffic_index].ExpiryTime < 0 {
  155. newExpiryByEmail[dbClientTraffics[traffic_index].Email] = now - dbClientTraffics[traffic_index].ExpiryTime
  156. }
  157. }
  158. if len(newExpiryByEmail) == 0 {
  159. return dbClientTraffics, nil
  160. }
  161. delayedEmails := make([]string, 0, len(newExpiryByEmail))
  162. for email := range newExpiryByEmail {
  163. delayedEmails = append(delayedEmails, email)
  164. }
  165. // Resolve the owning inbounds through the client_inbounds link, which is
  166. // authoritative. client_traffics.inbound_id goes stale when an inbound is
  167. // deleted and recreated, which would leave the negative expiry unconverted.
  168. var inboundIds []int
  169. err := tx.Table("client_inbounds").
  170. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  171. Where("clients.email IN (?)", delayedEmails).
  172. Distinct().
  173. Pluck("client_inbounds.inbound_id", &inboundIds).Error
  174. if err != nil {
  175. return nil, err
  176. }
  177. if len(inboundIds) == 0 {
  178. return dbClientTraffics, nil
  179. }
  180. var inbounds []*model.Inbound
  181. err = tx.Model(model.Inbound{}).Where("id IN (?)", inboundIds).Find(&inbounds).Error
  182. if err != nil {
  183. return nil, err
  184. }
  185. for inbound_index := range inbounds {
  186. settings := map[string]any{}
  187. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  188. clients, ok := settings["clients"].([]any)
  189. if ok {
  190. var newClients []any
  191. for client_index := range clients {
  192. c := clients[client_index].(map[string]any)
  193. email, _ := c["email"].(string)
  194. if newExpiry, ok := newExpiryByEmail[email]; ok {
  195. c["expiryTime"] = newExpiry
  196. c["updated_at"] = now
  197. }
  198. if _, ok := c["created_at"]; !ok {
  199. c["created_at"] = now
  200. }
  201. if _, ok := c["updated_at"]; !ok {
  202. c["updated_at"] = now
  203. }
  204. newClients = append(newClients, any(c))
  205. }
  206. settings["clients"] = newClients
  207. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  208. if err != nil {
  209. return nil, err
  210. }
  211. inbounds[inbound_index].Settings = string(modifiedSettings)
  212. }
  213. }
  214. for traffic_index := range dbClientTraffics {
  215. if newExpiry, ok := newExpiryByEmail[dbClientTraffics[traffic_index].Email]; ok {
  216. dbClientTraffics[traffic_index].ExpiryTime = newExpiry
  217. }
  218. }
  219. err = tx.Save(inbounds).Error
  220. if err != nil {
  221. logger.Warning("AddClientTraffic update inbounds ", err)
  222. logger.Error(inbounds)
  223. } else {
  224. for _, ib := range inbounds {
  225. if ib == nil {
  226. continue
  227. }
  228. cs, gcErr := s.GetClients(ib)
  229. if gcErr != nil {
  230. logger.Warning("AddClientTraffic sync clients: GetClients failed", gcErr)
  231. continue
  232. }
  233. if syncErr := s.clientService.SyncInbound(tx, ib.Id, cs); syncErr != nil {
  234. logger.Warning("AddClientTraffic sync clients: SyncInbound failed", syncErr)
  235. }
  236. }
  237. }
  238. return dbClientTraffics, nil
  239. }
  240. func (s *InboundService) autoRenewClients(tx *gorm.DB) (bool, int64, error) {
  241. // check for time expired
  242. var traffics []*xray.ClientTraffic
  243. now := time.Now().Unix() * 1000
  244. var err, err1 error
  245. err = tx.Model(xray.ClientTraffic{}).
  246. Where("reset > 0 and expiry_time > 0 and expiry_time <= ?", now).
  247. Where("inbound_id NOT IN (?)", tx.Model(&model.Inbound{}).Select("id").Where("node_id IS NOT NULL")).
  248. Find(&traffics).Error
  249. if err != nil {
  250. return false, 0, err
  251. }
  252. // return if there is no client to renew
  253. if len(traffics) == 0 {
  254. return false, 0, nil
  255. }
  256. var inbound_ids []int
  257. var inbounds []*model.Inbound
  258. needRestart := false
  259. var clientsToAdd []struct {
  260. protocol string
  261. tag string
  262. client map[string]any
  263. }
  264. // Resolve the inbounds to renew through the client_inbounds link rather than
  265. // client_traffics.inbound_id, which goes stale after an inbound is deleted and
  266. // recreated and would otherwise skip the renew entirely.
  267. renewEmails := make([]string, 0, len(traffics))
  268. for _, traffic := range traffics {
  269. renewEmails = append(renewEmails, traffic.Email)
  270. }
  271. for _, batch := range chunkStrings(renewEmails, sqliteMaxVars) {
  272. var ids []int
  273. if err = tx.Table("client_inbounds").
  274. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  275. Where("clients.email IN ?", batch).
  276. Distinct().
  277. Pluck("client_inbounds.inbound_id", &ids).Error; err != nil {
  278. return false, 0, err
  279. }
  280. inbound_ids = append(inbound_ids, ids...)
  281. }
  282. // Dedupe so an inbound hosting N expired clients is fetched and saved once
  283. // per tick instead of N times across chunk boundaries.
  284. inbound_ids = uniqueInts(inbound_ids)
  285. // Chunked to stay under SQLite's bind-variable limit when many inbounds
  286. // are touched in a single tick.
  287. for _, batch := range chunkInts(inbound_ids, sqliteMaxVars) {
  288. var page []*model.Inbound
  289. if err = tx.Model(model.Inbound{}).Where("id IN ?", batch).Find(&page).Error; err != nil {
  290. return false, 0, err
  291. }
  292. inbounds = append(inbounds, page...)
  293. }
  294. for inbound_index := range inbounds {
  295. settings := map[string]any{}
  296. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  297. clients := settings["clients"].([]any)
  298. for client_index := range clients {
  299. c := clients[client_index].(map[string]any)
  300. for traffic_index, traffic := range traffics {
  301. if traffic.Email == c["email"].(string) {
  302. newExpiryTime := traffic.ExpiryTime
  303. for newExpiryTime < now {
  304. newExpiryTime += (int64(traffic.Reset) * 86400000)
  305. }
  306. c["expiryTime"] = newExpiryTime
  307. traffics[traffic_index].ExpiryTime = newExpiryTime
  308. traffics[traffic_index].Down = 0
  309. traffics[traffic_index].Up = 0
  310. if !traffic.Enable {
  311. traffics[traffic_index].Enable = true
  312. c["enable"] = true
  313. clientsToAdd = append(clientsToAdd,
  314. struct {
  315. protocol string
  316. tag string
  317. client map[string]any
  318. }{
  319. protocol: string(inbounds[inbound_index].Protocol),
  320. tag: inbounds[inbound_index].Tag,
  321. client: c,
  322. })
  323. }
  324. clients[client_index] = any(c)
  325. break
  326. }
  327. }
  328. }
  329. settings["clients"] = clients
  330. newSettings, err := json.MarshalIndent(settings, "", " ")
  331. if err != nil {
  332. return false, 0, err
  333. }
  334. inbounds[inbound_index].Settings = string(newSettings)
  335. }
  336. err = tx.Save(inbounds).Error
  337. if err != nil {
  338. return false, 0, err
  339. }
  340. for _, ib := range inbounds {
  341. if ib == nil {
  342. continue
  343. }
  344. cs, gcErr := s.GetClients(ib)
  345. if gcErr != nil {
  346. logger.Warning("autoRenewClients sync clients: GetClients failed", gcErr)
  347. continue
  348. }
  349. if syncErr := s.clientService.SyncInbound(tx, ib.Id, cs); syncErr != nil {
  350. logger.Warning("autoRenewClients sync clients: SyncInbound failed", syncErr)
  351. }
  352. }
  353. err = tx.Save(traffics).Error
  354. if err != nil {
  355. return false, 0, err
  356. }
  357. if p != nil {
  358. err1 = s.xrayApi.Init(p.GetAPIPort())
  359. if err1 != nil {
  360. return true, int64(len(traffics)), nil
  361. }
  362. for _, clientToAdd := range clientsToAdd {
  363. err1 = s.xrayApi.AddUser(clientToAdd.protocol, clientToAdd.tag, clientToAdd.client)
  364. if err1 != nil {
  365. needRestart = true
  366. }
  367. }
  368. s.xrayApi.Close()
  369. }
  370. return needRestart, int64(len(traffics)), nil
  371. }
  372. // AddClientStat inserts a per-client accounting row, no-op on email
  373. // conflict. Xray reports traffic per email, so the surviving row acts as
  374. // the shared accumulator for inbounds that re-use the same identity.
  375. func (s *InboundService) AddClientStat(tx *gorm.DB, inboundId int, client *model.Client) error {
  376. clientTraffic := xray.ClientTraffic{
  377. InboundId: inboundId,
  378. Email: client.Email,
  379. Total: client.TotalGB,
  380. ExpiryTime: client.ExpiryTime,
  381. Enable: client.Enable,
  382. Reset: client.Reset,
  383. }
  384. return tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}).
  385. Create(&clientTraffic).Error
  386. }
  387. func (s *InboundService) UpdateClientStat(tx *gorm.DB, email string, client *model.Client) error {
  388. result := tx.Model(xray.ClientTraffic{}).
  389. Where("email = ?", email).
  390. Updates(map[string]any{
  391. "enable": client.Enable,
  392. "email": client.Email,
  393. "total": client.TotalGB,
  394. "expiry_time": client.ExpiryTime,
  395. "reset": client.Reset,
  396. })
  397. err := result.Error
  398. return err
  399. }
  400. func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
  401. if err := tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error; err != nil {
  402. return err
  403. }
  404. return tx.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error
  405. }
  406. func (s *InboundService) delClientStatsByEmails(tx *gorm.DB, emails []string) error {
  407. const chunk = 400
  408. for start := 0; start < len(emails); start += chunk {
  409. end := min(start+chunk, len(emails))
  410. batch := emails[start:end]
  411. if err := tx.Where("email IN ?", batch).Delete(xray.ClientTraffic{}).Error; err != nil {
  412. return err
  413. }
  414. if err := tx.Where("email IN ?", batch).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  415. return err
  416. }
  417. }
  418. return nil
  419. }
  420. func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
  421. return submitTrafficWrite(func() error {
  422. db := database.GetDB()
  423. return db.Model(xray.ClientTraffic{}).
  424. Where("email = ?", clientEmail).
  425. Updates(map[string]any{"enable": true, "up": 0, "down": 0}).Error
  426. })
  427. }
  428. func (s *InboundService) ResetClientTraffic(id int, clientEmail string) (needRestart bool, err error) {
  429. err = submitTrafficWrite(func() error {
  430. var inner error
  431. needRestart, inner = s.resetClientTrafficLocked(id, clientEmail)
  432. return inner
  433. })
  434. return
  435. }
  436. func (s *InboundService) resetClientTrafficLocked(id int, clientEmail string) (bool, error) {
  437. needRestart := false
  438. traffic, err := s.GetClientTrafficByEmail(clientEmail)
  439. if err != nil {
  440. return false, err
  441. }
  442. if !traffic.Enable {
  443. inbound, err := s.GetInbound(id)
  444. if err != nil {
  445. return false, err
  446. }
  447. clients, err := s.GetClients(inbound)
  448. if err != nil {
  449. return false, err
  450. }
  451. for _, client := range clients {
  452. if client.Email == clientEmail && client.Enable {
  453. rt, push, dirty, perr := s.nodePushPlan(inbound)
  454. if perr != nil {
  455. return false, perr
  456. }
  457. if !push {
  458. if inbound.NodeID != nil {
  459. if dirty {
  460. if dErr := (&NodeService{}).MarkNodeDirty(*inbound.NodeID); dErr != nil {
  461. logger.Warning("mark node dirty failed:", dErr)
  462. }
  463. }
  464. } else {
  465. needRestart = true
  466. }
  467. break
  468. }
  469. cipher := ""
  470. if string(inbound.Protocol) == "shadowsocks" {
  471. var oldSettings map[string]any
  472. err = json.Unmarshal([]byte(inbound.Settings), &oldSettings)
  473. if err != nil {
  474. return false, err
  475. }
  476. cipher = oldSettings["method"].(string)
  477. }
  478. err1 := rt.AddUser(context.Background(), inbound, map[string]any{
  479. "email": client.Email,
  480. "id": client.ID,
  481. "auth": client.Auth,
  482. "security": client.Security,
  483. "flow": client.Flow,
  484. "password": client.Password,
  485. "cipher": cipher,
  486. })
  487. if err1 == nil {
  488. logger.Debug("Client enabled on", rt.Name(), "due to reset traffic:", clientEmail)
  489. } else if inbound.NodeID != nil {
  490. logger.Warning("Error in enabling client on", rt.Name(), ":", err1)
  491. if dErr := (&NodeService{}).MarkNodeDirty(*inbound.NodeID); dErr != nil {
  492. logger.Warning("mark node dirty failed:", dErr)
  493. }
  494. } else {
  495. logger.Debug("Error in enabling client on", rt.Name(), ":", err1)
  496. needRestart = true
  497. }
  498. break
  499. }
  500. }
  501. }
  502. traffic.Up = 0
  503. traffic.Down = 0
  504. traffic.Enable = true
  505. db := database.GetDB()
  506. err = db.Save(traffic).Error
  507. if err != nil {
  508. return false, err
  509. }
  510. now := time.Now().UnixMilli()
  511. _ = db.Model(model.Inbound{}).
  512. Where("id = ?", id).
  513. Update("last_traffic_reset_time", now).Error
  514. inbound, err := s.GetInbound(id)
  515. if err == nil && inbound != nil && inbound.NodeID != nil {
  516. if rt, rterr := s.runtimeFor(inbound); rterr == nil {
  517. if e := rt.ResetClientTraffic(context.Background(), inbound, clientEmail); e != nil {
  518. logger.Warning("ResetClientTraffic: remote propagation to", rt.Name(), "failed:", e)
  519. }
  520. } else {
  521. logger.Warning("ResetClientTraffic: runtime lookup failed:", rterr)
  522. }
  523. }
  524. return needRestart, nil
  525. }
  526. func (s *InboundService) ResetAllTraffics() error {
  527. return submitTrafficWrite(func() error {
  528. return s.resetAllTrafficsLocked()
  529. })
  530. }
  531. func (s *InboundService) resetAllTrafficsLocked() error {
  532. db := database.GetDB()
  533. now := time.Now().UnixMilli()
  534. if err := db.Model(model.Inbound{}).
  535. Where("user_id > ?", 0).
  536. Updates(map[string]any{
  537. "up": 0,
  538. "down": 0,
  539. "last_traffic_reset_time": now,
  540. }).Error; err != nil {
  541. return err
  542. }
  543. nodes, err := (&NodeService{}).GetAll()
  544. if err == nil {
  545. for _, node := range nodes {
  546. if rt, err := runtime.GetManager().RuntimeFor(&node.Id); err == nil {
  547. if e := rt.ResetAllTraffics(context.Background()); e != nil {
  548. logger.Warning("ResetAllTraffics: remote propagation to", rt.Name(), "failed:", e)
  549. }
  550. }
  551. }
  552. }
  553. return nil
  554. }
  555. func (s *InboundService) ResetInboundTraffic(id int) error {
  556. return submitTrafficWrite(func() error {
  557. db := database.GetDB()
  558. if err := db.Model(model.Inbound{}).
  559. Where("id = ?", id).
  560. Updates(map[string]any{"up": 0, "down": 0}).Error; err != nil {
  561. return err
  562. }
  563. inbound, err := s.GetInbound(id)
  564. if err == nil && inbound != nil && inbound.NodeID != nil {
  565. if rt, rterr := s.runtimeFor(inbound); rterr == nil {
  566. if e := rt.ResetInboundTraffic(context.Background(), inbound); e != nil {
  567. logger.Warning("ResetInboundTraffic: remote propagation to", rt.Name(), "failed:", e)
  568. }
  569. } else {
  570. logger.Warning("ResetInboundTraffic: runtime lookup failed:", rterr)
  571. }
  572. }
  573. return nil
  574. })
  575. }
  576. func (s *InboundService) DelDepletedClients(id int) (err error) {
  577. db := database.GetDB()
  578. tx := db.Begin()
  579. defer func() {
  580. if err == nil {
  581. tx.Commit()
  582. } else {
  583. tx.Rollback()
  584. }
  585. }()
  586. // Collect depleted emails globally — a shared-email row owned by one
  587. // inbound depletes every sibling that lists the email.
  588. now := time.Now().Unix() * 1000
  589. depletedClause := "reset = 0 and ((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?))"
  590. var depletedRows []xray.ClientTraffic
  591. err = db.Model(xray.ClientTraffic{}).
  592. Where(depletedClause, now).
  593. Find(&depletedRows).Error
  594. if err != nil {
  595. return err
  596. }
  597. if len(depletedRows) == 0 {
  598. return nil
  599. }
  600. depletedEmails := make(map[string]struct{}, len(depletedRows))
  601. for _, r := range depletedRows {
  602. if r.Email == "" {
  603. continue
  604. }
  605. depletedEmails[strings.ToLower(r.Email)] = struct{}{}
  606. }
  607. if len(depletedEmails) == 0 {
  608. return nil
  609. }
  610. var inbounds []*model.Inbound
  611. inboundQuery := db.Model(model.Inbound{})
  612. if id >= 0 {
  613. inboundQuery = inboundQuery.Where("id = ?", id)
  614. }
  615. if err = inboundQuery.Find(&inbounds).Error; err != nil {
  616. return err
  617. }
  618. for _, inbound := range inbounds {
  619. var settings map[string]any
  620. if err = json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  621. return err
  622. }
  623. rawClients, ok := settings["clients"].([]any)
  624. if !ok {
  625. continue
  626. }
  627. newClients := make([]any, 0, len(rawClients))
  628. removed := 0
  629. for _, client := range rawClients {
  630. c, ok := client.(map[string]any)
  631. if !ok {
  632. newClients = append(newClients, client)
  633. continue
  634. }
  635. email, _ := c["email"].(string)
  636. if _, isDepleted := depletedEmails[strings.ToLower(email)]; isDepleted {
  637. removed++
  638. continue
  639. }
  640. newClients = append(newClients, client)
  641. }
  642. if removed == 0 {
  643. continue
  644. }
  645. if len(newClients) == 0 {
  646. s.DelInbound(inbound.Id)
  647. continue
  648. }
  649. settings["clients"] = newClients
  650. ns, mErr := json.MarshalIndent(settings, "", " ")
  651. if mErr != nil {
  652. return mErr
  653. }
  654. inbound.Settings = string(ns)
  655. if err = tx.Save(inbound).Error; err != nil {
  656. return err
  657. }
  658. survivingClients, gcErr := s.GetClients(inbound)
  659. if gcErr != nil {
  660. err = gcErr
  661. return err
  662. }
  663. if err = s.clientService.SyncInbound(tx, inbound.Id, survivingClients); err != nil {
  664. return err
  665. }
  666. }
  667. // Drop now-orphaned rows. With id >= 0, a row is safe to drop only when
  668. // no out-of-scope inbound still references the email.
  669. if id < 0 {
  670. err = tx.Where(depletedClause, now).Delete(xray.ClientTraffic{}).Error
  671. return err
  672. }
  673. emails := make([]string, 0, len(depletedEmails))
  674. for e := range depletedEmails {
  675. emails = append(emails, e)
  676. }
  677. var stillReferenced []string
  678. emailExpr := database.JSONFieldText("client.value", "email")
  679. stillQuery := fmt.Sprintf(
  680. "SELECT DISTINCT LOWER(%s) %s WHERE LOWER(%s) IN ?",
  681. emailExpr,
  682. database.JSONClientsFromInbound(),
  683. emailExpr,
  684. )
  685. if err = tx.Raw(stillQuery, emails).Scan(&stillReferenced).Error; err != nil {
  686. return err
  687. }
  688. stillSet := make(map[string]struct{}, len(stillReferenced))
  689. for _, e := range stillReferenced {
  690. stillSet[e] = struct{}{}
  691. }
  692. toDelete := make([]string, 0, len(emails))
  693. for _, e := range emails {
  694. if _, kept := stillSet[e]; !kept {
  695. toDelete = append(toDelete, e)
  696. }
  697. }
  698. if len(toDelete) > 0 {
  699. if err = tx.Where("LOWER(email) IN ?", toDelete).Delete(xray.ClientTraffic{}).Error; err != nil {
  700. return err
  701. }
  702. }
  703. return nil
  704. }
  705. func (s *InboundService) GetClientTrafficTgBot(tgId int64) ([]*xray.ClientTraffic, error) {
  706. db := database.GetDB()
  707. var inbounds []*model.Inbound
  708. // Retrieve inbounds where settings contain the given tgId
  709. err := db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds).Error
  710. if err != nil && err != gorm.ErrRecordNotFound {
  711. logger.Errorf("Error retrieving inbounds with tgId %d: %v", tgId, err)
  712. return nil, err
  713. }
  714. var emails []string
  715. for _, inbound := range inbounds {
  716. clients, err := s.GetClients(inbound)
  717. if err != nil {
  718. logger.Errorf("Error retrieving clients for inbound %d: %v", inbound.Id, err)
  719. continue
  720. }
  721. for _, client := range clients {
  722. if client.TgID == tgId {
  723. emails = append(emails, client.Email)
  724. }
  725. }
  726. }
  727. // Chunked to stay under SQLite's bind-variable limit when a single Telegram
  728. // account owns thousands of clients across inbounds.
  729. uniqEmails := uniqueNonEmptyStrings(emails)
  730. traffics := make([]*xray.ClientTraffic, 0, len(uniqEmails))
  731. for _, batch := range chunkStrings(uniqEmails, sqliteMaxVars) {
  732. var page []*xray.ClientTraffic
  733. if err = db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  734. if err == gorm.ErrRecordNotFound {
  735. continue
  736. }
  737. logger.Errorf("Error retrieving ClientTraffic for emails %v: %v", batch, err)
  738. return nil, err
  739. }
  740. traffics = append(traffics, page...)
  741. }
  742. if len(traffics) == 0 {
  743. logger.Warning("No ClientTraffic records found for emails:", emails)
  744. return nil, nil
  745. }
  746. // Populate UUID and other client data for each traffic record
  747. for i := range traffics {
  748. if ct, client, e := s.GetClientByEmail(traffics[i].Email); e == nil && ct != nil && client != nil {
  749. traffics[i].Enable = client.Enable
  750. traffics[i].UUID = client.ID
  751. traffics[i].SubId = client.SubID
  752. }
  753. }
  754. return traffics, nil
  755. }
  756. func (s *InboundService) GetActiveClientTraffics(emails []string) ([]*xray.ClientTraffic, error) {
  757. uniq := uniqueNonEmptyStrings(emails)
  758. if len(uniq) == 0 {
  759. return nil, nil
  760. }
  761. db := database.GetDB()
  762. traffics := make([]*xray.ClientTraffic, 0, len(uniq))
  763. for _, batch := range chunkStrings(uniq, sqliteMaxVars) {
  764. var page []*xray.ClientTraffic
  765. if err := db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  766. return nil, err
  767. }
  768. traffics = append(traffics, page...)
  769. }
  770. return traffics, nil
  771. }
  772. // GetAllClientTraffics returns the full set of client_traffics rows so the
  773. // websocket broadcasters can ship a complete snapshot every cycle. The old
  774. // delta-only path (GetActiveClientTraffics on activeEmails) silently dropped
  775. // the per-client section whenever no client moved bytes in the cycle or a
  776. // node sync failed, leaving client rows in the UI stuck at stale numbers.
  777. func (s *InboundService) GetAllClientTraffics() ([]*xray.ClientTraffic, error) {
  778. db := database.GetDB()
  779. var traffics []*xray.ClientTraffic
  780. if err := db.Model(xray.ClientTraffic{}).Find(&traffics).Error; err != nil {
  781. return nil, err
  782. }
  783. return traffics, nil
  784. }
  785. type InboundTrafficSummary struct {
  786. Id int `json:"id"`
  787. Up int64 `json:"up"`
  788. Down int64 `json:"down"`
  789. Total int64 `json:"total"`
  790. Enable bool `json:"enable"`
  791. }
  792. func (s *InboundService) GetInboundsTrafficSummary() ([]InboundTrafficSummary, error) {
  793. db := database.GetDB()
  794. var summaries []InboundTrafficSummary
  795. if err := db.Model(&model.Inbound{}).
  796. Select("id, up, down, total, enable").
  797. Find(&summaries).Error; err != nil {
  798. return nil, err
  799. }
  800. return summaries, nil
  801. }
  802. func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.ClientTraffic, err error) {
  803. db := database.GetDB()
  804. var traffics []*xray.ClientTraffic
  805. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error; err != nil {
  806. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  807. return nil, err
  808. }
  809. if len(traffics) == 0 {
  810. return nil, nil
  811. }
  812. t := traffics[0]
  813. if rec, rErr := s.clientService.GetRecordByEmail(db, email); rErr == nil && rec != nil {
  814. c := rec.ToClient()
  815. t.UUID = c.ID
  816. t.SubId = c.SubID
  817. return t, nil
  818. }
  819. t2, client, err := s.GetClientByEmail(email)
  820. if err != nil {
  821. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  822. return nil, err
  823. }
  824. if t2 != nil && client != nil {
  825. t2.UUID = client.ID
  826. t2.SubId = client.SubID
  827. return t2, nil
  828. }
  829. return nil, nil
  830. }
  831. func (s *InboundService) UpdateClientTrafficByEmail(email string, upload int64, download int64) error {
  832. return submitTrafficWrite(func() error {
  833. db := database.GetDB()
  834. err := db.Model(xray.ClientTraffic{}).
  835. Where("email = ?", email).
  836. Updates(map[string]any{
  837. "up": upload,
  838. "down": download,
  839. }).Error
  840. if err != nil {
  841. logger.Warningf("Error updating ClientTraffic with email %s: %v", email, err)
  842. }
  843. return err
  844. })
  845. }
  846. func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) {
  847. db := database.GetDB()
  848. inbound := &model.Inbound{}
  849. traffic = &xray.ClientTraffic{}
  850. // Search for inbound settings that contain the query
  851. err = db.Model(model.Inbound{}).Where("settings LIKE ?", "%\""+query+"\"%").First(inbound).Error
  852. if err != nil {
  853. if err == gorm.ErrRecordNotFound {
  854. logger.Warningf("Inbound settings containing query %s not found: %v", query, err)
  855. return nil, err
  856. }
  857. logger.Errorf("Error searching for inbound settings with query %s: %v", query, err)
  858. return nil, err
  859. }
  860. traffic.InboundId = inbound.Id
  861. // Unmarshal settings to get clients
  862. settings := map[string][]model.Client{}
  863. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  864. logger.Errorf("Error unmarshalling inbound settings for inbound ID %d: %v", inbound.Id, err)
  865. return nil, err
  866. }
  867. clients := settings["clients"]
  868. for _, client := range clients {
  869. if (client.ID == query || client.Password == query) && client.Email != "" {
  870. traffic.Email = client.Email
  871. break
  872. }
  873. }
  874. if traffic.Email == "" {
  875. logger.Warningf("No client found with query %s in inbound ID %d", query, inbound.Id)
  876. return nil, gorm.ErrRecordNotFound
  877. }
  878. // Retrieve ClientTraffic based on the found email
  879. err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error
  880. if err != nil {
  881. if err == gorm.ErrRecordNotFound {
  882. logger.Warningf("ClientTraffic for email %s not found: %v", traffic.Email, err)
  883. return nil, err
  884. }
  885. logger.Errorf("Error retrieving ClientTraffic for email %s: %v", traffic.Email, err)
  886. return nil, err
  887. }
  888. return traffic, nil
  889. }