command.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package command
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common"
  5. "github.com/xtls/xray-core/common/errors"
  6. "github.com/xtls/xray-core/common/protocol"
  7. "github.com/xtls/xray-core/core"
  8. "github.com/xtls/xray-core/features/inbound"
  9. "github.com/xtls/xray-core/features/outbound"
  10. "github.com/xtls/xray-core/proxy"
  11. grpc "google.golang.org/grpc"
  12. )
  13. // InboundOperation is the interface for operations that applies to inbound handlers.
  14. type InboundOperation interface {
  15. // ApplyInbound applies this operation to the given inbound handler.
  16. ApplyInbound(context.Context, inbound.Handler) error
  17. }
  18. // OutboundOperation is the interface for operations that applies to outbound handlers.
  19. type OutboundOperation interface {
  20. // ApplyOutbound applies this operation to the given outbound handler.
  21. ApplyOutbound(context.Context, outbound.Handler) error
  22. }
  23. func getInbound(handler inbound.Handler) (proxy.Inbound, error) {
  24. gi, ok := handler.(proxy.GetInbound)
  25. if !ok {
  26. return nil, errors.New("can't get inbound proxy from handler.")
  27. }
  28. return gi.GetInbound(), nil
  29. }
  30. // ApplyInbound implements InboundOperation.
  31. func (op *AddUserOperation) ApplyInbound(ctx context.Context, handler inbound.Handler) error {
  32. p, err := getInbound(handler)
  33. if err != nil {
  34. return err
  35. }
  36. um, ok := p.(proxy.UserManager)
  37. if !ok {
  38. return errors.New("proxy is not a UserManager")
  39. }
  40. mUser, err := op.User.ToMemoryUser()
  41. if err != nil {
  42. return errors.New("failed to parse user").Base(err)
  43. }
  44. return um.AddUser(ctx, mUser)
  45. }
  46. // ApplyInbound implements InboundOperation.
  47. func (op *RemoveUserOperation) ApplyInbound(ctx context.Context, handler inbound.Handler) error {
  48. p, err := getInbound(handler)
  49. if err != nil {
  50. return err
  51. }
  52. um, ok := p.(proxy.UserManager)
  53. if !ok {
  54. return errors.New("proxy is not a UserManager")
  55. }
  56. return um.RemoveUser(ctx, op.Email)
  57. }
  58. type handlerServer struct {
  59. s *core.Instance
  60. ihm inbound.Manager
  61. ohm outbound.Manager
  62. }
  63. func (s *handlerServer) AddInbound(ctx context.Context, request *AddInboundRequest) (*AddInboundResponse, error) {
  64. if err := core.AddInboundHandler(s.s, request.Inbound); err != nil {
  65. return nil, err
  66. }
  67. return &AddInboundResponse{}, nil
  68. }
  69. func (s *handlerServer) RemoveInbound(ctx context.Context, request *RemoveInboundRequest) (*RemoveInboundResponse, error) {
  70. return &RemoveInboundResponse{}, s.ihm.RemoveHandler(ctx, request.Tag)
  71. }
  72. func (s *handlerServer) AlterInbound(ctx context.Context, request *AlterInboundRequest) (*AlterInboundResponse, error) {
  73. rawOperation, err := request.Operation.GetInstance()
  74. if err != nil {
  75. return nil, errors.New("unknown operation").Base(err)
  76. }
  77. operation, ok := rawOperation.(InboundOperation)
  78. if !ok {
  79. return nil, errors.New("not an inbound operation")
  80. }
  81. handler, err := s.ihm.GetHandler(ctx, request.Tag)
  82. if err != nil {
  83. return nil, errors.New("failed to get handler: ", request.Tag).Base(err)
  84. }
  85. return &AlterInboundResponse{}, operation.ApplyInbound(ctx, handler)
  86. }
  87. func (s *handlerServer) GetInboundUsers(ctx context.Context, request *GetInboundUserRequest) (*GetInboundUserResponse, error) {
  88. handler, err := s.ihm.GetHandler(ctx, request.Tag)
  89. if err != nil {
  90. return nil, errors.New("failed to get handler: ", request.Tag).Base(err)
  91. }
  92. p, err := getInbound(handler)
  93. if err != nil {
  94. return nil, err
  95. }
  96. um, ok := p.(proxy.UserManager)
  97. if !ok {
  98. return nil, errors.New("proxy is not a UserManager")
  99. }
  100. if len(request.Email) > 0 {
  101. return &GetInboundUserResponse{Users: []*protocol.User{protocol.ToProtoUser(um.GetUser(ctx, request.Email))}}, nil
  102. }
  103. var result = make([]*protocol.User, 0, 100)
  104. users := um.GetUsers(ctx)
  105. for _, u := range users {
  106. result = append(result, protocol.ToProtoUser(u))
  107. }
  108. return &GetInboundUserResponse{Users: result}, nil
  109. }
  110. func (s *handlerServer) GetInboundUsersCount(ctx context.Context, request *GetInboundUserRequest) (*GetInboundUsersCountResponse, error) {
  111. handler, err := s.ihm.GetHandler(ctx, request.Tag)
  112. if err != nil {
  113. return nil, errors.New("failed to get handler: ", request.Tag).Base(err)
  114. }
  115. p, err := getInbound(handler)
  116. if err != nil {
  117. return nil, err
  118. }
  119. um, ok := p.(proxy.UserManager)
  120. if !ok {
  121. return nil, errors.New("proxy is not a UserManager")
  122. }
  123. return &GetInboundUsersCountResponse{Count: um.GetUsersCount(ctx)}, nil
  124. }
  125. func (s *handlerServer) AddOutbound(ctx context.Context, request *AddOutboundRequest) (*AddOutboundResponse, error) {
  126. if err := core.AddOutboundHandler(s.s, request.Outbound); err != nil {
  127. return nil, err
  128. }
  129. return &AddOutboundResponse{}, nil
  130. }
  131. func (s *handlerServer) RemoveOutbound(ctx context.Context, request *RemoveOutboundRequest) (*RemoveOutboundResponse, error) {
  132. return &RemoveOutboundResponse{}, s.ohm.RemoveHandler(ctx, request.Tag)
  133. }
  134. func (s *handlerServer) AlterOutbound(ctx context.Context, request *AlterOutboundRequest) (*AlterOutboundResponse, error) {
  135. rawOperation, err := request.Operation.GetInstance()
  136. if err != nil {
  137. return nil, errors.New("unknown operation").Base(err)
  138. }
  139. operation, ok := rawOperation.(OutboundOperation)
  140. if !ok {
  141. return nil, errors.New("not an outbound operation")
  142. }
  143. handler := s.ohm.GetHandler(request.Tag)
  144. return &AlterOutboundResponse{}, operation.ApplyOutbound(ctx, handler)
  145. }
  146. func (s *handlerServer) mustEmbedUnimplementedHandlerServiceServer() {}
  147. type service struct {
  148. v *core.Instance
  149. }
  150. func (s *service) Register(server *grpc.Server) {
  151. hs := &handlerServer{
  152. s: s.v,
  153. }
  154. common.Must(s.v.RequireFeatures(func(im inbound.Manager, om outbound.Manager) {
  155. hs.ihm = im
  156. hs.ohm = om
  157. }))
  158. RegisterHandlerServiceServer(server, hs)
  159. // For compatibility purposes
  160. vCoreDesc := HandlerService_ServiceDesc
  161. vCoreDesc.ServiceName = "v2ray.core.app.proxyman.command.HandlerService"
  162. server.RegisterService(&vCoreDesc, hs)
  163. }
  164. func init() {
  165. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
  166. s := core.MustFromContext(ctx)
  167. return &service{v: s}, nil
  168. }))
  169. }