inbound.go 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597
  1. // Package service provides business logic services for the 3x-ui web panel,
  2. // including inbound/outbound management, user administration, settings, and Xray integration.
  3. package service
  4. import (
  5. "context"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "time"
  14. "github.com/google/uuid"
  15. "github.com/mhsanaei/3x-ui/v3/database"
  16. "github.com/mhsanaei/3x-ui/v3/database/model"
  17. "github.com/mhsanaei/3x-ui/v3/logger"
  18. "github.com/mhsanaei/3x-ui/v3/util/common"
  19. "github.com/mhsanaei/3x-ui/v3/web/runtime"
  20. "github.com/mhsanaei/3x-ui/v3/xray"
  21. "gorm.io/gorm"
  22. "gorm.io/gorm/clause"
  23. )
  24. var reportedRemoteTagConflict sync.Map
  25. type InboundService struct {
  26. xrayApi xray.XrayAPI
  27. clientService ClientService
  28. fallbackService FallbackService
  29. }
  30. func (s *InboundService) runtimeFor(ib *model.Inbound) (runtime.Runtime, error) {
  31. mgr := runtime.GetManager()
  32. if mgr == nil {
  33. return nil, fmt.Errorf("runtime manager not initialised")
  34. }
  35. return mgr.RuntimeFor(ib.NodeID)
  36. }
  37. type CopyClientsResult struct {
  38. Added []string `json:"added"`
  39. Skipped []string `json:"skipped"`
  40. Errors []string `json:"errors"`
  41. }
  42. // enrichClientStats parses each inbound's clients once, fills in the
  43. // UUID/SubId fields on the preloaded ClientStats, and tops up rows owned by
  44. // a sibling inbound (shared-email mode — the row is keyed on email so it
  45. // only preloads on its owning inbound).
  46. func (s *InboundService) enrichClientStats(db *gorm.DB, inbounds []*model.Inbound) {
  47. if len(inbounds) == 0 {
  48. return
  49. }
  50. clientsByInbound := make([][]model.Client, len(inbounds))
  51. seenByInbound := make([]map[string]struct{}, len(inbounds))
  52. missing := make(map[string]struct{})
  53. for i, inbound := range inbounds {
  54. clients, _ := s.GetClients(inbound)
  55. clientsByInbound[i] = clients
  56. seen := make(map[string]struct{}, len(inbound.ClientStats))
  57. for _, st := range inbound.ClientStats {
  58. if st.Email != "" {
  59. seen[strings.ToLower(st.Email)] = struct{}{}
  60. }
  61. }
  62. seenByInbound[i] = seen
  63. for _, c := range clients {
  64. if c.Email == "" {
  65. continue
  66. }
  67. if _, ok := seen[strings.ToLower(c.Email)]; !ok {
  68. missing[c.Email] = struct{}{}
  69. }
  70. }
  71. }
  72. if len(missing) > 0 {
  73. emails := make([]string, 0, len(missing))
  74. for e := range missing {
  75. emails = append(emails, e)
  76. }
  77. var extra []xray.ClientTraffic
  78. var loadErr error
  79. for _, batch := range chunkStrings(emails, sqlInChunk) {
  80. var page []xray.ClientTraffic
  81. if err := db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  82. loadErr = err
  83. break
  84. }
  85. extra = append(extra, page...)
  86. }
  87. if loadErr != nil {
  88. logger.Warning("enrichClientStats:", loadErr)
  89. } else {
  90. byEmail := make(map[string]xray.ClientTraffic, len(extra))
  91. for _, st := range extra {
  92. byEmail[strings.ToLower(st.Email)] = st
  93. }
  94. for i, inbound := range inbounds {
  95. for _, c := range clientsByInbound[i] {
  96. if c.Email == "" {
  97. continue
  98. }
  99. key := strings.ToLower(c.Email)
  100. if _, ok := seenByInbound[i][key]; ok {
  101. continue
  102. }
  103. if st, ok := byEmail[key]; ok {
  104. inbound.ClientStats = append(inbound.ClientStats, st)
  105. seenByInbound[i][key] = struct{}{}
  106. }
  107. }
  108. }
  109. }
  110. }
  111. for i, inbound := range inbounds {
  112. clients := clientsByInbound[i]
  113. if len(clients) == 0 || len(inbound.ClientStats) == 0 {
  114. continue
  115. }
  116. cMap := make(map[string]model.Client, len(clients))
  117. for _, c := range clients {
  118. cMap[strings.ToLower(c.Email)] = c
  119. }
  120. for j := range inbound.ClientStats {
  121. email := strings.ToLower(inbound.ClientStats[j].Email)
  122. if c, ok := cMap[email]; ok {
  123. inbound.ClientStats[j].UUID = c.ID
  124. inbound.ClientStats[j].SubId = c.SubID
  125. }
  126. }
  127. }
  128. }
  129. // GetInbounds retrieves all inbounds for a specific user with client stats.
  130. func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
  131. db := database.GetDB()
  132. var inbounds []*model.Inbound
  133. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Order("id ASC").Find(&inbounds).Error
  134. if err != nil && err != gorm.ErrRecordNotFound {
  135. return nil, err
  136. }
  137. s.enrichClientStats(db, inbounds)
  138. s.annotateFallbackParents(db, inbounds)
  139. return inbounds, nil
  140. }
  141. // GetInboundsSlim returns the same list of inbounds as GetInbounds but
  142. // strips every per-client field other than email / enable / comment from
  143. // settings.clients and skips UUID/SubId enrichment on ClientStats. The
  144. // inbounds page only needs those three to roll up client counts and
  145. // render badges, so this trims tens of bytes per client (UUID, password,
  146. // flow, security, totalGB, expiryTime, limitIp, tgId, ...) which adds
  147. // up fast on installs with thousands of clients.
  148. //
  149. // Full client data is still available through GET /panel/api/inbounds/get/:id
  150. // for the edit/info/qr/export/clone flows that need it.
  151. func (s *InboundService) GetInboundsSlim(userId int) ([]*model.Inbound, error) {
  152. db := database.GetDB()
  153. var inbounds []*model.Inbound
  154. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Order("id ASC").Find(&inbounds).Error
  155. if err != nil && err != gorm.ErrRecordNotFound {
  156. return nil, err
  157. }
  158. s.annotateFallbackParents(db, inbounds)
  159. for _, ib := range inbounds {
  160. ib.Settings = slimSettingsClients(ib.Settings)
  161. }
  162. return inbounds, nil
  163. }
  164. // slimSettingsClients rewrites the inbound settings JSON so settings.clients[]
  165. // keeps only the fields the list view actually reads. Returns the input
  166. // unchanged when the JSON can't be parsed or has no clients array.
  167. func slimSettingsClients(settings string) string {
  168. if settings == "" {
  169. return settings
  170. }
  171. var raw map[string]any
  172. if err := json.Unmarshal([]byte(settings), &raw); err != nil {
  173. return settings
  174. }
  175. clients, ok := raw["clients"].([]any)
  176. if !ok || len(clients) == 0 {
  177. return settings
  178. }
  179. slim := make([]any, 0, len(clients))
  180. for _, entry := range clients {
  181. c, ok := entry.(map[string]any)
  182. if !ok {
  183. continue
  184. }
  185. row := make(map[string]any, 3)
  186. if v, ok := c["email"]; ok {
  187. row["email"] = v
  188. }
  189. if v, ok := c["enable"]; ok {
  190. row["enable"] = v
  191. }
  192. if v, ok := c["comment"]; ok && v != "" {
  193. row["comment"] = v
  194. }
  195. slim = append(slim, row)
  196. }
  197. raw["clients"] = slim
  198. out, err := json.Marshal(raw)
  199. if err != nil {
  200. return settings
  201. }
  202. return string(out)
  203. }
  204. // annotateFallbackParents fills FallbackParent on each inbound that is
  205. // the child side of a fallback rule. One DB round-trip serves the full
  206. // list — the frontend needs this to rewrite the child's client-share
  207. // link so it points at the master's reachable endpoint.
  208. func (s *InboundService) annotateFallbackParents(db *gorm.DB, inbounds []*model.Inbound) {
  209. if len(inbounds) == 0 {
  210. return
  211. }
  212. childIds := make([]int, 0, len(inbounds))
  213. for _, ib := range inbounds {
  214. childIds = append(childIds, ib.Id)
  215. }
  216. var rows []model.InboundFallback
  217. if err := db.Where("child_id IN ?", childIds).
  218. Order("sort_order ASC, id ASC").
  219. Find(&rows).Error; err != nil {
  220. return
  221. }
  222. first := make(map[int]model.InboundFallback, len(rows))
  223. for _, r := range rows {
  224. if _, ok := first[r.ChildId]; !ok {
  225. first[r.ChildId] = r
  226. }
  227. }
  228. for _, ib := range inbounds {
  229. if r, ok := first[ib.Id]; ok {
  230. ib.FallbackParent = &model.FallbackParentInfo{
  231. MasterId: r.MasterId,
  232. Path: r.Path,
  233. }
  234. }
  235. }
  236. }
  237. type InboundOption struct {
  238. Id int `json:"id"`
  239. Remark string `json:"remark"`
  240. Tag string `json:"tag"`
  241. Protocol string `json:"protocol"`
  242. Port int `json:"port"`
  243. TlsFlowCapable bool `json:"tlsFlowCapable"`
  244. }
  245. func (s *InboundService) GetInboundOptions(userId int) ([]InboundOption, error) {
  246. db := database.GetDB()
  247. var rows []struct {
  248. Id int `gorm:"column:id"`
  249. Remark string `gorm:"column:remark"`
  250. Tag string `gorm:"column:tag"`
  251. Protocol string `gorm:"column:protocol"`
  252. Port int `gorm:"column:port"`
  253. StreamSettings string `gorm:"column:stream_settings"`
  254. }
  255. err := db.Table("inbounds").
  256. Select("id, remark, tag, protocol, port, stream_settings").
  257. Where("user_id = ?", userId).
  258. Order("id ASC").
  259. Scan(&rows).Error
  260. if err != nil && err != gorm.ErrRecordNotFound {
  261. return nil, err
  262. }
  263. out := make([]InboundOption, 0, len(rows))
  264. for _, r := range rows {
  265. out = append(out, InboundOption{
  266. Id: r.Id,
  267. Remark: r.Remark,
  268. Tag: r.Tag,
  269. Protocol: r.Protocol,
  270. Port: r.Port,
  271. TlsFlowCapable: inboundCanEnableTlsFlow(r.Protocol, r.StreamSettings),
  272. })
  273. }
  274. return out, nil
  275. }
  276. // inboundCanEnableTlsFlow mirrors Inbound.canEnableTlsFlow() from the frontend:
  277. // XTLS Vision is only valid for VLESS on TCP with tls or reality.
  278. func inboundCanEnableTlsFlow(protocol, streamSettings string) bool {
  279. if protocol != string(model.VLESS) {
  280. return false
  281. }
  282. if streamSettings == "" {
  283. return false
  284. }
  285. var stream struct {
  286. Network string `json:"network"`
  287. Security string `json:"security"`
  288. }
  289. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  290. return false
  291. }
  292. if stream.Network != "tcp" {
  293. return false
  294. }
  295. return stream.Security == "tls" || stream.Security == "reality"
  296. }
  297. // inboundCanHostFallbacks gates the settings.fallbacks injection.
  298. // Xray only honors fallbacks on VLESS and Trojan inbounds carried over
  299. // TCP transport with TLS or Reality security.
  300. func inboundCanHostFallbacks(ib *model.Inbound) bool {
  301. if ib == nil {
  302. return false
  303. }
  304. if ib.Protocol != model.VLESS && ib.Protocol != model.Trojan {
  305. return false
  306. }
  307. return inboundCanEnableTlsFlow(string(ib.Protocol), ib.StreamSettings) ||
  308. (ib.Protocol == model.Trojan && trojanStreamSupportsFallbacks(ib.StreamSettings))
  309. }
  310. // trojanStreamSupportsFallbacks mirrors the Trojan side of the same gate
  311. // (Trojan reuses XTLS-Vision capable streams: tcp + tls or reality).
  312. func trojanStreamSupportsFallbacks(streamSettings string) bool {
  313. if streamSettings == "" {
  314. return false
  315. }
  316. var stream struct {
  317. Network string `json:"network"`
  318. Security string `json:"security"`
  319. }
  320. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  321. return false
  322. }
  323. if stream.Network != "tcp" {
  324. return false
  325. }
  326. return stream.Security == "tls" || stream.Security == "reality"
  327. }
  328. // GetAllInbounds retrieves all inbounds with client stats.
  329. func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) {
  330. db := database.GetDB()
  331. var inbounds []*model.Inbound
  332. err := db.Model(model.Inbound{}).Preload("ClientStats").Find(&inbounds).Error
  333. if err != nil && err != gorm.ErrRecordNotFound {
  334. return nil, err
  335. }
  336. s.enrichClientStats(db, inbounds)
  337. return inbounds, nil
  338. }
  339. func (s *InboundService) GetInboundsByTrafficReset(period string) ([]*model.Inbound, error) {
  340. db := database.GetDB()
  341. var inbounds []*model.Inbound
  342. err := db.Model(model.Inbound{}).Where("traffic_reset = ?", period).Find(&inbounds).Error
  343. if err != nil && err != gorm.ErrRecordNotFound {
  344. return nil, err
  345. }
  346. return inbounds, nil
  347. }
  348. func (s *InboundService) GetClients(inbound *model.Inbound) ([]model.Client, error) {
  349. settings := map[string][]model.Client{}
  350. json.Unmarshal([]byte(inbound.Settings), &settings)
  351. if settings == nil {
  352. return nil, fmt.Errorf("setting is null")
  353. }
  354. clients := settings["clients"]
  355. if clients == nil {
  356. return nil, nil
  357. }
  358. return clients, nil
  359. }
  360. func (s *InboundService) getAllEmails() ([]string, error) {
  361. db := database.GetDB()
  362. var emails []string
  363. query := fmt.Sprintf(
  364. "SELECT DISTINCT %s %s",
  365. database.JSONFieldText("client.value", "email"),
  366. database.JSONClientsFromInbound(),
  367. )
  368. if err := db.Raw(query).Scan(&emails).Error; err != nil {
  369. return nil, err
  370. }
  371. return emails, nil
  372. }
  373. // getAllEmailSubIDs returns email→subId. An email seen with two different
  374. // non-empty subIds is locked (mapped to "") so neither identity can claim it.
  375. func (s *InboundService) getAllEmailSubIDs() (map[string]string, error) {
  376. db := database.GetDB()
  377. var rows []struct {
  378. Email string
  379. SubID string
  380. }
  381. query := fmt.Sprintf(
  382. "SELECT %s AS email, %s AS sub_id %s",
  383. database.JSONFieldText("client.value", "email"),
  384. database.JSONFieldText("client.value", "subId"),
  385. database.JSONClientsFromInbound(),
  386. )
  387. if err := db.Raw(query).Scan(&rows).Error; err != nil {
  388. return nil, err
  389. }
  390. result := make(map[string]string, len(rows))
  391. for _, r := range rows {
  392. email := strings.ToLower(r.Email)
  393. if email == "" {
  394. continue
  395. }
  396. subID := r.SubID
  397. if existing, ok := result[email]; ok {
  398. if existing != subID {
  399. result[email] = ""
  400. }
  401. continue
  402. }
  403. result[email] = subID
  404. }
  405. return result, nil
  406. }
  407. // emailUsedByOtherInbounds reports whether email lives in any inbound other
  408. // than exceptInboundId. Empty email returns false.
  409. func (s *InboundService) emailUsedByOtherInbounds(email string, exceptInboundId int) (bool, error) {
  410. if email == "" {
  411. return false, nil
  412. }
  413. db := database.GetDB()
  414. var count int64
  415. query := fmt.Sprintf(
  416. "SELECT COUNT(*) %s WHERE inbounds.id != ? AND LOWER(%s) = LOWER(?)",
  417. database.JSONClientsFromInbound(),
  418. database.JSONFieldText("client.value", "email"),
  419. )
  420. if err := db.Raw(query, exceptInboundId, email).Scan(&count).Error; err != nil {
  421. return false, err
  422. }
  423. return count > 0, nil
  424. }
  425. func (s *InboundService) emailsUsedByOtherInbounds(emails []string, exceptInboundId int) (map[string]bool, error) {
  426. shared := make(map[string]bool, len(emails))
  427. want := make(map[string]struct{}, len(emails))
  428. for _, e := range emails {
  429. e = strings.ToLower(strings.TrimSpace(e))
  430. if e != "" {
  431. want[e] = struct{}{}
  432. }
  433. }
  434. if len(want) == 0 {
  435. return shared, nil
  436. }
  437. db := database.GetDB()
  438. var rows []string
  439. query := fmt.Sprintf(
  440. "SELECT DISTINCT LOWER(%s) %s WHERE inbounds.id != ?",
  441. database.JSONFieldText("client.value", "email"),
  442. database.JSONClientsFromInbound(),
  443. )
  444. if err := db.Raw(query, exceptInboundId).Scan(&rows).Error; err != nil {
  445. return nil, err
  446. }
  447. for _, e := range rows {
  448. e = strings.ToLower(strings.TrimSpace(e))
  449. if _, ok := want[e]; ok {
  450. shared[e] = true
  451. }
  452. }
  453. return shared, nil
  454. }
  455. // normalizeStreamSettings clears StreamSettings for protocols that don't use it.
  456. // Only vmess, vless, trojan, shadowsocks, and hysteria protocols use streamSettings.
  457. func (s *InboundService) normalizeStreamSettings(inbound *model.Inbound) {
  458. protocolsWithStream := map[model.Protocol]bool{
  459. model.VMESS: true,
  460. model.VLESS: true,
  461. model.Trojan: true,
  462. model.Shadowsocks: true,
  463. model.Hysteria: true,
  464. }
  465. if !protocolsWithStream[inbound.Protocol] {
  466. inbound.StreamSettings = ""
  467. }
  468. }
  469. // AddInbound creates a new inbound configuration.
  470. // It validates port uniqueness, client email uniqueness, and required fields,
  471. // then saves the inbound to the database and optionally adds it to the running Xray instance.
  472. // Returns the created inbound, whether Xray needs restart, and any error.
  473. func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
  474. // Normalize streamSettings based on protocol
  475. s.normalizeStreamSettings(inbound)
  476. conflict, err := s.checkPortConflict(inbound, 0)
  477. if err != nil {
  478. return inbound, false, err
  479. }
  480. if conflict != nil {
  481. return inbound, false, common.NewError(conflict.String())
  482. }
  483. inbound.Tag, err = s.resolveInboundTag(inbound, 0)
  484. if err != nil {
  485. return inbound, false, err
  486. }
  487. clients, err := s.GetClients(inbound)
  488. if err != nil {
  489. return inbound, false, err
  490. }
  491. existEmail, err := s.clientService.checkEmailsExistForClients(s, clients, nil)
  492. if err != nil {
  493. return inbound, false, err
  494. }
  495. if existEmail != "" {
  496. return inbound, false, common.NewError("Duplicate email:", existEmail)
  497. }
  498. // Ensure created_at and updated_at on clients in settings
  499. if len(clients) > 0 {
  500. var settings map[string]any
  501. if err2 := json.Unmarshal([]byte(inbound.Settings), &settings); err2 == nil && settings != nil {
  502. now := time.Now().Unix() * 1000
  503. updatedClients := make([]model.Client, 0, len(clients))
  504. for _, c := range clients {
  505. if c.CreatedAt == 0 {
  506. c.CreatedAt = now
  507. }
  508. c.UpdatedAt = now
  509. updatedClients = append(updatedClients, c)
  510. }
  511. settings["clients"] = updatedClients
  512. if bs, err3 := json.MarshalIndent(settings, "", " "); err3 == nil {
  513. inbound.Settings = string(bs)
  514. } else {
  515. logger.Debug("Unable to marshal inbound settings with timestamps:", err3)
  516. }
  517. } else if err2 != nil {
  518. logger.Debug("Unable to parse inbound settings for timestamps:", err2)
  519. }
  520. }
  521. // Secure client ID
  522. for _, client := range clients {
  523. switch inbound.Protocol {
  524. case "trojan":
  525. if client.Password == "" {
  526. return inbound, false, common.NewError("empty client ID")
  527. }
  528. case "shadowsocks":
  529. if client.Email == "" {
  530. return inbound, false, common.NewError("empty client ID")
  531. }
  532. case "hysteria":
  533. if client.Auth == "" {
  534. return inbound, false, common.NewError("empty client ID")
  535. }
  536. default:
  537. if client.ID == "" {
  538. return inbound, false, common.NewError("empty client ID")
  539. }
  540. }
  541. }
  542. db := database.GetDB()
  543. tx := db.Begin()
  544. defer func() {
  545. if err == nil {
  546. tx.Commit()
  547. } else {
  548. tx.Rollback()
  549. }
  550. }()
  551. err = tx.Save(inbound).Error
  552. if err == nil {
  553. if len(inbound.ClientStats) == 0 {
  554. for _, client := range clients {
  555. s.AddClientStat(tx, inbound.Id, &client)
  556. }
  557. }
  558. } else {
  559. return inbound, false, err
  560. }
  561. if err = s.clientService.SyncInbound(tx, inbound.Id, clients); err != nil {
  562. return inbound, false, err
  563. }
  564. needRestart := false
  565. if inbound.Enable {
  566. rt, rterr := s.runtimeFor(inbound)
  567. if rterr != nil {
  568. err = rterr
  569. return inbound, false, err
  570. }
  571. if err1 := rt.AddInbound(context.Background(), inbound); err1 == nil {
  572. logger.Debug("New inbound added on", rt.Name(), ":", inbound.Tag)
  573. } else {
  574. logger.Debug("Unable to add inbound on", rt.Name(), ":", err1)
  575. if inbound.NodeID != nil {
  576. err = err1
  577. return inbound, false, err
  578. }
  579. needRestart = true
  580. }
  581. }
  582. return inbound, needRestart, err
  583. }
  584. func (s *InboundService) DelInbound(id int) (bool, error) {
  585. db := database.GetDB()
  586. needRestart := false
  587. var ib model.Inbound
  588. loadErr := db.Model(model.Inbound{}).Where("id = ?", id).First(&ib).Error
  589. if loadErr == nil {
  590. shouldPushToRuntime := ib.NodeID != nil || ib.Enable
  591. if shouldPushToRuntime {
  592. rt, rterr := s.runtimeFor(&ib)
  593. if rterr != nil {
  594. logger.Warning("DelInbound: runtime lookup failed, deleting central row anyway:", rterr)
  595. if ib.NodeID == nil {
  596. needRestart = true
  597. }
  598. } else if err1 := rt.DelInbound(context.Background(), &ib); err1 == nil {
  599. logger.Debug("Inbound deleted on", rt.Name(), ":", ib.Tag)
  600. } else {
  601. logger.Warning("DelInbound on", rt.Name(), "failed, deleting central row anyway:", err1)
  602. if ib.NodeID == nil {
  603. needRestart = true
  604. }
  605. }
  606. } else {
  607. logger.Debug("DelInbound: skipping runtime push for disabled local inbound id:", id)
  608. }
  609. } else {
  610. logger.Debug("DelInbound: inbound not found, id:", id)
  611. }
  612. if err := s.clientService.DetachInbound(db, id); err != nil {
  613. return false, err
  614. }
  615. if err := db.Delete(model.Inbound{}, id).Error; err != nil {
  616. return needRestart, err
  617. }
  618. if !database.IsPostgres() {
  619. var count int64
  620. if err := db.Model(&model.Inbound{}).Count(&count).Error; err != nil {
  621. return needRestart, err
  622. }
  623. if count == 0 {
  624. if err := db.Exec("DELETE FROM sqlite_sequence WHERE name = ?", "inbounds").Error; err != nil {
  625. return needRestart, err
  626. }
  627. }
  628. }
  629. return needRestart, nil
  630. }
  631. type BulkDelInboundResult struct {
  632. Deleted int `json:"deleted"`
  633. Skipped []BulkDelInboundReport `json:"skipped,omitempty"`
  634. }
  635. type BulkDelInboundReport struct {
  636. Id int `json:"id"`
  637. Reason string `json:"reason"`
  638. }
  639. // DelInbounds removes every inbound in the list, reusing the single-delete
  640. // path per id. Failures are recorded in Skipped and processing continues for
  641. // the rest; the aggregated needRestart is returned so the caller restarts
  642. // xray at most once.
  643. func (s *InboundService) DelInbounds(ids []int) (BulkDelInboundResult, bool, error) {
  644. result := BulkDelInboundResult{}
  645. needRestart := false
  646. for _, id := range ids {
  647. r, err := s.DelInbound(id)
  648. if err != nil {
  649. result.Skipped = append(result.Skipped, BulkDelInboundReport{Id: id, Reason: err.Error()})
  650. continue
  651. }
  652. result.Deleted++
  653. if r {
  654. needRestart = true
  655. }
  656. }
  657. return result, needRestart, nil
  658. }
  659. func (s *InboundService) GetInbound(id int) (*model.Inbound, error) {
  660. db := database.GetDB()
  661. inbound := &model.Inbound{}
  662. err := db.Model(model.Inbound{}).First(inbound, id).Error
  663. if err != nil {
  664. return nil, err
  665. }
  666. return inbound, nil
  667. }
  668. func (s *InboundService) GetInboundDetail(id int) (*model.Inbound, error) {
  669. db := database.GetDB()
  670. inbound := &model.Inbound{}
  671. err := db.Model(model.Inbound{}).Preload("ClientStats").First(inbound, id).Error
  672. if err != nil {
  673. return nil, err
  674. }
  675. s.enrichClientStats(db, []*model.Inbound{inbound})
  676. return inbound, nil
  677. }
  678. func (s *InboundService) SetInboundEnable(id int, enable bool) (bool, error) {
  679. inbound, err := s.GetInbound(id)
  680. if err != nil {
  681. return false, err
  682. }
  683. if inbound.Enable == enable {
  684. return false, nil
  685. }
  686. db := database.GetDB()
  687. if err := db.Model(model.Inbound{}).Where("id = ?", id).
  688. Update("enable", enable).Error; err != nil {
  689. return false, err
  690. }
  691. inbound.Enable = enable
  692. needRestart := false
  693. rt, rterr := s.runtimeFor(inbound)
  694. if rterr != nil {
  695. if inbound.NodeID != nil {
  696. return false, rterr
  697. }
  698. return true, nil
  699. }
  700. // Remote nodes interpret DelInbound as a real row delete (it hits
  701. // panel/api/inbounds/del/:id on the remote), so toggling the enable
  702. // switch on a remote inbound used to wipe the row entirely (#4402).
  703. // PATCH the remote row via UpdateInbound instead — preserves the
  704. // settings/client history and just flips the enable flag.
  705. if inbound.NodeID != nil {
  706. if err := rt.UpdateInbound(context.Background(), inbound, inbound); err != nil {
  707. logger.Debug("SetInboundEnable: remote UpdateInbound on", rt.Name(), "failed:", err)
  708. return false, err
  709. }
  710. return false, nil
  711. }
  712. if err := rt.DelInbound(context.Background(), inbound); err != nil &&
  713. !strings.Contains(err.Error(), "not found") {
  714. logger.Debug("SetInboundEnable: DelInbound on", rt.Name(), "failed:", err)
  715. needRestart = true
  716. }
  717. if !enable {
  718. return needRestart, nil
  719. }
  720. runtimeInbound, err := s.buildRuntimeInboundForAPI(db, inbound)
  721. if err != nil {
  722. logger.Debug("SetInboundEnable: build runtime config failed:", err)
  723. return true, nil
  724. }
  725. if err := rt.AddInbound(context.Background(), runtimeInbound); err != nil {
  726. logger.Debug("SetInboundEnable: AddInbound on", rt.Name(), "failed:", err)
  727. needRestart = true
  728. }
  729. return needRestart, nil
  730. }
  731. func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
  732. // Normalize streamSettings based on protocol
  733. s.normalizeStreamSettings(inbound)
  734. conflict, err := s.checkPortConflict(inbound, inbound.Id)
  735. if err != nil {
  736. return inbound, false, err
  737. }
  738. if conflict != nil {
  739. return inbound, false, common.NewError(conflict.String())
  740. }
  741. oldInbound, err := s.GetInbound(inbound.Id)
  742. if err != nil {
  743. return inbound, false, err
  744. }
  745. inbound.NodeID = oldInbound.NodeID
  746. tag := oldInbound.Tag
  747. oldBits := inboundTransports(oldInbound.Protocol, oldInbound.StreamSettings, oldInbound.Settings)
  748. oldTagWasAuto := isAutoGeneratedTag(tag, oldInbound.Port, oldInbound.NodeID, oldBits)
  749. db := database.GetDB()
  750. tx := db.Begin()
  751. defer func() {
  752. if err != nil {
  753. tx.Rollback()
  754. } else {
  755. tx.Commit()
  756. }
  757. }()
  758. err = s.updateClientTraffics(tx, oldInbound, inbound)
  759. if err != nil {
  760. return inbound, false, err
  761. }
  762. // Ensure created_at and updated_at exist in inbound.Settings clients
  763. {
  764. var oldSettings map[string]any
  765. _ = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
  766. emailToCreated := map[string]int64{}
  767. emailToUpdated := map[string]int64{}
  768. if oldSettings != nil {
  769. if oc, ok := oldSettings["clients"].([]any); ok {
  770. for _, it := range oc {
  771. if m, ok2 := it.(map[string]any); ok2 {
  772. if email, ok3 := m["email"].(string); ok3 {
  773. switch v := m["created_at"].(type) {
  774. case float64:
  775. emailToCreated[email] = int64(v)
  776. case int64:
  777. emailToCreated[email] = v
  778. }
  779. switch v := m["updated_at"].(type) {
  780. case float64:
  781. emailToUpdated[email] = int64(v)
  782. case int64:
  783. emailToUpdated[email] = v
  784. }
  785. }
  786. }
  787. }
  788. }
  789. }
  790. var newSettings map[string]any
  791. if err2 := json.Unmarshal([]byte(inbound.Settings), &newSettings); err2 == nil && newSettings != nil {
  792. now := time.Now().Unix() * 1000
  793. if nSlice, ok := newSettings["clients"].([]any); ok {
  794. for i := range nSlice {
  795. if m, ok2 := nSlice[i].(map[string]any); ok2 {
  796. email, _ := m["email"].(string)
  797. if _, ok3 := m["created_at"]; !ok3 {
  798. if v, ok4 := emailToCreated[email]; ok4 && v > 0 {
  799. m["created_at"] = v
  800. } else {
  801. m["created_at"] = now
  802. }
  803. }
  804. // Preserve client's updated_at if present; do not bump on parent inbound update
  805. if _, hasUpdated := m["updated_at"]; !hasUpdated {
  806. if v, ok4 := emailToUpdated[email]; ok4 && v > 0 {
  807. m["updated_at"] = v
  808. }
  809. }
  810. nSlice[i] = m
  811. }
  812. }
  813. newSettings["clients"] = nSlice
  814. if bs, err3 := json.MarshalIndent(newSettings, "", " "); err3 == nil {
  815. inbound.Settings = string(bs)
  816. }
  817. }
  818. }
  819. }
  820. oldInbound.Total = inbound.Total
  821. oldInbound.Remark = inbound.Remark
  822. oldInbound.Enable = inbound.Enable
  823. oldInbound.ExpiryTime = inbound.ExpiryTime
  824. oldInbound.TrafficReset = inbound.TrafficReset
  825. oldInbound.Listen = inbound.Listen
  826. oldInbound.Port = inbound.Port
  827. oldInbound.Protocol = inbound.Protocol
  828. oldInbound.Settings = inbound.Settings
  829. oldInbound.StreamSettings = inbound.StreamSettings
  830. oldInbound.Sniffing = inbound.Sniffing
  831. if oldTagWasAuto && inbound.Tag == tag {
  832. inbound.Tag = ""
  833. }
  834. oldInbound.Tag, err = s.resolveInboundTag(inbound, inbound.Id)
  835. if err != nil {
  836. return inbound, false, err
  837. }
  838. inbound.Tag = oldInbound.Tag
  839. needRestart := false
  840. rt, rterr := s.runtimeFor(oldInbound)
  841. if rterr != nil {
  842. if oldInbound.NodeID != nil {
  843. err = rterr
  844. return inbound, false, err
  845. }
  846. needRestart = true
  847. } else {
  848. oldSnapshot := *oldInbound
  849. oldSnapshot.Tag = tag
  850. if oldInbound.NodeID == nil {
  851. if err2 := rt.DelInbound(context.Background(), &oldSnapshot); err2 == nil {
  852. logger.Debug("Old inbound deleted on", rt.Name(), ":", tag)
  853. }
  854. if inbound.Enable {
  855. runtimeInbound, err2 := s.buildRuntimeInboundForAPI(tx, oldInbound)
  856. if err2 != nil {
  857. logger.Debug("Unable to prepare runtime inbound config:", err2)
  858. needRestart = true
  859. } else if err2 := rt.AddInbound(context.Background(), runtimeInbound); err2 == nil {
  860. logger.Debug("Updated inbound added on", rt.Name(), ":", oldInbound.Tag)
  861. } else {
  862. logger.Debug("Unable to update inbound on", rt.Name(), ":", err2)
  863. needRestart = true
  864. }
  865. }
  866. } else {
  867. if !inbound.Enable {
  868. if err2 := rt.DelInbound(context.Background(), &oldSnapshot); err2 != nil {
  869. err = err2
  870. return inbound, false, err
  871. }
  872. } else if err2 := rt.UpdateInbound(context.Background(), &oldSnapshot, oldInbound); err2 != nil {
  873. err = err2
  874. return inbound, false, err
  875. }
  876. }
  877. }
  878. if err = tx.Save(oldInbound).Error; err != nil {
  879. return inbound, false, err
  880. }
  881. newClients, gcErr := s.GetClients(oldInbound)
  882. if gcErr != nil {
  883. err = gcErr
  884. return inbound, false, err
  885. }
  886. if err = s.clientService.SyncInbound(tx, oldInbound.Id, newClients); err != nil {
  887. return inbound, false, err
  888. }
  889. return inbound, needRestart, nil
  890. }
  891. func (s *InboundService) buildRuntimeInboundForAPI(tx *gorm.DB, inbound *model.Inbound) (*model.Inbound, error) {
  892. if inbound == nil {
  893. return nil, fmt.Errorf("inbound is nil")
  894. }
  895. runtimeInbound := *inbound
  896. settings := map[string]any{}
  897. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  898. return nil, err
  899. }
  900. clients, ok := settings["clients"].([]any)
  901. if !ok {
  902. return &runtimeInbound, nil
  903. }
  904. var clientStats []xray.ClientTraffic
  905. err := tx.Model(xray.ClientTraffic{}).
  906. Where("inbound_id = ?", inbound.Id).
  907. Select("email", "enable").
  908. Find(&clientStats).Error
  909. if err != nil {
  910. return nil, err
  911. }
  912. enableMap := make(map[string]bool, len(clientStats))
  913. for _, clientTraffic := range clientStats {
  914. enableMap[clientTraffic.Email] = clientTraffic.Enable
  915. }
  916. finalClients := make([]any, 0, len(clients))
  917. for _, client := range clients {
  918. c, ok := client.(map[string]any)
  919. if !ok {
  920. continue
  921. }
  922. email, _ := c["email"].(string)
  923. if enable, exists := enableMap[email]; exists && !enable {
  924. continue
  925. }
  926. if manualEnable, ok := c["enable"].(bool); ok && !manualEnable {
  927. continue
  928. }
  929. finalClients = append(finalClients, c)
  930. }
  931. settings["clients"] = finalClients
  932. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  933. if err != nil {
  934. return nil, err
  935. }
  936. runtimeInbound.Settings = string(modifiedSettings)
  937. return &runtimeInbound, nil
  938. }
  939. // updateClientTraffics syncs the ClientTraffic rows with the inbound's clients
  940. // list: removes rows for emails that disappeared, inserts rows for newly-added
  941. // emails. Uses sets for O(N) lookup — the previous nested-loop implementation
  942. // was O(N²) and degraded into multi-second pauses on inbounds with thousands
  943. // of clients (toggling, saving, or deleting any such inbound felt frozen).
  944. func (s *InboundService) updateClientTraffics(tx *gorm.DB, oldInbound *model.Inbound, newInbound *model.Inbound) error {
  945. oldClients, err := s.GetClients(oldInbound)
  946. if err != nil {
  947. return err
  948. }
  949. newClients, err := s.GetClients(newInbound)
  950. if err != nil {
  951. return err
  952. }
  953. // Email is the unique key for ClientTraffic rows. Clients without an
  954. // email have no stats row to sync — skip them on both sides instead of
  955. // risking a unique-constraint hit or accidental delete of an unrelated row.
  956. oldEmails := make(map[string]struct{}, len(oldClients))
  957. for i := range oldClients {
  958. if oldClients[i].Email == "" {
  959. continue
  960. }
  961. oldEmails[oldClients[i].Email] = struct{}{}
  962. }
  963. newEmails := make(map[string]struct{}, len(newClients))
  964. for i := range newClients {
  965. if newClients[i].Email == "" {
  966. continue
  967. }
  968. newEmails[newClients[i].Email] = struct{}{}
  969. }
  970. // Drop stats rows for removed emails — but not when a sibling inbound
  971. // still references the email, since the row is the shared accumulator.
  972. for i := range oldClients {
  973. email := oldClients[i].Email
  974. if email == "" {
  975. continue
  976. }
  977. if _, kept := newEmails[email]; kept {
  978. continue
  979. }
  980. stillUsed, err := s.emailUsedByOtherInbounds(email, oldInbound.Id)
  981. if err != nil {
  982. return err
  983. }
  984. if stillUsed {
  985. continue
  986. }
  987. if err := s.DelClientStat(tx, email); err != nil {
  988. return err
  989. }
  990. }
  991. for i := range newClients {
  992. email := newClients[i].Email
  993. if email == "" {
  994. continue
  995. }
  996. if _, existed := oldEmails[email]; existed {
  997. if err := s.UpdateClientStat(tx, email, &newClients[i]); err != nil {
  998. return err
  999. }
  1000. continue
  1001. }
  1002. if err := s.AddClientStat(tx, oldInbound.Id, &newClients[i]); err != nil {
  1003. return err
  1004. }
  1005. }
  1006. return nil
  1007. }
  1008. func (s *InboundService) getClientPrimaryKey(protocol model.Protocol, client model.Client) string {
  1009. switch protocol {
  1010. case model.Trojan:
  1011. return client.Password
  1012. case model.Shadowsocks:
  1013. return client.Email
  1014. case model.Hysteria:
  1015. return client.Auth
  1016. default:
  1017. return client.ID
  1018. }
  1019. }
  1020. func (s *InboundService) writeBackClientSubID(sourceInboundID int, sourceProtocol model.Protocol, client model.Client, subID string) (bool, error) {
  1021. client.SubID = subID
  1022. client.UpdatedAt = time.Now().UnixMilli()
  1023. clientID := s.getClientPrimaryKey(sourceProtocol, client)
  1024. if clientID == "" {
  1025. return false, common.NewError("empty client ID")
  1026. }
  1027. settingsBytes, err := json.Marshal(map[string][]model.Client{
  1028. "clients": {client},
  1029. })
  1030. if err != nil {
  1031. return false, err
  1032. }
  1033. updatePayload := &model.Inbound{
  1034. Id: sourceInboundID,
  1035. Settings: string(settingsBytes),
  1036. }
  1037. return s.clientService.UpdateInboundClient(s, updatePayload, clientID)
  1038. }
  1039. func (s *InboundService) generateRandomCredential(targetProtocol model.Protocol) string {
  1040. switch targetProtocol {
  1041. case model.VMESS, model.VLESS:
  1042. return uuid.NewString()
  1043. default:
  1044. return strings.ReplaceAll(uuid.NewString(), "-", "")
  1045. }
  1046. }
  1047. func (s *InboundService) buildTargetClientFromSource(source model.Client, targetInbound *model.Inbound, email string, flow string) (model.Client, error) {
  1048. nowTs := time.Now().UnixMilli()
  1049. target := source
  1050. target.Email = email
  1051. target.CreatedAt = nowTs
  1052. target.UpdatedAt = nowTs
  1053. target.ID = ""
  1054. target.Password = ""
  1055. target.Auth = ""
  1056. target.Flow = ""
  1057. targetProtocol := targetInbound.Protocol
  1058. switch targetProtocol {
  1059. case model.VMESS:
  1060. target.ID = s.generateRandomCredential(targetProtocol)
  1061. case model.VLESS:
  1062. target.ID = s.generateRandomCredential(targetProtocol)
  1063. if (flow == "xtls-rprx-vision" || flow == "xtls-rprx-vision-udp443") &&
  1064. inboundCanEnableTlsFlow(string(targetProtocol), targetInbound.StreamSettings) {
  1065. target.Flow = flow
  1066. }
  1067. case model.Trojan, model.Shadowsocks:
  1068. target.Password = s.generateRandomCredential(targetProtocol)
  1069. case model.Hysteria:
  1070. target.Auth = s.generateRandomCredential(targetProtocol)
  1071. default:
  1072. target.ID = s.generateRandomCredential(targetProtocol)
  1073. }
  1074. return target, nil
  1075. }
  1076. func (s *InboundService) nextAvailableCopiedEmail(originalEmail string, targetID int, occupied map[string]struct{}) string {
  1077. base := fmt.Sprintf("%s_%d", originalEmail, targetID)
  1078. candidate := base
  1079. suffix := 0
  1080. for {
  1081. if _, exists := occupied[strings.ToLower(candidate)]; !exists {
  1082. occupied[strings.ToLower(candidate)] = struct{}{}
  1083. return candidate
  1084. }
  1085. suffix++
  1086. candidate = fmt.Sprintf("%s_%d", base, suffix)
  1087. }
  1088. }
  1089. func (s *InboundService) CopyInboundClients(targetInboundID int, sourceInboundID int, clientEmails []string, flow string) (*CopyClientsResult, bool, error) {
  1090. result := &CopyClientsResult{
  1091. Added: []string{},
  1092. Skipped: []string{},
  1093. Errors: []string{},
  1094. }
  1095. if targetInboundID == sourceInboundID {
  1096. return result, false, common.NewError("source and target inbounds must be different")
  1097. }
  1098. targetInbound, err := s.GetInbound(targetInboundID)
  1099. if err != nil {
  1100. return result, false, err
  1101. }
  1102. sourceInbound, err := s.GetInbound(sourceInboundID)
  1103. if err != nil {
  1104. return result, false, err
  1105. }
  1106. sourceClients, err := s.GetClients(sourceInbound)
  1107. if err != nil {
  1108. return result, false, err
  1109. }
  1110. if len(sourceClients) == 0 {
  1111. return result, false, nil
  1112. }
  1113. allowedEmails := map[string]struct{}{}
  1114. if len(clientEmails) > 0 {
  1115. for _, email := range clientEmails {
  1116. allowedEmails[strings.ToLower(strings.TrimSpace(email))] = struct{}{}
  1117. }
  1118. }
  1119. occupiedEmails := map[string]struct{}{}
  1120. allEmails, err := s.getAllEmails()
  1121. if err != nil {
  1122. return result, false, err
  1123. }
  1124. for _, email := range allEmails {
  1125. clean := strings.Trim(email, "\"")
  1126. if clean != "" {
  1127. occupiedEmails[strings.ToLower(clean)] = struct{}{}
  1128. }
  1129. }
  1130. newClients := make([]model.Client, 0)
  1131. needRestart := false
  1132. for _, sourceClient := range sourceClients {
  1133. originalEmail := strings.TrimSpace(sourceClient.Email)
  1134. if originalEmail == "" {
  1135. continue
  1136. }
  1137. if len(allowedEmails) > 0 {
  1138. if _, ok := allowedEmails[strings.ToLower(originalEmail)]; !ok {
  1139. continue
  1140. }
  1141. }
  1142. if sourceClient.SubID == "" {
  1143. newSubID := uuid.NewString()
  1144. subNeedRestart, subErr := s.writeBackClientSubID(sourceInbound.Id, sourceInbound.Protocol, sourceClient, newSubID)
  1145. if subErr != nil {
  1146. result.Errors = append(result.Errors, fmt.Sprintf("%s: failed to write source subId: %v", originalEmail, subErr))
  1147. continue
  1148. }
  1149. if subNeedRestart {
  1150. needRestart = true
  1151. }
  1152. sourceClient.SubID = newSubID
  1153. }
  1154. targetEmail := s.nextAvailableCopiedEmail(originalEmail, targetInboundID, occupiedEmails)
  1155. targetClient, buildErr := s.buildTargetClientFromSource(sourceClient, targetInbound, targetEmail, flow)
  1156. if buildErr != nil {
  1157. result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", originalEmail, buildErr))
  1158. continue
  1159. }
  1160. newClients = append(newClients, targetClient)
  1161. result.Added = append(result.Added, targetEmail)
  1162. }
  1163. if len(newClients) == 0 {
  1164. return result, needRestart, nil
  1165. }
  1166. settingsPayload, err := json.Marshal(map[string][]model.Client{
  1167. "clients": newClients,
  1168. })
  1169. if err != nil {
  1170. return result, needRestart, err
  1171. }
  1172. addNeedRestart, err := s.clientService.AddInboundClient(s, &model.Inbound{
  1173. Id: targetInboundID,
  1174. Settings: string(settingsPayload),
  1175. })
  1176. if err != nil {
  1177. return result, needRestart, err
  1178. }
  1179. if addNeedRestart {
  1180. needRestart = true
  1181. }
  1182. return result, needRestart, nil
  1183. }
  1184. const resetGracePeriodMs int64 = 30000
  1185. // onlineGracePeriodMs must comfortably exceed the 5s traffic-poll interval —
  1186. // Xray's stats counters often report a zero delta for an active session across
  1187. // a single poll, so a 5s grace would still drop the client on the next tick.
  1188. // ~4 polls of slack keeps idle-but-connected clients visible without lingering
  1189. // long after a real disconnect.
  1190. const onlineGracePeriodMs int64 = 20000
  1191. type nodeTrafficCounter struct {
  1192. Up int64
  1193. Down int64
  1194. }
  1195. func (s *InboundService) upsertNodeBaseline(tx *gorm.DB, nodeID int, email string, up, down int64) error {
  1196. return tx.Clauses(clause.OnConflict{
  1197. Columns: []clause.Column{{Name: "node_id"}, {Name: "email"}},
  1198. DoUpdates: clause.AssignmentColumns([]string{"up", "down"}),
  1199. }).Create(&model.NodeClientTraffic{NodeId: nodeID, Email: email, Up: up, Down: down}).Error
  1200. }
  1201. func (s *InboundService) SetRemoteTraffic(nodeID int, snap *runtime.TrafficSnapshot) (bool, error) {
  1202. var structuralChange bool
  1203. err := submitTrafficWrite(func() error {
  1204. var inner error
  1205. structuralChange, inner = s.setRemoteTrafficLocked(nodeID, snap)
  1206. return inner
  1207. })
  1208. return structuralChange, err
  1209. }
  1210. func (s *InboundService) setRemoteTrafficLocked(nodeID int, snap *runtime.TrafficSnapshot) (bool, error) {
  1211. if snap == nil || nodeID <= 0 {
  1212. return false, nil
  1213. }
  1214. db := database.GetDB()
  1215. now := time.Now().UnixMilli()
  1216. var central []model.Inbound
  1217. if err := db.Model(model.Inbound{}).
  1218. Where("node_id = ?", nodeID).
  1219. Find(&central).Error; err != nil {
  1220. return false, err
  1221. }
  1222. // Index under the stored tag and its prefix-flipped form so a snap matches
  1223. // whether the n<id>- prefix lives on the node side, the central side, or
  1224. // neither — a mismatch must never spawn a duplicate central inbound.
  1225. tagToCentral := make(map[string]*model.Inbound, len(central)*2)
  1226. prefix := nodeTagPrefix(&nodeID)
  1227. for i := range central {
  1228. tagToCentral[central[i].Tag] = &central[i]
  1229. if prefix != "" {
  1230. if stripped, found := strings.CutPrefix(central[i].Tag, prefix); found {
  1231. tagToCentral[stripped] = &central[i]
  1232. } else {
  1233. tagToCentral[prefix+central[i].Tag] = &central[i]
  1234. }
  1235. }
  1236. }
  1237. var centralClientStats []xray.ClientTraffic
  1238. if len(central) > 0 {
  1239. ids := make([]int, 0, len(central))
  1240. for i := range central {
  1241. ids = append(ids, central[i].Id)
  1242. }
  1243. if err := db.Model(xray.ClientTraffic{}).
  1244. Where("inbound_id IN ?", ids).
  1245. Find(&centralClientStats).Error; err != nil {
  1246. return false, err
  1247. }
  1248. }
  1249. type csKey struct {
  1250. inboundID int
  1251. email string
  1252. }
  1253. centralCS := make(map[csKey]*xray.ClientTraffic, len(centralClientStats))
  1254. centralCSByEmail := make(map[string]*xray.ClientTraffic, len(centralClientStats))
  1255. for i := range centralClientStats {
  1256. centralCS[csKey{centralClientStats[i].InboundId, centralClientStats[i].Email}] = &centralClientStats[i]
  1257. centralCSByEmail[centralClientStats[i].Email] = &centralClientStats[i]
  1258. }
  1259. nodeBaselines := make(map[string]nodeTrafficCounter)
  1260. var baselineRows []model.NodeClientTraffic
  1261. if err := db.Model(&model.NodeClientTraffic{}).
  1262. Where("node_id = ?", nodeID).
  1263. Find(&baselineRows).Error; err != nil {
  1264. return false, err
  1265. }
  1266. for i := range baselineRows {
  1267. nodeBaselines[baselineRows[i].Email] = nodeTrafficCounter{Up: baselineRows[i].Up, Down: baselineRows[i].Down}
  1268. }
  1269. var existingEmailsList []string
  1270. if err := db.Model(xray.ClientTraffic{}).Pluck("email", &existingEmailsList).Error; err != nil {
  1271. return false, err
  1272. }
  1273. existingEmails := make(map[string]struct{}, len(existingEmailsList))
  1274. for _, e := range existingEmailsList {
  1275. existingEmails[e] = struct{}{}
  1276. }
  1277. var defaultUserId int
  1278. if len(central) > 0 {
  1279. defaultUserId = central[0].UserId
  1280. } else {
  1281. var u model.User
  1282. if err := db.Model(model.User{}).Order("id asc").First(&u).Error; err == nil {
  1283. defaultUserId = u.Id
  1284. } else {
  1285. defaultUserId = 1
  1286. }
  1287. }
  1288. tx := db.Begin()
  1289. committed := false
  1290. defer func() {
  1291. if !committed {
  1292. tx.Rollback()
  1293. }
  1294. }()
  1295. structuralChange := false
  1296. snapTags := make(map[string]struct{}, len(snap.Inbounds))
  1297. for _, snapIb := range snap.Inbounds {
  1298. if snapIb == nil {
  1299. continue
  1300. }
  1301. snapTags[snapIb.Tag] = struct{}{}
  1302. // Record the prefix-flipped form too so the orphan sweep below keeps a
  1303. // central inbound whether its tag carries the n<id>- prefix or not.
  1304. if prefix != "" {
  1305. if stripped, found := strings.CutPrefix(snapIb.Tag, prefix); found {
  1306. snapTags[stripped] = struct{}{}
  1307. } else {
  1308. snapTags[prefix+snapIb.Tag] = struct{}{}
  1309. }
  1310. }
  1311. c, ok := tagToCentral[snapIb.Tag]
  1312. if !ok {
  1313. // Try snap.Tag first; on collision fall back to the n<id>-
  1314. // prefixed form so local+node can both own the same port.
  1315. pickFreeTag := func() (string, error) {
  1316. candidates := []string{snapIb.Tag}
  1317. if prefix != "" && !strings.HasPrefix(snapIb.Tag, prefix) {
  1318. candidates = append(candidates, prefix+snapIb.Tag)
  1319. }
  1320. for _, t := range candidates {
  1321. var owner model.Inbound
  1322. err := tx.Where("tag = ?", t).First(&owner).Error
  1323. if errors.Is(err, gorm.ErrRecordNotFound) {
  1324. return t, nil
  1325. }
  1326. if err != nil {
  1327. return "", err
  1328. }
  1329. }
  1330. return "", nil
  1331. }
  1332. chosenTag, err := pickFreeTag()
  1333. if err != nil {
  1334. logger.Warningf("setRemoteTraffic: check tag %q failed: %v", snapIb.Tag, err)
  1335. continue
  1336. }
  1337. if chosenTag == "" {
  1338. key := fmt.Sprintf("%d:%s", nodeID, snapIb.Tag)
  1339. if _, seen := reportedRemoteTagConflict.LoadOrStore(key, struct{}{}); !seen {
  1340. logger.Warningf(
  1341. "setRemoteTraffic: tag %q from node %d collides with an existing inbound even after the n%d- prefix — skipping (rename one side to remove the duplicate)",
  1342. snapIb.Tag, nodeID, nodeID,
  1343. )
  1344. }
  1345. continue
  1346. }
  1347. newIb := model.Inbound{
  1348. UserId: defaultUserId,
  1349. NodeID: &nodeID,
  1350. Tag: chosenTag,
  1351. Listen: snapIb.Listen,
  1352. Port: snapIb.Port,
  1353. Protocol: snapIb.Protocol,
  1354. Settings: snapIb.Settings,
  1355. StreamSettings: snapIb.StreamSettings,
  1356. Sniffing: snapIb.Sniffing,
  1357. TrafficReset: snapIb.TrafficReset,
  1358. Enable: snapIb.Enable,
  1359. Remark: snapIb.Remark,
  1360. Total: snapIb.Total,
  1361. ExpiryTime: snapIb.ExpiryTime,
  1362. Up: snapIb.Up,
  1363. Down: snapIb.Down,
  1364. }
  1365. if err := tx.Create(&newIb).Error; err != nil {
  1366. logger.Warningf("setRemoteTraffic: create central inbound for tag %q failed: %v", snapIb.Tag, err)
  1367. continue
  1368. }
  1369. tagToCentral[snapIb.Tag] = &newIb
  1370. if newIb.Tag != snapIb.Tag {
  1371. tagToCentral[newIb.Tag] = &newIb
  1372. }
  1373. structuralChange = true
  1374. continue
  1375. }
  1376. inGrace := c.LastTrafficResetTime > 0 && now-c.LastTrafficResetTime < resetGracePeriodMs
  1377. updates := map[string]any{
  1378. "enable": snapIb.Enable,
  1379. "remark": snapIb.Remark,
  1380. "listen": snapIb.Listen,
  1381. "port": snapIb.Port,
  1382. "protocol": snapIb.Protocol,
  1383. "total": snapIb.Total,
  1384. "expiry_time": snapIb.ExpiryTime,
  1385. "settings": snapIb.Settings,
  1386. "stream_settings": snapIb.StreamSettings,
  1387. "sniffing": snapIb.Sniffing,
  1388. "traffic_reset": snapIb.TrafficReset,
  1389. }
  1390. if !inGrace || (snapIb.Up+snapIb.Down) <= (c.Up+c.Down) {
  1391. updates["up"] = snapIb.Up
  1392. updates["down"] = snapIb.Down
  1393. }
  1394. if c.Settings != snapIb.Settings ||
  1395. c.Remark != snapIb.Remark ||
  1396. c.Listen != snapIb.Listen ||
  1397. c.Port != snapIb.Port ||
  1398. c.Total != snapIb.Total ||
  1399. c.ExpiryTime != snapIb.ExpiryTime ||
  1400. c.Enable != snapIb.Enable {
  1401. structuralChange = true
  1402. }
  1403. if err := tx.Model(model.Inbound{}).
  1404. Where("id = ?", c.Id).
  1405. Updates(updates).Error; err != nil {
  1406. return false, err
  1407. }
  1408. }
  1409. for _, c := range central {
  1410. if _, kept := snapTags[c.Tag]; kept {
  1411. continue
  1412. }
  1413. var goneEmails []string
  1414. if err := tx.Model(xray.ClientTraffic{}).
  1415. Where("inbound_id = ?", c.Id).
  1416. Pluck("email", &goneEmails).Error; err != nil {
  1417. return false, err
  1418. }
  1419. if len(goneEmails) > 0 {
  1420. if err := tx.Where("node_id = ? AND email IN ?", nodeID, goneEmails).
  1421. Delete(&model.NodeClientTraffic{}).Error; err != nil {
  1422. return false, err
  1423. }
  1424. }
  1425. if err := tx.Where("inbound_id = ?", c.Id).
  1426. Delete(&xray.ClientTraffic{}).Error; err != nil {
  1427. return false, err
  1428. }
  1429. if err := s.clientService.DetachInbound(tx, c.Id); err != nil {
  1430. return false, err
  1431. }
  1432. if err := tx.Where("id = ?", c.Id).
  1433. Delete(&model.Inbound{}).Error; err != nil {
  1434. return false, err
  1435. }
  1436. delete(tagToCentral, c.Tag)
  1437. structuralChange = true
  1438. }
  1439. for _, snapIb := range snap.Inbounds {
  1440. if snapIb == nil {
  1441. continue
  1442. }
  1443. c, ok := tagToCentral[snapIb.Tag]
  1444. if !ok {
  1445. continue
  1446. }
  1447. snapEmails := make(map[string]struct{}, len(snapIb.ClientStats))
  1448. for _, cs := range snapIb.ClientStats {
  1449. snapEmails[cs.Email] = struct{}{}
  1450. base, seen := nodeBaselines[cs.Email]
  1451. var deltaUp, deltaDown int64
  1452. if seen {
  1453. if deltaUp = cs.Up - base.Up; deltaUp < 0 {
  1454. deltaUp = cs.Up
  1455. }
  1456. if deltaDown = cs.Down - base.Down; deltaDown < 0 {
  1457. deltaDown = cs.Down
  1458. }
  1459. }
  1460. if _, rowExists := existingEmails[cs.Email]; !rowExists {
  1461. row := &xray.ClientTraffic{
  1462. InboundId: c.Id,
  1463. Email: cs.Email,
  1464. Enable: cs.Enable,
  1465. Total: cs.Total,
  1466. ExpiryTime: cs.ExpiryTime,
  1467. Reset: cs.Reset,
  1468. Up: cs.Up,
  1469. Down: cs.Down,
  1470. LastOnline: cs.LastOnline,
  1471. }
  1472. if err := tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}).
  1473. Create(row).Error; err != nil {
  1474. return false, err
  1475. }
  1476. centralCS[csKey{c.Id, cs.Email}] = row
  1477. centralCSByEmail[cs.Email] = row
  1478. existingEmails[cs.Email] = struct{}{}
  1479. structuralChange = true
  1480. if err := s.upsertNodeBaseline(tx, nodeID, cs.Email, cs.Up, cs.Down); err != nil {
  1481. return false, err
  1482. }
  1483. nodeBaselines[cs.Email] = nodeTrafficCounter{Up: cs.Up, Down: cs.Down}
  1484. continue
  1485. }
  1486. if existing := centralCSByEmail[cs.Email]; existing != nil &&
  1487. (existing.Enable != cs.Enable ||
  1488. existing.Total != cs.Total ||
  1489. existing.ExpiryTime != cs.ExpiryTime ||
  1490. existing.Reset != cs.Reset) {
  1491. structuralChange = true
  1492. }
  1493. // Only allow the node to disable a client (cs.Enable=false), never
  1494. // to re-enable one the panel has already disabled. A stale snapshot
  1495. // from the node arriving after a central disable would otherwise
  1496. // overwrite enable=false back to true, letting the client accumulate
  1497. // far more traffic than their limit before being disabled again.
  1498. enableExpr := "CASE WHEN ? = 0 THEN 0 ELSE enable END"
  1499. if err := tx.Exec(
  1500. fmt.Sprintf(
  1501. `UPDATE client_traffics
  1502. SET up = up + ?, down = down + ?, enable = %s, total = ?, expiry_time = ?, reset = ?,
  1503. last_online = %s
  1504. WHERE email = ?`,
  1505. enableExpr,
  1506. database.GreatestExpr("last_online", "?"),
  1507. ),
  1508. deltaUp, deltaDown, cs.Enable, cs.Total, cs.ExpiryTime, cs.Reset,
  1509. cs.LastOnline, cs.Email,
  1510. ).Error; err != nil {
  1511. return false, err
  1512. }
  1513. if err := s.upsertNodeBaseline(tx, nodeID, cs.Email, cs.Up, cs.Down); err != nil {
  1514. return false, err
  1515. }
  1516. nodeBaselines[cs.Email] = nodeTrafficCounter{Up: cs.Up, Down: cs.Down}
  1517. }
  1518. for k, existing := range centralCS {
  1519. if k.inboundID != c.Id {
  1520. continue
  1521. }
  1522. if _, kept := snapEmails[k.email]; kept {
  1523. continue
  1524. }
  1525. if err := tx.Where("node_id = ? AND email = ?", nodeID, existing.Email).
  1526. Delete(&model.NodeClientTraffic{}).Error; err != nil {
  1527. return false, err
  1528. }
  1529. if err := tx.Where("inbound_id = ? AND email = ?", c.Id, existing.Email).
  1530. Delete(&xray.ClientTraffic{}).Error; err != nil {
  1531. return false, err
  1532. }
  1533. structuralChange = true
  1534. }
  1535. }
  1536. type oldSet struct {
  1537. inboundID int
  1538. emails map[string]struct{}
  1539. }
  1540. var perInboundOld []oldSet
  1541. for _, snapIb := range snap.Inbounds {
  1542. if snapIb == nil {
  1543. continue
  1544. }
  1545. c, ok := tagToCentral[snapIb.Tag]
  1546. if !ok {
  1547. continue
  1548. }
  1549. var oldEmailsRows []string
  1550. if err := tx.Table("clients").
  1551. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  1552. Where("client_inbounds.inbound_id = ?", c.Id).
  1553. Pluck("email", &oldEmailsRows).Error; err == nil {
  1554. oldEmails := make(map[string]struct{}, len(oldEmailsRows))
  1555. for _, e := range oldEmailsRows {
  1556. if e != "" {
  1557. oldEmails[e] = struct{}{}
  1558. }
  1559. }
  1560. perInboundOld = append(perInboundOld, oldSet{inboundID: c.Id, emails: oldEmails})
  1561. }
  1562. clients, gcErr := s.GetClients(snapIb)
  1563. if gcErr != nil {
  1564. logger.Warningf("setRemoteTraffic: parse clients for tag %q failed: %v", snapIb.Tag, gcErr)
  1565. continue
  1566. }
  1567. csEnableByEmail := make(map[string]bool, len(snapIb.ClientStats))
  1568. for _, cs := range snapIb.ClientStats {
  1569. csEnableByEmail[cs.Email] = cs.Enable
  1570. }
  1571. filtered := clients[:0]
  1572. for i := range clients {
  1573. if isClientEmailTombstoned(clients[i].Email) {
  1574. continue
  1575. }
  1576. if cse, hit := csEnableByEmail[clients[i].Email]; hit && !cse {
  1577. clients[i].Enable = false
  1578. }
  1579. filtered = append(filtered, clients[i])
  1580. }
  1581. localEmails := make([]string, 0, len(filtered))
  1582. for i := range filtered {
  1583. if filtered[i].Email != "" {
  1584. localEmails = append(localEmails, filtered[i].Email)
  1585. }
  1586. }
  1587. if len(localEmails) > 0 {
  1588. var localMeta []struct {
  1589. Email string
  1590. Comment string `gorm:"column:comment"`
  1591. }
  1592. if err := tx.Table("clients").
  1593. Select("email, comment").
  1594. Where("email IN ?", localEmails).
  1595. Find(&localMeta).Error; err == nil {
  1596. commentByEmail := make(map[string]string, len(localMeta))
  1597. for _, m := range localMeta {
  1598. commentByEmail[m.Email] = m.Comment
  1599. }
  1600. for i := range filtered {
  1601. if cmt, ok := commentByEmail[filtered[i].Email]; ok {
  1602. filtered[i].Comment = cmt
  1603. }
  1604. }
  1605. }
  1606. }
  1607. if err := s.clientService.SyncInbound(tx, c.Id, filtered); err != nil {
  1608. logger.Warningf("setRemoteTraffic: sync clients for tag %q failed: %v", snapIb.Tag, err)
  1609. }
  1610. }
  1611. for _, old := range perInboundOld {
  1612. var stillAttached []string
  1613. if err := tx.Table("clients").
  1614. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  1615. Where("client_inbounds.inbound_id = ?", old.inboundID).
  1616. Pluck("email", &stillAttached).Error; err != nil {
  1617. continue
  1618. }
  1619. stillSet := make(map[string]struct{}, len(stillAttached))
  1620. for _, e := range stillAttached {
  1621. stillSet[e] = struct{}{}
  1622. }
  1623. for email := range old.emails {
  1624. if _, kept := stillSet[email]; kept {
  1625. continue
  1626. }
  1627. var attachmentCount int64
  1628. if err := tx.Table("client_inbounds").
  1629. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  1630. Where("clients.email = ?", email).
  1631. Count(&attachmentCount).Error; err != nil {
  1632. continue
  1633. }
  1634. if attachmentCount > 0 {
  1635. continue
  1636. }
  1637. if err := tx.Where("email = ?", email).Delete(&model.ClientRecord{}).Error; err != nil {
  1638. logger.Warningf("setRemoteTraffic: delete ClientRecord %q failed: %v", email, err)
  1639. }
  1640. if err := tx.Where("email = ?", email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  1641. logger.Warningf("setRemoteTraffic: delete ClientTraffic %q failed: %v", email, err)
  1642. }
  1643. if err := tx.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  1644. logger.Warningf("setRemoteTraffic: delete NodeClientTraffic %q failed: %v", email, err)
  1645. }
  1646. structuralChange = true
  1647. }
  1648. }
  1649. if err := tx.Commit().Error; err != nil {
  1650. return false, err
  1651. }
  1652. committed = true
  1653. if p != nil {
  1654. p.SetNodeOnlineClients(nodeID, snap.OnlineEmails)
  1655. }
  1656. return structuralChange, nil
  1657. }
  1658. func (s *InboundService) AddTraffic(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (needRestart bool, clientsDisabled bool, err error) {
  1659. var disabledNodeIDs []int
  1660. err = submitTrafficWrite(func() error {
  1661. var inner error
  1662. needRestart, clientsDisabled, disabledNodeIDs, inner = s.addTrafficLocked(inboundTraffics, clientTraffics)
  1663. return inner
  1664. })
  1665. if err == nil && len(disabledNodeIDs) > 0 {
  1666. s.restartRemoteNodesOnDisable(disabledNodeIDs)
  1667. }
  1668. return
  1669. }
  1670. func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (bool, bool, []int, error) {
  1671. var err error
  1672. db := database.GetDB()
  1673. tx := db.Begin()
  1674. defer func() {
  1675. if err != nil {
  1676. tx.Rollback()
  1677. } else {
  1678. tx.Commit()
  1679. }
  1680. }()
  1681. err = s.addInboundTraffic(tx, inboundTraffics)
  1682. if err != nil {
  1683. return false, false, nil, err
  1684. }
  1685. err = s.addClientTraffic(tx, clientTraffics)
  1686. if err != nil {
  1687. return false, false, nil, err
  1688. }
  1689. needRestart0, count, err := s.autoRenewClients(tx)
  1690. if err != nil {
  1691. logger.Warning("Error in renew clients:", err)
  1692. } else if count > 0 {
  1693. logger.Debugf("%v clients renewed", count)
  1694. }
  1695. disabledClientsCount := int64(0)
  1696. needRestart1, count, disabledNodeIDs, err := s.disableInvalidClients(tx)
  1697. if err != nil {
  1698. logger.Warning("Error in disabling invalid clients:", err)
  1699. } else if count > 0 {
  1700. logger.Debugf("%v clients disabled", count)
  1701. disabledClientsCount = count
  1702. }
  1703. needRestart2, count, err := s.disableInvalidInbounds(tx)
  1704. if err != nil {
  1705. logger.Warning("Error in disabling invalid inbounds:", err)
  1706. } else if count > 0 {
  1707. logger.Debugf("%v inbounds disabled", count)
  1708. }
  1709. return needRestart0 || needRestart1 || needRestart2, disabledClientsCount > 0, disabledNodeIDs, nil
  1710. }
  1711. func (s *InboundService) addInboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
  1712. if len(traffics) == 0 {
  1713. return nil
  1714. }
  1715. var err error
  1716. for _, traffic := range traffics {
  1717. if traffic.IsInbound {
  1718. err = tx.Model(&model.Inbound{}).Where("tag = ? AND node_id IS NULL", traffic.Tag).
  1719. Updates(map[string]any{
  1720. "up": gorm.Expr("up + ?", traffic.Up),
  1721. "down": gorm.Expr("down + ?", traffic.Down),
  1722. }).Error
  1723. if err != nil {
  1724. return err
  1725. }
  1726. }
  1727. }
  1728. return nil
  1729. }
  1730. func (s *InboundService) addClientTraffic(tx *gorm.DB, traffics []*xray.ClientTraffic) (err error) {
  1731. if len(traffics) == 0 {
  1732. return nil
  1733. }
  1734. emails := make([]string, 0, len(traffics))
  1735. for _, traffic := range traffics {
  1736. emails = append(emails, traffic.Email)
  1737. }
  1738. dbClientTraffics := make([]*xray.ClientTraffic, 0, len(traffics))
  1739. err = tx.Model(xray.ClientTraffic{}).
  1740. Where("email IN (?) AND inbound_id NOT IN (?)", emails,
  1741. tx.Model(&model.Inbound{}).Select("id").Where("node_id IS NOT NULL")).
  1742. Find(&dbClientTraffics).Error
  1743. if err != nil {
  1744. return err
  1745. }
  1746. // Avoid empty slice error
  1747. if len(dbClientTraffics) == 0 {
  1748. return nil
  1749. }
  1750. dbClientTraffics, err = s.adjustTraffics(tx, dbClientTraffics)
  1751. if err != nil {
  1752. return err
  1753. }
  1754. // Index by email for O(N) merge — the previous nested loop was O(N²)
  1755. // and dominated each cron tick on inbounds with thousands of active
  1756. // clients (7500 × 7500 = 56M string comparisons every 10 seconds).
  1757. trafficByEmail := make(map[string]*xray.ClientTraffic, len(traffics))
  1758. for i := range traffics {
  1759. if traffics[i] != nil {
  1760. trafficByEmail[traffics[i].Email] = traffics[i]
  1761. }
  1762. }
  1763. now := time.Now().UnixMilli()
  1764. for dbTraffic_index := range dbClientTraffics {
  1765. t, ok := trafficByEmail[dbClientTraffics[dbTraffic_index].Email]
  1766. if !ok {
  1767. continue
  1768. }
  1769. dbClientTraffics[dbTraffic_index].Up += t.Up
  1770. dbClientTraffics[dbTraffic_index].Down += t.Down
  1771. if t.Up+t.Down > 0 {
  1772. dbClientTraffics[dbTraffic_index].LastOnline = now
  1773. }
  1774. }
  1775. err = tx.Save(dbClientTraffics).Error
  1776. if err != nil {
  1777. logger.Warning("AddClientTraffic update data ", err)
  1778. }
  1779. return nil
  1780. }
  1781. func (s *InboundService) adjustTraffics(tx *gorm.DB, dbClientTraffics []*xray.ClientTraffic) ([]*xray.ClientTraffic, error) {
  1782. now := time.Now().UnixMilli()
  1783. // "Start After First Use" stores a negative expiry (the duration). On the
  1784. // first traffic tick it becomes an absolute deadline of now+duration. Compute
  1785. // it once per email so every inbound the client is attached to lands on the
  1786. // same value (recomputing per inbound would skip all but the first one).
  1787. newExpiryByEmail := make(map[string]int64, len(dbClientTraffics))
  1788. for traffic_index := range dbClientTraffics {
  1789. if dbClientTraffics[traffic_index].ExpiryTime < 0 {
  1790. newExpiryByEmail[dbClientTraffics[traffic_index].Email] = now - dbClientTraffics[traffic_index].ExpiryTime
  1791. }
  1792. }
  1793. if len(newExpiryByEmail) == 0 {
  1794. return dbClientTraffics, nil
  1795. }
  1796. delayedEmails := make([]string, 0, len(newExpiryByEmail))
  1797. for email := range newExpiryByEmail {
  1798. delayedEmails = append(delayedEmails, email)
  1799. }
  1800. // Resolve the owning inbounds through the client_inbounds link, which is
  1801. // authoritative. client_traffics.inbound_id goes stale when an inbound is
  1802. // deleted and recreated, which would leave the negative expiry unconverted.
  1803. var inboundIds []int
  1804. err := tx.Table("client_inbounds").
  1805. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  1806. Where("clients.email IN (?)", delayedEmails).
  1807. Distinct().
  1808. Pluck("client_inbounds.inbound_id", &inboundIds).Error
  1809. if err != nil {
  1810. return nil, err
  1811. }
  1812. if len(inboundIds) == 0 {
  1813. return dbClientTraffics, nil
  1814. }
  1815. var inbounds []*model.Inbound
  1816. err = tx.Model(model.Inbound{}).Where("id IN (?)", inboundIds).Find(&inbounds).Error
  1817. if err != nil {
  1818. return nil, err
  1819. }
  1820. for inbound_index := range inbounds {
  1821. settings := map[string]any{}
  1822. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  1823. clients, ok := settings["clients"].([]any)
  1824. if ok {
  1825. var newClients []any
  1826. for client_index := range clients {
  1827. c := clients[client_index].(map[string]any)
  1828. email, _ := c["email"].(string)
  1829. if newExpiry, ok := newExpiryByEmail[email]; ok {
  1830. c["expiryTime"] = newExpiry
  1831. c["updated_at"] = now
  1832. }
  1833. if _, ok := c["created_at"]; !ok {
  1834. c["created_at"] = now
  1835. }
  1836. if _, ok := c["updated_at"]; !ok {
  1837. c["updated_at"] = now
  1838. }
  1839. newClients = append(newClients, any(c))
  1840. }
  1841. settings["clients"] = newClients
  1842. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  1843. if err != nil {
  1844. return nil, err
  1845. }
  1846. inbounds[inbound_index].Settings = string(modifiedSettings)
  1847. }
  1848. }
  1849. for traffic_index := range dbClientTraffics {
  1850. if newExpiry, ok := newExpiryByEmail[dbClientTraffics[traffic_index].Email]; ok {
  1851. dbClientTraffics[traffic_index].ExpiryTime = newExpiry
  1852. }
  1853. }
  1854. err = tx.Save(inbounds).Error
  1855. if err != nil {
  1856. logger.Warning("AddClientTraffic update inbounds ", err)
  1857. logger.Error(inbounds)
  1858. } else {
  1859. for _, ib := range inbounds {
  1860. if ib == nil {
  1861. continue
  1862. }
  1863. cs, gcErr := s.GetClients(ib)
  1864. if gcErr != nil {
  1865. logger.Warning("AddClientTraffic sync clients: GetClients failed", gcErr)
  1866. continue
  1867. }
  1868. if syncErr := s.clientService.SyncInbound(tx, ib.Id, cs); syncErr != nil {
  1869. logger.Warning("AddClientTraffic sync clients: SyncInbound failed", syncErr)
  1870. }
  1871. }
  1872. }
  1873. return dbClientTraffics, nil
  1874. }
  1875. func (s *InboundService) autoRenewClients(tx *gorm.DB) (bool, int64, error) {
  1876. // check for time expired
  1877. var traffics []*xray.ClientTraffic
  1878. now := time.Now().Unix() * 1000
  1879. var err, err1 error
  1880. err = tx.Model(xray.ClientTraffic{}).
  1881. Where("reset > 0 and expiry_time > 0 and expiry_time <= ?", now).
  1882. Where("inbound_id NOT IN (?)", tx.Model(&model.Inbound{}).Select("id").Where("node_id IS NOT NULL")).
  1883. Find(&traffics).Error
  1884. if err != nil {
  1885. return false, 0, err
  1886. }
  1887. // return if there is no client to renew
  1888. if len(traffics) == 0 {
  1889. return false, 0, nil
  1890. }
  1891. var inbound_ids []int
  1892. var inbounds []*model.Inbound
  1893. needRestart := false
  1894. var clientsToAdd []struct {
  1895. protocol string
  1896. tag string
  1897. client map[string]any
  1898. }
  1899. // Resolve the inbounds to renew through the client_inbounds link rather than
  1900. // client_traffics.inbound_id, which goes stale after an inbound is deleted and
  1901. // recreated and would otherwise skip the renew entirely.
  1902. renewEmails := make([]string, 0, len(traffics))
  1903. for _, traffic := range traffics {
  1904. renewEmails = append(renewEmails, traffic.Email)
  1905. }
  1906. for _, batch := range chunkStrings(renewEmails, sqliteMaxVars) {
  1907. var ids []int
  1908. if err = tx.Table("client_inbounds").
  1909. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  1910. Where("clients.email IN ?", batch).
  1911. Distinct().
  1912. Pluck("client_inbounds.inbound_id", &ids).Error; err != nil {
  1913. return false, 0, err
  1914. }
  1915. inbound_ids = append(inbound_ids, ids...)
  1916. }
  1917. // Dedupe so an inbound hosting N expired clients is fetched and saved once
  1918. // per tick instead of N times across chunk boundaries.
  1919. inbound_ids = uniqueInts(inbound_ids)
  1920. // Chunked to stay under SQLite's bind-variable limit when many inbounds
  1921. // are touched in a single tick.
  1922. for _, batch := range chunkInts(inbound_ids, sqliteMaxVars) {
  1923. var page []*model.Inbound
  1924. if err = tx.Model(model.Inbound{}).Where("id IN ?", batch).Find(&page).Error; err != nil {
  1925. return false, 0, err
  1926. }
  1927. inbounds = append(inbounds, page...)
  1928. }
  1929. for inbound_index := range inbounds {
  1930. settings := map[string]any{}
  1931. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  1932. clients := settings["clients"].([]any)
  1933. for client_index := range clients {
  1934. c := clients[client_index].(map[string]any)
  1935. for traffic_index, traffic := range traffics {
  1936. if traffic.Email == c["email"].(string) {
  1937. newExpiryTime := traffic.ExpiryTime
  1938. for newExpiryTime < now {
  1939. newExpiryTime += (int64(traffic.Reset) * 86400000)
  1940. }
  1941. c["expiryTime"] = newExpiryTime
  1942. traffics[traffic_index].ExpiryTime = newExpiryTime
  1943. traffics[traffic_index].Down = 0
  1944. traffics[traffic_index].Up = 0
  1945. if !traffic.Enable {
  1946. traffics[traffic_index].Enable = true
  1947. c["enable"] = true
  1948. clientsToAdd = append(clientsToAdd,
  1949. struct {
  1950. protocol string
  1951. tag string
  1952. client map[string]any
  1953. }{
  1954. protocol: string(inbounds[inbound_index].Protocol),
  1955. tag: inbounds[inbound_index].Tag,
  1956. client: c,
  1957. })
  1958. }
  1959. clients[client_index] = any(c)
  1960. break
  1961. }
  1962. }
  1963. }
  1964. settings["clients"] = clients
  1965. newSettings, err := json.MarshalIndent(settings, "", " ")
  1966. if err != nil {
  1967. return false, 0, err
  1968. }
  1969. inbounds[inbound_index].Settings = string(newSettings)
  1970. }
  1971. err = tx.Save(inbounds).Error
  1972. if err != nil {
  1973. return false, 0, err
  1974. }
  1975. for _, ib := range inbounds {
  1976. if ib == nil {
  1977. continue
  1978. }
  1979. cs, gcErr := s.GetClients(ib)
  1980. if gcErr != nil {
  1981. logger.Warning("autoRenewClients sync clients: GetClients failed", gcErr)
  1982. continue
  1983. }
  1984. if syncErr := s.clientService.SyncInbound(tx, ib.Id, cs); syncErr != nil {
  1985. logger.Warning("autoRenewClients sync clients: SyncInbound failed", syncErr)
  1986. }
  1987. }
  1988. err = tx.Save(traffics).Error
  1989. if err != nil {
  1990. return false, 0, err
  1991. }
  1992. if p != nil {
  1993. err1 = s.xrayApi.Init(p.GetAPIPort())
  1994. if err1 != nil {
  1995. return true, int64(len(traffics)), nil
  1996. }
  1997. for _, clientToAdd := range clientsToAdd {
  1998. err1 = s.xrayApi.AddUser(clientToAdd.protocol, clientToAdd.tag, clientToAdd.client)
  1999. if err1 != nil {
  2000. needRestart = true
  2001. }
  2002. }
  2003. s.xrayApi.Close()
  2004. }
  2005. return needRestart, int64(len(traffics)), nil
  2006. }
  2007. func (s *InboundService) disableInvalidInbounds(tx *gorm.DB) (bool, int64, error) {
  2008. now := time.Now().Unix() * 1000
  2009. needRestart := false
  2010. if p != nil {
  2011. var tags []string
  2012. err := tx.Table("inbounds").
  2013. Select("inbounds.tag").
  2014. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ? and node_id IS NULL", now, true).
  2015. Scan(&tags).Error
  2016. if err != nil {
  2017. return false, 0, err
  2018. }
  2019. s.xrayApi.Init(p.GetAPIPort())
  2020. for _, tag := range tags {
  2021. err1 := s.xrayApi.DelInbound(tag)
  2022. if err1 == nil {
  2023. logger.Debug("Inbound disabled by api:", tag)
  2024. } else {
  2025. logger.Debug("Error in disabling inbound by api:", err1)
  2026. needRestart = true
  2027. }
  2028. }
  2029. s.xrayApi.Close()
  2030. }
  2031. result := tx.Model(model.Inbound{}).
  2032. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ? and node_id IS NULL", now, true).
  2033. Update("enable", false)
  2034. err := result.Error
  2035. count := result.RowsAffected
  2036. return needRestart, count, err
  2037. }
  2038. func (s *InboundService) disableInvalidClients(tx *gorm.DB) (bool, int64, []int, error) {
  2039. now := time.Now().Unix() * 1000
  2040. needRestart := false
  2041. var depletedRows []xray.ClientTraffic
  2042. err := tx.Model(xray.ClientTraffic{}).
  2043. Where("((total > 0 AND up + down >= total) OR (expiry_time > 0 AND expiry_time <= ?)) AND enable = ?", now, true).
  2044. Find(&depletedRows).Error
  2045. if err != nil {
  2046. return false, 0, nil, err
  2047. }
  2048. if len(depletedRows) == 0 {
  2049. return false, 0, nil, nil
  2050. }
  2051. depletedEmails := make([]string, 0, len(depletedRows))
  2052. for i := range depletedRows {
  2053. if depletedRows[i].Email == "" {
  2054. continue
  2055. }
  2056. depletedEmails = append(depletedEmails, depletedRows[i].Email)
  2057. }
  2058. type target struct {
  2059. InboundID int `gorm:"column:inbound_id"`
  2060. NodeID *int `gorm:"column:node_id"`
  2061. Tag string
  2062. Email string
  2063. }
  2064. var targets []target
  2065. if len(depletedEmails) > 0 {
  2066. err = tx.Raw(`
  2067. SELECT inbounds.id AS inbound_id, inbounds.node_id AS node_id,
  2068. inbounds.tag AS tag, clients.email AS email
  2069. FROM clients
  2070. JOIN client_inbounds ON client_inbounds.client_id = clients.id
  2071. JOIN inbounds ON inbounds.id = client_inbounds.inbound_id
  2072. WHERE clients.email IN ?
  2073. `, depletedEmails).Scan(&targets).Error
  2074. if err != nil {
  2075. return false, 0, nil, err
  2076. }
  2077. }
  2078. var localTargets []target
  2079. localByInbound := make(map[int]map[string]struct{})
  2080. remoteByInbound := make(map[int][]target)
  2081. for _, t := range targets {
  2082. if t.NodeID == nil {
  2083. localTargets = append(localTargets, t)
  2084. if localByInbound[t.InboundID] == nil {
  2085. localByInbound[t.InboundID] = make(map[string]struct{})
  2086. }
  2087. localByInbound[t.InboundID][t.Email] = struct{}{}
  2088. } else {
  2089. remoteByInbound[t.InboundID] = append(remoteByInbound[t.InboundID], t)
  2090. }
  2091. }
  2092. if p != nil && len(localTargets) > 0 {
  2093. s.xrayApi.Init(p.GetAPIPort())
  2094. for _, t := range localTargets {
  2095. err1 := s.xrayApi.RemoveUser(t.Tag, t.Email)
  2096. if err1 == nil {
  2097. logger.Debug("Client disabled by api:", t.Email)
  2098. } else if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", t.Email)) {
  2099. logger.Debug("User is already disabled. Nothing to do more...")
  2100. } else {
  2101. logger.Debug("Error in disabling client by api:", err1)
  2102. needRestart = true
  2103. }
  2104. }
  2105. s.xrayApi.Close()
  2106. }
  2107. for inboundID, emails := range localByInbound {
  2108. if _, _, mErr := s.markClientsDisabledInSettings(tx, inboundID, emails); mErr != nil {
  2109. logger.Warning("disableInvalidClients: settings.JSON sync failed for inbound", inboundID, ":", mErr)
  2110. }
  2111. }
  2112. result := tx.Model(xray.ClientTraffic{}).
  2113. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
  2114. Update("enable", false)
  2115. err = result.Error
  2116. count := result.RowsAffected
  2117. if err != nil {
  2118. return needRestart, count, nil, err
  2119. }
  2120. if len(depletedEmails) > 0 {
  2121. if err := tx.Model(&model.ClientRecord{}).
  2122. Where("email IN ?", depletedEmails).
  2123. Updates(map[string]any{"enable": false, "updated_at": now}).Error; err != nil {
  2124. logger.Warning("disableInvalidClients update clients.enable:", err)
  2125. }
  2126. }
  2127. disabledNodeIDs := make(map[int]struct{})
  2128. for inboundID, group := range remoteByInbound {
  2129. emails := make(map[string]struct{}, len(group))
  2130. for _, t := range group {
  2131. emails[t.Email] = struct{}{}
  2132. }
  2133. if pushErr := s.disableRemoteClients(tx, inboundID, emails); pushErr != nil {
  2134. logger.Warning("disableInvalidClients: push to remote failed for inbound", inboundID, ":", pushErr)
  2135. needRestart = true
  2136. } else {
  2137. for _, t := range group {
  2138. if t.NodeID != nil {
  2139. disabledNodeIDs[*t.NodeID] = struct{}{}
  2140. }
  2141. }
  2142. }
  2143. }
  2144. nodeIDs := make([]int, 0, len(disabledNodeIDs))
  2145. for nodeID := range disabledNodeIDs {
  2146. nodeIDs = append(nodeIDs, nodeID)
  2147. }
  2148. return needRestart, count, nodeIDs, nil
  2149. }
  2150. func (s *InboundService) restartRemoteNodesOnDisable(nodeIDs []int) {
  2151. restartOnDisable, err := (&SettingService{}).GetRestartXrayOnClientDisable()
  2152. if err != nil {
  2153. logger.Warning("disableInvalidClients: get RestartXrayOnClientDisable failed:", err)
  2154. return
  2155. }
  2156. if !restartOnDisable {
  2157. return
  2158. }
  2159. for _, nodeID := range nodeIDs {
  2160. nodeIDCopy := nodeID
  2161. rt, rtErr := runtime.GetManager().RuntimeFor(&nodeIDCopy)
  2162. if rtErr != nil {
  2163. logger.Warning("disableInvalidClients: get runtime for node", nodeID, "failed:", rtErr)
  2164. continue
  2165. }
  2166. if rtErr = rt.RestartXray(context.Background()); rtErr != nil {
  2167. logger.Warning("disableInvalidClients: restart xray on node", nodeID, "failed:", rtErr)
  2168. }
  2169. }
  2170. }
  2171. // markClientsDisabledInSettings flips client.enable=false in the inbound's
  2172. // stored settings JSON for the given emails and returns both the pre and
  2173. // post snapshots so a caller pushing to a remote node has the diff to hand.
  2174. func (s *InboundService) markClientsDisabledInSettings(tx *gorm.DB, inboundID int, emails map[string]struct{}) (oldIb, newIb *model.Inbound, err error) {
  2175. var ib model.Inbound
  2176. if err := tx.Model(&model.Inbound{}).Where("id = ?", inboundID).First(&ib).Error; err != nil {
  2177. return nil, nil, err
  2178. }
  2179. snapshot := ib
  2180. settings := map[string]any{}
  2181. if err := json.Unmarshal([]byte(ib.Settings), &settings); err != nil {
  2182. return nil, nil, err
  2183. }
  2184. clients, _ := settings["clients"].([]any)
  2185. now := time.Now().Unix() * 1000
  2186. mutated := false
  2187. for i := range clients {
  2188. entry, ok := clients[i].(map[string]any)
  2189. if !ok {
  2190. continue
  2191. }
  2192. email, _ := entry["email"].(string)
  2193. if _, hit := emails[email]; !hit {
  2194. continue
  2195. }
  2196. if cur, _ := entry["enable"].(bool); cur == false {
  2197. continue
  2198. }
  2199. entry["enable"] = false
  2200. entry["updated_at"] = now
  2201. clients[i] = entry
  2202. mutated = true
  2203. }
  2204. if !mutated {
  2205. return &snapshot, &ib, nil
  2206. }
  2207. settings["clients"] = clients
  2208. bs, marshalErr := json.MarshalIndent(settings, "", " ")
  2209. if marshalErr != nil {
  2210. return nil, nil, marshalErr
  2211. }
  2212. ib.Settings = string(bs)
  2213. if err := tx.Model(&model.Inbound{}).Where("id = ?", inboundID).
  2214. Update("settings", ib.Settings).Error; err != nil {
  2215. return nil, nil, err
  2216. }
  2217. return &snapshot, &ib, nil
  2218. }
  2219. func (s *InboundService) disableRemoteClients(tx *gorm.DB, inboundID int, emails map[string]struct{}) error {
  2220. oldSnapshot, ib, err := s.markClientsDisabledInSettings(tx, inboundID, emails)
  2221. if err != nil {
  2222. return err
  2223. }
  2224. rt, err := s.runtimeFor(ib)
  2225. if err != nil {
  2226. return err
  2227. }
  2228. if err := rt.UpdateInbound(context.Background(), oldSnapshot, ib); err != nil {
  2229. return err
  2230. }
  2231. return nil
  2232. }
  2233. func (s *InboundService) GetInboundTags() (string, error) {
  2234. db := database.GetDB()
  2235. var inboundTags []string
  2236. err := db.Model(model.Inbound{}).Select("tag").Find(&inboundTags).Error
  2237. if err != nil && err != gorm.ErrRecordNotFound {
  2238. return "", err
  2239. }
  2240. tags, _ := json.Marshal(inboundTags)
  2241. return string(tags), nil
  2242. }
  2243. func (s *InboundService) GetClientReverseTags() (string, error) {
  2244. db := database.GetDB()
  2245. var inbounds []model.Inbound
  2246. err := db.Model(model.Inbound{}).Select("settings").Where("protocol = ?", "vless").Find(&inbounds).Error
  2247. if err != nil && err != gorm.ErrRecordNotFound {
  2248. return "[]", err
  2249. }
  2250. tagSet := make(map[string]struct{})
  2251. for _, inbound := range inbounds {
  2252. var settings map[string]any
  2253. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  2254. continue
  2255. }
  2256. clients, ok := settings["clients"].([]any)
  2257. if !ok {
  2258. continue
  2259. }
  2260. for _, client := range clients {
  2261. clientMap, ok := client.(map[string]any)
  2262. if !ok {
  2263. continue
  2264. }
  2265. reverse, ok := clientMap["reverse"].(map[string]any)
  2266. if !ok {
  2267. continue
  2268. }
  2269. tag, _ := reverse["tag"].(string)
  2270. tag = strings.TrimSpace(tag)
  2271. if tag != "" {
  2272. tagSet[tag] = struct{}{}
  2273. }
  2274. }
  2275. }
  2276. rawTags := make([]string, 0, len(tagSet))
  2277. for tag := range tagSet {
  2278. rawTags = append(rawTags, tag)
  2279. }
  2280. sort.Strings(rawTags)
  2281. result, _ := json.Marshal(rawTags)
  2282. return string(result), nil
  2283. }
  2284. func (s *InboundService) MigrationRemoveOrphanedTraffics() {
  2285. db := database.GetDB()
  2286. query := fmt.Sprintf(
  2287. "DELETE FROM client_traffics WHERE email NOT IN (SELECT %s %s)",
  2288. database.JSONFieldText("client.value", "email"),
  2289. database.JSONClientsFromInbound(),
  2290. )
  2291. db.Exec(query)
  2292. }
  2293. // AddClientStat inserts a per-client accounting row, no-op on email
  2294. // conflict. Xray reports traffic per email, so the surviving row acts as
  2295. // the shared accumulator for inbounds that re-use the same identity.
  2296. func (s *InboundService) AddClientStat(tx *gorm.DB, inboundId int, client *model.Client) error {
  2297. clientTraffic := xray.ClientTraffic{
  2298. InboundId: inboundId,
  2299. Email: client.Email,
  2300. Total: client.TotalGB,
  2301. ExpiryTime: client.ExpiryTime,
  2302. Enable: client.Enable,
  2303. Reset: client.Reset,
  2304. }
  2305. return tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}).
  2306. Create(&clientTraffic).Error
  2307. }
  2308. func (s *InboundService) UpdateClientStat(tx *gorm.DB, email string, client *model.Client) error {
  2309. result := tx.Model(xray.ClientTraffic{}).
  2310. Where("email = ?", email).
  2311. Updates(map[string]any{
  2312. "enable": client.Enable,
  2313. "email": client.Email,
  2314. "total": client.TotalGB,
  2315. "expiry_time": client.ExpiryTime,
  2316. "reset": client.Reset,
  2317. })
  2318. err := result.Error
  2319. return err
  2320. }
  2321. func (s *InboundService) UpdateClientIPs(tx *gorm.DB, oldEmail string, newEmail string) error {
  2322. return tx.Model(model.InboundClientIps{}).Where("client_email = ?", oldEmail).Update("client_email", newEmail).Error
  2323. }
  2324. func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
  2325. if err := tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error; err != nil {
  2326. return err
  2327. }
  2328. return tx.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error
  2329. }
  2330. func (s *InboundService) DelClientIPs(tx *gorm.DB, email string) error {
  2331. return tx.Where("client_email = ?", email).Delete(model.InboundClientIps{}).Error
  2332. }
  2333. func (s *InboundService) delClientStatsByEmails(tx *gorm.DB, emails []string) error {
  2334. const chunk = 400
  2335. for start := 0; start < len(emails); start += chunk {
  2336. end := min(start+chunk, len(emails))
  2337. batch := emails[start:end]
  2338. if err := tx.Where("email IN ?", batch).Delete(xray.ClientTraffic{}).Error; err != nil {
  2339. return err
  2340. }
  2341. if err := tx.Where("email IN ?", batch).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  2342. return err
  2343. }
  2344. }
  2345. return nil
  2346. }
  2347. func (s *InboundService) delClientIPsByEmails(tx *gorm.DB, emails []string) error {
  2348. const chunk = 400
  2349. for start := 0; start < len(emails); start += chunk {
  2350. end := min(start+chunk, len(emails))
  2351. if err := tx.Where("client_email IN ?", emails[start:end]).Delete(model.InboundClientIps{}).Error; err != nil {
  2352. return err
  2353. }
  2354. }
  2355. return nil
  2356. }
  2357. func (s *InboundService) GetClientInboundByTrafficID(trafficId int) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  2358. db := database.GetDB()
  2359. var traffics []*xray.ClientTraffic
  2360. err = db.Model(xray.ClientTraffic{}).Where("id = ?", trafficId).Find(&traffics).Error
  2361. if err != nil {
  2362. logger.Warningf("Error retrieving ClientTraffic with trafficId %d: %v", trafficId, err)
  2363. return nil, nil, err
  2364. }
  2365. if len(traffics) == 0 {
  2366. return nil, nil, nil
  2367. }
  2368. traffic = traffics[0]
  2369. inbound, err = s.GetInbound(traffic.InboundId)
  2370. if errors.Is(err, gorm.ErrRecordNotFound) {
  2371. // client_traffics.inbound_id goes stale when an inbound is deleted and
  2372. // recreated; fall back to the authoritative client_inbounds link by email.
  2373. ids, idErr := s.clientService.GetInboundIdsForEmail(db, traffic.Email)
  2374. if idErr != nil {
  2375. return traffic, nil, idErr
  2376. }
  2377. if len(ids) > 0 {
  2378. inbound, err = s.GetInbound(ids[0])
  2379. }
  2380. }
  2381. return traffic, inbound, err
  2382. }
  2383. func (s *InboundService) GetClientInboundByEmail(email string) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  2384. db := database.GetDB()
  2385. var traffics []*xray.ClientTraffic
  2386. err = db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error
  2387. if err != nil {
  2388. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  2389. return nil, nil, err
  2390. }
  2391. if len(traffics) == 0 {
  2392. return nil, nil, nil
  2393. }
  2394. traffic = traffics[0]
  2395. inbound, err = s.GetInbound(traffic.InboundId)
  2396. if errors.Is(err, gorm.ErrRecordNotFound) {
  2397. // client_traffics.inbound_id is a legacy single-inbound pointer that goes
  2398. // stale when an inbound is deleted and recreated: the email-keyed traffic
  2399. // row survives but still references the missing inbound. Fall back to the
  2400. // authoritative client_inbounds link so email lookups (reset, info, …) work.
  2401. ids, idErr := s.clientService.GetInboundIdsForEmail(db, email)
  2402. if idErr != nil {
  2403. return traffic, nil, idErr
  2404. }
  2405. if len(ids) > 0 {
  2406. inbound, err = s.GetInbound(ids[0])
  2407. }
  2408. }
  2409. return traffic, inbound, err
  2410. }
  2411. func (s *InboundService) GetClientByEmail(clientEmail string) (*xray.ClientTraffic, *model.Client, error) {
  2412. traffic, inbound, err := s.GetClientInboundByEmail(clientEmail)
  2413. if err != nil {
  2414. return nil, nil, err
  2415. }
  2416. if inbound == nil {
  2417. return nil, nil, common.NewError("Inbound Not Found For Email:", clientEmail)
  2418. }
  2419. clients, err := s.GetClients(inbound)
  2420. if err != nil {
  2421. return nil, nil, err
  2422. }
  2423. for _, client := range clients {
  2424. if client.Email == clientEmail {
  2425. return traffic, &client, nil
  2426. }
  2427. }
  2428. return nil, nil, common.NewError("Client Not Found In Inbound For Email:", clientEmail)
  2429. }
  2430. func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
  2431. return submitTrafficWrite(func() error {
  2432. db := database.GetDB()
  2433. return db.Model(xray.ClientTraffic{}).
  2434. Where("email = ?", clientEmail).
  2435. Updates(map[string]any{"enable": true, "up": 0, "down": 0}).Error
  2436. })
  2437. }
  2438. func (s *InboundService) ResetClientTraffic(id int, clientEmail string) (needRestart bool, err error) {
  2439. err = submitTrafficWrite(func() error {
  2440. var inner error
  2441. needRestart, inner = s.resetClientTrafficLocked(id, clientEmail)
  2442. return inner
  2443. })
  2444. return
  2445. }
  2446. func (s *InboundService) resetClientTrafficLocked(id int, clientEmail string) (bool, error) {
  2447. needRestart := false
  2448. traffic, err := s.GetClientTrafficByEmail(clientEmail)
  2449. if err != nil {
  2450. return false, err
  2451. }
  2452. if !traffic.Enable {
  2453. inbound, err := s.GetInbound(id)
  2454. if err != nil {
  2455. return false, err
  2456. }
  2457. clients, err := s.GetClients(inbound)
  2458. if err != nil {
  2459. return false, err
  2460. }
  2461. for _, client := range clients {
  2462. if client.Email == clientEmail && client.Enable {
  2463. rt, rterr := s.runtimeFor(inbound)
  2464. if rterr != nil {
  2465. if inbound.NodeID != nil {
  2466. return false, rterr
  2467. }
  2468. needRestart = true
  2469. break
  2470. }
  2471. cipher := ""
  2472. if string(inbound.Protocol) == "shadowsocks" {
  2473. var oldSettings map[string]any
  2474. err = json.Unmarshal([]byte(inbound.Settings), &oldSettings)
  2475. if err != nil {
  2476. return false, err
  2477. }
  2478. cipher = oldSettings["method"].(string)
  2479. }
  2480. err1 := rt.AddUser(context.Background(), inbound, map[string]any{
  2481. "email": client.Email,
  2482. "id": client.ID,
  2483. "auth": client.Auth,
  2484. "security": client.Security,
  2485. "flow": client.Flow,
  2486. "password": client.Password,
  2487. "cipher": cipher,
  2488. })
  2489. if err1 == nil {
  2490. logger.Debug("Client enabled on", rt.Name(), "due to reset traffic:", clientEmail)
  2491. } else {
  2492. logger.Debug("Error in enabling client on", rt.Name(), ":", err1)
  2493. needRestart = true
  2494. }
  2495. break
  2496. }
  2497. }
  2498. }
  2499. traffic.Up = 0
  2500. traffic.Down = 0
  2501. traffic.Enable = true
  2502. db := database.GetDB()
  2503. err = db.Save(traffic).Error
  2504. if err != nil {
  2505. return false, err
  2506. }
  2507. now := time.Now().UnixMilli()
  2508. _ = db.Model(model.Inbound{}).
  2509. Where("id = ?", id).
  2510. Update("last_traffic_reset_time", now).Error
  2511. inbound, err := s.GetInbound(id)
  2512. if err == nil && inbound != nil && inbound.NodeID != nil {
  2513. if rt, rterr := s.runtimeFor(inbound); rterr == nil {
  2514. if e := rt.ResetClientTraffic(context.Background(), inbound, clientEmail); e != nil {
  2515. logger.Warning("ResetClientTraffic: remote propagation to", rt.Name(), "failed:", e)
  2516. }
  2517. } else {
  2518. logger.Warning("ResetClientTraffic: runtime lookup failed:", rterr)
  2519. }
  2520. }
  2521. return needRestart, nil
  2522. }
  2523. func (s *InboundService) ResetAllTraffics() error {
  2524. return submitTrafficWrite(func() error {
  2525. return s.resetAllTrafficsLocked()
  2526. })
  2527. }
  2528. func (s *InboundService) resetAllTrafficsLocked() error {
  2529. db := database.GetDB()
  2530. now := time.Now().UnixMilli()
  2531. if err := db.Model(model.Inbound{}).
  2532. Where("user_id > ?", 0).
  2533. Updates(map[string]any{
  2534. "up": 0,
  2535. "down": 0,
  2536. "last_traffic_reset_time": now,
  2537. }).Error; err != nil {
  2538. return err
  2539. }
  2540. return nil
  2541. }
  2542. func (s *InboundService) ResetInboundTraffic(id int) error {
  2543. return submitTrafficWrite(func() error {
  2544. db := database.GetDB()
  2545. return db.Model(model.Inbound{}).
  2546. Where("id = ?", id).
  2547. Updates(map[string]any{"up": 0, "down": 0}).Error
  2548. })
  2549. }
  2550. // EmailsByInbound returns the list of client emails currently configured on
  2551. // an inbound's settings.clients[]. Used by the "delete all clients" flow on
  2552. // the inbounds page, which then feeds the list into ClientService.BulkDelete.
  2553. func (s *InboundService) EmailsByInbound(inboundId int) ([]string, error) {
  2554. inbound, err := s.GetInbound(inboundId)
  2555. if err != nil {
  2556. return nil, err
  2557. }
  2558. clients, err := s.GetClients(inbound)
  2559. if err != nil {
  2560. return nil, err
  2561. }
  2562. emails := make([]string, 0, len(clients))
  2563. for _, c := range clients {
  2564. if e := strings.TrimSpace(c.Email); e != "" {
  2565. emails = append(emails, e)
  2566. }
  2567. }
  2568. return emails, nil
  2569. }
  2570. func (s *InboundService) DelDepletedClients(id int) (err error) {
  2571. db := database.GetDB()
  2572. tx := db.Begin()
  2573. defer func() {
  2574. if err == nil {
  2575. tx.Commit()
  2576. } else {
  2577. tx.Rollback()
  2578. }
  2579. }()
  2580. // Collect depleted emails globally — a shared-email row owned by one
  2581. // inbound depletes every sibling that lists the email.
  2582. now := time.Now().Unix() * 1000
  2583. depletedClause := "reset = 0 and ((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?))"
  2584. var depletedRows []xray.ClientTraffic
  2585. err = db.Model(xray.ClientTraffic{}).
  2586. Where(depletedClause, now).
  2587. Find(&depletedRows).Error
  2588. if err != nil {
  2589. return err
  2590. }
  2591. if len(depletedRows) == 0 {
  2592. return nil
  2593. }
  2594. depletedEmails := make(map[string]struct{}, len(depletedRows))
  2595. for _, r := range depletedRows {
  2596. if r.Email == "" {
  2597. continue
  2598. }
  2599. depletedEmails[strings.ToLower(r.Email)] = struct{}{}
  2600. }
  2601. if len(depletedEmails) == 0 {
  2602. return nil
  2603. }
  2604. var inbounds []*model.Inbound
  2605. inboundQuery := db.Model(model.Inbound{})
  2606. if id >= 0 {
  2607. inboundQuery = inboundQuery.Where("id = ?", id)
  2608. }
  2609. if err = inboundQuery.Find(&inbounds).Error; err != nil {
  2610. return err
  2611. }
  2612. for _, inbound := range inbounds {
  2613. var settings map[string]any
  2614. if err = json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  2615. return err
  2616. }
  2617. rawClients, ok := settings["clients"].([]any)
  2618. if !ok {
  2619. continue
  2620. }
  2621. newClients := make([]any, 0, len(rawClients))
  2622. removed := 0
  2623. for _, client := range rawClients {
  2624. c, ok := client.(map[string]any)
  2625. if !ok {
  2626. newClients = append(newClients, client)
  2627. continue
  2628. }
  2629. email, _ := c["email"].(string)
  2630. if _, isDepleted := depletedEmails[strings.ToLower(email)]; isDepleted {
  2631. removed++
  2632. continue
  2633. }
  2634. newClients = append(newClients, client)
  2635. }
  2636. if removed == 0 {
  2637. continue
  2638. }
  2639. if len(newClients) == 0 {
  2640. s.DelInbound(inbound.Id)
  2641. continue
  2642. }
  2643. settings["clients"] = newClients
  2644. ns, mErr := json.MarshalIndent(settings, "", " ")
  2645. if mErr != nil {
  2646. return mErr
  2647. }
  2648. inbound.Settings = string(ns)
  2649. if err = tx.Save(inbound).Error; err != nil {
  2650. return err
  2651. }
  2652. survivingClients, gcErr := s.GetClients(inbound)
  2653. if gcErr != nil {
  2654. err = gcErr
  2655. return err
  2656. }
  2657. if err = s.clientService.SyncInbound(tx, inbound.Id, survivingClients); err != nil {
  2658. return err
  2659. }
  2660. }
  2661. // Drop now-orphaned rows. With id >= 0, a row is safe to drop only when
  2662. // no out-of-scope inbound still references the email.
  2663. if id < 0 {
  2664. err = tx.Where(depletedClause, now).Delete(xray.ClientTraffic{}).Error
  2665. return err
  2666. }
  2667. emails := make([]string, 0, len(depletedEmails))
  2668. for e := range depletedEmails {
  2669. emails = append(emails, e)
  2670. }
  2671. var stillReferenced []string
  2672. emailExpr := database.JSONFieldText("client.value", "email")
  2673. stillQuery := fmt.Sprintf(
  2674. "SELECT DISTINCT LOWER(%s) %s WHERE LOWER(%s) IN ?",
  2675. emailExpr,
  2676. database.JSONClientsFromInbound(),
  2677. emailExpr,
  2678. )
  2679. if err = tx.Raw(stillQuery, emails).Scan(&stillReferenced).Error; err != nil {
  2680. return err
  2681. }
  2682. stillSet := make(map[string]struct{}, len(stillReferenced))
  2683. for _, e := range stillReferenced {
  2684. stillSet[e] = struct{}{}
  2685. }
  2686. toDelete := make([]string, 0, len(emails))
  2687. for _, e := range emails {
  2688. if _, kept := stillSet[e]; !kept {
  2689. toDelete = append(toDelete, e)
  2690. }
  2691. }
  2692. if len(toDelete) > 0 {
  2693. if err = tx.Where("LOWER(email) IN ?", toDelete).Delete(xray.ClientTraffic{}).Error; err != nil {
  2694. return err
  2695. }
  2696. }
  2697. return nil
  2698. }
  2699. func (s *InboundService) GetClientTrafficTgBot(tgId int64) ([]*xray.ClientTraffic, error) {
  2700. db := database.GetDB()
  2701. var inbounds []*model.Inbound
  2702. // Retrieve inbounds where settings contain the given tgId
  2703. err := db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds).Error
  2704. if err != nil && err != gorm.ErrRecordNotFound {
  2705. logger.Errorf("Error retrieving inbounds with tgId %d: %v", tgId, err)
  2706. return nil, err
  2707. }
  2708. var emails []string
  2709. for _, inbound := range inbounds {
  2710. clients, err := s.GetClients(inbound)
  2711. if err != nil {
  2712. logger.Errorf("Error retrieving clients for inbound %d: %v", inbound.Id, err)
  2713. continue
  2714. }
  2715. for _, client := range clients {
  2716. if client.TgID == tgId {
  2717. emails = append(emails, client.Email)
  2718. }
  2719. }
  2720. }
  2721. // Chunked to stay under SQLite's bind-variable limit when a single Telegram
  2722. // account owns thousands of clients across inbounds.
  2723. uniqEmails := uniqueNonEmptyStrings(emails)
  2724. traffics := make([]*xray.ClientTraffic, 0, len(uniqEmails))
  2725. for _, batch := range chunkStrings(uniqEmails, sqliteMaxVars) {
  2726. var page []*xray.ClientTraffic
  2727. if err = db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  2728. if err == gorm.ErrRecordNotFound {
  2729. continue
  2730. }
  2731. logger.Errorf("Error retrieving ClientTraffic for emails %v: %v", batch, err)
  2732. return nil, err
  2733. }
  2734. traffics = append(traffics, page...)
  2735. }
  2736. if len(traffics) == 0 {
  2737. logger.Warning("No ClientTraffic records found for emails:", emails)
  2738. return nil, nil
  2739. }
  2740. // Populate UUID and other client data for each traffic record
  2741. for i := range traffics {
  2742. if ct, client, e := s.GetClientByEmail(traffics[i].Email); e == nil && ct != nil && client != nil {
  2743. traffics[i].Enable = client.Enable
  2744. traffics[i].UUID = client.ID
  2745. traffics[i].SubId = client.SubID
  2746. }
  2747. }
  2748. return traffics, nil
  2749. }
  2750. // sqliteMaxVars is a safe ceiling for the number of bind parameters in a
  2751. // single SQL statement. SQLite's SQLITE_MAX_VARIABLE_NUMBER is 999 on builds
  2752. // before 3.32 and 32766 after; staying under 999 keeps queries portable
  2753. // across forks/old binaries and also bounds per-query memory on truly large
  2754. // installs (>32k clients) where even modern SQLite would refuse a single IN.
  2755. const sqliteMaxVars = 900
  2756. // uniqueNonEmptyStrings returns a deduplicated copy of in with empty strings
  2757. // removed, preserving the order of first occurrence.
  2758. func uniqueNonEmptyStrings(in []string) []string {
  2759. if len(in) == 0 {
  2760. return nil
  2761. }
  2762. seen := make(map[string]struct{}, len(in))
  2763. out := make([]string, 0, len(in))
  2764. for _, v := range in {
  2765. if v == "" {
  2766. continue
  2767. }
  2768. if _, ok := seen[v]; ok {
  2769. continue
  2770. }
  2771. seen[v] = struct{}{}
  2772. out = append(out, v)
  2773. }
  2774. return out
  2775. }
  2776. // uniqueInts returns a deduplicated copy of in, preserving order of first occurrence.
  2777. func uniqueInts(in []int) []int {
  2778. if len(in) == 0 {
  2779. return nil
  2780. }
  2781. seen := make(map[int]struct{}, len(in))
  2782. out := make([]int, 0, len(in))
  2783. for _, v := range in {
  2784. if _, ok := seen[v]; ok {
  2785. continue
  2786. }
  2787. seen[v] = struct{}{}
  2788. out = append(out, v)
  2789. }
  2790. return out
  2791. }
  2792. // chunkStrings splits s into consecutive sub-slices of at most size elements.
  2793. // Returns nil for an empty input or non-positive size.
  2794. func chunkStrings(s []string, size int) [][]string {
  2795. if size <= 0 || len(s) == 0 {
  2796. return nil
  2797. }
  2798. out := make([][]string, 0, (len(s)+size-1)/size)
  2799. for i := 0; i < len(s); i += size {
  2800. end := min(i+size, len(s))
  2801. out = append(out, s[i:end])
  2802. }
  2803. return out
  2804. }
  2805. // chunkInts splits s into consecutive sub-slices of at most size elements.
  2806. // Returns nil for an empty input or non-positive size.
  2807. func chunkInts(s []int, size int) [][]int {
  2808. if size <= 0 || len(s) == 0 {
  2809. return nil
  2810. }
  2811. out := make([][]int, 0, (len(s)+size-1)/size)
  2812. for i := 0; i < len(s); i += size {
  2813. end := min(i+size, len(s))
  2814. out = append(out, s[i:end])
  2815. }
  2816. return out
  2817. }
  2818. func (s *InboundService) GetActiveClientTraffics(emails []string) ([]*xray.ClientTraffic, error) {
  2819. uniq := uniqueNonEmptyStrings(emails)
  2820. if len(uniq) == 0 {
  2821. return nil, nil
  2822. }
  2823. db := database.GetDB()
  2824. traffics := make([]*xray.ClientTraffic, 0, len(uniq))
  2825. for _, batch := range chunkStrings(uniq, sqliteMaxVars) {
  2826. var page []*xray.ClientTraffic
  2827. if err := db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  2828. return nil, err
  2829. }
  2830. traffics = append(traffics, page...)
  2831. }
  2832. return traffics, nil
  2833. }
  2834. // GetAllClientTraffics returns the full set of client_traffics rows so the
  2835. // websocket broadcasters can ship a complete snapshot every cycle. The old
  2836. // delta-only path (GetActiveClientTraffics on activeEmails) silently dropped
  2837. // the per-client section whenever no client moved bytes in the cycle or a
  2838. // node sync failed, leaving client rows in the UI stuck at stale numbers.
  2839. func (s *InboundService) GetAllClientTraffics() ([]*xray.ClientTraffic, error) {
  2840. db := database.GetDB()
  2841. var traffics []*xray.ClientTraffic
  2842. if err := db.Model(xray.ClientTraffic{}).Find(&traffics).Error; err != nil {
  2843. return nil, err
  2844. }
  2845. return traffics, nil
  2846. }
  2847. type InboundTrafficSummary struct {
  2848. Id int `json:"id"`
  2849. Up int64 `json:"up"`
  2850. Down int64 `json:"down"`
  2851. Total int64 `json:"total"`
  2852. Enable bool `json:"enable"`
  2853. }
  2854. func (s *InboundService) GetInboundsTrafficSummary() ([]InboundTrafficSummary, error) {
  2855. db := database.GetDB()
  2856. var summaries []InboundTrafficSummary
  2857. if err := db.Model(&model.Inbound{}).
  2858. Select("id, up, down, total, enable").
  2859. Find(&summaries).Error; err != nil {
  2860. return nil, err
  2861. }
  2862. return summaries, nil
  2863. }
  2864. func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.ClientTraffic, err error) {
  2865. db := database.GetDB()
  2866. var traffics []*xray.ClientTraffic
  2867. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error; err != nil {
  2868. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  2869. return nil, err
  2870. }
  2871. if len(traffics) == 0 {
  2872. return nil, nil
  2873. }
  2874. t := traffics[0]
  2875. if rec, rErr := s.clientService.GetRecordByEmail(db, email); rErr == nil && rec != nil {
  2876. c := rec.ToClient()
  2877. t.UUID = c.ID
  2878. t.SubId = c.SubID
  2879. return t, nil
  2880. }
  2881. t2, client, err := s.GetClientByEmail(email)
  2882. if err != nil {
  2883. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  2884. return nil, err
  2885. }
  2886. if t2 != nil && client != nil {
  2887. t2.UUID = client.ID
  2888. t2.SubId = client.SubID
  2889. return t2, nil
  2890. }
  2891. return nil, nil
  2892. }
  2893. func (s *InboundService) UpdateClientTrafficByEmail(email string, upload int64, download int64) error {
  2894. return submitTrafficWrite(func() error {
  2895. db := database.GetDB()
  2896. err := db.Model(xray.ClientTraffic{}).
  2897. Where("email = ?", email).
  2898. Updates(map[string]any{
  2899. "up": upload,
  2900. "down": download,
  2901. }).Error
  2902. if err != nil {
  2903. logger.Warningf("Error updating ClientTraffic with email %s: %v", email, err)
  2904. }
  2905. return err
  2906. })
  2907. }
  2908. func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) {
  2909. db := database.GetDB()
  2910. inbound := &model.Inbound{}
  2911. traffic = &xray.ClientTraffic{}
  2912. // Search for inbound settings that contain the query
  2913. err = db.Model(model.Inbound{}).Where("settings LIKE ?", "%\""+query+"\"%").First(inbound).Error
  2914. if err != nil {
  2915. if err == gorm.ErrRecordNotFound {
  2916. logger.Warningf("Inbound settings containing query %s not found: %v", query, err)
  2917. return nil, err
  2918. }
  2919. logger.Errorf("Error searching for inbound settings with query %s: %v", query, err)
  2920. return nil, err
  2921. }
  2922. traffic.InboundId = inbound.Id
  2923. // Unmarshal settings to get clients
  2924. settings := map[string][]model.Client{}
  2925. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  2926. logger.Errorf("Error unmarshalling inbound settings for inbound ID %d: %v", inbound.Id, err)
  2927. return nil, err
  2928. }
  2929. clients := settings["clients"]
  2930. for _, client := range clients {
  2931. if (client.ID == query || client.Password == query) && client.Email != "" {
  2932. traffic.Email = client.Email
  2933. break
  2934. }
  2935. }
  2936. if traffic.Email == "" {
  2937. logger.Warningf("No client found with query %s in inbound ID %d", query, inbound.Id)
  2938. return nil, gorm.ErrRecordNotFound
  2939. }
  2940. // Retrieve ClientTraffic based on the found email
  2941. err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error
  2942. if err != nil {
  2943. if err == gorm.ErrRecordNotFound {
  2944. logger.Warningf("ClientTraffic for email %s not found: %v", traffic.Email, err)
  2945. return nil, err
  2946. }
  2947. logger.Errorf("Error retrieving ClientTraffic for email %s: %v", traffic.Email, err)
  2948. return nil, err
  2949. }
  2950. return traffic, nil
  2951. }
  2952. func (s *InboundService) GetInboundClientIps(clientEmail string) (string, error) {
  2953. db := database.GetDB()
  2954. InboundClientIps := &model.InboundClientIps{}
  2955. err := db.Model(model.InboundClientIps{}).Where("client_email = ?", clientEmail).First(InboundClientIps).Error
  2956. if err != nil {
  2957. return "", err
  2958. }
  2959. if InboundClientIps.Ips == "" {
  2960. return "", nil
  2961. }
  2962. // Try to parse as new format (with timestamps)
  2963. type IPWithTimestamp struct {
  2964. IP string `json:"ip"`
  2965. Timestamp int64 `json:"timestamp"`
  2966. }
  2967. var ipsWithTime []IPWithTimestamp
  2968. err = json.Unmarshal([]byte(InboundClientIps.Ips), &ipsWithTime)
  2969. // If successfully parsed as new format, return with timestamps
  2970. if err == nil && len(ipsWithTime) > 0 {
  2971. return InboundClientIps.Ips, nil
  2972. }
  2973. // Otherwise, assume it's old format (simple string array)
  2974. // Try to parse as simple array and convert to new format
  2975. var oldIps []string
  2976. err = json.Unmarshal([]byte(InboundClientIps.Ips), &oldIps)
  2977. if err == nil && len(oldIps) > 0 {
  2978. // Convert old format to new format with current timestamp
  2979. newIpsWithTime := make([]IPWithTimestamp, len(oldIps))
  2980. for i, ip := range oldIps {
  2981. newIpsWithTime[i] = IPWithTimestamp{
  2982. IP: ip,
  2983. Timestamp: time.Now().Unix(),
  2984. }
  2985. }
  2986. result, _ := json.Marshal(newIpsWithTime)
  2987. return string(result), nil
  2988. }
  2989. // Return as-is if parsing fails
  2990. return InboundClientIps.Ips, nil
  2991. }
  2992. func (s *InboundService) ClearClientIps(clientEmail string) error {
  2993. db := database.GetDB()
  2994. result := db.Model(model.InboundClientIps{}).
  2995. Where("client_email = ?", clientEmail).
  2996. Update("ips", "")
  2997. err := result.Error
  2998. if err != nil {
  2999. return err
  3000. }
  3001. return nil
  3002. }
  3003. func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error) {
  3004. db := database.GetDB()
  3005. var inbounds []*model.Inbound
  3006. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("remark like ?", "%"+query+"%").Find(&inbounds).Error
  3007. if err != nil && err != gorm.ErrRecordNotFound {
  3008. return nil, err
  3009. }
  3010. return inbounds, nil
  3011. }
  3012. func (s *InboundService) MigrationRequirements() {
  3013. db := database.GetDB()
  3014. tx := db.Begin()
  3015. var err error
  3016. defer func() {
  3017. if err == nil {
  3018. tx.Commit()
  3019. if !database.IsPostgres() {
  3020. if dbErr := db.Exec(`VACUUM "main"`).Error; dbErr != nil {
  3021. logger.Warningf("VACUUM failed: %v", dbErr)
  3022. }
  3023. }
  3024. } else {
  3025. tx.Rollback()
  3026. }
  3027. }()
  3028. if tx.Migrator().HasColumn(&model.Inbound{}, "all_time") {
  3029. if err = tx.Migrator().DropColumn(&model.Inbound{}, "all_time"); err != nil {
  3030. return
  3031. }
  3032. }
  3033. if tx.Migrator().HasColumn(&xray.ClientTraffic{}, "all_time") {
  3034. if err = tx.Migrator().DropColumn(&xray.ClientTraffic{}, "all_time"); err != nil {
  3035. return
  3036. }
  3037. }
  3038. // Fix inbounds based problems
  3039. var inbounds []*model.Inbound
  3040. err = tx.Model(model.Inbound{}).Where("protocol IN (?)", []string{"vmess", "vless", "trojan", "shadowsocks", "hysteria"}).Find(&inbounds).Error
  3041. if err != nil && err != gorm.ErrRecordNotFound {
  3042. return
  3043. }
  3044. for inbound_index := range inbounds {
  3045. settings := map[string]any{}
  3046. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  3047. if raw, exists := settings["clients"]; exists && raw == nil {
  3048. settings["clients"] = []any{}
  3049. }
  3050. clients, ok := settings["clients"].([]any)
  3051. if ok {
  3052. // Fix Client configuration problems
  3053. newClients := make([]any, 0, len(clients))
  3054. hasVisionFlow := false
  3055. for client_index := range clients {
  3056. c := clients[client_index].(map[string]any)
  3057. // Add email='' if it is not exists
  3058. if _, ok := c["email"]; !ok {
  3059. c["email"] = ""
  3060. }
  3061. // Convert string tgId to int64
  3062. if _, ok := c["tgId"]; ok {
  3063. var tgId any = c["tgId"]
  3064. if tgIdStr, ok2 := tgId.(string); ok2 {
  3065. tgIdInt64, err := strconv.ParseInt(strings.ReplaceAll(tgIdStr, " ", ""), 10, 64)
  3066. if err == nil {
  3067. c["tgId"] = tgIdInt64
  3068. }
  3069. }
  3070. }
  3071. // Remove "flow": "xtls-rprx-direct"
  3072. if _, ok := c["flow"]; ok {
  3073. if c["flow"] == "xtls-rprx-direct" {
  3074. c["flow"] = ""
  3075. }
  3076. }
  3077. if flow, _ := c["flow"].(string); flow == "xtls-rprx-vision" {
  3078. hasVisionFlow = true
  3079. }
  3080. // Backfill created_at and updated_at
  3081. if _, ok := c["created_at"]; !ok {
  3082. c["created_at"] = time.Now().Unix() * 1000
  3083. }
  3084. c["updated_at"] = time.Now().Unix() * 1000
  3085. newClients = append(newClients, any(c))
  3086. }
  3087. settings["clients"] = newClients
  3088. // Drop orphaned testseed: VLESS-only field, only meaningful when at least
  3089. // one client uses the exact xtls-rprx-vision flow. Older versions saved it
  3090. // for any non-empty flow (including the UDP variant) or kept it after the
  3091. // flow was cleared from the client modal — clean those up here.
  3092. if inbounds[inbound_index].Protocol == model.VLESS && !hasVisionFlow {
  3093. delete(settings, "testseed")
  3094. }
  3095. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  3096. if err != nil {
  3097. return
  3098. }
  3099. inbounds[inbound_index].Settings = string(modifiedSettings)
  3100. }
  3101. // Add client traffic row for all clients which has email
  3102. modelClients, err := s.GetClients(inbounds[inbound_index])
  3103. if err != nil {
  3104. return
  3105. }
  3106. for _, modelClient := range modelClients {
  3107. if len(modelClient.Email) > 0 {
  3108. var count int64
  3109. tx.Model(xray.ClientTraffic{}).Where("email = ?", modelClient.Email).Count(&count)
  3110. if count == 0 {
  3111. s.AddClientStat(tx, inbounds[inbound_index].Id, &modelClient)
  3112. }
  3113. }
  3114. }
  3115. // Heal clients table for installs where the one-shot seeder
  3116. // skipped clients due to a tgId-string unmarshal error.
  3117. if syncErr := s.clientService.SyncInbound(tx, inbounds[inbound_index].Id, modelClients); syncErr != nil {
  3118. logger.Warning("MigrationRequirements sync clients failed:", syncErr)
  3119. }
  3120. }
  3121. tx.Save(inbounds)
  3122. // Remove orphaned traffics
  3123. tx.Where("inbound_id = 0").Delete(xray.ClientTraffic{})
  3124. // Migrate old MultiDomain to External Proxy
  3125. var externalProxy []struct {
  3126. Id int
  3127. Port int
  3128. StreamSettings []byte
  3129. }
  3130. externalProxyQuery := `select id, port, stream_settings
  3131. from inbounds
  3132. WHERE protocol in ('vmess','vless','trojan')
  3133. AND json_extract(stream_settings, '$.security') = 'tls'
  3134. AND json_extract(stream_settings, '$.tlsSettings.settings.domains') IS NOT NULL`
  3135. if database.IsPostgres() {
  3136. externalProxyQuery = `select id, port, stream_settings
  3137. from inbounds
  3138. WHERE protocol in ('vmess','vless','trojan')
  3139. AND NULLIF(stream_settings, '')::jsonb #>> '{security}' = 'tls'
  3140. AND NULLIF(stream_settings, '')::jsonb #> '{tlsSettings,settings,domains}' IS NOT NULL`
  3141. }
  3142. err = tx.Raw(externalProxyQuery).Scan(&externalProxy).Error
  3143. if err != nil || len(externalProxy) == 0 {
  3144. return
  3145. }
  3146. for _, ep := range externalProxy {
  3147. var reverses any
  3148. var stream map[string]any
  3149. json.Unmarshal(ep.StreamSettings, &stream)
  3150. if tlsSettings, ok := stream["tlsSettings"].(map[string]any); ok {
  3151. if settings, ok := tlsSettings["settings"].(map[string]any); ok {
  3152. if domains, ok := settings["domains"].([]any); ok {
  3153. for _, domain := range domains {
  3154. if domainMap, ok := domain.(map[string]any); ok {
  3155. domainMap["forceTls"] = "same"
  3156. domainMap["port"] = ep.Port
  3157. domainMap["dest"] = domainMap["domain"].(string)
  3158. delete(domainMap, "domain")
  3159. }
  3160. }
  3161. }
  3162. reverses = settings["domains"]
  3163. delete(settings, "domains")
  3164. }
  3165. }
  3166. stream["externalProxy"] = reverses
  3167. newStream, _ := json.MarshalIndent(stream, " ", " ")
  3168. tx.Model(model.Inbound{}).Where("id = ?", ep.Id).Update("stream_settings", newStream)
  3169. }
  3170. err = tx.Raw(`UPDATE inbounds
  3171. SET tag = REPLACE(tag, '0.0.0.0:', '')
  3172. WHERE INSTR(tag, '0.0.0.0:') > 0;`).Error
  3173. if err != nil {
  3174. return
  3175. }
  3176. }
  3177. func (s *InboundService) MigrateDB() {
  3178. s.MigrationRequirements()
  3179. s.MigrationRemoveOrphanedTraffics()
  3180. }
  3181. func (s *InboundService) GetOnlineClients() []string {
  3182. if p == nil {
  3183. return []string{}
  3184. }
  3185. return p.GetOnlineClients()
  3186. }
  3187. func (s *InboundService) GetOnlineClientsByNode() map[int][]string {
  3188. if p == nil {
  3189. return map[int][]string{}
  3190. }
  3191. return p.GetOnlineClientsByNode()
  3192. }
  3193. func (s *InboundService) GetActiveInboundsByNode() map[int][]string {
  3194. if p == nil {
  3195. return map[int][]string{}
  3196. }
  3197. return p.GetActiveInboundsByNode()
  3198. }
  3199. func (s *InboundService) SetNodeOnlineClients(nodeID int, emails []string) {
  3200. if p != nil {
  3201. p.SetNodeOnlineClients(nodeID, emails)
  3202. }
  3203. }
  3204. func (s *InboundService) ClearNodeOnlineClients(nodeID int) {
  3205. if p != nil {
  3206. p.ClearNodeOnlineClients(nodeID)
  3207. }
  3208. }
  3209. func (s *InboundService) GetClientsLastOnline() (map[string]int64, error) {
  3210. db := database.GetDB()
  3211. var rows []xray.ClientTraffic
  3212. err := db.Model(&xray.ClientTraffic{}).Select("email, last_online").Find(&rows).Error
  3213. if err != nil && err != gorm.ErrRecordNotFound {
  3214. return nil, err
  3215. }
  3216. result := make(map[string]int64, len(rows))
  3217. for _, r := range rows {
  3218. result[r.Email] = r.LastOnline
  3219. }
  3220. return result, nil
  3221. }
  3222. // RefreshLocalOnlineClients folds the emails and inbound tags active on this
  3223. // panel's own xray this poll into the local online/active sets, applying the
  3224. // grace window and pruning stale entries. Pass nil to only prune. See
  3225. // xray.Process for why the local sets are kept separate from the shared
  3226. // last_online column.
  3227. func (s *InboundService) RefreshLocalOnlineClients(activeEmails, activeInboundTags []string) {
  3228. if p != nil {
  3229. p.RefreshLocalOnline(activeEmails, activeInboundTags, time.Now().UnixMilli(), onlineGracePeriodMs)
  3230. }
  3231. }
  3232. func (s *InboundService) FilterAndSortClientEmails(emails []string) ([]string, []string, error) {
  3233. db := database.GetDB()
  3234. // Step 1: Get ClientTraffic records for emails in the input list.
  3235. // Chunked to stay under SQLite's bind-variable limit on huge inputs.
  3236. uniqEmails := uniqueNonEmptyStrings(emails)
  3237. clients := make([]xray.ClientTraffic, 0, len(uniqEmails))
  3238. for _, batch := range chunkStrings(uniqEmails, sqliteMaxVars) {
  3239. var page []xray.ClientTraffic
  3240. if err := db.Where("email IN ?", batch).Find(&page).Error; err != nil && err != gorm.ErrRecordNotFound {
  3241. return nil, nil, err
  3242. }
  3243. clients = append(clients, page...)
  3244. }
  3245. // Step 2: Sort clients by (Up + Down) descending
  3246. sort.Slice(clients, func(i, j int) bool {
  3247. return (clients[i].Up + clients[i].Down) > (clients[j].Up + clients[j].Down)
  3248. })
  3249. // Step 3: Extract sorted valid emails and track found ones
  3250. validEmails := make([]string, 0, len(clients))
  3251. found := make(map[string]bool)
  3252. for _, client := range clients {
  3253. validEmails = append(validEmails, client.Email)
  3254. found[client.Email] = true
  3255. }
  3256. // Step 4: Identify emails that were not found in the database
  3257. extraEmails := make([]string, 0)
  3258. for _, email := range emails {
  3259. if !found[email] {
  3260. extraEmails = append(extraEmails, email)
  3261. }
  3262. }
  3263. return validEmails, extraEmails, nil
  3264. }
  3265. type SubLinkProvider interface {
  3266. SubLinksForSubId(host, subId string) ([]string, error)
  3267. LinksForClient(host string, inbound *model.Inbound, email string) []string
  3268. }
  3269. var registeredSubLinkProvider SubLinkProvider
  3270. func RegisterSubLinkProvider(p SubLinkProvider) {
  3271. registeredSubLinkProvider = p
  3272. }
  3273. func (s *InboundService) GetSubLinks(host, subId string) ([]string, error) {
  3274. if registeredSubLinkProvider == nil {
  3275. return nil, common.NewError("sub link provider not registered")
  3276. }
  3277. return registeredSubLinkProvider.SubLinksForSubId(host, subId)
  3278. }
  3279. func (s *InboundService) GetAllClientLinks(host string, email string) ([]string, error) {
  3280. if email == "" {
  3281. return nil, common.NewError("client email is required")
  3282. }
  3283. if registeredSubLinkProvider == nil {
  3284. return nil, common.NewError("sub link provider not registered")
  3285. }
  3286. rec, err := s.clientService.GetRecordByEmail(nil, email)
  3287. if err != nil {
  3288. return nil, err
  3289. }
  3290. inboundIds, err := s.clientService.GetInboundIdsForRecord(rec.Id)
  3291. if err != nil {
  3292. return nil, err
  3293. }
  3294. var links []string
  3295. for _, ibId := range inboundIds {
  3296. inbound, getErr := s.GetInbound(ibId)
  3297. if getErr != nil {
  3298. return nil, getErr
  3299. }
  3300. links = append(links, registeredSubLinkProvider.LinksForClient(host, inbound, email)...)
  3301. }
  3302. return links, nil
  3303. }