1
0

inbound.go 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552
  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. if err := tx.Exec(
  1494. fmt.Sprintf(
  1495. `UPDATE client_traffics
  1496. SET up = up + ?, down = down + ?, enable = ?, total = ?, expiry_time = ?, reset = ?,
  1497. last_online = %s
  1498. WHERE email = ?`,
  1499. database.GreatestExpr("last_online", "?"),
  1500. ),
  1501. deltaUp, deltaDown, cs.Enable, cs.Total, cs.ExpiryTime, cs.Reset,
  1502. cs.LastOnline, cs.Email,
  1503. ).Error; err != nil {
  1504. return false, err
  1505. }
  1506. if err := s.upsertNodeBaseline(tx, nodeID, cs.Email, cs.Up, cs.Down); err != nil {
  1507. return false, err
  1508. }
  1509. nodeBaselines[cs.Email] = nodeTrafficCounter{Up: cs.Up, Down: cs.Down}
  1510. }
  1511. for k, existing := range centralCS {
  1512. if k.inboundID != c.Id {
  1513. continue
  1514. }
  1515. if _, kept := snapEmails[k.email]; kept {
  1516. continue
  1517. }
  1518. if err := tx.Where("node_id = ? AND email = ?", nodeID, existing.Email).
  1519. Delete(&model.NodeClientTraffic{}).Error; err != nil {
  1520. return false, err
  1521. }
  1522. if err := tx.Where("inbound_id = ? AND email = ?", c.Id, existing.Email).
  1523. Delete(&xray.ClientTraffic{}).Error; err != nil {
  1524. return false, err
  1525. }
  1526. structuralChange = true
  1527. }
  1528. }
  1529. type oldSet struct {
  1530. inboundID int
  1531. emails map[string]struct{}
  1532. }
  1533. var perInboundOld []oldSet
  1534. for _, snapIb := range snap.Inbounds {
  1535. if snapIb == nil {
  1536. continue
  1537. }
  1538. c, ok := tagToCentral[snapIb.Tag]
  1539. if !ok {
  1540. continue
  1541. }
  1542. var oldEmailsRows []string
  1543. if err := tx.Table("clients").
  1544. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  1545. Where("client_inbounds.inbound_id = ?", c.Id).
  1546. Pluck("email", &oldEmailsRows).Error; err == nil {
  1547. oldEmails := make(map[string]struct{}, len(oldEmailsRows))
  1548. for _, e := range oldEmailsRows {
  1549. if e != "" {
  1550. oldEmails[e] = struct{}{}
  1551. }
  1552. }
  1553. perInboundOld = append(perInboundOld, oldSet{inboundID: c.Id, emails: oldEmails})
  1554. }
  1555. clients, gcErr := s.GetClients(snapIb)
  1556. if gcErr != nil {
  1557. logger.Warningf("setRemoteTraffic: parse clients for tag %q failed: %v", snapIb.Tag, gcErr)
  1558. continue
  1559. }
  1560. csEnableByEmail := make(map[string]bool, len(snapIb.ClientStats))
  1561. for _, cs := range snapIb.ClientStats {
  1562. csEnableByEmail[cs.Email] = cs.Enable
  1563. }
  1564. filtered := clients[:0]
  1565. for i := range clients {
  1566. if isClientEmailTombstoned(clients[i].Email) {
  1567. continue
  1568. }
  1569. if cse, hit := csEnableByEmail[clients[i].Email]; hit && !cse {
  1570. clients[i].Enable = false
  1571. }
  1572. filtered = append(filtered, clients[i])
  1573. }
  1574. localEmails := make([]string, 0, len(filtered))
  1575. for i := range filtered {
  1576. if filtered[i].Email != "" {
  1577. localEmails = append(localEmails, filtered[i].Email)
  1578. }
  1579. }
  1580. if len(localEmails) > 0 {
  1581. var localMeta []struct {
  1582. Email string
  1583. Comment string `gorm:"column:comment"`
  1584. }
  1585. if err := tx.Table("clients").
  1586. Select("email, comment").
  1587. Where("email IN ?", localEmails).
  1588. Find(&localMeta).Error; err == nil {
  1589. commentByEmail := make(map[string]string, len(localMeta))
  1590. for _, m := range localMeta {
  1591. commentByEmail[m.Email] = m.Comment
  1592. }
  1593. for i := range filtered {
  1594. if cmt, ok := commentByEmail[filtered[i].Email]; ok {
  1595. filtered[i].Comment = cmt
  1596. }
  1597. }
  1598. }
  1599. }
  1600. if err := s.clientService.SyncInbound(tx, c.Id, filtered); err != nil {
  1601. logger.Warningf("setRemoteTraffic: sync clients for tag %q failed: %v", snapIb.Tag, err)
  1602. }
  1603. }
  1604. for _, old := range perInboundOld {
  1605. var stillAttached []string
  1606. if err := tx.Table("clients").
  1607. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  1608. Where("client_inbounds.inbound_id = ?", old.inboundID).
  1609. Pluck("email", &stillAttached).Error; err != nil {
  1610. continue
  1611. }
  1612. stillSet := make(map[string]struct{}, len(stillAttached))
  1613. for _, e := range stillAttached {
  1614. stillSet[e] = struct{}{}
  1615. }
  1616. for email := range old.emails {
  1617. if _, kept := stillSet[email]; kept {
  1618. continue
  1619. }
  1620. var attachmentCount int64
  1621. if err := tx.Table("client_inbounds").
  1622. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  1623. Where("clients.email = ?", email).
  1624. Count(&attachmentCount).Error; err != nil {
  1625. continue
  1626. }
  1627. if attachmentCount > 0 {
  1628. continue
  1629. }
  1630. if err := tx.Where("email = ?", email).Delete(&model.ClientRecord{}).Error; err != nil {
  1631. logger.Warningf("setRemoteTraffic: delete ClientRecord %q failed: %v", email, err)
  1632. }
  1633. if err := tx.Where("email = ?", email).Delete(&xray.ClientTraffic{}).Error; err != nil {
  1634. logger.Warningf("setRemoteTraffic: delete ClientTraffic %q failed: %v", email, err)
  1635. }
  1636. if err := tx.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  1637. logger.Warningf("setRemoteTraffic: delete NodeClientTraffic %q failed: %v", email, err)
  1638. }
  1639. structuralChange = true
  1640. }
  1641. }
  1642. if err := tx.Commit().Error; err != nil {
  1643. return false, err
  1644. }
  1645. committed = true
  1646. if p != nil {
  1647. p.SetNodeOnlineClients(nodeID, snap.OnlineEmails)
  1648. }
  1649. return structuralChange, nil
  1650. }
  1651. func (s *InboundService) AddTraffic(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (needRestart bool, clientsDisabled bool, err error) {
  1652. err = submitTrafficWrite(func() error {
  1653. var inner error
  1654. needRestart, clientsDisabled, inner = s.addTrafficLocked(inboundTraffics, clientTraffics)
  1655. return inner
  1656. })
  1657. return
  1658. }
  1659. func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (bool, bool, error) {
  1660. var err error
  1661. db := database.GetDB()
  1662. tx := db.Begin()
  1663. defer func() {
  1664. if err != nil {
  1665. tx.Rollback()
  1666. } else {
  1667. tx.Commit()
  1668. }
  1669. }()
  1670. err = s.addInboundTraffic(tx, inboundTraffics)
  1671. if err != nil {
  1672. return false, false, err
  1673. }
  1674. err = s.addClientTraffic(tx, clientTraffics)
  1675. if err != nil {
  1676. return false, false, err
  1677. }
  1678. needRestart0, count, err := s.autoRenewClients(tx)
  1679. if err != nil {
  1680. logger.Warning("Error in renew clients:", err)
  1681. } else if count > 0 {
  1682. logger.Debugf("%v clients renewed", count)
  1683. }
  1684. disabledClientsCount := int64(0)
  1685. needRestart1, count, err := s.disableInvalidClients(tx)
  1686. if err != nil {
  1687. logger.Warning("Error in disabling invalid clients:", err)
  1688. } else if count > 0 {
  1689. logger.Debugf("%v clients disabled", count)
  1690. disabledClientsCount = count
  1691. }
  1692. needRestart2, count, err := s.disableInvalidInbounds(tx)
  1693. if err != nil {
  1694. logger.Warning("Error in disabling invalid inbounds:", err)
  1695. } else if count > 0 {
  1696. logger.Debugf("%v inbounds disabled", count)
  1697. }
  1698. return needRestart0 || needRestart1 || needRestart2, disabledClientsCount > 0, nil
  1699. }
  1700. func (s *InboundService) addInboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
  1701. if len(traffics) == 0 {
  1702. return nil
  1703. }
  1704. var err error
  1705. for _, traffic := range traffics {
  1706. if traffic.IsInbound {
  1707. err = tx.Model(&model.Inbound{}).Where("tag = ? AND node_id IS NULL", traffic.Tag).
  1708. Updates(map[string]any{
  1709. "up": gorm.Expr("up + ?", traffic.Up),
  1710. "down": gorm.Expr("down + ?", traffic.Down),
  1711. }).Error
  1712. if err != nil {
  1713. return err
  1714. }
  1715. }
  1716. }
  1717. return nil
  1718. }
  1719. func (s *InboundService) addClientTraffic(tx *gorm.DB, traffics []*xray.ClientTraffic) (err error) {
  1720. if len(traffics) == 0 {
  1721. return nil
  1722. }
  1723. emails := make([]string, 0, len(traffics))
  1724. for _, traffic := range traffics {
  1725. emails = append(emails, traffic.Email)
  1726. }
  1727. dbClientTraffics := make([]*xray.ClientTraffic, 0, len(traffics))
  1728. err = tx.Model(xray.ClientTraffic{}).
  1729. Where("email IN (?) AND inbound_id NOT IN (?)", emails,
  1730. tx.Model(&model.Inbound{}).Select("id").Where("node_id IS NOT NULL")).
  1731. Find(&dbClientTraffics).Error
  1732. if err != nil {
  1733. return err
  1734. }
  1735. // Avoid empty slice error
  1736. if len(dbClientTraffics) == 0 {
  1737. return nil
  1738. }
  1739. dbClientTraffics, err = s.adjustTraffics(tx, dbClientTraffics)
  1740. if err != nil {
  1741. return err
  1742. }
  1743. // Index by email for O(N) merge — the previous nested loop was O(N²)
  1744. // and dominated each cron tick on inbounds with thousands of active
  1745. // clients (7500 × 7500 = 56M string comparisons every 10 seconds).
  1746. trafficByEmail := make(map[string]*xray.ClientTraffic, len(traffics))
  1747. for i := range traffics {
  1748. if traffics[i] != nil {
  1749. trafficByEmail[traffics[i].Email] = traffics[i]
  1750. }
  1751. }
  1752. now := time.Now().UnixMilli()
  1753. for dbTraffic_index := range dbClientTraffics {
  1754. t, ok := trafficByEmail[dbClientTraffics[dbTraffic_index].Email]
  1755. if !ok {
  1756. continue
  1757. }
  1758. dbClientTraffics[dbTraffic_index].Up += t.Up
  1759. dbClientTraffics[dbTraffic_index].Down += t.Down
  1760. if t.Up+t.Down > 0 {
  1761. dbClientTraffics[dbTraffic_index].LastOnline = now
  1762. }
  1763. }
  1764. err = tx.Save(dbClientTraffics).Error
  1765. if err != nil {
  1766. logger.Warning("AddClientTraffic update data ", err)
  1767. }
  1768. return nil
  1769. }
  1770. func (s *InboundService) adjustTraffics(tx *gorm.DB, dbClientTraffics []*xray.ClientTraffic) ([]*xray.ClientTraffic, error) {
  1771. now := time.Now().UnixMilli()
  1772. // "Start After First Use" stores a negative expiry (the duration). On the
  1773. // first traffic tick it becomes an absolute deadline of now+duration. Compute
  1774. // it once per email so every inbound the client is attached to lands on the
  1775. // same value (recomputing per inbound would skip all but the first one).
  1776. newExpiryByEmail := make(map[string]int64, len(dbClientTraffics))
  1777. for traffic_index := range dbClientTraffics {
  1778. if dbClientTraffics[traffic_index].ExpiryTime < 0 {
  1779. newExpiryByEmail[dbClientTraffics[traffic_index].Email] = now - dbClientTraffics[traffic_index].ExpiryTime
  1780. }
  1781. }
  1782. if len(newExpiryByEmail) == 0 {
  1783. return dbClientTraffics, nil
  1784. }
  1785. delayedEmails := make([]string, 0, len(newExpiryByEmail))
  1786. for email := range newExpiryByEmail {
  1787. delayedEmails = append(delayedEmails, email)
  1788. }
  1789. // Resolve the owning inbounds through the client_inbounds link, which is
  1790. // authoritative. client_traffics.inbound_id goes stale when an inbound is
  1791. // deleted and recreated, which would leave the negative expiry unconverted.
  1792. var inboundIds []int
  1793. err := tx.Table("client_inbounds").
  1794. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  1795. Where("clients.email IN (?)", delayedEmails).
  1796. Distinct().
  1797. Pluck("client_inbounds.inbound_id", &inboundIds).Error
  1798. if err != nil {
  1799. return nil, err
  1800. }
  1801. if len(inboundIds) == 0 {
  1802. return dbClientTraffics, nil
  1803. }
  1804. var inbounds []*model.Inbound
  1805. err = tx.Model(model.Inbound{}).Where("id IN (?)", inboundIds).Find(&inbounds).Error
  1806. if err != nil {
  1807. return nil, err
  1808. }
  1809. for inbound_index := range inbounds {
  1810. settings := map[string]any{}
  1811. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  1812. clients, ok := settings["clients"].([]any)
  1813. if ok {
  1814. var newClients []any
  1815. for client_index := range clients {
  1816. c := clients[client_index].(map[string]any)
  1817. email, _ := c["email"].(string)
  1818. if newExpiry, ok := newExpiryByEmail[email]; ok {
  1819. c["expiryTime"] = newExpiry
  1820. c["updated_at"] = now
  1821. }
  1822. if _, ok := c["created_at"]; !ok {
  1823. c["created_at"] = now
  1824. }
  1825. if _, ok := c["updated_at"]; !ok {
  1826. c["updated_at"] = now
  1827. }
  1828. newClients = append(newClients, any(c))
  1829. }
  1830. settings["clients"] = newClients
  1831. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  1832. if err != nil {
  1833. return nil, err
  1834. }
  1835. inbounds[inbound_index].Settings = string(modifiedSettings)
  1836. }
  1837. }
  1838. for traffic_index := range dbClientTraffics {
  1839. if newExpiry, ok := newExpiryByEmail[dbClientTraffics[traffic_index].Email]; ok {
  1840. dbClientTraffics[traffic_index].ExpiryTime = newExpiry
  1841. }
  1842. }
  1843. err = tx.Save(inbounds).Error
  1844. if err != nil {
  1845. logger.Warning("AddClientTraffic update inbounds ", err)
  1846. logger.Error(inbounds)
  1847. } else {
  1848. for _, ib := range inbounds {
  1849. if ib == nil {
  1850. continue
  1851. }
  1852. cs, gcErr := s.GetClients(ib)
  1853. if gcErr != nil {
  1854. logger.Warning("AddClientTraffic sync clients: GetClients failed", gcErr)
  1855. continue
  1856. }
  1857. if syncErr := s.clientService.SyncInbound(tx, ib.Id, cs); syncErr != nil {
  1858. logger.Warning("AddClientTraffic sync clients: SyncInbound failed", syncErr)
  1859. }
  1860. }
  1861. }
  1862. return dbClientTraffics, nil
  1863. }
  1864. func (s *InboundService) autoRenewClients(tx *gorm.DB) (bool, int64, error) {
  1865. // check for time expired
  1866. var traffics []*xray.ClientTraffic
  1867. now := time.Now().Unix() * 1000
  1868. var err, err1 error
  1869. err = tx.Model(xray.ClientTraffic{}).
  1870. Where("reset > 0 and expiry_time > 0 and expiry_time <= ?", now).
  1871. Where("inbound_id NOT IN (?)", tx.Model(&model.Inbound{}).Select("id").Where("node_id IS NOT NULL")).
  1872. Find(&traffics).Error
  1873. if err != nil {
  1874. return false, 0, err
  1875. }
  1876. // return if there is no client to renew
  1877. if len(traffics) == 0 {
  1878. return false, 0, nil
  1879. }
  1880. var inbound_ids []int
  1881. var inbounds []*model.Inbound
  1882. needRestart := false
  1883. var clientsToAdd []struct {
  1884. protocol string
  1885. tag string
  1886. client map[string]any
  1887. }
  1888. // Resolve the inbounds to renew through the client_inbounds link rather than
  1889. // client_traffics.inbound_id, which goes stale after an inbound is deleted and
  1890. // recreated and would otherwise skip the renew entirely.
  1891. renewEmails := make([]string, 0, len(traffics))
  1892. for _, traffic := range traffics {
  1893. renewEmails = append(renewEmails, traffic.Email)
  1894. }
  1895. for _, batch := range chunkStrings(renewEmails, sqliteMaxVars) {
  1896. var ids []int
  1897. if err = tx.Table("client_inbounds").
  1898. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  1899. Where("clients.email IN ?", batch).
  1900. Distinct().
  1901. Pluck("client_inbounds.inbound_id", &ids).Error; err != nil {
  1902. return false, 0, err
  1903. }
  1904. inbound_ids = append(inbound_ids, ids...)
  1905. }
  1906. // Dedupe so an inbound hosting N expired clients is fetched and saved once
  1907. // per tick instead of N times across chunk boundaries.
  1908. inbound_ids = uniqueInts(inbound_ids)
  1909. // Chunked to stay under SQLite's bind-variable limit when many inbounds
  1910. // are touched in a single tick.
  1911. for _, batch := range chunkInts(inbound_ids, sqliteMaxVars) {
  1912. var page []*model.Inbound
  1913. if err = tx.Model(model.Inbound{}).Where("id IN ?", batch).Find(&page).Error; err != nil {
  1914. return false, 0, err
  1915. }
  1916. inbounds = append(inbounds, page...)
  1917. }
  1918. for inbound_index := range inbounds {
  1919. settings := map[string]any{}
  1920. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  1921. clients := settings["clients"].([]any)
  1922. for client_index := range clients {
  1923. c := clients[client_index].(map[string]any)
  1924. for traffic_index, traffic := range traffics {
  1925. if traffic.Email == c["email"].(string) {
  1926. newExpiryTime := traffic.ExpiryTime
  1927. for newExpiryTime < now {
  1928. newExpiryTime += (int64(traffic.Reset) * 86400000)
  1929. }
  1930. c["expiryTime"] = newExpiryTime
  1931. traffics[traffic_index].ExpiryTime = newExpiryTime
  1932. traffics[traffic_index].Down = 0
  1933. traffics[traffic_index].Up = 0
  1934. if !traffic.Enable {
  1935. traffics[traffic_index].Enable = true
  1936. c["enable"] = true
  1937. clientsToAdd = append(clientsToAdd,
  1938. struct {
  1939. protocol string
  1940. tag string
  1941. client map[string]any
  1942. }{
  1943. protocol: string(inbounds[inbound_index].Protocol),
  1944. tag: inbounds[inbound_index].Tag,
  1945. client: c,
  1946. })
  1947. }
  1948. clients[client_index] = any(c)
  1949. break
  1950. }
  1951. }
  1952. }
  1953. settings["clients"] = clients
  1954. newSettings, err := json.MarshalIndent(settings, "", " ")
  1955. if err != nil {
  1956. return false, 0, err
  1957. }
  1958. inbounds[inbound_index].Settings = string(newSettings)
  1959. }
  1960. err = tx.Save(inbounds).Error
  1961. if err != nil {
  1962. return false, 0, err
  1963. }
  1964. for _, ib := range inbounds {
  1965. if ib == nil {
  1966. continue
  1967. }
  1968. cs, gcErr := s.GetClients(ib)
  1969. if gcErr != nil {
  1970. logger.Warning("autoRenewClients sync clients: GetClients failed", gcErr)
  1971. continue
  1972. }
  1973. if syncErr := s.clientService.SyncInbound(tx, ib.Id, cs); syncErr != nil {
  1974. logger.Warning("autoRenewClients sync clients: SyncInbound failed", syncErr)
  1975. }
  1976. }
  1977. err = tx.Save(traffics).Error
  1978. if err != nil {
  1979. return false, 0, err
  1980. }
  1981. if p != nil {
  1982. err1 = s.xrayApi.Init(p.GetAPIPort())
  1983. if err1 != nil {
  1984. return true, int64(len(traffics)), nil
  1985. }
  1986. for _, clientToAdd := range clientsToAdd {
  1987. err1 = s.xrayApi.AddUser(clientToAdd.protocol, clientToAdd.tag, clientToAdd.client)
  1988. if err1 != nil {
  1989. needRestart = true
  1990. }
  1991. }
  1992. s.xrayApi.Close()
  1993. }
  1994. return needRestart, int64(len(traffics)), nil
  1995. }
  1996. func (s *InboundService) disableInvalidInbounds(tx *gorm.DB) (bool, int64, error) {
  1997. now := time.Now().Unix() * 1000
  1998. needRestart := false
  1999. if p != nil {
  2000. var tags []string
  2001. err := tx.Table("inbounds").
  2002. Select("inbounds.tag").
  2003. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ? and node_id IS NULL", now, true).
  2004. Scan(&tags).Error
  2005. if err != nil {
  2006. return false, 0, err
  2007. }
  2008. s.xrayApi.Init(p.GetAPIPort())
  2009. for _, tag := range tags {
  2010. err1 := s.xrayApi.DelInbound(tag)
  2011. if err1 == nil {
  2012. logger.Debug("Inbound disabled by api:", tag)
  2013. } else {
  2014. logger.Debug("Error in disabling inbound by api:", err1)
  2015. needRestart = true
  2016. }
  2017. }
  2018. s.xrayApi.Close()
  2019. }
  2020. result := tx.Model(model.Inbound{}).
  2021. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ? and node_id IS NULL", now, true).
  2022. Update("enable", false)
  2023. err := result.Error
  2024. count := result.RowsAffected
  2025. return needRestart, count, err
  2026. }
  2027. func (s *InboundService) disableInvalidClients(tx *gorm.DB) (bool, int64, error) {
  2028. now := time.Now().Unix() * 1000
  2029. needRestart := false
  2030. var depletedRows []xray.ClientTraffic
  2031. err := tx.Model(xray.ClientTraffic{}).
  2032. Where("((total > 0 AND up + down >= total) OR (expiry_time > 0 AND expiry_time <= ?)) AND enable = ?", now, true).
  2033. Find(&depletedRows).Error
  2034. if err != nil {
  2035. return false, 0, err
  2036. }
  2037. if len(depletedRows) == 0 {
  2038. return false, 0, nil
  2039. }
  2040. depletedEmails := make([]string, 0, len(depletedRows))
  2041. for i := range depletedRows {
  2042. if depletedRows[i].Email == "" {
  2043. continue
  2044. }
  2045. depletedEmails = append(depletedEmails, depletedRows[i].Email)
  2046. }
  2047. type target struct {
  2048. InboundID int `gorm:"column:inbound_id"`
  2049. NodeID *int `gorm:"column:node_id"`
  2050. Tag string
  2051. Email string
  2052. }
  2053. var targets []target
  2054. if len(depletedEmails) > 0 {
  2055. err = tx.Raw(`
  2056. SELECT inbounds.id AS inbound_id, inbounds.node_id AS node_id,
  2057. inbounds.tag AS tag, clients.email AS email
  2058. FROM clients
  2059. JOIN client_inbounds ON client_inbounds.client_id = clients.id
  2060. JOIN inbounds ON inbounds.id = client_inbounds.inbound_id
  2061. WHERE clients.email IN ?
  2062. `, depletedEmails).Scan(&targets).Error
  2063. if err != nil {
  2064. return false, 0, err
  2065. }
  2066. }
  2067. var localTargets []target
  2068. localByInbound := make(map[int]map[string]struct{})
  2069. remoteByInbound := make(map[int][]target)
  2070. for _, t := range targets {
  2071. if t.NodeID == nil {
  2072. localTargets = append(localTargets, t)
  2073. if localByInbound[t.InboundID] == nil {
  2074. localByInbound[t.InboundID] = make(map[string]struct{})
  2075. }
  2076. localByInbound[t.InboundID][t.Email] = struct{}{}
  2077. } else {
  2078. remoteByInbound[t.InboundID] = append(remoteByInbound[t.InboundID], t)
  2079. }
  2080. }
  2081. if p != nil && len(localTargets) > 0 {
  2082. s.xrayApi.Init(p.GetAPIPort())
  2083. for _, t := range localTargets {
  2084. err1 := s.xrayApi.RemoveUser(t.Tag, t.Email)
  2085. if err1 == nil {
  2086. logger.Debug("Client disabled by api:", t.Email)
  2087. } else if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", t.Email)) {
  2088. logger.Debug("User is already disabled. Nothing to do more...")
  2089. } else {
  2090. logger.Debug("Error in disabling client by api:", err1)
  2091. needRestart = true
  2092. }
  2093. }
  2094. s.xrayApi.Close()
  2095. }
  2096. for inboundID, emails := range localByInbound {
  2097. if _, _, mErr := s.markClientsDisabledInSettings(tx, inboundID, emails); mErr != nil {
  2098. logger.Warning("disableInvalidClients: settings.JSON sync failed for inbound", inboundID, ":", mErr)
  2099. }
  2100. }
  2101. result := tx.Model(xray.ClientTraffic{}).
  2102. Where("((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?)) and enable = ?", now, true).
  2103. Update("enable", false)
  2104. err = result.Error
  2105. count := result.RowsAffected
  2106. if err != nil {
  2107. return needRestart, count, err
  2108. }
  2109. if len(depletedEmails) > 0 {
  2110. if err := tx.Model(&model.ClientRecord{}).
  2111. Where("email IN ?", depletedEmails).
  2112. Updates(map[string]any{"enable": false, "updated_at": now}).Error; err != nil {
  2113. logger.Warning("disableInvalidClients update clients.enable:", err)
  2114. }
  2115. }
  2116. for inboundID, group := range remoteByInbound {
  2117. emails := make(map[string]struct{}, len(group))
  2118. for _, t := range group {
  2119. emails[t.Email] = struct{}{}
  2120. }
  2121. if pushErr := s.disableRemoteClients(tx, inboundID, emails); pushErr != nil {
  2122. logger.Warning("disableInvalidClients: push to remote failed for inbound", inboundID, ":", pushErr)
  2123. needRestart = true
  2124. }
  2125. }
  2126. return needRestart, count, nil
  2127. }
  2128. // markClientsDisabledInSettings flips client.enable=false in the inbound's
  2129. // stored settings JSON for the given emails and returns both the pre and
  2130. // post snapshots so a caller pushing to a remote node has the diff to hand.
  2131. func (s *InboundService) markClientsDisabledInSettings(tx *gorm.DB, inboundID int, emails map[string]struct{}) (oldIb, newIb *model.Inbound, err error) {
  2132. var ib model.Inbound
  2133. if err := tx.Model(&model.Inbound{}).Where("id = ?", inboundID).First(&ib).Error; err != nil {
  2134. return nil, nil, err
  2135. }
  2136. snapshot := ib
  2137. settings := map[string]any{}
  2138. if err := json.Unmarshal([]byte(ib.Settings), &settings); err != nil {
  2139. return nil, nil, err
  2140. }
  2141. clients, _ := settings["clients"].([]any)
  2142. now := time.Now().Unix() * 1000
  2143. mutated := false
  2144. for i := range clients {
  2145. entry, ok := clients[i].(map[string]any)
  2146. if !ok {
  2147. continue
  2148. }
  2149. email, _ := entry["email"].(string)
  2150. if _, hit := emails[email]; !hit {
  2151. continue
  2152. }
  2153. if cur, _ := entry["enable"].(bool); cur == false {
  2154. continue
  2155. }
  2156. entry["enable"] = false
  2157. entry["updated_at"] = now
  2158. clients[i] = entry
  2159. mutated = true
  2160. }
  2161. if !mutated {
  2162. return &snapshot, &ib, nil
  2163. }
  2164. settings["clients"] = clients
  2165. bs, marshalErr := json.MarshalIndent(settings, "", " ")
  2166. if marshalErr != nil {
  2167. return nil, nil, marshalErr
  2168. }
  2169. ib.Settings = string(bs)
  2170. if err := tx.Model(&model.Inbound{}).Where("id = ?", inboundID).
  2171. Update("settings", ib.Settings).Error; err != nil {
  2172. return nil, nil, err
  2173. }
  2174. return &snapshot, &ib, nil
  2175. }
  2176. func (s *InboundService) disableRemoteClients(tx *gorm.DB, inboundID int, emails map[string]struct{}) error {
  2177. oldSnapshot, ib, err := s.markClientsDisabledInSettings(tx, inboundID, emails)
  2178. if err != nil {
  2179. return err
  2180. }
  2181. rt, err := s.runtimeFor(ib)
  2182. if err != nil {
  2183. return err
  2184. }
  2185. if err := rt.UpdateInbound(context.Background(), oldSnapshot, ib); err != nil {
  2186. return err
  2187. }
  2188. return nil
  2189. }
  2190. func (s *InboundService) GetInboundTags() (string, error) {
  2191. db := database.GetDB()
  2192. var inboundTags []string
  2193. err := db.Model(model.Inbound{}).Select("tag").Find(&inboundTags).Error
  2194. if err != nil && err != gorm.ErrRecordNotFound {
  2195. return "", err
  2196. }
  2197. tags, _ := json.Marshal(inboundTags)
  2198. return string(tags), nil
  2199. }
  2200. func (s *InboundService) GetClientReverseTags() (string, error) {
  2201. db := database.GetDB()
  2202. var inbounds []model.Inbound
  2203. err := db.Model(model.Inbound{}).Select("settings").Where("protocol = ?", "vless").Find(&inbounds).Error
  2204. if err != nil && err != gorm.ErrRecordNotFound {
  2205. return "[]", err
  2206. }
  2207. tagSet := make(map[string]struct{})
  2208. for _, inbound := range inbounds {
  2209. var settings map[string]any
  2210. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  2211. continue
  2212. }
  2213. clients, ok := settings["clients"].([]any)
  2214. if !ok {
  2215. continue
  2216. }
  2217. for _, client := range clients {
  2218. clientMap, ok := client.(map[string]any)
  2219. if !ok {
  2220. continue
  2221. }
  2222. reverse, ok := clientMap["reverse"].(map[string]any)
  2223. if !ok {
  2224. continue
  2225. }
  2226. tag, _ := reverse["tag"].(string)
  2227. tag = strings.TrimSpace(tag)
  2228. if tag != "" {
  2229. tagSet[tag] = struct{}{}
  2230. }
  2231. }
  2232. }
  2233. rawTags := make([]string, 0, len(tagSet))
  2234. for tag := range tagSet {
  2235. rawTags = append(rawTags, tag)
  2236. }
  2237. sort.Strings(rawTags)
  2238. result, _ := json.Marshal(rawTags)
  2239. return string(result), nil
  2240. }
  2241. func (s *InboundService) MigrationRemoveOrphanedTraffics() {
  2242. db := database.GetDB()
  2243. query := fmt.Sprintf(
  2244. "DELETE FROM client_traffics WHERE email NOT IN (SELECT %s %s)",
  2245. database.JSONFieldText("client.value", "email"),
  2246. database.JSONClientsFromInbound(),
  2247. )
  2248. db.Exec(query)
  2249. }
  2250. // AddClientStat inserts a per-client accounting row, no-op on email
  2251. // conflict. Xray reports traffic per email, so the surviving row acts as
  2252. // the shared accumulator for inbounds that re-use the same identity.
  2253. func (s *InboundService) AddClientStat(tx *gorm.DB, inboundId int, client *model.Client) error {
  2254. clientTraffic := xray.ClientTraffic{
  2255. InboundId: inboundId,
  2256. Email: client.Email,
  2257. Total: client.TotalGB,
  2258. ExpiryTime: client.ExpiryTime,
  2259. Enable: client.Enable,
  2260. Reset: client.Reset,
  2261. }
  2262. return tx.Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "email"}}, DoNothing: true}).
  2263. Create(&clientTraffic).Error
  2264. }
  2265. func (s *InboundService) UpdateClientStat(tx *gorm.DB, email string, client *model.Client) error {
  2266. result := tx.Model(xray.ClientTraffic{}).
  2267. Where("email = ?", email).
  2268. Updates(map[string]any{
  2269. "enable": client.Enable,
  2270. "email": client.Email,
  2271. "total": client.TotalGB,
  2272. "expiry_time": client.ExpiryTime,
  2273. "reset": client.Reset,
  2274. })
  2275. err := result.Error
  2276. return err
  2277. }
  2278. func (s *InboundService) UpdateClientIPs(tx *gorm.DB, oldEmail string, newEmail string) error {
  2279. return tx.Model(model.InboundClientIps{}).Where("client_email = ?", oldEmail).Update("client_email", newEmail).Error
  2280. }
  2281. func (s *InboundService) DelClientStat(tx *gorm.DB, email string) error {
  2282. if err := tx.Where("email = ?", email).Delete(xray.ClientTraffic{}).Error; err != nil {
  2283. return err
  2284. }
  2285. return tx.Where("email = ?", email).Delete(&model.NodeClientTraffic{}).Error
  2286. }
  2287. func (s *InboundService) DelClientIPs(tx *gorm.DB, email string) error {
  2288. return tx.Where("client_email = ?", email).Delete(model.InboundClientIps{}).Error
  2289. }
  2290. func (s *InboundService) delClientStatsByEmails(tx *gorm.DB, emails []string) error {
  2291. const chunk = 400
  2292. for start := 0; start < len(emails); start += chunk {
  2293. end := min(start+chunk, len(emails))
  2294. batch := emails[start:end]
  2295. if err := tx.Where("email IN ?", batch).Delete(xray.ClientTraffic{}).Error; err != nil {
  2296. return err
  2297. }
  2298. if err := tx.Where("email IN ?", batch).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  2299. return err
  2300. }
  2301. }
  2302. return nil
  2303. }
  2304. func (s *InboundService) delClientIPsByEmails(tx *gorm.DB, emails []string) error {
  2305. const chunk = 400
  2306. for start := 0; start < len(emails); start += chunk {
  2307. end := min(start+chunk, len(emails))
  2308. if err := tx.Where("client_email IN ?", emails[start:end]).Delete(model.InboundClientIps{}).Error; err != nil {
  2309. return err
  2310. }
  2311. }
  2312. return nil
  2313. }
  2314. func (s *InboundService) GetClientInboundByTrafficID(trafficId int) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  2315. db := database.GetDB()
  2316. var traffics []*xray.ClientTraffic
  2317. err = db.Model(xray.ClientTraffic{}).Where("id = ?", trafficId).Find(&traffics).Error
  2318. if err != nil {
  2319. logger.Warningf("Error retrieving ClientTraffic with trafficId %d: %v", trafficId, err)
  2320. return nil, nil, err
  2321. }
  2322. if len(traffics) == 0 {
  2323. return nil, nil, nil
  2324. }
  2325. traffic = traffics[0]
  2326. inbound, err = s.GetInbound(traffic.InboundId)
  2327. if errors.Is(err, gorm.ErrRecordNotFound) {
  2328. // client_traffics.inbound_id goes stale when an inbound is deleted and
  2329. // recreated; fall back to the authoritative client_inbounds link by email.
  2330. ids, idErr := s.clientService.GetInboundIdsForEmail(db, traffic.Email)
  2331. if idErr != nil {
  2332. return traffic, nil, idErr
  2333. }
  2334. if len(ids) > 0 {
  2335. inbound, err = s.GetInbound(ids[0])
  2336. }
  2337. }
  2338. return traffic, inbound, err
  2339. }
  2340. func (s *InboundService) GetClientInboundByEmail(email string) (traffic *xray.ClientTraffic, inbound *model.Inbound, err error) {
  2341. db := database.GetDB()
  2342. var traffics []*xray.ClientTraffic
  2343. err = db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error
  2344. if err != nil {
  2345. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  2346. return nil, nil, err
  2347. }
  2348. if len(traffics) == 0 {
  2349. return nil, nil, nil
  2350. }
  2351. traffic = traffics[0]
  2352. inbound, err = s.GetInbound(traffic.InboundId)
  2353. if errors.Is(err, gorm.ErrRecordNotFound) {
  2354. // client_traffics.inbound_id is a legacy single-inbound pointer that goes
  2355. // stale when an inbound is deleted and recreated: the email-keyed traffic
  2356. // row survives but still references the missing inbound. Fall back to the
  2357. // authoritative client_inbounds link so email lookups (reset, info, …) work.
  2358. ids, idErr := s.clientService.GetInboundIdsForEmail(db, email)
  2359. if idErr != nil {
  2360. return traffic, nil, idErr
  2361. }
  2362. if len(ids) > 0 {
  2363. inbound, err = s.GetInbound(ids[0])
  2364. }
  2365. }
  2366. return traffic, inbound, err
  2367. }
  2368. func (s *InboundService) GetClientByEmail(clientEmail string) (*xray.ClientTraffic, *model.Client, error) {
  2369. traffic, inbound, err := s.GetClientInboundByEmail(clientEmail)
  2370. if err != nil {
  2371. return nil, nil, err
  2372. }
  2373. if inbound == nil {
  2374. return nil, nil, common.NewError("Inbound Not Found For Email:", clientEmail)
  2375. }
  2376. clients, err := s.GetClients(inbound)
  2377. if err != nil {
  2378. return nil, nil, err
  2379. }
  2380. for _, client := range clients {
  2381. if client.Email == clientEmail {
  2382. return traffic, &client, nil
  2383. }
  2384. }
  2385. return nil, nil, common.NewError("Client Not Found In Inbound For Email:", clientEmail)
  2386. }
  2387. func (s *InboundService) ResetClientTrafficByEmail(clientEmail string) error {
  2388. return submitTrafficWrite(func() error {
  2389. db := database.GetDB()
  2390. return db.Model(xray.ClientTraffic{}).
  2391. Where("email = ?", clientEmail).
  2392. Updates(map[string]any{"enable": true, "up": 0, "down": 0}).Error
  2393. })
  2394. }
  2395. func (s *InboundService) ResetClientTraffic(id int, clientEmail string) (needRestart bool, err error) {
  2396. err = submitTrafficWrite(func() error {
  2397. var inner error
  2398. needRestart, inner = s.resetClientTrafficLocked(id, clientEmail)
  2399. return inner
  2400. })
  2401. return
  2402. }
  2403. func (s *InboundService) resetClientTrafficLocked(id int, clientEmail string) (bool, error) {
  2404. needRestart := false
  2405. traffic, err := s.GetClientTrafficByEmail(clientEmail)
  2406. if err != nil {
  2407. return false, err
  2408. }
  2409. if !traffic.Enable {
  2410. inbound, err := s.GetInbound(id)
  2411. if err != nil {
  2412. return false, err
  2413. }
  2414. clients, err := s.GetClients(inbound)
  2415. if err != nil {
  2416. return false, err
  2417. }
  2418. for _, client := range clients {
  2419. if client.Email == clientEmail && client.Enable {
  2420. rt, rterr := s.runtimeFor(inbound)
  2421. if rterr != nil {
  2422. if inbound.NodeID != nil {
  2423. return false, rterr
  2424. }
  2425. needRestart = true
  2426. break
  2427. }
  2428. cipher := ""
  2429. if string(inbound.Protocol) == "shadowsocks" {
  2430. var oldSettings map[string]any
  2431. err = json.Unmarshal([]byte(inbound.Settings), &oldSettings)
  2432. if err != nil {
  2433. return false, err
  2434. }
  2435. cipher = oldSettings["method"].(string)
  2436. }
  2437. err1 := rt.AddUser(context.Background(), inbound, map[string]any{
  2438. "email": client.Email,
  2439. "id": client.ID,
  2440. "auth": client.Auth,
  2441. "security": client.Security,
  2442. "flow": client.Flow,
  2443. "password": client.Password,
  2444. "cipher": cipher,
  2445. })
  2446. if err1 == nil {
  2447. logger.Debug("Client enabled on", rt.Name(), "due to reset traffic:", clientEmail)
  2448. } else {
  2449. logger.Debug("Error in enabling client on", rt.Name(), ":", err1)
  2450. needRestart = true
  2451. }
  2452. break
  2453. }
  2454. }
  2455. }
  2456. traffic.Up = 0
  2457. traffic.Down = 0
  2458. traffic.Enable = true
  2459. db := database.GetDB()
  2460. err = db.Save(traffic).Error
  2461. if err != nil {
  2462. return false, err
  2463. }
  2464. now := time.Now().UnixMilli()
  2465. _ = db.Model(model.Inbound{}).
  2466. Where("id = ?", id).
  2467. Update("last_traffic_reset_time", now).Error
  2468. inbound, err := s.GetInbound(id)
  2469. if err == nil && inbound != nil && inbound.NodeID != nil {
  2470. if rt, rterr := s.runtimeFor(inbound); rterr == nil {
  2471. if e := rt.ResetClientTraffic(context.Background(), inbound, clientEmail); e != nil {
  2472. logger.Warning("ResetClientTraffic: remote propagation to", rt.Name(), "failed:", e)
  2473. }
  2474. } else {
  2475. logger.Warning("ResetClientTraffic: runtime lookup failed:", rterr)
  2476. }
  2477. }
  2478. return needRestart, nil
  2479. }
  2480. func (s *InboundService) ResetAllTraffics() error {
  2481. return submitTrafficWrite(func() error {
  2482. return s.resetAllTrafficsLocked()
  2483. })
  2484. }
  2485. func (s *InboundService) resetAllTrafficsLocked() error {
  2486. db := database.GetDB()
  2487. now := time.Now().UnixMilli()
  2488. if err := db.Model(model.Inbound{}).
  2489. Where("user_id > ?", 0).
  2490. Updates(map[string]any{
  2491. "up": 0,
  2492. "down": 0,
  2493. "last_traffic_reset_time": now,
  2494. }).Error; err != nil {
  2495. return err
  2496. }
  2497. return nil
  2498. }
  2499. func (s *InboundService) ResetInboundTraffic(id int) error {
  2500. return submitTrafficWrite(func() error {
  2501. db := database.GetDB()
  2502. return db.Model(model.Inbound{}).
  2503. Where("id = ?", id).
  2504. Updates(map[string]any{"up": 0, "down": 0}).Error
  2505. })
  2506. }
  2507. // EmailsByInbound returns the list of client emails currently configured on
  2508. // an inbound's settings.clients[]. Used by the "delete all clients" flow on
  2509. // the inbounds page, which then feeds the list into ClientService.BulkDelete.
  2510. func (s *InboundService) EmailsByInbound(inboundId int) ([]string, error) {
  2511. inbound, err := s.GetInbound(inboundId)
  2512. if err != nil {
  2513. return nil, err
  2514. }
  2515. clients, err := s.GetClients(inbound)
  2516. if err != nil {
  2517. return nil, err
  2518. }
  2519. emails := make([]string, 0, len(clients))
  2520. for _, c := range clients {
  2521. if e := strings.TrimSpace(c.Email); e != "" {
  2522. emails = append(emails, e)
  2523. }
  2524. }
  2525. return emails, nil
  2526. }
  2527. func (s *InboundService) DelDepletedClients(id int) (err error) {
  2528. db := database.GetDB()
  2529. tx := db.Begin()
  2530. defer func() {
  2531. if err == nil {
  2532. tx.Commit()
  2533. } else {
  2534. tx.Rollback()
  2535. }
  2536. }()
  2537. // Collect depleted emails globally — a shared-email row owned by one
  2538. // inbound depletes every sibling that lists the email.
  2539. now := time.Now().Unix() * 1000
  2540. depletedClause := "reset = 0 and ((total > 0 and up + down >= total) or (expiry_time > 0 and expiry_time <= ?))"
  2541. var depletedRows []xray.ClientTraffic
  2542. err = db.Model(xray.ClientTraffic{}).
  2543. Where(depletedClause, now).
  2544. Find(&depletedRows).Error
  2545. if err != nil {
  2546. return err
  2547. }
  2548. if len(depletedRows) == 0 {
  2549. return nil
  2550. }
  2551. depletedEmails := make(map[string]struct{}, len(depletedRows))
  2552. for _, r := range depletedRows {
  2553. if r.Email == "" {
  2554. continue
  2555. }
  2556. depletedEmails[strings.ToLower(r.Email)] = struct{}{}
  2557. }
  2558. if len(depletedEmails) == 0 {
  2559. return nil
  2560. }
  2561. var inbounds []*model.Inbound
  2562. inboundQuery := db.Model(model.Inbound{})
  2563. if id >= 0 {
  2564. inboundQuery = inboundQuery.Where("id = ?", id)
  2565. }
  2566. if err = inboundQuery.Find(&inbounds).Error; err != nil {
  2567. return err
  2568. }
  2569. for _, inbound := range inbounds {
  2570. var settings map[string]any
  2571. if err = json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  2572. return err
  2573. }
  2574. rawClients, ok := settings["clients"].([]any)
  2575. if !ok {
  2576. continue
  2577. }
  2578. newClients := make([]any, 0, len(rawClients))
  2579. removed := 0
  2580. for _, client := range rawClients {
  2581. c, ok := client.(map[string]any)
  2582. if !ok {
  2583. newClients = append(newClients, client)
  2584. continue
  2585. }
  2586. email, _ := c["email"].(string)
  2587. if _, isDepleted := depletedEmails[strings.ToLower(email)]; isDepleted {
  2588. removed++
  2589. continue
  2590. }
  2591. newClients = append(newClients, client)
  2592. }
  2593. if removed == 0 {
  2594. continue
  2595. }
  2596. if len(newClients) == 0 {
  2597. s.DelInbound(inbound.Id)
  2598. continue
  2599. }
  2600. settings["clients"] = newClients
  2601. ns, mErr := json.MarshalIndent(settings, "", " ")
  2602. if mErr != nil {
  2603. return mErr
  2604. }
  2605. inbound.Settings = string(ns)
  2606. if err = tx.Save(inbound).Error; err != nil {
  2607. return err
  2608. }
  2609. survivingClients, gcErr := s.GetClients(inbound)
  2610. if gcErr != nil {
  2611. err = gcErr
  2612. return err
  2613. }
  2614. if err = s.clientService.SyncInbound(tx, inbound.Id, survivingClients); err != nil {
  2615. return err
  2616. }
  2617. }
  2618. // Drop now-orphaned rows. With id >= 0, a row is safe to drop only when
  2619. // no out-of-scope inbound still references the email.
  2620. if id < 0 {
  2621. err = tx.Where(depletedClause, now).Delete(xray.ClientTraffic{}).Error
  2622. return err
  2623. }
  2624. emails := make([]string, 0, len(depletedEmails))
  2625. for e := range depletedEmails {
  2626. emails = append(emails, e)
  2627. }
  2628. var stillReferenced []string
  2629. emailExpr := database.JSONFieldText("client.value", "email")
  2630. stillQuery := fmt.Sprintf(
  2631. "SELECT DISTINCT LOWER(%s) %s WHERE LOWER(%s) IN ?",
  2632. emailExpr,
  2633. database.JSONClientsFromInbound(),
  2634. emailExpr,
  2635. )
  2636. if err = tx.Raw(stillQuery, emails).Scan(&stillReferenced).Error; err != nil {
  2637. return err
  2638. }
  2639. stillSet := make(map[string]struct{}, len(stillReferenced))
  2640. for _, e := range stillReferenced {
  2641. stillSet[e] = struct{}{}
  2642. }
  2643. toDelete := make([]string, 0, len(emails))
  2644. for _, e := range emails {
  2645. if _, kept := stillSet[e]; !kept {
  2646. toDelete = append(toDelete, e)
  2647. }
  2648. }
  2649. if len(toDelete) > 0 {
  2650. if err = tx.Where("LOWER(email) IN ?", toDelete).Delete(xray.ClientTraffic{}).Error; err != nil {
  2651. return err
  2652. }
  2653. }
  2654. return nil
  2655. }
  2656. func (s *InboundService) GetClientTrafficTgBot(tgId int64) ([]*xray.ClientTraffic, error) {
  2657. db := database.GetDB()
  2658. var inbounds []*model.Inbound
  2659. // Retrieve inbounds where settings contain the given tgId
  2660. err := db.Model(model.Inbound{}).Where("settings LIKE ?", fmt.Sprintf(`%%"tgId": %d%%`, tgId)).Find(&inbounds).Error
  2661. if err != nil && err != gorm.ErrRecordNotFound {
  2662. logger.Errorf("Error retrieving inbounds with tgId %d: %v", tgId, err)
  2663. return nil, err
  2664. }
  2665. var emails []string
  2666. for _, inbound := range inbounds {
  2667. clients, err := s.GetClients(inbound)
  2668. if err != nil {
  2669. logger.Errorf("Error retrieving clients for inbound %d: %v", inbound.Id, err)
  2670. continue
  2671. }
  2672. for _, client := range clients {
  2673. if client.TgID == tgId {
  2674. emails = append(emails, client.Email)
  2675. }
  2676. }
  2677. }
  2678. // Chunked to stay under SQLite's bind-variable limit when a single Telegram
  2679. // account owns thousands of clients across inbounds.
  2680. uniqEmails := uniqueNonEmptyStrings(emails)
  2681. traffics := make([]*xray.ClientTraffic, 0, len(uniqEmails))
  2682. for _, batch := range chunkStrings(uniqEmails, sqliteMaxVars) {
  2683. var page []*xray.ClientTraffic
  2684. if err = db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  2685. if err == gorm.ErrRecordNotFound {
  2686. continue
  2687. }
  2688. logger.Errorf("Error retrieving ClientTraffic for emails %v: %v", batch, err)
  2689. return nil, err
  2690. }
  2691. traffics = append(traffics, page...)
  2692. }
  2693. if len(traffics) == 0 {
  2694. logger.Warning("No ClientTraffic records found for emails:", emails)
  2695. return nil, nil
  2696. }
  2697. // Populate UUID and other client data for each traffic record
  2698. for i := range traffics {
  2699. if ct, client, e := s.GetClientByEmail(traffics[i].Email); e == nil && ct != nil && client != nil {
  2700. traffics[i].Enable = client.Enable
  2701. traffics[i].UUID = client.ID
  2702. traffics[i].SubId = client.SubID
  2703. }
  2704. }
  2705. return traffics, nil
  2706. }
  2707. // sqliteMaxVars is a safe ceiling for the number of bind parameters in a
  2708. // single SQL statement. SQLite's SQLITE_MAX_VARIABLE_NUMBER is 999 on builds
  2709. // before 3.32 and 32766 after; staying under 999 keeps queries portable
  2710. // across forks/old binaries and also bounds per-query memory on truly large
  2711. // installs (>32k clients) where even modern SQLite would refuse a single IN.
  2712. const sqliteMaxVars = 900
  2713. // uniqueNonEmptyStrings returns a deduplicated copy of in with empty strings
  2714. // removed, preserving the order of first occurrence.
  2715. func uniqueNonEmptyStrings(in []string) []string {
  2716. if len(in) == 0 {
  2717. return nil
  2718. }
  2719. seen := make(map[string]struct{}, len(in))
  2720. out := make([]string, 0, len(in))
  2721. for _, v := range in {
  2722. if v == "" {
  2723. continue
  2724. }
  2725. if _, ok := seen[v]; ok {
  2726. continue
  2727. }
  2728. seen[v] = struct{}{}
  2729. out = append(out, v)
  2730. }
  2731. return out
  2732. }
  2733. // uniqueInts returns a deduplicated copy of in, preserving order of first occurrence.
  2734. func uniqueInts(in []int) []int {
  2735. if len(in) == 0 {
  2736. return nil
  2737. }
  2738. seen := make(map[int]struct{}, len(in))
  2739. out := make([]int, 0, len(in))
  2740. for _, v := range in {
  2741. if _, ok := seen[v]; ok {
  2742. continue
  2743. }
  2744. seen[v] = struct{}{}
  2745. out = append(out, v)
  2746. }
  2747. return out
  2748. }
  2749. // chunkStrings splits s into consecutive sub-slices of at most size elements.
  2750. // Returns nil for an empty input or non-positive size.
  2751. func chunkStrings(s []string, size int) [][]string {
  2752. if size <= 0 || len(s) == 0 {
  2753. return nil
  2754. }
  2755. out := make([][]string, 0, (len(s)+size-1)/size)
  2756. for i := 0; i < len(s); i += size {
  2757. end := min(i+size, len(s))
  2758. out = append(out, s[i:end])
  2759. }
  2760. return out
  2761. }
  2762. // chunkInts splits s into consecutive sub-slices of at most size elements.
  2763. // Returns nil for an empty input or non-positive size.
  2764. func chunkInts(s []int, size int) [][]int {
  2765. if size <= 0 || len(s) == 0 {
  2766. return nil
  2767. }
  2768. out := make([][]int, 0, (len(s)+size-1)/size)
  2769. for i := 0; i < len(s); i += size {
  2770. end := min(i+size, len(s))
  2771. out = append(out, s[i:end])
  2772. }
  2773. return out
  2774. }
  2775. func (s *InboundService) GetActiveClientTraffics(emails []string) ([]*xray.ClientTraffic, error) {
  2776. uniq := uniqueNonEmptyStrings(emails)
  2777. if len(uniq) == 0 {
  2778. return nil, nil
  2779. }
  2780. db := database.GetDB()
  2781. traffics := make([]*xray.ClientTraffic, 0, len(uniq))
  2782. for _, batch := range chunkStrings(uniq, sqliteMaxVars) {
  2783. var page []*xray.ClientTraffic
  2784. if err := db.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Find(&page).Error; err != nil {
  2785. return nil, err
  2786. }
  2787. traffics = append(traffics, page...)
  2788. }
  2789. return traffics, nil
  2790. }
  2791. // GetAllClientTraffics returns the full set of client_traffics rows so the
  2792. // websocket broadcasters can ship a complete snapshot every cycle. The old
  2793. // delta-only path (GetActiveClientTraffics on activeEmails) silently dropped
  2794. // the per-client section whenever no client moved bytes in the cycle or a
  2795. // node sync failed, leaving client rows in the UI stuck at stale numbers.
  2796. func (s *InboundService) GetAllClientTraffics() ([]*xray.ClientTraffic, error) {
  2797. db := database.GetDB()
  2798. var traffics []*xray.ClientTraffic
  2799. if err := db.Model(xray.ClientTraffic{}).Find(&traffics).Error; err != nil {
  2800. return nil, err
  2801. }
  2802. return traffics, nil
  2803. }
  2804. type InboundTrafficSummary struct {
  2805. Id int `json:"id"`
  2806. Up int64 `json:"up"`
  2807. Down int64 `json:"down"`
  2808. Total int64 `json:"total"`
  2809. Enable bool `json:"enable"`
  2810. }
  2811. func (s *InboundService) GetInboundsTrafficSummary() ([]InboundTrafficSummary, error) {
  2812. db := database.GetDB()
  2813. var summaries []InboundTrafficSummary
  2814. if err := db.Model(&model.Inbound{}).
  2815. Select("id, up, down, total, enable").
  2816. Find(&summaries).Error; err != nil {
  2817. return nil, err
  2818. }
  2819. return summaries, nil
  2820. }
  2821. func (s *InboundService) GetClientTrafficByEmail(email string) (traffic *xray.ClientTraffic, err error) {
  2822. db := database.GetDB()
  2823. var traffics []*xray.ClientTraffic
  2824. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).Find(&traffics).Error; err != nil {
  2825. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  2826. return nil, err
  2827. }
  2828. if len(traffics) == 0 {
  2829. return nil, nil
  2830. }
  2831. t := traffics[0]
  2832. if rec, rErr := s.clientService.GetRecordByEmail(db, email); rErr == nil && rec != nil {
  2833. c := rec.ToClient()
  2834. t.UUID = c.ID
  2835. t.SubId = c.SubID
  2836. return t, nil
  2837. }
  2838. t2, client, err := s.GetClientByEmail(email)
  2839. if err != nil {
  2840. logger.Warningf("Error retrieving ClientTraffic with email %s: %v", email, err)
  2841. return nil, err
  2842. }
  2843. if t2 != nil && client != nil {
  2844. t2.UUID = client.ID
  2845. t2.SubId = client.SubID
  2846. return t2, nil
  2847. }
  2848. return nil, nil
  2849. }
  2850. func (s *InboundService) UpdateClientTrafficByEmail(email string, upload int64, download int64) error {
  2851. return submitTrafficWrite(func() error {
  2852. db := database.GetDB()
  2853. err := db.Model(xray.ClientTraffic{}).
  2854. Where("email = ?", email).
  2855. Updates(map[string]any{
  2856. "up": upload,
  2857. "down": download,
  2858. }).Error
  2859. if err != nil {
  2860. logger.Warningf("Error updating ClientTraffic with email %s: %v", email, err)
  2861. }
  2862. return err
  2863. })
  2864. }
  2865. func (s *InboundService) SearchClientTraffic(query string) (traffic *xray.ClientTraffic, err error) {
  2866. db := database.GetDB()
  2867. inbound := &model.Inbound{}
  2868. traffic = &xray.ClientTraffic{}
  2869. // Search for inbound settings that contain the query
  2870. err = db.Model(model.Inbound{}).Where("settings LIKE ?", "%\""+query+"\"%").First(inbound).Error
  2871. if err != nil {
  2872. if err == gorm.ErrRecordNotFound {
  2873. logger.Warningf("Inbound settings containing query %s not found: %v", query, err)
  2874. return nil, err
  2875. }
  2876. logger.Errorf("Error searching for inbound settings with query %s: %v", query, err)
  2877. return nil, err
  2878. }
  2879. traffic.InboundId = inbound.Id
  2880. // Unmarshal settings to get clients
  2881. settings := map[string][]model.Client{}
  2882. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  2883. logger.Errorf("Error unmarshalling inbound settings for inbound ID %d: %v", inbound.Id, err)
  2884. return nil, err
  2885. }
  2886. clients := settings["clients"]
  2887. for _, client := range clients {
  2888. if (client.ID == query || client.Password == query) && client.Email != "" {
  2889. traffic.Email = client.Email
  2890. break
  2891. }
  2892. }
  2893. if traffic.Email == "" {
  2894. logger.Warningf("No client found with query %s in inbound ID %d", query, inbound.Id)
  2895. return nil, gorm.ErrRecordNotFound
  2896. }
  2897. // Retrieve ClientTraffic based on the found email
  2898. err = db.Model(xray.ClientTraffic{}).Where("email = ?", traffic.Email).First(traffic).Error
  2899. if err != nil {
  2900. if err == gorm.ErrRecordNotFound {
  2901. logger.Warningf("ClientTraffic for email %s not found: %v", traffic.Email, err)
  2902. return nil, err
  2903. }
  2904. logger.Errorf("Error retrieving ClientTraffic for email %s: %v", traffic.Email, err)
  2905. return nil, err
  2906. }
  2907. return traffic, nil
  2908. }
  2909. func (s *InboundService) GetInboundClientIps(clientEmail string) (string, error) {
  2910. db := database.GetDB()
  2911. InboundClientIps := &model.InboundClientIps{}
  2912. err := db.Model(model.InboundClientIps{}).Where("client_email = ?", clientEmail).First(InboundClientIps).Error
  2913. if err != nil {
  2914. return "", err
  2915. }
  2916. if InboundClientIps.Ips == "" {
  2917. return "", nil
  2918. }
  2919. // Try to parse as new format (with timestamps)
  2920. type IPWithTimestamp struct {
  2921. IP string `json:"ip"`
  2922. Timestamp int64 `json:"timestamp"`
  2923. }
  2924. var ipsWithTime []IPWithTimestamp
  2925. err = json.Unmarshal([]byte(InboundClientIps.Ips), &ipsWithTime)
  2926. // If successfully parsed as new format, return with timestamps
  2927. if err == nil && len(ipsWithTime) > 0 {
  2928. return InboundClientIps.Ips, nil
  2929. }
  2930. // Otherwise, assume it's old format (simple string array)
  2931. // Try to parse as simple array and convert to new format
  2932. var oldIps []string
  2933. err = json.Unmarshal([]byte(InboundClientIps.Ips), &oldIps)
  2934. if err == nil && len(oldIps) > 0 {
  2935. // Convert old format to new format with current timestamp
  2936. newIpsWithTime := make([]IPWithTimestamp, len(oldIps))
  2937. for i, ip := range oldIps {
  2938. newIpsWithTime[i] = IPWithTimestamp{
  2939. IP: ip,
  2940. Timestamp: time.Now().Unix(),
  2941. }
  2942. }
  2943. result, _ := json.Marshal(newIpsWithTime)
  2944. return string(result), nil
  2945. }
  2946. // Return as-is if parsing fails
  2947. return InboundClientIps.Ips, nil
  2948. }
  2949. func (s *InboundService) ClearClientIps(clientEmail string) error {
  2950. db := database.GetDB()
  2951. result := db.Model(model.InboundClientIps{}).
  2952. Where("client_email = ?", clientEmail).
  2953. Update("ips", "")
  2954. err := result.Error
  2955. if err != nil {
  2956. return err
  2957. }
  2958. return nil
  2959. }
  2960. func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error) {
  2961. db := database.GetDB()
  2962. var inbounds []*model.Inbound
  2963. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("remark like ?", "%"+query+"%").Find(&inbounds).Error
  2964. if err != nil && err != gorm.ErrRecordNotFound {
  2965. return nil, err
  2966. }
  2967. return inbounds, nil
  2968. }
  2969. func (s *InboundService) MigrationRequirements() {
  2970. db := database.GetDB()
  2971. tx := db.Begin()
  2972. var err error
  2973. defer func() {
  2974. if err == nil {
  2975. tx.Commit()
  2976. if !database.IsPostgres() {
  2977. if dbErr := db.Exec(`VACUUM "main"`).Error; dbErr != nil {
  2978. logger.Warningf("VACUUM failed: %v", dbErr)
  2979. }
  2980. }
  2981. } else {
  2982. tx.Rollback()
  2983. }
  2984. }()
  2985. if tx.Migrator().HasColumn(&model.Inbound{}, "all_time") {
  2986. if err = tx.Migrator().DropColumn(&model.Inbound{}, "all_time"); err != nil {
  2987. return
  2988. }
  2989. }
  2990. if tx.Migrator().HasColumn(&xray.ClientTraffic{}, "all_time") {
  2991. if err = tx.Migrator().DropColumn(&xray.ClientTraffic{}, "all_time"); err != nil {
  2992. return
  2993. }
  2994. }
  2995. // Fix inbounds based problems
  2996. var inbounds []*model.Inbound
  2997. err = tx.Model(model.Inbound{}).Where("protocol IN (?)", []string{"vmess", "vless", "trojan", "shadowsocks", "hysteria"}).Find(&inbounds).Error
  2998. if err != nil && err != gorm.ErrRecordNotFound {
  2999. return
  3000. }
  3001. for inbound_index := range inbounds {
  3002. settings := map[string]any{}
  3003. json.Unmarshal([]byte(inbounds[inbound_index].Settings), &settings)
  3004. if raw, exists := settings["clients"]; exists && raw == nil {
  3005. settings["clients"] = []any{}
  3006. }
  3007. clients, ok := settings["clients"].([]any)
  3008. if ok {
  3009. // Fix Client configuration problems
  3010. newClients := make([]any, 0, len(clients))
  3011. hasVisionFlow := false
  3012. for client_index := range clients {
  3013. c := clients[client_index].(map[string]any)
  3014. // Add email='' if it is not exists
  3015. if _, ok := c["email"]; !ok {
  3016. c["email"] = ""
  3017. }
  3018. // Convert string tgId to int64
  3019. if _, ok := c["tgId"]; ok {
  3020. var tgId any = c["tgId"]
  3021. if tgIdStr, ok2 := tgId.(string); ok2 {
  3022. tgIdInt64, err := strconv.ParseInt(strings.ReplaceAll(tgIdStr, " ", ""), 10, 64)
  3023. if err == nil {
  3024. c["tgId"] = tgIdInt64
  3025. }
  3026. }
  3027. }
  3028. // Remove "flow": "xtls-rprx-direct"
  3029. if _, ok := c["flow"]; ok {
  3030. if c["flow"] == "xtls-rprx-direct" {
  3031. c["flow"] = ""
  3032. }
  3033. }
  3034. if flow, _ := c["flow"].(string); flow == "xtls-rprx-vision" {
  3035. hasVisionFlow = true
  3036. }
  3037. // Backfill created_at and updated_at
  3038. if _, ok := c["created_at"]; !ok {
  3039. c["created_at"] = time.Now().Unix() * 1000
  3040. }
  3041. c["updated_at"] = time.Now().Unix() * 1000
  3042. newClients = append(newClients, any(c))
  3043. }
  3044. settings["clients"] = newClients
  3045. // Drop orphaned testseed: VLESS-only field, only meaningful when at least
  3046. // one client uses the exact xtls-rprx-vision flow. Older versions saved it
  3047. // for any non-empty flow (including the UDP variant) or kept it after the
  3048. // flow was cleared from the client modal — clean those up here.
  3049. if inbounds[inbound_index].Protocol == model.VLESS && !hasVisionFlow {
  3050. delete(settings, "testseed")
  3051. }
  3052. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  3053. if err != nil {
  3054. return
  3055. }
  3056. inbounds[inbound_index].Settings = string(modifiedSettings)
  3057. }
  3058. // Add client traffic row for all clients which has email
  3059. modelClients, err := s.GetClients(inbounds[inbound_index])
  3060. if err != nil {
  3061. return
  3062. }
  3063. for _, modelClient := range modelClients {
  3064. if len(modelClient.Email) > 0 {
  3065. var count int64
  3066. tx.Model(xray.ClientTraffic{}).Where("email = ?", modelClient.Email).Count(&count)
  3067. if count == 0 {
  3068. s.AddClientStat(tx, inbounds[inbound_index].Id, &modelClient)
  3069. }
  3070. }
  3071. }
  3072. // Heal clients table for installs where the one-shot seeder
  3073. // skipped clients due to a tgId-string unmarshal error.
  3074. if syncErr := s.clientService.SyncInbound(tx, inbounds[inbound_index].Id, modelClients); syncErr != nil {
  3075. logger.Warning("MigrationRequirements sync clients failed:", syncErr)
  3076. }
  3077. }
  3078. tx.Save(inbounds)
  3079. // Remove orphaned traffics
  3080. tx.Where("inbound_id = 0").Delete(xray.ClientTraffic{})
  3081. // Migrate old MultiDomain to External Proxy
  3082. var externalProxy []struct {
  3083. Id int
  3084. Port int
  3085. StreamSettings []byte
  3086. }
  3087. externalProxyQuery := `select id, port, stream_settings
  3088. from inbounds
  3089. WHERE protocol in ('vmess','vless','trojan')
  3090. AND json_extract(stream_settings, '$.security') = 'tls'
  3091. AND json_extract(stream_settings, '$.tlsSettings.settings.domains') IS NOT NULL`
  3092. if database.IsPostgres() {
  3093. externalProxyQuery = `select id, port, stream_settings
  3094. from inbounds
  3095. WHERE protocol in ('vmess','vless','trojan')
  3096. AND NULLIF(stream_settings, '')::jsonb #>> '{security}' = 'tls'
  3097. AND NULLIF(stream_settings, '')::jsonb #> '{tlsSettings,settings,domains}' IS NOT NULL`
  3098. }
  3099. err = tx.Raw(externalProxyQuery).Scan(&externalProxy).Error
  3100. if err != nil || len(externalProxy) == 0 {
  3101. return
  3102. }
  3103. for _, ep := range externalProxy {
  3104. var reverses any
  3105. var stream map[string]any
  3106. json.Unmarshal(ep.StreamSettings, &stream)
  3107. if tlsSettings, ok := stream["tlsSettings"].(map[string]any); ok {
  3108. if settings, ok := tlsSettings["settings"].(map[string]any); ok {
  3109. if domains, ok := settings["domains"].([]any); ok {
  3110. for _, domain := range domains {
  3111. if domainMap, ok := domain.(map[string]any); ok {
  3112. domainMap["forceTls"] = "same"
  3113. domainMap["port"] = ep.Port
  3114. domainMap["dest"] = domainMap["domain"].(string)
  3115. delete(domainMap, "domain")
  3116. }
  3117. }
  3118. }
  3119. reverses = settings["domains"]
  3120. delete(settings, "domains")
  3121. }
  3122. }
  3123. stream["externalProxy"] = reverses
  3124. newStream, _ := json.MarshalIndent(stream, " ", " ")
  3125. tx.Model(model.Inbound{}).Where("id = ?", ep.Id).Update("stream_settings", newStream)
  3126. }
  3127. err = tx.Raw(`UPDATE inbounds
  3128. SET tag = REPLACE(tag, '0.0.0.0:', '')
  3129. WHERE INSTR(tag, '0.0.0.0:') > 0;`).Error
  3130. if err != nil {
  3131. return
  3132. }
  3133. }
  3134. func (s *InboundService) MigrateDB() {
  3135. s.MigrationRequirements()
  3136. s.MigrationRemoveOrphanedTraffics()
  3137. }
  3138. func (s *InboundService) GetOnlineClients() []string {
  3139. if p == nil {
  3140. return []string{}
  3141. }
  3142. return p.GetOnlineClients()
  3143. }
  3144. func (s *InboundService) GetOnlineClientsByNode() map[int][]string {
  3145. if p == nil {
  3146. return map[int][]string{}
  3147. }
  3148. return p.GetOnlineClientsByNode()
  3149. }
  3150. func (s *InboundService) GetActiveInboundsByNode() map[int][]string {
  3151. if p == nil {
  3152. return map[int][]string{}
  3153. }
  3154. return p.GetActiveInboundsByNode()
  3155. }
  3156. func (s *InboundService) SetNodeOnlineClients(nodeID int, emails []string) {
  3157. if p != nil {
  3158. p.SetNodeOnlineClients(nodeID, emails)
  3159. }
  3160. }
  3161. func (s *InboundService) ClearNodeOnlineClients(nodeID int) {
  3162. if p != nil {
  3163. p.ClearNodeOnlineClients(nodeID)
  3164. }
  3165. }
  3166. func (s *InboundService) GetClientsLastOnline() (map[string]int64, error) {
  3167. db := database.GetDB()
  3168. var rows []xray.ClientTraffic
  3169. err := db.Model(&xray.ClientTraffic{}).Select("email, last_online").Find(&rows).Error
  3170. if err != nil && err != gorm.ErrRecordNotFound {
  3171. return nil, err
  3172. }
  3173. result := make(map[string]int64, len(rows))
  3174. for _, r := range rows {
  3175. result[r.Email] = r.LastOnline
  3176. }
  3177. return result, nil
  3178. }
  3179. // RefreshLocalOnlineClients folds the emails and inbound tags active on this
  3180. // panel's own xray this poll into the local online/active sets, applying the
  3181. // grace window and pruning stale entries. Pass nil to only prune. See
  3182. // xray.Process for why the local sets are kept separate from the shared
  3183. // last_online column.
  3184. func (s *InboundService) RefreshLocalOnlineClients(activeEmails, activeInboundTags []string) {
  3185. if p != nil {
  3186. p.RefreshLocalOnline(activeEmails, activeInboundTags, time.Now().UnixMilli(), onlineGracePeriodMs)
  3187. }
  3188. }
  3189. func (s *InboundService) FilterAndSortClientEmails(emails []string) ([]string, []string, error) {
  3190. db := database.GetDB()
  3191. // Step 1: Get ClientTraffic records for emails in the input list.
  3192. // Chunked to stay under SQLite's bind-variable limit on huge inputs.
  3193. uniqEmails := uniqueNonEmptyStrings(emails)
  3194. clients := make([]xray.ClientTraffic, 0, len(uniqEmails))
  3195. for _, batch := range chunkStrings(uniqEmails, sqliteMaxVars) {
  3196. var page []xray.ClientTraffic
  3197. if err := db.Where("email IN ?", batch).Find(&page).Error; err != nil && err != gorm.ErrRecordNotFound {
  3198. return nil, nil, err
  3199. }
  3200. clients = append(clients, page...)
  3201. }
  3202. // Step 2: Sort clients by (Up + Down) descending
  3203. sort.Slice(clients, func(i, j int) bool {
  3204. return (clients[i].Up + clients[i].Down) > (clients[j].Up + clients[j].Down)
  3205. })
  3206. // Step 3: Extract sorted valid emails and track found ones
  3207. validEmails := make([]string, 0, len(clients))
  3208. found := make(map[string]bool)
  3209. for _, client := range clients {
  3210. validEmails = append(validEmails, client.Email)
  3211. found[client.Email] = true
  3212. }
  3213. // Step 4: Identify emails that were not found in the database
  3214. extraEmails := make([]string, 0)
  3215. for _, email := range emails {
  3216. if !found[email] {
  3217. extraEmails = append(extraEmails, email)
  3218. }
  3219. }
  3220. return validEmails, extraEmails, nil
  3221. }
  3222. type SubLinkProvider interface {
  3223. SubLinksForSubId(host, subId string) ([]string, error)
  3224. LinksForClient(host string, inbound *model.Inbound, email string) []string
  3225. }
  3226. var registeredSubLinkProvider SubLinkProvider
  3227. func RegisterSubLinkProvider(p SubLinkProvider) {
  3228. registeredSubLinkProvider = p
  3229. }
  3230. func (s *InboundService) GetSubLinks(host, subId string) ([]string, error) {
  3231. if registeredSubLinkProvider == nil {
  3232. return nil, common.NewError("sub link provider not registered")
  3233. }
  3234. return registeredSubLinkProvider.SubLinksForSubId(host, subId)
  3235. }
  3236. func (s *InboundService) GetAllClientLinks(host string, email string) ([]string, error) {
  3237. if email == "" {
  3238. return nil, common.NewError("client email is required")
  3239. }
  3240. if registeredSubLinkProvider == nil {
  3241. return nil, common.NewError("sub link provider not registered")
  3242. }
  3243. rec, err := s.clientService.GetRecordByEmail(nil, email)
  3244. if err != nil {
  3245. return nil, err
  3246. }
  3247. inboundIds, err := s.clientService.GetInboundIdsForRecord(rec.Id)
  3248. if err != nil {
  3249. return nil, err
  3250. }
  3251. var links []string
  3252. for _, ibId := range inboundIds {
  3253. inbound, getErr := s.GetInbound(ibId)
  3254. if getErr != nil {
  3255. return nil, getErr
  3256. }
  3257. links = append(links, registeredSubLinkProvider.LinksForClient(host, inbound, email)...)
  3258. }
  3259. return links, nil
  3260. }