1
0

local.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package runtime
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "strings"
  7. "sync"
  8. "github.com/mhsanaei/3x-ui/v3/database/model"
  9. "github.com/mhsanaei/3x-ui/v3/xray"
  10. )
  11. type LocalDeps struct {
  12. APIPort func() int
  13. SetNeedRestart func()
  14. }
  15. type Local struct {
  16. deps LocalDeps
  17. mu sync.Mutex
  18. }
  19. func NewLocal(deps LocalDeps) *Local {
  20. return &Local{deps: deps}
  21. }
  22. func (l *Local) Name() string { return "local" }
  23. func (l *Local) withAPI(fn func(api *xray.XrayAPI) error) error {
  24. l.mu.Lock()
  25. defer l.mu.Unlock()
  26. port := l.deps.APIPort()
  27. if port <= 0 {
  28. return errors.New("local xray is not running")
  29. }
  30. var api xray.XrayAPI
  31. if err := api.Init(port); err != nil {
  32. return err
  33. }
  34. defer api.Close()
  35. return fn(&api)
  36. }
  37. func (l *Local) AddInbound(_ context.Context, ib *model.Inbound) error {
  38. body, err := json.MarshalIndent(ib.GenXrayInboundConfig(), "", " ")
  39. if err != nil {
  40. return err
  41. }
  42. return l.withAPI(func(api *xray.XrayAPI) error {
  43. return api.AddInbound(body)
  44. })
  45. }
  46. func (l *Local) DelInbound(_ context.Context, ib *model.Inbound) error {
  47. return l.withAPI(func(api *xray.XrayAPI) error {
  48. return api.DelInbound(ib.Tag)
  49. })
  50. }
  51. func (l *Local) UpdateInbound(ctx context.Context, oldIb, newIb *model.Inbound) error {
  52. _ = l.DelInbound(ctx, oldIb)
  53. if !newIb.Enable {
  54. return nil
  55. }
  56. return l.AddInbound(ctx, newIb)
  57. }
  58. func (l *Local) AddUser(_ context.Context, ib *model.Inbound, userMap map[string]any) error {
  59. return l.withAPI(func(api *xray.XrayAPI) error {
  60. return api.AddUser(string(ib.Protocol), ib.Tag, userMap)
  61. })
  62. }
  63. func (l *Local) RemoveUser(_ context.Context, ib *model.Inbound, email string) error {
  64. return l.withAPI(func(api *xray.XrayAPI) error {
  65. return api.RemoveUser(ib.Tag, email)
  66. })
  67. }
  68. func (l *Local) AddClient(ctx context.Context, ib *model.Inbound, client model.Client) error {
  69. if !client.Enable {
  70. return nil
  71. }
  72. user := map[string]any{
  73. "email": client.Email,
  74. "id": client.ID,
  75. "security": client.Security,
  76. "flow": client.Flow,
  77. "auth": client.Auth,
  78. "password": client.Password,
  79. }
  80. return l.AddUser(ctx, ib, user)
  81. }
  82. func (l *Local) DeleteUser(ctx context.Context, ib *model.Inbound, email string) error {
  83. if email == "" {
  84. return nil
  85. }
  86. if err := l.RemoveUser(ctx, ib, email); err != nil {
  87. if strings.Contains(err.Error(), "not found") {
  88. return nil
  89. }
  90. return err
  91. }
  92. return nil
  93. }
  94. func (l *Local) UpdateUser(ctx context.Context, ib *model.Inbound, oldEmail string, payload model.Client) error {
  95. if oldEmail != "" {
  96. if err := l.RemoveUser(ctx, ib, oldEmail); err != nil && !strings.Contains(err.Error(), "not found") {
  97. return err
  98. }
  99. }
  100. if !payload.Enable {
  101. return nil
  102. }
  103. user := map[string]any{
  104. "email": payload.Email,
  105. "id": payload.ID,
  106. "security": payload.Security,
  107. "flow": payload.Flow,
  108. "auth": payload.Auth,
  109. "password": payload.Password,
  110. }
  111. return l.AddUser(ctx, ib, user)
  112. }
  113. func (l *Local) RestartXray(_ context.Context) error {
  114. if l.deps.SetNeedRestart != nil {
  115. l.deps.SetNeedRestart()
  116. }
  117. return nil
  118. }
  119. func (l *Local) ResetClientTraffic(_ context.Context, _ *model.Inbound, _ string) error {
  120. return nil
  121. }
  122. func (l *Local) ResetAllTraffics(_ context.Context) error {
  123. return nil
  124. }