local.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. if oldIb.Protocol == model.MTProto || newIb.Protocol == model.MTProto {
  66. return l.updateMtprotoInbound(ctx, oldIb, newIb)
  67. }
  68. _ = l.DelInbound(ctx, oldIb)
  69. if !newIb.Enable {
  70. return nil
  71. }
  72. return l.AddInbound(ctx, newIb)
  73. }
  74. // updateMtprotoInbound applies an inbound update without the Del+Add sequence
  75. // the xray path uses: Remove would drop the manager's fingerprint state, which
  76. // is what lets Ensure keep the running mtg process (and its live connections)
  77. // when nothing in the generated config changed. The sidecar is only stopped
  78. // when the inbound is disabled, loses its last active secret, or moves to a
  79. // different protocol.
  80. func (l *Local) updateMtprotoInbound(ctx context.Context, oldIb, newIb *model.Inbound) error {
  81. if oldIb.Protocol == model.MTProto && newIb.Protocol != model.MTProto {
  82. mtproto.GetManager().Remove(oldIb.Id)
  83. if !newIb.Enable {
  84. return nil
  85. }
  86. return l.AddInbound(ctx, newIb)
  87. }
  88. if oldIb.Protocol != model.MTProto {
  89. _ = l.DelInbound(ctx, oldIb)
  90. }
  91. if !newIb.Enable {
  92. mtproto.GetManager().Remove(newIb.Id)
  93. return nil
  94. }
  95. inst, ok := mtproto.InstanceFromInbound(newIb)
  96. if !ok {
  97. mtproto.GetManager().Remove(newIb.Id)
  98. return nil
  99. }
  100. return mtproto.GetManager().Ensure(inst)
  101. }
  102. func (l *Local) AddUser(_ context.Context, ib *model.Inbound, userMap map[string]any) error {
  103. if ib.Protocol == model.MTProto {
  104. return nil
  105. }
  106. return l.withAPI(func(api *xray.XrayAPI) error {
  107. return api.AddUser(string(ib.Protocol), ib.Tag, userMap)
  108. })
  109. }
  110. func (l *Local) RemoveUser(_ context.Context, ib *model.Inbound, email string) error {
  111. if ib.Protocol == model.MTProto {
  112. return nil
  113. }
  114. return l.withAPI(func(api *xray.XrayAPI) error {
  115. return api.RemoveUser(ib.Tag, email)
  116. })
  117. }
  118. func (l *Local) AddClient(ctx context.Context, ib *model.Inbound, client model.Client) error {
  119. if !client.Enable {
  120. return nil
  121. }
  122. user := map[string]any{
  123. "email": client.Email,
  124. "id": client.ID,
  125. "security": client.Security,
  126. "flow": client.Flow,
  127. "auth": client.Auth,
  128. "password": client.Password,
  129. "publicKey": client.PublicKey,
  130. "allowedIPs": client.AllowedIPs,
  131. "preSharedKey": client.PreSharedKey,
  132. "keepAlive": wgKeepAlive(client.KeepAlive),
  133. }
  134. return l.AddUser(ctx, ib, user)
  135. }
  136. func (l *Local) DeleteUser(ctx context.Context, ib *model.Inbound, email string) error {
  137. if email == "" {
  138. return nil
  139. }
  140. if err := l.RemoveUser(ctx, ib, email); err != nil {
  141. if strings.Contains(err.Error(), "not found") {
  142. return nil
  143. }
  144. return err
  145. }
  146. return nil
  147. }
  148. func (l *Local) DeleteClient(context.Context, string) error {
  149. return nil
  150. }
  151. func (l *Local) UpdateUser(ctx context.Context, ib *model.Inbound, oldEmail string, payload model.Client) error {
  152. if oldEmail != "" {
  153. if err := l.RemoveUser(ctx, ib, oldEmail); err != nil && !strings.Contains(err.Error(), "not found") {
  154. return err
  155. }
  156. }
  157. if !payload.Enable {
  158. return nil
  159. }
  160. user := map[string]any{
  161. "email": payload.Email,
  162. "id": payload.ID,
  163. "security": payload.Security,
  164. "flow": payload.Flow,
  165. "auth": payload.Auth,
  166. "password": payload.Password,
  167. "publicKey": payload.PublicKey,
  168. "allowedIPs": payload.AllowedIPs,
  169. "preSharedKey": payload.PreSharedKey,
  170. "keepAlive": wgKeepAlive(payload.KeepAlive),
  171. }
  172. return l.AddUser(ctx, ib, user)
  173. }
  174. func wgKeepAlive(seconds int) string {
  175. if seconds <= 0 {
  176. return ""
  177. }
  178. return strconv.Itoa(seconds)
  179. }
  180. func (l *Local) RestartXray(_ context.Context) error {
  181. if l.deps.SetNeedRestart != nil {
  182. l.deps.SetNeedRestart()
  183. }
  184. return nil
  185. }
  186. func (l *Local) ResetClientTraffic(_ context.Context, _ *model.Inbound, _ string) error {
  187. return nil
  188. }
  189. func (l *Local) ResetAllTraffics(_ context.Context) error {
  190. return nil
  191. }
  192. func (l *Local) ResetInboundTraffic(_ context.Context, _ *model.Inbound) error {
  193. return nil
  194. }