1
0

client_crud.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. package service
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "net/netip"
  8. "runtime/debug"
  9. "slices"
  10. "strings"
  11. "sync"
  12. "sync/atomic"
  13. "time"
  14. "unicode"
  15. "github.com/google/uuid"
  16. "github.com/mhsanaei/3x-ui/v3/internal/database"
  17. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  18. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  19. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  20. "github.com/mhsanaei/3x-ui/v3/internal/util/random"
  21. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  22. "gorm.io/gorm"
  23. )
  24. func hasForbiddenClientChar(s string) bool {
  25. for _, r := range s {
  26. if r == '/' || r == '\\' || r < 0x20 || r == 0x7f || unicode.IsSpace(r) {
  27. return true
  28. }
  29. }
  30. return false
  31. }
  32. func validateClientEmail(email string) error {
  33. if hasForbiddenClientChar(email) {
  34. return common.NewError("client email contains an invalid character:", email)
  35. }
  36. return nil
  37. }
  38. func validateClientSubID(subID string) error {
  39. if hasForbiddenClientChar(subID) {
  40. return common.NewError("client subId contains an invalid character:", subID)
  41. }
  42. return nil
  43. }
  44. // Rejected rather than coerced: an unknown cycle would leave the operator with
  45. // a field that reads as configured while no job ever selects the client.
  46. func validateClientTrafficReset(period string, day int) error {
  47. switch period {
  48. case "", "never", "hourly", "daily", "weekly", "monthly":
  49. default:
  50. return common.NewError("client trafficReset must be never, hourly, daily, weekly or monthly, got:", period)
  51. }
  52. if day < 0 || day > 31 {
  53. return common.NewError("client trafficResetDay must be between 0 and 31, got:", day)
  54. }
  55. return nil
  56. }
  57. // Rejected rather than clamped: nextCalendarRenewal would silently move an
  58. // out-of-range day, and a negative one drops out of the renewal query entirely.
  59. func validateClientResetDay(day int) error {
  60. if day < 0 || day > 31 {
  61. return common.NewError("client resetDay must be between 0 and 31, got:", day)
  62. }
  63. return nil
  64. }
  65. // Rejected rather than coerced: a negative cap reads as "unlimited" to a caller
  66. // but selects nothing, so the client would silently stop renewing.
  67. func validateClientResetMax(resetMax int) error {
  68. if resetMax < 0 {
  69. return common.NewError("client resetMax must not be negative, got:", resetMax)
  70. }
  71. return nil
  72. }
  73. // normalizeClientTrafficReset stores what the inbound path would store, so the
  74. // day never reaches the DB as a 0 that three layers downstream each clamp to 1.
  75. func normalizeClientTrafficReset(c *model.Client) {
  76. if c.TrafficReset == "" {
  77. c.TrafficReset = "never"
  78. }
  79. c.TrafficResetDay = normalizeTrafficResetDay(c.TrafficResetDay)
  80. }
  81. // ClientResetCycle is the slice of a client the reset job needs: enough to know
  82. // whether it is due, and whether its disable is the quota's doing or the operator's.
  83. type ClientResetCycle struct {
  84. Email string
  85. TrafficResetDay int
  86. Enable bool
  87. Total int64
  88. Used int64
  89. }
  90. // Depleted reports a client the quota switched off. A reset restores that one;
  91. // a client disabled below its quota was switched off by hand and stays off.
  92. func (c ClientResetCycle) Depleted() bool {
  93. return c.Total > 0 && c.Used >= c.Total
  94. }
  95. // GetClientsByTrafficReset returns the clients whose own reset cycle matches the
  96. // period, independent of the cycle configured on the inbounds they belong to.
  97. func (s *ClientService) GetClientsByTrafficReset(period string) ([]ClientResetCycle, error) {
  98. var cycles []ClientResetCycle
  99. err := database.GetDB().Table("clients c").
  100. Select("c.email, c.traffic_reset_day, c.enable, COALESCE(ct.total, 0) AS total, COALESCE(ct.up, 0) + COALESCE(ct.down, 0) AS used").
  101. Joins("LEFT JOIN client_traffics ct ON ct.email = c.email").
  102. Where("c.traffic_reset = ?", period).
  103. Scan(&cycles).Error
  104. if err != nil {
  105. return nil, err
  106. }
  107. return cycles, nil
  108. }
  109. // Create applies the client to every requested inbound: one failing inbound no
  110. // longer aborts the others, so the error can name several and needRestart holds.
  111. func (s *ClientService) Create(inboundSvc *InboundService, payload *ClientCreatePayload) (bool, error) {
  112. if payload == nil {
  113. return false, common.NewError("empty payload")
  114. }
  115. client := payload.Client
  116. if strings.TrimSpace(client.Email) == "" {
  117. return false, common.NewError("client email is required")
  118. }
  119. if err := validateClientEmail(client.Email); err != nil {
  120. return false, err
  121. }
  122. if err := validateClientSubID(client.SubID); err != nil {
  123. return false, err
  124. }
  125. if err := validateClientResetDay(client.ResetDay); err != nil {
  126. return false, err
  127. }
  128. if err := validateClientResetMax(client.ResetMax); err != nil {
  129. return false, err
  130. }
  131. if err := validateClientTrafficReset(client.TrafficReset, client.TrafficResetDay); err != nil {
  132. return false, err
  133. }
  134. normalizeClientTrafficReset(&client)
  135. if len(payload.InboundIds) == 0 {
  136. return false, common.NewError("at least one inbound is required")
  137. }
  138. if client.SubID == "" {
  139. client.SubID = uuid.NewString()
  140. }
  141. if !client.Enable {
  142. client.Enable = true
  143. }
  144. now := time.Now().UnixMilli()
  145. if client.CreatedAt == 0 {
  146. client.CreatedAt = now
  147. }
  148. client.UpdatedAt = now
  149. existing := &model.ClientRecord{}
  150. err := database.GetDB().Where("email = ?", client.Email).First(existing).Error
  151. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  152. return false, err
  153. }
  154. emailTaken := !errors.Is(err, gorm.ErrRecordNotFound)
  155. if emailTaken {
  156. if existing.SubID == "" || existing.SubID != client.SubID {
  157. return false, common.NewError("email already in use:", client.Email)
  158. }
  159. // Reuse stored credentials when re-adding an existing identity, or
  160. // fillProtocolDefaults mints a fresh UUID that desyncs other inbounds.
  161. if client.ID == "" {
  162. client.ID = existing.UUID
  163. }
  164. if client.Password == "" {
  165. client.Password = existing.Password
  166. }
  167. if client.Auth == "" {
  168. client.Auth = existing.Auth
  169. }
  170. if client.Secret == "" {
  171. client.Secret = existing.Secret
  172. }
  173. }
  174. if client.SubID != "" {
  175. var subTaken int64
  176. if err := database.GetDB().Model(&model.ClientRecord{}).
  177. Where("sub_id = ? AND email <> ?", client.SubID, client.Email).
  178. Count(&subTaken).Error; err != nil {
  179. return false, err
  180. }
  181. if subTaken > 0 {
  182. return false, common.NewError("subId already in use:", client.SubID)
  183. }
  184. }
  185. // Prepared before any inbound is written: fillProtocolDefaults mints the
  186. // shared credentials on the first inbound and every later one reuses them.
  187. adds := make([]*model.Inbound, 0, len(payload.InboundIds))
  188. for _, ibId := range payload.InboundIds {
  189. inbound, getErr := inboundSvc.GetInbound(ibId)
  190. if getErr != nil {
  191. return false, fmt.Errorf("inbound %d: %w", ibId, getErr)
  192. }
  193. if err := s.fillProtocolDefaults(&client, inbound); err != nil {
  194. return false, fmt.Errorf("inbound %d: %w", ibId, err)
  195. }
  196. clientForInbound := client
  197. if ips, ok := client.AllowedIPsByInbound[ibId]; ok {
  198. clientForInbound.AllowedIPs = ips
  199. } else if !addressesFitAmneziaWGInbound(clientForInbound.AllowedIPs, inbound) {
  200. // The shared AllowedIPs value (e.g. from a single-field legacy
  201. // caller) came from a different subnet than this inbound's own --
  202. // clear it so defaultAmneziaWGClients allocates a fresh, correct
  203. // address for THIS inbound instead of persisting an unroutable
  204. // peer. Same reasoning as addressesFitAmneziaWGInbound's own doc
  205. // comment on the Attach path.
  206. clientForInbound.AllowedIPs = nil
  207. }
  208. settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(clientForInbound, inbound)}})
  209. if mErr != nil {
  210. return false, fmt.Errorf("inbound %d: %w", ibId, mErr)
  211. }
  212. adds = append(adds, &model.Inbound{Id: ibId, Settings: string(settingsPayload)})
  213. }
  214. needRestart, fanoutErr := s.fanoutInboundClientAdds(inboundSvc, adds)
  215. if fanoutErr != nil {
  216. // Never on a failed create: this retrims the devices of an email that
  217. // already existed, and a create the panel reported as failed must not.
  218. return needRestart, fanoutErr
  219. }
  220. // A re-created email is a live identity again: a delete tombstone left
  221. // standing makes the next node merge prune the new client's inbound links.
  222. withdrawClientTombstones(client.Email)
  223. return needRestart, s.setClientLimitHwidByEmail(nil, client.Email, payload.LimitHwid)
  224. }
  225. // inboundFanoutConcurrency caps how many inbounds one client op applies at
  226. // once, so a client spanning many of them can't start an unbounded RPC burst.
  227. const inboundFanoutConcurrency = 4
  228. // inboundApply is one inbound's share of a client op, ready to run.
  229. type inboundApply struct {
  230. id int
  231. run func() (bool, error)
  232. }
  233. // fanoutInboundApplies runs the applies with the node pushes overlapping, so a
  234. // client spanning several nodes no longer costs one RPC round-trip per node.
  235. func fanoutInboundApplies(applies []inboundApply) (bool, error) {
  236. var needRestart atomic.Bool
  237. errs := make([]error, len(applies))
  238. sem := make(chan struct{}, inboundFanoutConcurrency)
  239. var wg sync.WaitGroup
  240. for i := range applies {
  241. wg.Add(1)
  242. sem <- struct{}{}
  243. go func() {
  244. defer wg.Done()
  245. defer func() { <-sem }()
  246. // Off the request goroutine gin's Recovery no longer covers this,
  247. // so an unrecovered panic here would take the whole panel down.
  248. defer func() {
  249. if r := recover(); r != nil {
  250. // The apply may already have committed, so ask for the
  251. // restart the lost return value can no longer report.
  252. needRestart.Store(true)
  253. errs[i] = fmt.Errorf("inbound %d: panic: %v", applies[i].id, r)
  254. logger.Errorf("panic applying client change to inbound %d: %v\n%s", applies[i].id, r, debug.Stack())
  255. }
  256. }()
  257. nr, err := applies[i].run()
  258. if nr {
  259. needRestart.Store(true)
  260. }
  261. if err != nil {
  262. errs[i] = fmt.Errorf("inbound %d: %w", applies[i].id, err)
  263. }
  264. }()
  265. }
  266. wg.Wait()
  267. return needRestart.Load(), errors.Join(errs...)
  268. }
  269. // fanoutInboundClientAdds applies one payload per inbound.
  270. func (s *ClientService) fanoutInboundClientAdds(inboundSvc *InboundService, adds []*model.Inbound) (bool, error) {
  271. applies := make([]inboundApply, 0, len(adds))
  272. for _, add := range adds {
  273. applies = append(applies, inboundApply{id: add.Id, run: func() (bool, error) {
  274. return s.AddInboundClient(inboundSvc, add)
  275. }})
  276. }
  277. return fanoutInboundApplies(applies)
  278. }
  279. // fanoutInboundResults runs one job per inbound with the node pushes
  280. // overlapping, so a bulk op costs one RPC round-trip instead of one per node.
  281. // limit is the caller's own cap: an op that allocates tunnel addresses passes 1,
  282. // because allocation reads a cross-inbound used-set before it writes.
  283. func fanoutInboundResults[T any](inboundIds []int, limit int, run func(i int) T) ([]T, []error) {
  284. if limit < 1 {
  285. limit = 1
  286. }
  287. out := make([]T, len(inboundIds))
  288. errs := make([]error, len(inboundIds))
  289. sem := make(chan struct{}, limit)
  290. var wg sync.WaitGroup
  291. for i := range inboundIds {
  292. wg.Add(1)
  293. sem <- struct{}{}
  294. go func() {
  295. defer wg.Done()
  296. defer func() { <-sem }()
  297. // Off the request goroutine gin's Recovery no longer covers this,
  298. // so an unrecovered panic here would take the whole panel down.
  299. defer func() {
  300. if r := recover(); r != nil {
  301. errs[i] = fmt.Errorf("inbound %d: panic: %v", inboundIds[i], r)
  302. logger.Errorf("panic applying bulk client change to inbound %d: %v\n%s", inboundIds[i], r, debug.Stack())
  303. }
  304. }()
  305. out[i] = run(i)
  306. }()
  307. }
  308. wg.Wait()
  309. return out, errs
  310. }
  311. // addFanoutLimit serializes an add that touches a tunnel inbound. WireGuard and
  312. // AmneziaWG pick a free peer address by reading every inbound's used-set first,
  313. // so two overlapping allocations hand out the same one and the second is refused.
  314. func addFanoutLimit(anyTunnel bool) int {
  315. if anyTunnel {
  316. return 1
  317. }
  318. return inboundFanoutConcurrency
  319. }
  320. // sortedInboundIds gives the fanout a stable order, so which inbound wins a
  321. // per-email report no longer depends on Go's map iteration order.
  322. func sortedInboundIds[V any](byInbound map[int]V) []int {
  323. ids := make([]int, 0, len(byInbound))
  324. for id := range byInbound {
  325. ids = append(ids, id)
  326. }
  327. slices.Sort(ids)
  328. return ids
  329. }
  330. // markInboundNodesDirty makes a half-applied client edit unobservable to a node
  331. // snapshot merge, which skips a node whose config is already flagged dirty.
  332. func markInboundNodesDirty(inboundIds []int) error {
  333. if len(inboundIds) == 0 {
  334. return nil
  335. }
  336. var nodeIDs []int
  337. for _, batch := range chunkInts(inboundIds, sqlInChunk) {
  338. var ids []int
  339. if err := database.GetDB().Model(&model.Inbound{}).
  340. Where("id IN ? AND node_id IS NOT NULL", batch).
  341. Distinct().Pluck("node_id", &ids).Error; err != nil {
  342. return err
  343. }
  344. nodeIDs = append(nodeIDs, ids...)
  345. }
  346. if len(nodeIDs) == 0 {
  347. return nil
  348. }
  349. return runSerializedTx(func(tx *gorm.DB) error {
  350. svc := &NodeService{}
  351. for _, id := range nodeIDs {
  352. if err := svc.MarkNodeDirtyTx(tx, id); err != nil {
  353. return err
  354. }
  355. }
  356. return nil
  357. })
  358. }
  359. func (s *ClientService) fillProtocolDefaults(c *model.Client, ib *model.Inbound) error {
  360. switch ib.Protocol {
  361. case model.VMESS, model.VLESS:
  362. if c.ID == "" {
  363. c.ID = uuid.NewString()
  364. }
  365. case model.Trojan:
  366. if c.Password == "" {
  367. c.Password = strings.ReplaceAll(uuid.NewString(), "-", "")
  368. }
  369. case model.Shadowsocks:
  370. method := shadowsocksMethodFromSettings(ib.Settings)
  371. if c.Password == "" || !validShadowsocksClientKey(method, c.Password) {
  372. c.Password = randomShadowsocksClientKey(method)
  373. }
  374. case model.Hysteria:
  375. if c.Auth == "" {
  376. c.Auth = strings.ReplaceAll(uuid.NewString(), "-", "")
  377. }
  378. case model.MTProto:
  379. if c.Secret == "" {
  380. c.Secret = model.GenerateFakeTLSSecret(mtprotoDomainFromSettings(ib.Settings))
  381. }
  382. case model.TUIC:
  383. if c.ID == "" {
  384. c.ID = uuid.NewString()
  385. }
  386. if c.Password == "" {
  387. c.Password = strings.ReplaceAll(uuid.NewString(), "-", "")
  388. }
  389. }
  390. return nil
  391. }
  392. // defaultMtprotoDomain is the FakeTLS fronting domain used when an mtproto
  393. // inbound carries no fakeTlsDomain of its own; it mirrors the frontend default.
  394. const defaultMtprotoDomain = "www.cloudflare.com"
  395. // mtprotoDomainFromSettings returns the inbound-level FakeTLS domain, falling
  396. // back to the default when unset, so a generated client secret always fronts a
  397. // real hostname.
  398. func mtprotoDomainFromSettings(settings string) string {
  399. domain := ""
  400. if settings != "" {
  401. var m map[string]any
  402. if err := json.Unmarshal([]byte(settings), &m); err == nil {
  403. domain, _ = m["fakeTlsDomain"].(string)
  404. }
  405. }
  406. domain = strings.TrimSpace(domain)
  407. if domain == "" {
  408. return defaultMtprotoDomain
  409. }
  410. return domain
  411. }
  412. func clientWithInboundFlow(c model.Client, ib *model.Inbound) model.Client {
  413. if ib.DisableFlow || !inboundCanEnableTlsFlow(string(ib.Protocol), ib.StreamSettings, ib.Settings) {
  414. c.Flow = ""
  415. }
  416. return c
  417. }
  418. func shadowsocksMethodFromSettings(settings string) string {
  419. if settings == "" {
  420. return ""
  421. }
  422. var m map[string]any
  423. if err := json.Unmarshal([]byte(settings), &m); err != nil {
  424. return ""
  425. }
  426. method, _ := m["method"].(string)
  427. return method
  428. }
  429. func randomShadowsocksClientKey(method string) string {
  430. if n := shadowsocksKeyBytes(method); n > 0 {
  431. return random.Base64Bytes(n)
  432. }
  433. return strings.ReplaceAll(uuid.NewString(), "-", "")
  434. }
  435. func validShadowsocksClientKey(method, key string) bool {
  436. n := shadowsocksKeyBytes(method)
  437. if n == 0 {
  438. return key != ""
  439. }
  440. decoded, err := base64.StdEncoding.DecodeString(key)
  441. if err != nil {
  442. return false
  443. }
  444. return len(decoded) == n
  445. }
  446. func shadowsocksKeyBytes(method string) int {
  447. switch method {
  448. case "2022-blake3-aes-128-gcm":
  449. return 16
  450. case "2022-blake3-aes-256-gcm", "2022-blake3-chacha20-poly1305":
  451. return 32
  452. }
  453. return 0
  454. }
  455. // normalizeShadowsocksClientKeys rewrites any Shadowsocks-2022 client password
  456. // whose decoded length no longer matches settings.method, which happens after the
  457. // inbound method is switched between ciphers of different key sizes (e.g.
  458. // aes-256↔aes-128). A wrong-length uPSK makes xray reject the user, so the link
  459. // fails to connect; regenerating restores a valid key (clients must re-fetch).
  460. // Non-Shadowsocks / legacy-SS settings pass through unchanged.
  461. func normalizeShadowsocksClientKeys(settings string) (string, bool) {
  462. method := shadowsocksMethodFromSettings(settings)
  463. if shadowsocksKeyBytes(method) == 0 {
  464. return settings, false
  465. }
  466. var m map[string]any
  467. if err := json.Unmarshal([]byte(settings), &m); err != nil {
  468. return settings, false
  469. }
  470. clients, ok := m["clients"].([]any)
  471. if !ok {
  472. return settings, false
  473. }
  474. changed := false
  475. for i := range clients {
  476. c, ok := clients[i].(map[string]any)
  477. if !ok {
  478. continue
  479. }
  480. if pw, _ := c["password"].(string); validShadowsocksClientKey(method, pw) {
  481. continue
  482. }
  483. c["password"] = randomShadowsocksClientKey(method)
  484. clients[i] = c
  485. changed = true
  486. }
  487. if !changed {
  488. return settings, false
  489. }
  490. m["clients"] = clients
  491. bs, err := json.MarshalIndent(m, "", " ")
  492. if err != nil {
  493. return settings, false
  494. }
  495. return string(bs), true
  496. }
  497. func applyShadowsocksClientMethod(clients []any, settings map[string]any) {
  498. method, _ := settings["method"].(string)
  499. is2022 := strings.HasPrefix(method, "2022-blake3-")
  500. for i := range clients {
  501. cm, ok := clients[i].(map[string]any)
  502. if !ok {
  503. continue
  504. }
  505. if is2022 {
  506. if _, hasKey := cm["method"]; hasKey {
  507. delete(cm, "method")
  508. clients[i] = cm
  509. }
  510. continue
  511. }
  512. if method == "" {
  513. continue
  514. }
  515. if existing, _ := cm["method"].(string); existing != "" {
  516. continue
  517. }
  518. cm["method"] = method
  519. clients[i] = cm
  520. }
  521. }
  522. func (s *ClientService) Update(inboundSvc *InboundService, id int, updated model.Client, limitHwid int, inboundFilter ...int) (bool, error) {
  523. existing, err := s.GetByID(id)
  524. if err != nil {
  525. return false, err
  526. }
  527. inboundIds, err := s.GetInboundIdsForRecord(id)
  528. if err != nil {
  529. return false, err
  530. }
  531. // The rename rewrites the one shared client record, so every node holding
  532. // this client goes stale — not just the ones an inboundIds filter applies.
  533. attachedIds := append([]int(nil), inboundIds...)
  534. if len(inboundFilter) > 0 {
  535. allow := make(map[int]struct{}, len(inboundFilter))
  536. for _, fid := range inboundFilter {
  537. allow[fid] = struct{}{}
  538. }
  539. filtered := inboundIds[:0:0]
  540. for _, ibId := range inboundIds {
  541. if _, ok := allow[ibId]; ok {
  542. filtered = append(filtered, ibId)
  543. }
  544. }
  545. inboundIds = filtered
  546. }
  547. if strings.TrimSpace(updated.Email) == "" {
  548. return false, common.NewError("client email is required")
  549. }
  550. if err := validateClientEmail(updated.Email); err != nil {
  551. return false, err
  552. }
  553. if err := validateClientSubID(updated.SubID); err != nil {
  554. return false, err
  555. }
  556. if err := validateClientResetDay(updated.ResetDay); err != nil {
  557. return false, err
  558. }
  559. if err := validateClientResetMax(updated.ResetMax); err != nil {
  560. return false, err
  561. }
  562. if err := validateClientTrafficReset(updated.TrafficReset, updated.TrafficResetDay); err != nil {
  563. return false, err
  564. }
  565. normalizeClientTrafficReset(&updated)
  566. if updated.SubID == "" {
  567. updated.SubID = existing.SubID
  568. }
  569. if updated.SubID == "" {
  570. updated.SubID = uuid.NewString()
  571. }
  572. updated.UpdatedAt = time.Now().UnixMilli()
  573. if updated.CreatedAt == 0 {
  574. updated.CreatedAt = existing.CreatedAt
  575. }
  576. // Preserve existing credentials when the caller omits them, so a partial
  577. // update (e.g. only changing traffic/expiry) doesn't silently rotate the
  578. // client's UUID/password/auth via fillProtocolDefaults. Supplying a new
  579. // value still rotates it intentionally.
  580. if updated.ID == "" {
  581. updated.ID = existing.UUID
  582. }
  583. if updated.Password == "" {
  584. updated.Password = existing.Password
  585. }
  586. if updated.Auth == "" {
  587. updated.Auth = existing.Auth
  588. }
  589. if updated.Secret == "" {
  590. updated.Secret = existing.Secret
  591. }
  592. if updated.Email != existing.Email {
  593. var collisionCount int64
  594. if err := database.GetDB().Model(&model.ClientRecord{}).
  595. Where("email = ? AND id <> ?", updated.Email, id).
  596. Count(&collisionCount).Error; err != nil {
  597. return false, err
  598. }
  599. if collisionCount > 0 {
  600. return false, common.NewError("Duplicate email:", updated.Email)
  601. }
  602. }
  603. if updated.SubID != existing.SubID {
  604. var subCollision int64
  605. if err := database.GetDB().Model(&model.ClientRecord{}).
  606. Where("sub_id = ? AND id <> ?", updated.SubID, id).
  607. Count(&subCollision).Error; err != nil {
  608. return false, err
  609. }
  610. if subCollision > 0 {
  611. return false, common.NewError("Duplicate subId:", updated.SubID)
  612. }
  613. }
  614. tunnelCount, tcErr := tunnelInboundCount(inboundIds)
  615. if tcErr != nil {
  616. return false, tcErr
  617. }
  618. // Built before any inbound is written, as in Create: fillProtocolDefaults
  619. // mints the shared credentials on the first inbound, later ones reuse them.
  620. applies := make([]inboundApply, 0, len(inboundIds))
  621. for _, ibId := range inboundIds {
  622. inbound, getErr := inboundSvc.GetInbound(ibId)
  623. if getErr != nil {
  624. if errors.Is(getErr, gorm.ErrRecordNotFound) {
  625. if err := database.GetDB().
  626. Where("client_id = ? AND inbound_id = ?", id, ibId).
  627. Delete(&model.ClientInbound{}).Error; err != nil {
  628. return false, err
  629. }
  630. continue
  631. }
  632. return false, getErr
  633. }
  634. if existing.Email == "" {
  635. continue
  636. }
  637. if err := s.fillProtocolDefaults(&updated, inbound); err != nil {
  638. return false, err
  639. }
  640. clientForInbound := updated
  641. if ips, ok := updated.AllowedIPsByInbound[ibId]; ok {
  642. clientForInbound.AllowedIPs = ips
  643. } else if tunnelCount > 1 && (inbound.Protocol == model.WireGuard || inbound.Protocol == model.AmneziaWG) {
  644. // One shared peer field set cannot describe several peers: broadcast
  645. // it and they all end up with the same keys and tunnel address.
  646. clientForInbound.AllowedIPs = nil
  647. clientForInbound.PrivateKey = ""
  648. clientForInbound.PublicKey = ""
  649. clientForInbound.PreSharedKey = ""
  650. } else if !addressesFitAmneziaWGInbound(clientForInbound.AllowedIPs, inbound) {
  651. // A single shared AllowedIPs field (the common case for a caller
  652. // that never sends AllowedIPsByInbound) must never overwrite an
  653. // inbound it doesn't belong to -- e.g. a client attached to both
  654. // wg and awg saving its wg-labeled address would otherwise get
  655. // that same address silently written into the awg peer config
  656. // too. Clearing it here makes UpdateInboundClient's own
  657. // empty-AllowedIPs carry-forward (see its WireGuard/AmneziaWG
  658. // branch) preserve THIS inbound's existing, correct value
  659. // instead.
  660. clientForInbound.AllowedIPs = nil
  661. }
  662. settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(clientForInbound, inbound)}})
  663. if mErr != nil {
  664. return false, mErr
  665. }
  666. data := &model.Inbound{Id: ibId, Settings: string(settingsPayload)}
  667. applies = append(applies, inboundApply{id: ibId, run: func() (bool, error) {
  668. return s.UpdateInboundClient(inboundSvc, data, existing.Email)
  669. }})
  670. }
  671. // Each apply marks only its OWN node dirty, so between the first and last
  672. // one a merge could resurrect the pre-edit email as a second client.
  673. if err := markInboundNodesDirty(attachedIds); err != nil {
  674. return false, err
  675. }
  676. needRestart, applyErr := fanoutInboundApplies(applies)
  677. if applyErr != nil {
  678. return needRestart, applyErr
  679. }
  680. // UpdateInboundClient renames the record atomically with each inbound's
  681. // settings JSON; this direct write only covers records with no inbound left.
  682. if updated.Email != existing.Email {
  683. if err := database.GetDB().Model(&model.ClientRecord{}).
  684. Where("id = ? AND email = ?", id, existing.Email).
  685. Update("email", updated.Email).Error; err != nil {
  686. return needRestart, err
  687. }
  688. }
  689. if len(inboundIds) == 0 {
  690. merged := *existing
  691. applyClientRecordMerge(&merged, updated.ToRecord())
  692. if err := database.GetDB().Model(&model.ClientRecord{}).
  693. Where("id = ?", id).
  694. Updates(map[string]any{
  695. "sub_id": merged.SubID,
  696. "uuid": merged.UUID,
  697. "password": merged.Password,
  698. "auth": merged.Auth,
  699. "secret": merged.Secret,
  700. "flow": merged.Flow,
  701. "security": merged.Security,
  702. "wg_private_key": merged.PrivateKey,
  703. "wg_public_key": merged.PublicKey,
  704. "wg_allowed_ips": merged.AllowedIPs,
  705. "wg_pre_shared_key": merged.PreSharedKey,
  706. "wg_keep_alive": merged.KeepAlive,
  707. "limit_ip": merged.LimitIP,
  708. "total_gb": merged.TotalGB,
  709. "expiry_time": merged.ExpiryTime,
  710. "tg_id": merged.TgID,
  711. "comment": merged.Comment,
  712. "reset": merged.Reset,
  713. "reset_day": merged.ResetDay,
  714. "reset_max": merged.ResetMax,
  715. "traffic_reset": merged.TrafficReset,
  716. "traffic_reset_day": merged.TrafficResetDay,
  717. }).Error; err != nil {
  718. return needRestart, err
  719. }
  720. }
  721. reverseStr := ""
  722. if updated.Reverse != nil && strings.TrimSpace(updated.Reverse.Tag) != "" {
  723. if b, mErr := json.Marshal(updated.Reverse); mErr == nil {
  724. reverseStr = string(b)
  725. }
  726. }
  727. if err := database.GetDB().Model(&model.ClientRecord{}).
  728. Where("id = ?", id).
  729. Update("reverse", reverseStr).Error; err != nil {
  730. return needRestart, err
  731. }
  732. // Persist the group explicitly. SyncInbound deliberately preserves the
  733. // stored group when the inbound settings carry none — so a node snapshot or a
  734. // group-less settings rebuild can't wipe it (see SyncInbound + its tests).
  735. // That guard also meant clearing the group in the client editor never took
  736. // effect. The editor always round-trips the field, so apply it here,
  737. // including the empty string that removes the client from its group.
  738. if err := database.GetDB().Model(&model.ClientRecord{}).
  739. Where("id = ?", id).
  740. UpdateColumn("group_name", updated.Group).Error; err != nil {
  741. return needRestart, err
  742. }
  743. // Same shape as the group write above: SyncInbound keeps a stored ad-tag
  744. // when the incoming settings carry none, so clearing the override must be
  745. // applied here, where the editor always round-trips the field.
  746. if err := database.GetDB().Model(&model.ClientRecord{}).
  747. Where("id = ?", id).
  748. UpdateColumn("ad_tag", updated.AdTag).Error; err != nil {
  749. return needRestart, err
  750. }
  751. if err := database.GetDB().Model(&model.ClientRecord{}).
  752. Where("id = ?", id).
  753. UpdateColumn("enable", updated.Enable).Error; err != nil {
  754. return needRestart, err
  755. }
  756. if err := s.setClientLimitHwidByEmail(nil, updated.Email, limitHwid); err != nil {
  757. return needRestart, err
  758. }
  759. if err := database.GetDB().Model(&model.ClientRecord{}).
  760. Where("id = ?", id).
  761. UpdateColumn("updated_at", time.Now().UnixMilli()).Error; err != nil {
  762. return needRestart, err
  763. }
  764. return needRestart, nil
  765. }
  766. func (s *ClientService) Delete(inboundSvc *InboundService, id int, keepTraffic bool) (bool, error) {
  767. existing, err := s.GetByID(id)
  768. if err != nil {
  769. return false, err
  770. }
  771. tombstoneClientEmail(existing.Email)
  772. inboundIds, err := s.GetInboundIdsForRecord(id)
  773. if err != nil {
  774. withdrawClientTombstones(existing.Email)
  775. return false, err
  776. }
  777. applies := make([]inboundApply, 0, len(inboundIds))
  778. var delErrs []error
  779. for _, ibId := range inboundIds {
  780. if _, getErr := inboundSvc.GetInbound(ibId); getErr != nil {
  781. if errors.Is(getErr, gorm.ErrRecordNotFound) {
  782. continue
  783. }
  784. delErrs = append(delErrs, fmt.Errorf("inbound %d: %w", ibId, getErr))
  785. continue
  786. }
  787. // Always delete by email — the client's stable identity. This removes
  788. // every matching entry from the inbound's settings even when the stored
  789. // credential (UUID/password/auth) drifted from the inbound JSON, or a
  790. // duplicate entry with the same email exists.
  791. if existing.Email == "" {
  792. continue
  793. }
  794. applies = append(applies, inboundApply{id: ibId, run: func() (bool, error) {
  795. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, existing.Email, keepTraffic, true)
  796. // The client is already absent from this inbound (data drift or a
  797. // retried delete). Skip it — deletion stays idempotent.
  798. if errors.Is(delErr, ErrClientNotInInbound) {
  799. return nr, nil
  800. }
  801. return nr, delErr
  802. }})
  803. }
  804. needRestart, applyErr := fanoutInboundApplies(applies)
  805. if applyErr != nil {
  806. delErrs = append(delErrs, applyErr)
  807. }
  808. // A failed inbound still holds the client in its settings JSON: keep the
  809. // record so the next delete retries exactly the leftovers, and report it.
  810. // The tombstone lifts with it, or the next node merge finishes the deletion.
  811. if len(delErrs) > 0 {
  812. withdrawClientTombstones(existing.Email)
  813. return needRestart, errors.Join(delErrs...)
  814. }
  815. db := database.GetDB()
  816. if err := db.Transaction(func(tx *gorm.DB) error {
  817. if existing.Email != "" {
  818. if err := adjustGroupBaselinesForRemovedTraffic(tx, []string{existing.Email}); err != nil {
  819. return err
  820. }
  821. }
  822. if err := tx.Where("client_id = ?", id).Delete(&model.ClientInbound{}).Error; err != nil {
  823. return err
  824. }
  825. if err := tx.Where("client_id = ?", id).Delete(&model.ClientExternalLink{}).Error; err != nil {
  826. return err
  827. }
  828. if err := clearClientHwidsBySubIDTx(tx, existing.SubID); err != nil {
  829. return err
  830. }
  831. if !keepTraffic && existing.Email != "" {
  832. if err := tx.Where("email = ?", existing.Email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  833. return err
  834. }
  835. if err := clearGlobalTraffic(tx, existing.Email); err != nil {
  836. return err
  837. }
  838. if err := tx.Where("client_email = ?", existing.Email).Delete(&model.InboundClientIps{}).Error; err != nil {
  839. return err
  840. }
  841. if err := tx.Where("email = ?", existing.Email).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  842. return err
  843. }
  844. }
  845. return tx.Delete(&model.ClientRecord{}, id).Error
  846. }); err != nil {
  847. withdrawClientTombstones(existing.Email)
  848. return needRestart, err
  849. }
  850. return needRestart, nil
  851. }
  852. // hasTunnelAttachment reports whether any of inboundIds is a currently
  853. // existing WireGuard or AmneziaWG inbound. Inbounds that fail to load are
  854. // skipped rather than treated as an error -- Attach's own loop already
  855. // surfaces a real error for any inbound it can't load when it gets there.
  856. func (s *ClientService) hasTunnelAttachment(inboundSvc *InboundService, inboundIds []int) bool {
  857. for _, ibId := range inboundIds {
  858. inbound, err := inboundSvc.GetInbound(ibId)
  859. if err != nil {
  860. continue
  861. }
  862. if inbound.Protocol == model.WireGuard || inbound.Protocol == model.AmneziaWG {
  863. return true
  864. }
  865. }
  866. return false
  867. }
  868. // tunnelInboundCount reports how many of inboundIds are WireGuard/AmneziaWG,
  869. // i.e. how many independent peers one shared field set would be written to.
  870. func tunnelInboundCount(inboundIds []int) (int64, error) {
  871. if len(inboundIds) == 0 {
  872. return 0, nil
  873. }
  874. var n int64
  875. err := database.GetDB().Model(&model.Inbound{}).
  876. Where("id IN ? AND protocol IN ?", inboundIds, []model.Protocol{model.WireGuard, model.AmneziaWG}).
  877. Count(&n).Error
  878. return n, err
  879. }
  880. // addressesFitAmneziaWGInbound reports whether every entry in addrs falls
  881. // inside ib's own configured subnet(s). AmneziaWG only: its kernel interface
  882. // Address is exactly that subnet, so an address inherited from elsewhere (an
  883. // identity attached to a WireGuard inbound first, say) produces a peer that
  884. // can never connect -- Attach allocates fresh instead.
  885. func addressesFitAmneziaWGInbound(addrs []string, ib *model.Inbound) bool {
  886. if ib.Protocol != model.AmneziaWG || len(addrs) == 0 {
  887. return true
  888. }
  889. v4Base, v6Base, err := defaultAmneziaWGSubnetBases(ib.Settings)
  890. if err != nil {
  891. return false
  892. }
  893. bases := make([]netip.Prefix, 0, 2)
  894. for _, base := range []string{v4Base, v6Base} {
  895. if base == "" {
  896. continue
  897. }
  898. prefix, pErr := netip.ParsePrefix(base)
  899. if pErr != nil {
  900. return false
  901. }
  902. bases = append(bases, prefix)
  903. }
  904. for _, a := range addrs {
  905. host := wireguardHostAddr(a)
  906. if !host.IsValid() {
  907. return false
  908. }
  909. fits := false
  910. for _, prefix := range bases {
  911. if prefix.Contains(host) {
  912. fits = true
  913. break
  914. }
  915. }
  916. if !fits {
  917. return false
  918. }
  919. }
  920. return true
  921. }
  922. // Attach applies the client to every requested inbound: one failing inbound no
  923. // longer aborts the others, so the error can name several and needRestart holds.
  924. func (s *ClientService) Attach(inboundSvc *InboundService, id int, inboundIds []int) (bool, error) {
  925. existing, err := s.GetByID(id)
  926. if err != nil {
  927. return false, err
  928. }
  929. currentIds, err := s.GetInboundIdsForRecord(id)
  930. if err != nil {
  931. return false, err
  932. }
  933. have := make(map[int]struct{}, len(currentIds))
  934. for _, x := range currentIds {
  935. have[x] = struct{}{}
  936. }
  937. clientWire := existing.ToClient()
  938. flow, ffErr := s.EffectiveFlow(nil, id)
  939. if ffErr != nil {
  940. return false, ffErr
  941. }
  942. clientWire.Flow = flow
  943. clientWire.UpdatedAt = time.Now().UnixMilli()
  944. // If this identity has no CURRENT WireGuard/AmneziaWG attachment,
  945. // clientWire.AllowedIPs (from the ClientRecord) is a leftover from
  946. // whenever it last had one -- nothing reserves it anymore. Clear it so
  947. // attaching to a tunnel inbound now allocates a fresh address instead
  948. // of resurrecting the old one, which may no longer even be the lowest
  949. // free slot. Left untouched when the identity already has an active
  950. // tunnel elsewhere, so extending it to a second protocol still keeps
  951. // the same address on both.
  952. if !s.hasTunnelAttachment(inboundSvc, currentIds) {
  953. clientWire.AllowedIPs = nil
  954. }
  955. adds := make([]*model.Inbound, 0, len(inboundIds))
  956. for _, ibId := range inboundIds {
  957. if _, attached := have[ibId]; attached {
  958. continue
  959. }
  960. inbound, getErr := inboundSvc.GetInbound(ibId)
  961. if getErr != nil {
  962. return false, fmt.Errorf("inbound %d: %w", ibId, getErr)
  963. }
  964. copyClient := *clientWire
  965. if !addressesFitAmneziaWGInbound(copyClient.AllowedIPs, inbound) {
  966. copyClient.AllowedIPs = nil
  967. }
  968. if err := s.fillProtocolDefaults(&copyClient, inbound); err != nil {
  969. return false, fmt.Errorf("inbound %d: %w", ibId, err)
  970. }
  971. settingsPayload, mErr := json.Marshal(map[string][]model.Client{"clients": {clientWithInboundFlow(copyClient, inbound)}})
  972. if mErr != nil {
  973. return false, fmt.Errorf("inbound %d: %w", ibId, mErr)
  974. }
  975. adds = append(adds, &model.Inbound{Id: ibId, Settings: string(settingsPayload)})
  976. }
  977. return s.fanoutInboundClientAdds(inboundSvc, adds)
  978. }
  979. func (s *ClientService) CreateOne(inboundSvc *InboundService, inboundId int, client model.Client) (bool, error) {
  980. return s.Create(inboundSvc, &ClientCreatePayload{
  981. Client: client,
  982. InboundIds: []int{inboundId},
  983. })
  984. }
  985. func (s *ClientService) DetachByEmail(inboundSvc *InboundService, inboundId int, email string) (bool, error) {
  986. if email == "" {
  987. return false, common.NewError("client email is required")
  988. }
  989. rec, err := s.GetRecordByEmail(nil, email)
  990. if err != nil {
  991. return false, err
  992. }
  993. return s.Detach(inboundSvc, rec.Id, []int{inboundId})
  994. }
  995. func (s *ClientService) AttachByEmail(inboundSvc *InboundService, email string, inboundIds []int) (bool, error) {
  996. if email == "" {
  997. return false, common.NewError("client email is required")
  998. }
  999. rec, err := s.GetRecordByEmail(nil, email)
  1000. if err != nil {
  1001. return false, err
  1002. }
  1003. return s.Attach(inboundSvc, rec.Id, inboundIds)
  1004. }
  1005. func (s *ClientService) DetachByEmailMany(inboundSvc *InboundService, email string, inboundIds []int) (bool, error) {
  1006. if email == "" {
  1007. return false, common.NewError("client email is required")
  1008. }
  1009. rec, err := s.GetRecordByEmail(nil, email)
  1010. if err != nil {
  1011. return false, err
  1012. }
  1013. return s.Detach(inboundSvc, rec.Id, inboundIds)
  1014. }
  1015. func (s *ClientService) DeleteByEmail(inboundSvc *InboundService, email string, keepTraffic bool) (bool, error) {
  1016. if email == "" {
  1017. return false, common.NewError("client email is required")
  1018. }
  1019. rec, err := s.GetRecordByEmail(nil, email)
  1020. if err == nil {
  1021. return s.Delete(inboundSvc, rec.Id, keepTraffic)
  1022. }
  1023. if !errors.Is(err, gorm.ErrRecordNotFound) {
  1024. return false, err
  1025. }
  1026. inboundIds, idsErr := s.findInboundIdsByClientEmail(email)
  1027. if idsErr != nil {
  1028. return false, idsErr
  1029. }
  1030. if len(inboundIds) == 0 {
  1031. return false, common.NewError(fmt.Sprintf("client %q not found in any inbound or client record", email))
  1032. }
  1033. applies := make([]inboundApply, 0, len(inboundIds))
  1034. for _, ibId := range inboundIds {
  1035. applies = append(applies, inboundApply{id: ibId, run: func() (bool, error) {
  1036. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, email, keepTraffic, true)
  1037. if errors.Is(delErr, ErrClientNotInInbound) {
  1038. return nr, nil
  1039. }
  1040. return nr, delErr
  1041. }})
  1042. }
  1043. needRestart, delErr := fanoutInboundApplies(applies)
  1044. if delErr != nil {
  1045. return needRestart, delErr
  1046. }
  1047. if !keepTraffic {
  1048. db := database.GetDB()
  1049. if err := db.Where("email = ?", email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  1050. return needRestart, err
  1051. }
  1052. if err := clearGlobalTraffic(db, email); err != nil {
  1053. return needRestart, err
  1054. }
  1055. if err := db.Where("client_email = ?", email).Delete(&model.InboundClientIps{}).Error; err != nil {
  1056. return needRestart, err
  1057. }
  1058. if err := db.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  1059. return needRestart, err
  1060. }
  1061. }
  1062. return needRestart, nil
  1063. }
  1064. func (s *ClientService) UpdateByEmail(inboundSvc *InboundService, email string, updated model.Client, limitHwid int, inboundFilter ...int) (bool, error) {
  1065. if email == "" {
  1066. return false, common.NewError("client email is required")
  1067. }
  1068. rec, err := s.GetRecordByEmail(nil, email)
  1069. if err != nil {
  1070. return false, err
  1071. }
  1072. return s.Update(inboundSvc, rec.Id, updated, limitHwid, inboundFilter...)
  1073. }
  1074. func (s *ClientService) Detach(inboundSvc *InboundService, id int, inboundIds []int) (bool, error) {
  1075. existing, err := s.GetByID(id)
  1076. if err != nil {
  1077. return false, err
  1078. }
  1079. currentIds, err := s.GetInboundIdsForRecord(id)
  1080. if err != nil {
  1081. return false, err
  1082. }
  1083. have := make(map[int]struct{}, len(currentIds))
  1084. for _, x := range currentIds {
  1085. have[x] = struct{}{}
  1086. }
  1087. applies := make([]inboundApply, 0, len(inboundIds))
  1088. for _, ibId := range inboundIds {
  1089. if _, attached := have[ibId]; !attached {
  1090. continue
  1091. }
  1092. if _, getErr := inboundSvc.GetInbound(ibId); getErr != nil {
  1093. return false, getErr
  1094. }
  1095. // Detach by email — the client's stable identity (see Delete).
  1096. if existing.Email == "" {
  1097. continue
  1098. }
  1099. applies = append(applies, inboundApply{id: ibId, run: func() (bool, error) {
  1100. nr, delErr := s.DelInboundClientByEmail(inboundSvc, ibId, existing.Email, true, false)
  1101. if errors.Is(delErr, ErrClientNotInInbound) {
  1102. return nr, nil
  1103. }
  1104. return nr, delErr
  1105. }})
  1106. }
  1107. return fanoutInboundApplies(applies)
  1108. }