command.go 4.4 KB

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