command.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/inbound"
  8. "v2ray.com/core/features/outbound"
  9. "v2ray.com/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. rawHandler, err := core.CreateObject(s.s, request.Inbound)
  63. if err != nil {
  64. return nil, err
  65. }
  66. handler, ok := rawHandler.(inbound.Handler)
  67. if !ok {
  68. return nil, newError("not an InboundHandler.")
  69. }
  70. return &AddInboundResponse{}, s.ihm.AddHandler(ctx, handler)
  71. }
  72. func (s *handlerServer) RemoveInbound(ctx context.Context, request *RemoveInboundRequest) (*RemoveInboundResponse, error) {
  73. return &RemoveInboundResponse{}, s.ihm.RemoveHandler(ctx, request.Tag)
  74. }
  75. func (s *handlerServer) AlterInbound(ctx context.Context, request *AlterInboundRequest) (*AlterInboundResponse, error) {
  76. rawOperation, err := request.Operation.GetInstance()
  77. if err != nil {
  78. return nil, newError("unknown operation").Base(err)
  79. }
  80. operation, ok := rawOperation.(InboundOperation)
  81. if !ok {
  82. return nil, newError("not an inbound operation")
  83. }
  84. handler, err := s.ihm.GetHandler(ctx, request.Tag)
  85. if err != nil {
  86. return nil, newError("failed to get handler: ", request.Tag).Base(err)
  87. }
  88. return &AlterInboundResponse{}, operation.ApplyInbound(ctx, handler)
  89. }
  90. func (s *handlerServer) AddOutbound(ctx context.Context, request *AddOutboundRequest) (*AddOutboundResponse, error) {
  91. rawHandler, err := core.CreateObject(s.s, request.Outbound)
  92. if err != nil {
  93. return nil, err
  94. }
  95. handler, ok := rawHandler.(outbound.Handler)
  96. if !ok {
  97. return nil, newError("not an OutboundHandler.")
  98. }
  99. return &AddOutboundResponse{}, s.ohm.AddHandler(ctx, handler)
  100. }
  101. func (s *handlerServer) RemoveOutbound(ctx context.Context, request *RemoveOutboundRequest) (*RemoveOutboundResponse, error) {
  102. return &RemoveOutboundResponse{}, s.ohm.RemoveHandler(ctx, request.Tag)
  103. }
  104. func (s *handlerServer) AlterOutbound(ctx context.Context, request *AlterOutboundRequest) (*AlterOutboundResponse, error) {
  105. rawOperation, err := request.Operation.GetInstance()
  106. if err != nil {
  107. return nil, newError("unknown operation").Base(err)
  108. }
  109. operation, ok := rawOperation.(OutboundOperation)
  110. if !ok {
  111. return nil, newError("not an outbound operation")
  112. }
  113. handler := s.ohm.GetHandler(request.Tag)
  114. return &AlterOutboundResponse{}, operation.ApplyOutbound(ctx, handler)
  115. }
  116. type service struct {
  117. v *core.Instance
  118. }
  119. func (s *service) Register(server *grpc.Server) {
  120. RegisterHandlerServiceServer(server, &handlerServer{
  121. s: s.v,
  122. ihm: s.v.InboundHandlerManager(),
  123. ohm: s.v.OutboundHandlerManager(),
  124. })
  125. }
  126. func init() {
  127. common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
  128. s := core.MustFromContext(ctx)
  129. return &service{v: s}, nil
  130. }))
  131. }