local.go 3.5 KB

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