inbound_traffic.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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.
  120. trafficByEmail := make(map[string]*xray.ClientTraffic, len(traffics))
  121. for i := range traffics {
  122. if traffics[i] != nil {
  123. trafficByEmail[traffics[i].Email] = traffics[i]
  124. }
  125. }
  126. now := time.Now().UnixMilli()
  127. // Use atomic per-row UPDATE instead of read-modify-write Save. tx.Save
  128. // issues UPDATEs in slice order, which varies between concurrent callers;
  129. // on PostgreSQL two transactions locking the same rows in opposite order
  130. // deadlock. An atomic "SET up = up + ?" never holds a row lock across a
  131. // subsequent lock acquisition, so concurrent writers cannot deadlock.
  132. for _, ct := range dbClientTraffics {
  133. t, ok := trafficByEmail[ct.Email]
  134. if !ok || (t.Up == 0 && t.Down == 0) {
  135. continue
  136. }
  137. if err = tx.Exec(
  138. fmt.Sprintf(
  139. `UPDATE client_traffics SET up = up + ?, down = down + ?, last_online = %s WHERE email = ?`,
  140. database.GreatestExpr("last_online", "?"),
  141. ),
  142. t.Up, t.Down, now, ct.Email,
  143. ).Error; err != nil {
  144. logger.Warning("AddClientTraffic update data ", err)
  145. }
  146. }
  147. // adjustTraffics converts delayed-start rows (negative ExpiryTime → absolute
  148. // deadline) in-memory. Persist that conversion now since the traffic UPDATE
  149. // above only touches up/down/last_online.
  150. for _, ct := range dbClientTraffics {
  151. if ct.ExpiryTime > 0 {
  152. if err = tx.Exec(
  153. `UPDATE client_traffics SET expiry_time = ? WHERE email = ? AND expiry_time < 0`,
  154. ct.ExpiryTime, ct.Email,
  155. ).Error; err != nil {
  156. logger.Warning("AddClientTraffic update expiry_time ", err)
  157. }
  158. }
  159. }
  160. return nil
  161. }
  162. func (s *InboundService) adjustTraffics(tx *gorm.DB, dbClientTraffics []*xray.ClientTraffic) ([]*xray.ClientTraffic, error) {
  163. now := time.Now().UnixMilli()
  164. // "Start After First Use" stores a negative expiry (the duration). On the
  165. // first traffic tick it becomes an absolute deadline of now+duration. Compute
  166. // it once per email so every inbound the client is attached to lands on the
  167. // same value (recomputing per inbound would skip all but the first one).
  168. newExpiryByEmail := make(map[string]int64, len(dbClientTraffics))
  169. for traffic_index := range dbClientTraffics {
  170. if dbClientTraffics[traffic_index].ExpiryTime < 0 {
  171. newExpiryByEmail[dbClientTraffics[traffic_index].Email] = now - dbClientTraffics[traffic_index].ExpiryTime
  172. }
  173. }
  174. if len(newExpiryByEmail) == 0 {
  175. return dbClientTraffics, nil
  176. }
  177. delayedEmails := make([]string, 0, len(newExpiryByEmail))
  178. for email := range newExpiryByEmail {
  179. delayedEmails = append(delayedEmails, email)
  180. }
  181. // Resolve the owning inbounds through the client_inbounds link, which is
  182. // authoritative. client_traffics.inbound_id goes stale when an inbound is
  183. // deleted and recreated, which would leave the negative expiry unconverted.
  184. var inboundIds []int
  185. err := tx.Table("client_inbounds").
  186. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  187. Where("clients.email IN (?)", delayedEmails).
  188. Distinct().
  189. Pluck("client_inbounds.inbound_id", &inboundIds).Error
  190. if err != nil {
  191. return nil, err
  192. }
  193. if len(inboundIds) == 0 {
  194. return dbClientTraffics, nil
  195. }
  196. var inbounds []*model.Inbound
  197. err = tx.Model(model.Inbound{}).Where("id IN (?)", inboundIds).Find(&inbounds).Error
  198. if err != nil {
  199. return nil, err
  200. }
  201. for inbound_index := range inbounds {
  202. settings := map[string]any{}
  203. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  204. clients, ok := settings["clients"].([]any)
  205. if ok {
  206. var newClients []any
  207. for client_index := range clients {
  208. c := clients[client_index].(map[string]any)
  209. email, _ := c["email"].(string)
  210. if newExpiry, ok := newExpiryByEmail[email]; ok {
  211. c["expiryTime"] = newExpiry
  212. c["updated_at"] = now
  213. }
  214. if _, ok := c["created_at"]; !ok {
  215. c["created_at"] = now
  216. }
  217. if _, ok := c["updated_at"]; !ok {
  218. c["updated_at"] = now
  219. }
  220. newClients = append(newClients, any(c))
  221. }
  222. settings["clients"] = newClients
  223. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  224. if err != nil {
  225. return nil, err
  226. }
  227. inbounds[inbound_index].Settings = string(modifiedSettings)
  228. }
  229. }
  230. for traffic_index := range dbClientTraffics {
  231. if newExpiry, ok := newExpiryByEmail[dbClientTraffics[traffic_index].Email]; ok {
  232. dbClientTraffics[traffic_index].ExpiryTime = newExpiry
  233. }
  234. }
  235. err = tx.Save(inbounds).Error
  236. if err != nil {
  237. logger.Warning("AddClientTraffic update inbounds ", err)
  238. logger.Error(inbounds)
  239. } else {
  240. for _, ib := range inbounds {
  241. if ib == nil {
  242. continue
  243. }
  244. cs, gcErr := s.GetClients(ib)
  245. if gcErr != nil {
  246. logger.Warning("AddClientTraffic sync clients: GetClients failed", gcErr)
  247. continue
  248. }
  249. if syncErr := s.clientService.SyncInbound(tx, ib.Id, cs); syncErr != nil {
  250. logger.Warning("AddClientTraffic sync clients: SyncInbound failed", syncErr)
  251. }
  252. }
  253. }
  254. return dbClientTraffics, nil
  255. }
  256. func (s *InboundService) autoRenewClients(tx *gorm.DB) (bool, int64, error) {
  257. // check for time expired
  258. var traffics []*xray.ClientTraffic
  259. now := time.Now().Unix() * 1000
  260. var err, err1 error
  261. // Filter to clients that have at least one local inbound. Using
  262. // client_traffics.inbound_id is wrong: it goes stale after an inbound is
  263. // deleted/recreated and always points to the first inbound the client was
  264. // attached to, so it could be a node inbound even when the client also has
  265. // local inbounds. The email-based join through client_inbounds is authoritative.
  266. err = tx.Model(xray.ClientTraffic{}).
  267. Where("reset > 0 and expiry_time > 0 and expiry_time <= ?", now).
  268. Where("email IN (?)", tx.Table("client_inbounds ci").
  269. Select("c.email").
  270. Joins("JOIN clients c ON c.id = ci.client_id").
  271. Joins("JOIN inbounds i ON i.id = ci.inbound_id").
  272. Where("i.node_id IS NULL")).
  273. Find(&traffics).Error
  274. if err != nil {
  275. return false, 0, err
  276. }
  277. // return if there is no client to renew
  278. if len(traffics) == 0 {
  279. return false, 0, nil
  280. }
  281. var inbound_ids []int
  282. var inbounds []*model.Inbound
  283. needRestart := false
  284. var clientsToAdd []struct {
  285. protocol string
  286. tag string
  287. client map[string]any
  288. }
  289. // Resolve the inbounds to renew through the client_inbounds link rather than
  290. // client_traffics.inbound_id, which goes stale after an inbound is deleted and
  291. // recreated and would otherwise skip the renew entirely.
  292. renewEmails := make([]string, 0, len(traffics))
  293. for _, traffic := range traffics {
  294. renewEmails = append(renewEmails, traffic.Email)
  295. }
  296. for _, batch := range chunkStrings(renewEmails, sqliteMaxVars) {
  297. var ids []int
  298. if err = tx.Table("client_inbounds").
  299. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  300. Where("clients.email IN ?", batch).
  301. Distinct().
  302. Pluck("client_inbounds.inbound_id", &ids).Error; err != nil {
  303. return false, 0, err
  304. }
  305. inbound_ids = append(inbound_ids, ids...)
  306. }
  307. // Dedupe so an inbound hosting N expired clients is fetched and saved once
  308. // per tick instead of N times across chunk boundaries.
  309. inbound_ids = uniqueInts(inbound_ids)
  310. // Chunked to stay under SQLite's bind-variable limit when many inbounds
  311. // are touched in a single tick.
  312. for _, batch := range chunkInts(inbound_ids, sqliteMaxVars) {
  313. var page []*model.Inbound
  314. if err = tx.Model(model.Inbound{}).Where("id IN ?", batch).Find(&page).Error; err != nil {
  315. return false, 0, err
  316. }
  317. inbounds = append(inbounds, page...)
  318. }
  319. for inbound_index := range inbounds {
  320. settings := map[string]any{}
  321. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  322. clients, _ := settings["clients"].([]any)
  323. if len(clients) == 0 {
  324. continue
  325. }
  326. for client_index := range clients {
  327. c := clients[client_index].(map[string]any)
  328. for traffic_index, traffic := range traffics {
  329. if traffic.Email == c["email"].(string) {
  330. newExpiryTime := traffic.ExpiryTime
  331. for newExpiryTime < now {
  332. newExpiryTime += (int64(traffic.Reset) * 86400000)
  333. }
  334. c["expiryTime"] = newExpiryTime
  335. traffics[traffic_index].ExpiryTime = newExpiryTime
  336. traffics[traffic_index].Down = 0
  337. traffics[traffic_index].Up = 0
  338. if !traffic.Enable {
  339. traffics[traffic_index].Enable = true
  340. c["enable"] = true
  341. clientsToAdd = append(clientsToAdd,
  342. struct {
  343. protocol string
  344. tag string
  345. client map[string]any
  346. }{
  347. protocol: string(inbounds[inbound_index].Protocol),
  348. tag: inbounds[inbound_index].Tag,
  349. client: c,
  350. })
  351. }
  352. clients[client_index] = any(c)
  353. break
  354. }
  355. }
  356. }
  357. settings["clients"] = clients
  358. newSettings, err := json.MarshalIndent(settings, "", " ")
  359. if err != nil {
  360. return false, 0, err
  361. }
  362. inbounds[inbound_index].Settings = string(newSettings)
  363. }
  364. err = tx.Save(inbounds).Error
  365. if err != nil {
  366. return false, 0, err
  367. }
  368. for _, ib := range inbounds {
  369. if ib == nil {
  370. continue
  371. }
  372. cs, gcErr := s.GetClients(ib)
  373. if gcErr != nil {
  374. logger.Warning("autoRenewClients sync clients: GetClients failed", gcErr)
  375. continue
  376. }
  377. if syncErr := s.clientService.SyncInbound(tx, ib.Id, cs); syncErr != nil {
  378. logger.Warning("autoRenewClients sync clients: SyncInbound failed", syncErr)
  379. }
  380. }
  381. err = tx.Save(traffics).Error
  382. if err != nil {
  383. return false, 0, err
  384. }
  385. // A renewed client starts a fresh quota window: drop the cross-panel rows
  386. // too, or the stale pushed totals would re-deplete it immediately.
  387. if err = clearGlobalTraffic(tx, renewEmails...); err != nil {
  388. return false, 0, err
  389. }
  390. if p != nil {
  391. err1 = s.xrayApi.Init(p.GetAPIPort())
  392. if err1 != nil {
  393. return true, int64(len(traffics)), nil
  394. }
  395. for _, clientToAdd := range clientsToAdd {
  396. err1 = s.xrayApi.AddUser(clientToAdd.protocol, clientToAdd.tag, clientToAdd.client)
  397. if err1 != nil {
  398. needRestart = true
  399. }
  400. }
  401. s.xrayApi.Close()
  402. }
  403. return needRestart, int64(len(traffics)), nil
  404. }
  405. // AddClientStat inserts a per-client accounting row, no-op on email
  406. // conflict. Xray reports traffic per email, so the surviving row acts as
  407. // the shared accumulator for inbounds that re-use the same identity.
  408. func (s *InboundService) AddClientStat(tx *gorm.DB, inboundId int, client *model.Client) error {
  409. clientTraffic := xray.ClientTraffic{
  410. InboundId: inboundId,
  411. Email: client.Email,
  412. Total: client.TotalGB,
  413. ExpiryTime: client.ExpiryTime,
  414. Enable: client.Enable,
  415. Reset: client.Reset,
  416. }
  417. return tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}).
  418. Create(&clientTraffic).Error
  419. }
  420. func (s *InboundService) UpdateClientStat(tx *gorm.DB, email string, client *model.Client) error {
  421. result := tx.Model(xray.ClientTraffic{}).
  422. Where("email = ?", email).
  423. Updates(map[string]any{
  424. "enable": client.Enable,
  425. "email": client.Email,
  426. "total": client.TotalGB,
  427. "expiry_time": client.ExpiryTime,
  428. "reset": client.Reset,
  429. })
  430. err := result.Error
  431. return err
  432. }
  433. func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
  434. if err := tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error; err != nil {
  435. return err
  436. }
  437. if err := clearGlobalTraffic(tx, email); err != nil {
  438. return err
  439. }
  440. return tx.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error
  441. }
  442. func (s *InboundService) delClientStatsByEmails(tx *gorm.DB, emails []string) error {
  443. const chunk = 400
  444. for start := 0; start < len(emails); start += chunk {
  445. end := min(start+chunk, len(emails))
  446. batch := emails[start:end]
  447. if err := tx.Where("email IN ?", batch).Delete(xray.ClientTraffic{}).Error; err != nil {
  448. return err
  449. }
  450. if err := tx.Where("email IN ?", batch).Delete(&model.ClientGlobalTraffic{}).Error; err != nil {
  451. return err
  452. }
  453. if err := tx.Where("email IN ?", batch).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  454. return err
  455. }
  456. }
  457. return nil
  458. }
  459. func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
  460. return submitTrafficWrite(func() error {
  461. db := database.GetDB()
  462. if err := clearGlobalTraffic(db, clientEmail); err != nil {
  463. return err
  464. }
  465. return db.Model(xray.ClientTraffic{}).
  466. Where("email = ?", clientEmail).
  467. Updates(map[string]any{"enable": true, "up": 0, "down": 0}).Error
  468. })
  469. }
  470. func (s *InboundService) ResetClientTraffic(id int, clientEmail string) (needRestart bool, err error) {
  471. err = submitTrafficWrite(func() error {
  472. var inner error
  473. needRestart, inner = s.resetClientTrafficLocked(id, clientEmail)
  474. return inner
  475. })
  476. return
  477. }
  478. func (s *InboundService) resetClientTrafficLocked(id int, clientEmail string) (bool, error) {
  479. needRestart := false
  480. traffic, err := s.GetClientTrafficByEmail(clientEmail)
  481. if err != nil {
  482. return false, err
  483. }
  484. if !traffic.Enable {
  485. inbound, err := s.GetInbound(id)
  486. if err != nil {
  487. return false, err
  488. }
  489. clients, err := s.GetClients(inbound)
  490. if err != nil {
  491. return false, err
  492. }
  493. for _, client := range clients {
  494. if client.Email == clientEmail && client.Enable {
  495. rt, push, dirty, perr := s.nodePushPlan(inbound)
  496. if perr != nil {
  497. return false, perr
  498. }
  499. if !push {
  500. if inbound.NodeID != nil {
  501. if dirty {
  502. if dErr := (&NodeService{}).MarkNodeDirty(*inbound.NodeID); dErr != nil {
  503. logger.Warning("mark node dirty failed:", dErr)
  504. }
  505. }
  506. } else {
  507. needRestart = true
  508. }
  509. break
  510. }
  511. cipher := ""
  512. if string(inbound.Protocol) == "shadowsocks" {
  513. var oldSettings map[string]any
  514. err = json.Unmarshal([]byte(inbound.Settings), &oldSettings)
  515. if err != nil {
  516. return false, err
  517. }
  518. cipher = oldSettings["method"].(string)
  519. }
  520. err1 := rt.AddUser(context.Background(), inbound, map[string]any{
  521. "email": client.Email,
  522. "id": client.ID,
  523. "auth": client.Auth,
  524. "security": client.Security,
  525. "flow": client.Flow,
  526. "password": client.Password,
  527. "cipher": cipher,
  528. })
  529. if err1 == nil {
  530. logger.Debug("Client enabled on", rt.Name(), "due to reset traffic:", clientEmail)
  531. } else if inbound.NodeID != nil {
  532. logger.Warning("Error in enabling client on", rt.Name(), ":", err1)
  533. if dErr := (&NodeService{}).MarkNodeDirty(*inbound.NodeID); dErr != nil {
  534. logger.Warning("mark node dirty failed:", dErr)
  535. }
  536. } else {
  537. logger.Debug("Error in enabling client on", rt.Name(), ":", err1)
  538. needRestart = true
  539. }
  540. break
  541. }
  542. }
  543. }
  544. traffic.Up = 0
  545. traffic.Down = 0
  546. traffic.Enable = true
  547. db := database.GetDB()
  548. err = db.Save(traffic).Error
  549. if err != nil {
  550. return false, err
  551. }
  552. if err := clearGlobalTraffic(db, clientEmail); err != nil {
  553. return false, err
  554. }
  555. now := time.Now().UnixMilli()
  556. _ = db.Model(model.Inbound{}).
  557. Where("id = ?", id).
  558. Update("last_traffic_reset_time", now).Error
  559. inbound, err := s.GetInbound(id)
  560. if err == nil && inbound != nil && inbound.NodeID != nil {
  561. if rt, rterr := s.runtimeFor(inbound); rterr == nil {
  562. if e := rt.ResetClientTraffic(context.Background(), inbound, clientEmail); e != nil {
  563. logger.Warning("ResetClientTraffic: remote propagation to", rt.Name(), "failed:", e)
  564. }
  565. } else {
  566. logger.Warning("ResetClientTraffic: runtime lookup failed:", rterr)
  567. }
  568. }
  569. return needRestart, nil
  570. }
  571. func (s *InboundService) ResetAllTraffics() error {
  572. return submitTrafficWrite(func() error {
  573. return s.resetAllTrafficsLocked()
  574. })
  575. }
  576. func (s *InboundService) resetAllTrafficsLocked() error {
  577. db := database.GetDB()
  578. now := time.Now().UnixMilli()
  579. if err := db.Model(model.Inbound{}).
  580. Where("user_id > ?", 0).
  581. Updates(map[string]any{
  582. "up": 0,
  583. "down": 0,
  584. "last_traffic_reset_time": now,
  585. }).Error; err != nil {
  586. return err
  587. }
  588. nodes, err := (&NodeService{}).GetAll()
  589. if err == nil {
  590. for _, node := range nodes {
  591. if rt, err := runtime.GetManager().RuntimeFor(&node.Id); err == nil {
  592. if e := rt.ResetAllTraffics(context.Background()); e != nil {
  593. logger.Warning("ResetAllTraffics: remote propagation to", rt.Name(), "failed:", e)
  594. }
  595. }
  596. }
  597. }
  598. return nil
  599. }
  600. func (s *InboundService) ResetInboundTraffic(id int) error {
  601. return submitTrafficWrite(func() error {
  602. db := database.GetDB()
  603. if err := db.Model(model.Inbound{}).
  604. Where("id = ?", id).
  605. Updates(map[string]any{"up": 0, "down": 0}).Error; err != nil {
  606. return err
  607. }
  608. inbound, err := s.GetInbound(id)
  609. if err == nil && inbound != nil && inbound.NodeID != nil {
  610. if rt, rterr := s.runtimeFor(inbound); rterr == nil {
  611. if e := rt.ResetInboundTraffic(context.Background(), inbound); e != nil {
  612. logger.Warning("ResetInboundTraffic: remote propagation to", rt.Name(), "failed:", e)
  613. }
  614. } else {
  615. logger.Warning("ResetInboundTraffic: runtime lookup failed:", rterr)
  616. }
  617. }
  618. return nil
  619. })
  620. }
  621. func (s *InboundService) DelDepletedClients(id int) (err error) {
  622. db := database.GetDB()
  623. tx := db.Begin()
  624. defer func() {
  625. if err == nil {
  626. tx.Commit()
  627. } else {
  628. tx.Rollback()
  629. }
  630. }()
  631. // Collect depleted emails globally — a shared-email row owned by one
  632. // inbound depletes every sibling that lists the email.
  633. now := time.Now().Unix() * 1000
  634. depletedClause := "reset = 0 and ((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?))"
  635. var depletedRows []xray.ClientTraffic
  636. err = db.Model(xray.ClientTraffic{}).
  637. Where(depletedClause, now).
  638. Find(&depletedRows).Error
  639. if err != nil {
  640. return err
  641. }
  642. if len(depletedRows) == 0 {
  643. return nil
  644. }
  645. depletedEmails := make(map[string]struct{}, len(depletedRows))
  646. for _, r := range depletedRows {
  647. if r.Email == "" {
  648. continue
  649. }
  650. depletedEmails[strings.ToLower(r.Email)] = struct{}{}
  651. }
  652. if len(depletedEmails) == 0 {
  653. return nil
  654. }
  655. var inbounds []*model.Inbound
  656. inboundQuery := db.Model(model.Inbound{})
  657. if id >= 0 {
  658. inboundQuery = inboundQuery.Where("id = ?", id)
  659. }
  660. if err = inboundQuery.Find(&inbounds).Error; err != nil {
  661. return err
  662. }
  663. for _, inbound := range inbounds {
  664. var settings map[string]any
  665. if err = json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  666. return err
  667. }
  668. rawClients, ok := settings["clients"].([]any)
  669. if !ok {
  670. continue
  671. }
  672. newClients := make([]any, 0, len(rawClients))
  673. removed := 0
  674. for _, client := range rawClients {
  675. c, ok := client.(map[string]any)
  676. if !ok {
  677. newClients = append(newClients, client)
  678. continue
  679. }
  680. email, _ := c["email"].(string)
  681. if _, isDepleted := depletedEmails[strings.ToLower(email)]; isDepleted {
  682. removed++
  683. continue
  684. }
  685. newClients = append(newClients, client)
  686. }
  687. if removed == 0 {
  688. continue
  689. }
  690. if len(newClients) == 0 {
  691. s.DelInbound(inbound.Id)
  692. continue
  693. }
  694. settings["clients"] = newClients
  695. ns, mErr := json.MarshalIndent(settings, "", " ")
  696. if mErr != nil {
  697. return mErr
  698. }
  699. inbound.Settings = string(ns)
  700. if err = tx.Save(inbound).Error; err != nil {
  701. return err
  702. }
  703. survivingClients, gcErr := s.GetClients(inbound)
  704. if gcErr != nil {
  705. err = gcErr
  706. return err
  707. }
  708. if err = s.clientService.SyncInbound(tx, inbound.Id, survivingClients); err != nil {
  709. return err
  710. }
  711. }
  712. // Drop now-orphaned rows. With id >= 0, a row is safe to drop only when
  713. // no out-of-scope inbound still references the email.
  714. if id < 0 {
  715. err = tx.Where(depletedClause, now).Delete(xray.ClientTraffic{}).Error
  716. return err
  717. }
  718. emails := make([]string, 0, len(depletedEmails))
  719. for e := range depletedEmails {
  720. emails = append(emails, e)
  721. }
  722. var stillReferenced []string
  723. emailExpr := database.JSONFieldText("client.value", "email")
  724. stillQuery := fmt.Sprintf(
  725. "SELECT DISTINCT LOWER(%s) %s WHERE LOWER(%s) IN ?",
  726. emailExpr,
  727. database.JSONClientsFromInbound(),
  728. emailExpr,
  729. )
  730. if err = tx.Raw(stillQuery, emails).Scan(&stillReferenced).Error; err != nil {
  731. return err
  732. }
  733. stillSet := make(map[string]struct{}, len(stillReferenced))
  734. for _, e := range stillReferenced {
  735. stillSet[e] = struct{}{}
  736. }
  737. toDelete := make([]string, 0, len(emails))
  738. for _, e := range emails {
  739. if _, kept := stillSet[e]; !kept {
  740. toDelete = append(toDelete, e)
  741. }
  742. }
  743. if len(toDelete) > 0 {
  744. if err = tx.Where("LOWER(email) IN ?", toDelete).Delete(xray.ClientTraffic{}).Error; err != nil {
  745. return err
  746. }
  747. }
  748. return nil
  749. }
  750. func (s *InboundService) GetClientTrafficTgBot(tgId int64) ([]*xray.ClientTraffic, error) {
  751. db := database.GetDB()
  752. var inbounds []*model.Inbound
  753. // Retrieve inbounds where settings contain the given tgId
  754. err := db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds).Error
  755. if err != nil && err != gorm.ErrRecordNotFound {
  756. logger.Errorf("Error retrieving inbounds with tgId %d: %v", tgId, err)
  757. return nil, err
  758. }
  759. var emails []string
  760. for _, inbound := range inbounds {
  761. clients, err := s.GetClients(inbound)
  762. if err != nil {
  763. logger.Errorf("Error retrieving clients for inbound %d: %v", inbound.Id, err)
  764. continue
  765. }
  766. for _, client := range clients {
  767. if client.TgID == tgId {
  768. emails = append(emails, client.Email)
  769. }
  770. }
  771. }
  772. // Chunked to stay under SQLite's bind-variable limit when a single Telegram
  773. // account owns thousands of clients across inbounds.
  774. uniqEmails := uniqueNonEmptyStrings(emails)
  775. traffics := make([]*xray.ClientTraffic, 0, len(uniqEmails))
  776. for _, batch := range chunkStrings(uniqEmails, sqliteMaxVars) {
  777. var page []*xray.ClientTraffic
  778. if err = db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  779. if err == gorm.ErrRecordNotFound {
  780. continue
  781. }
  782. logger.Errorf("Error retrieving ClientTraffic for emails %v: %v", batch, err)
  783. return nil, err
  784. }
  785. traffics = append(traffics, page...)
  786. }
  787. if len(traffics) == 0 {
  788. logger.Warning("No ClientTraffic records found for emails:", emails)
  789. return nil, nil
  790. }
  791. // Populate UUID and other client data for each traffic record
  792. for i := range traffics {
  793. if ct, client, e := s.GetClientByEmail(traffics[i].Email); e == nil && ct != nil && client != nil {
  794. traffics[i].Enable = client.Enable
  795. traffics[i].UUID = client.ID
  796. traffics[i].SubId = client.SubID
  797. }
  798. }
  799. return traffics, nil
  800. }
  801. // BumpClientsLastOnline sets client_traffics.last_online to now for the given
  802. // emails. Used in online-API mode for clients that hold a live connection but
  803. // moved no bytes this poll — the traffic path (addClientTraffic) only bumps
  804. // last_online on a non-zero delta, so idle-but-connected clients would
  805. // otherwise show a stale "last online" while being reported online.
  806. func (s *InboundService) BumpClientsLastOnline(emails []string) error {
  807. uniq := uniqueNonEmptyStrings(emails)
  808. if len(uniq) == 0 {
  809. return nil
  810. }
  811. now := time.Now().UnixMilli()
  812. return submitTrafficWrite(func() error {
  813. db := database.GetDB()
  814. for _, batch := range chunkStrings(uniq, sqliteMaxVars) {
  815. if err := db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Update("last_online", now).Error; err != nil {
  816. return err
  817. }
  818. }
  819. return nil
  820. })
  821. }
  822. func (s *InboundService) GetActiveClientTraffics(emails []string) ([]*xray.ClientTraffic, error) {
  823. uniq := uniqueNonEmptyStrings(emails)
  824. if len(uniq) == 0 {
  825. return nil, nil
  826. }
  827. db := database.GetDB()
  828. traffics := make([]*xray.ClientTraffic, 0, len(uniq))
  829. for _, batch := range chunkStrings(uniq, sqliteMaxVars) {
  830. var page []*xray.ClientTraffic
  831. if err := db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  832. return nil, err
  833. }
  834. traffics = append(traffics, page...)
  835. }
  836. return traffics, nil
  837. }
  838. // GetAllClientTraffics returns the full set of client_traffics rows so the
  839. // websocket broadcasters can ship a complete snapshot every cycle. The old
  840. // delta-only path (GetActiveClientTraffics on activeEmails) silently dropped
  841. // the per-client section whenever no client moved bytes in the cycle or a
  842. // node sync failed, leaving client rows in the UI stuck at stale numbers.
  843. func (s *InboundService) GetAllClientTraffics() ([]*xray.ClientTraffic, error) {
  844. db := database.GetDB()
  845. var traffics []*xray.ClientTraffic
  846. if err := db.Model(xray.ClientTraffic{}).Find(&traffics).Error; err != nil {
  847. return nil, err
  848. }
  849. overlayGlobalTraffic(db, traffics)
  850. return traffics, nil
  851. }
  852. type InboundTrafficSummary struct {
  853. Id int `json:"id"`
  854. Up int64 `json:"up"`
  855. Down int64 `json:"down"`
  856. Total int64 `json:"total"`
  857. Enable bool `json:"enable"`
  858. }
  859. func (s *InboundService) GetInboundsTrafficSummary() ([]InboundTrafficSummary, error) {
  860. db := database.GetDB()
  861. var summaries []InboundTrafficSummary
  862. if err := db.Model(&model.Inbound{}).
  863. Select("id, up, down, total, enable").
  864. Find(&summaries).Error; err != nil {
  865. return nil, err
  866. }
  867. return summaries, nil
  868. }
  869. func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.ClientTraffic, err error) {
  870. db := database.GetDB()
  871. var traffics []*xray.ClientTraffic
  872. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error; err != nil {
  873. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  874. return nil, err
  875. }
  876. if len(traffics) == 0 {
  877. return nil, nil
  878. }
  879. overlayGlobalTraffic(db, traffics)
  880. t := traffics[0]
  881. if rec, rErr := s.clientService.GetRecordByEmail(db, email); rErr == nil && rec != nil {
  882. c := rec.ToClient()
  883. t.UUID = c.ID
  884. t.SubId = c.SubID
  885. return t, nil
  886. }
  887. t2, client, err := s.GetClientByEmail(email)
  888. if err != nil {
  889. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  890. return nil, err
  891. }
  892. if t2 != nil && client != nil {
  893. t2.UUID = client.ID
  894. t2.SubId = client.SubID
  895. return t2, nil
  896. }
  897. return nil, nil
  898. }
  899. func (s *InboundService) UpdateClientTrafficByEmail(email string, upload int64, download int64) error {
  900. return submitTrafficWrite(func() error {
  901. db := database.GetDB()
  902. err := db.Model(xray.ClientTraffic{}).
  903. Where("email = ?", email).
  904. Updates(map[string]any{
  905. "up": upload,
  906. "down": download,
  907. }).Error
  908. if err != nil {
  909. logger.Warningf("Error updating ClientTraffic with email %s: %v", email, err)
  910. }
  911. return err
  912. })
  913. }
  914. func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) {
  915. db := database.GetDB()
  916. inbound := &model.Inbound{}
  917. traffic = &xray.ClientTraffic{}
  918. // Search for inbound settings that contain the query
  919. err = db.Model(model.Inbound{}).Where("settings LIKE ?", "%\""+query+"\"%").First(inbound).Error
  920. if err != nil {
  921. if err == gorm.ErrRecordNotFound {
  922. logger.Warningf("Inbound settings containing query %s not found: %v", query, err)
  923. return nil, err
  924. }
  925. logger.Errorf("Error searching for inbound settings with query %s: %v", query, err)
  926. return nil, err
  927. }
  928. traffic.InboundId = inbound.Id
  929. // Unmarshal settings to get clients
  930. settings := map[string][]model.Client{}
  931. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  932. logger.Errorf("Error unmarshalling inbound settings for inbound ID %d: %v", inbound.Id, err)
  933. return nil, err
  934. }
  935. clients := settings["clients"]
  936. for _, client := range clients {
  937. if (client.ID == query || client.Password == query) && client.Email != "" {
  938. traffic.Email = client.Email
  939. break
  940. }
  941. }
  942. if traffic.Email == "" {
  943. logger.Warningf("No client found with query %s in inbound ID %d", query, inbound.Id)
  944. return nil, gorm.ErrRecordNotFound
  945. }
  946. // Retrieve ClientTraffic based on the found email
  947. err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error
  948. if err != nil {
  949. if err == gorm.ErrRecordNotFound {
  950. logger.Warningf("ClientTraffic for email %s not found: %v", traffic.Email, err)
  951. return nil, err
  952. }
  953. logger.Errorf("Error retrieving ClientTraffic for email %s: %v", traffic.Email, err)
  954. return nil, err
  955. }
  956. return traffic, nil
  957. }