local.go 4.1 KB

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