client_crud.go 38 KB

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