command.go 7.1 KB

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