client_locks.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package service
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "sync"
  6. "time"
  7. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  8. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  9. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  10. "gorm.io/gorm"
  11. )
  12. // Short-lived tombstone of just-deleted client emails so that a node snapshot
  13. // arriving between delete and node-side processing doesn't resurrect them.
  14. var (
  15. recentlyDeletedMu sync.Mutex
  16. recentlyDeleted = map[string]time.Time{}
  17. )
  18. const deleteTombstoneTTL = 90 * time.Second
  19. var (
  20. inboundMutationLocksMu sync.Mutex
  21. inboundMutationLocks = map[int]*sync.Mutex{}
  22. )
  23. func lockInbound(inboundId int) *sync.Mutex {
  24. inboundMutationLocksMu.Lock()
  25. m, ok := inboundMutationLocks[inboundId]
  26. if !ok {
  27. m = &sync.Mutex{}
  28. inboundMutationLocks[inboundId] = m
  29. }
  30. inboundMutationLocksMu.Unlock()
  31. m.Lock()
  32. return m
  33. }
  34. func compactOrphans(db *gorm.DB, clients []any) []any {
  35. if len(clients) == 0 {
  36. return clients
  37. }
  38. emails := make([]string, 0, len(clients))
  39. for _, c := range clients {
  40. cm, ok := c.(map[string]any)
  41. if !ok {
  42. continue
  43. }
  44. if e, _ := cm["email"].(string); e != "" {
  45. emails = append(emails, e)
  46. }
  47. }
  48. if len(emails) == 0 {
  49. return clients
  50. }
  51. existing := make(map[string]struct{}, len(emails))
  52. const orphanChunk = 400
  53. for start := 0; start < len(emails); start += orphanChunk {
  54. end := min(start+orphanChunk, len(emails))
  55. var found []string
  56. if err := db.Model(&model.ClientRecord{}).Where("email IN ?", emails[start:end]).Pluck("email", &found).Error; err != nil {
  57. logger.Warning("compactOrphans pluck:", err)
  58. return clients
  59. }
  60. for _, e := range found {
  61. existing[e] = struct{}{}
  62. }
  63. }
  64. if len(existing) == len(emails) {
  65. return clients
  66. }
  67. out := make([]any, 0, len(existing))
  68. for _, c := range clients {
  69. cm, ok := c.(map[string]any)
  70. if !ok {
  71. out = append(out, c)
  72. continue
  73. }
  74. e, _ := cm["email"].(string)
  75. if e == "" {
  76. out = append(out, c)
  77. continue
  78. }
  79. if _, ok := existing[e]; ok {
  80. out = append(out, c)
  81. }
  82. }
  83. return out
  84. }
  85. func tombstoneClientEmail(email string) {
  86. if email == "" {
  87. return
  88. }
  89. recentlyDeletedMu.Lock()
  90. defer recentlyDeletedMu.Unlock()
  91. recentlyDeleted[email] = time.Now()
  92. cutoff := time.Now().Add(-deleteTombstoneTTL)
  93. for e, ts := range recentlyDeleted {
  94. if ts.Before(cutoff) {
  95. delete(recentlyDeleted, e)
  96. }
  97. }
  98. }
  99. func withdrawClientTombstones(emails ...string) {
  100. if len(emails) == 0 {
  101. return
  102. }
  103. recentlyDeletedMu.Lock()
  104. defer recentlyDeletedMu.Unlock()
  105. for _, email := range emails {
  106. if email != "" {
  107. delete(recentlyDeleted, email)
  108. }
  109. }
  110. }
  111. func tombstoneClientEmails(emails []string) {
  112. if len(emails) == 0 {
  113. return
  114. }
  115. now := time.Now()
  116. cutoff := now.Add(-deleteTombstoneTTL)
  117. recentlyDeletedMu.Lock()
  118. defer recentlyDeletedMu.Unlock()
  119. for _, email := range emails {
  120. if email != "" {
  121. recentlyDeleted[email] = now
  122. }
  123. }
  124. for e, ts := range recentlyDeleted {
  125. if ts.Before(cutoff) {
  126. delete(recentlyDeleted, e)
  127. }
  128. }
  129. }
  130. func isClientEmailTombstoned(email string) bool {
  131. if email == "" {
  132. return false
  133. }
  134. recentlyDeletedMu.Lock()
  135. defer recentlyDeletedMu.Unlock()
  136. ts, ok := recentlyDeleted[email]
  137. if !ok {
  138. return false
  139. }
  140. if time.Since(ts) > deleteTombstoneTTL {
  141. delete(recentlyDeleted, email)
  142. return false
  143. }
  144. return true
  145. }
  146. // dedupeSettingsClients collapses duplicate same-email client entries inside a
  147. // settings JSON blob, keeping the first occurrence. Node snapshots produced by
  148. // builds without the addInboundClient duplicate guard can carry duplicates
  149. // (#5770); adopting them verbatim would copy the duplication into the central
  150. // inbound. Returns the filtered JSON and whether anything was removed.
  151. func dedupeSettingsClients(settings string) (string, bool) {
  152. if settings == "" {
  153. return settings, false
  154. }
  155. var parsed map[string]any
  156. if err := json.Unmarshal([]byte(settings), &parsed); err != nil {
  157. return settings, false
  158. }
  159. clients, _ := parsed["clients"].([]any)
  160. if len(clients) < 2 {
  161. return settings, false
  162. }
  163. seen := make(map[string]struct{}, len(clients))
  164. kept := make([]any, 0, len(clients))
  165. for _, c := range clients {
  166. if cm, ok := c.(map[string]any); ok {
  167. if email, _ := cm["email"].(string); email != "" {
  168. key := strings.ToLower(email)
  169. if _, dup := seen[key]; dup {
  170. continue
  171. }
  172. seen[key] = struct{}{}
  173. }
  174. }
  175. kept = append(kept, c)
  176. }
  177. if len(kept) == len(clients) {
  178. return settings, false
  179. }
  180. parsed["clients"] = kept
  181. b, err := json.MarshalIndent(parsed, "", " ")
  182. if err != nil {
  183. return settings, false
  184. }
  185. return string(b), true
  186. }
  187. // stripTombstonedClients drops just-deleted client entries from a node
  188. // snapshot's settings JSON so adopting a stale snapshot can't re-add them to
  189. // the central inbound while the delete tombstone is live. Returns the filtered
  190. // JSON and whether anything was removed.
  191. func stripTombstonedClients(settings string) (string, bool) {
  192. if settings == "" {
  193. return settings, false
  194. }
  195. var parsed map[string]any
  196. if err := json.Unmarshal([]byte(settings), &parsed); err != nil {
  197. return settings, false
  198. }
  199. clients, _ := parsed["clients"].([]any)
  200. if len(clients) == 0 {
  201. return settings, false
  202. }
  203. kept := make([]any, 0, len(clients))
  204. for _, c := range clients {
  205. if cm, ok := c.(map[string]any); ok {
  206. if email, _ := cm["email"].(string); email != "" && isClientEmailTombstoned(email) {
  207. continue
  208. }
  209. }
  210. kept = append(kept, c)
  211. }
  212. if len(kept) == len(clients) {
  213. return settings, false
  214. }
  215. parsed["clients"] = kept
  216. b, err := json.MarshalIndent(parsed, "", " ")
  217. if err != nil {
  218. return settings, false
  219. }
  220. return string(b), true
  221. }
  222. // liftClientLifecycleInSettings rewrites adopted settings from master traffic
  223. // so a lagging node blob cannot store pre-extension expiry/enable (#6228).
  224. func liftClientLifecycleInSettings(settings string, trafficByEmail map[string]*xray.ClientTraffic) (string, bool) {
  225. if settings == "" || len(trafficByEmail) == 0 {
  226. return settings, false
  227. }
  228. var parsed map[string]any
  229. if err := json.Unmarshal([]byte(settings), &parsed); err != nil {
  230. return settings, false
  231. }
  232. clients, _ := parsed["clients"].([]any)
  233. if len(clients) == 0 {
  234. return settings, false
  235. }
  236. changed := false
  237. for i := range clients {
  238. cm, ok := clients[i].(map[string]any)
  239. if !ok {
  240. continue
  241. }
  242. email, _ := cm["email"].(string)
  243. if email == "" {
  244. continue
  245. }
  246. tr := trafficByEmail[email]
  247. if tr == nil {
  248. continue
  249. }
  250. nodeExpiry, hasExpiry := jsonClientInt64(cm["expiryTime"])
  251. if !hasExpiry {
  252. continue
  253. }
  254. merged := mergeActivationExpiry(tr.ExpiryTime, nodeExpiry)
  255. if merged != nodeExpiry {
  256. cm["expiryTime"] = merged
  257. changed = true
  258. }
  259. // tr is the already-merged master row, authoritative in both directions:
  260. // a lagging blob must not re-enable a disabled client either (#4917).
  261. if nodeEnable, _ := cm["enable"].(bool); nodeEnable != tr.Enable {
  262. cm["enable"] = tr.Enable
  263. changed = true
  264. }
  265. clients[i] = cm
  266. }
  267. if !changed {
  268. return settings, false
  269. }
  270. parsed["clients"] = clients
  271. b, err := json.MarshalIndent(parsed, "", " ")
  272. if err != nil {
  273. return settings, false
  274. }
  275. return string(b), true
  276. }
  277. // settingsClientAbsoluteExpiries indexes the absolute (>0) expiryTime of every
  278. // client in a settings blob. Never nil, so callers can cache it per inbound.
  279. func settingsClientAbsoluteExpiries(settings string) map[string]int64 {
  280. out := map[string]int64{}
  281. if settings == "" {
  282. return out
  283. }
  284. var parsed map[string]any
  285. if err := json.Unmarshal([]byte(settings), &parsed); err != nil {
  286. return out
  287. }
  288. clients, _ := parsed["clients"].([]any)
  289. for _, c := range clients {
  290. cm, ok := c.(map[string]any)
  291. if !ok {
  292. continue
  293. }
  294. email, _ := cm["email"].(string)
  295. if email == "" {
  296. continue
  297. }
  298. if exp, has := jsonClientInt64(cm["expiryTime"]); has && exp > 0 {
  299. out[email] = exp
  300. }
  301. }
  302. return out
  303. }
  304. func jsonClientInt64(v any) (int64, bool) {
  305. // json.Unmarshal into map[string]any yields float64 for numbers.
  306. n, ok := v.(float64)
  307. if !ok {
  308. return 0, false
  309. }
  310. return int64(n), true
  311. }