1
0

runtime.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Package runtime abstracts the live xray engine that an inbound's
  2. // configuration is shipped to. Two implementations exist: Local talks
  3. // to the panel's own xray via gRPC (the original behaviour); Remote
  4. // talks to another 3x-ui panel's HTTP API as a managed Node.
  5. //
  6. // InboundService picks a Runtime per-inbound based on Inbound.NodeID.
  7. // The point of the abstraction is to keep `if node != nil` checks out
  8. // of the service code as Phase 2/3 features (traffic sync, subscription
  9. // per-node) build on top.
  10. package runtime
  11. import (
  12. "context"
  13. "github.com/mhsanaei/3x-ui/v3/database/model"
  14. )
  15. // Runtime is the live-engine adapter for one inbound's worth of
  16. // operations. Implementations must be safe for concurrent use — the
  17. // service layer does not synchronise calls.
  18. type Runtime interface {
  19. // Name identifies the adapter in logs ("local", "node:<name>").
  20. Name() string
  21. // AddInbound deploys an inbound to the engine. The Tag field on ib
  22. // is treated as the source of truth for identifying the inbound on
  23. // the remote side; Local ignores it.
  24. AddInbound(ctx context.Context, ib *model.Inbound) error
  25. // DelInbound removes the inbound identified by ib.Tag.
  26. DelInbound(ctx context.Context, ib *model.Inbound) error
  27. // UpdateInbound replaces the existing inbound with newIb. oldIb
  28. // carries the previous config so the adapter can compute a minimal
  29. // diff (Local: drop+add by tag; Remote: HTTP update by remote-id).
  30. UpdateInbound(ctx context.Context, oldIb, newIb *model.Inbound) error
  31. // AddUser hot-adds a client to the inbound identified by ib.Tag.
  32. // userMap matches the shape that xray.XrayAPI.AddUser already takes
  33. // — keys: email, id, password, auth, security, flow, cipher.
  34. AddUser(ctx context.Context, ib *model.Inbound, userMap map[string]any) error
  35. // RemoveUser hot-removes the client by email from ib's inbound.
  36. RemoveUser(ctx context.Context, ib *model.Inbound, email string) error
  37. // RestartXray asks the engine to fully restart. For Local this just
  38. // flips the SetToNeedRestart flag and lets the cron pick it up; for
  39. // Remote it issues an HTTP POST to /panel/api/server/restartXrayService.
  40. RestartXray(ctx context.Context) error
  41. // ResetClientTraffic zeros the up/down counters for one client on the
  42. // engine. Local: no-op — the central DB UPDATE that runs before this
  43. // call is sufficient, and xray's gRPC stats counter resets on the next
  44. // poll. Remote: HTTP POST so the next traffic sync doesn't pull the
  45. // pre-reset absolute back from the node.
  46. ResetClientTraffic(ctx context.Context, ib *model.Inbound, email string) error
  47. // ResetInboundClientTraffics zeros every client of one inbound. Same
  48. // Local/Remote split as ResetClientTraffic.
  49. ResetInboundClientTraffics(ctx context.Context, ib *model.Inbound) error
  50. // ResetAllTraffics zeros every inbound counter on the engine. Used by
  51. // the panel-wide "reset all traffic" action; called once per affected
  52. // node so that nodes with no inbounds for the current panel are skipped.
  53. ResetAllTraffics(ctx context.Context) error
  54. }