command.go 4.5 KB

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