command.go 4.5 KB

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