command.go 4.2 KB

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