command.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 a UserManager")
  35. }
  36. mUser, err := op.User.ToMemoryUser()
  37. if err != nil {
  38. return newError("failed to parse user").Base(err)
  39. }
  40. return um.AddUser(ctx, mUser)
  41. }
  42. // ApplyInbound implements InboundOperation.
  43. func (op *RemoveUserOperation) ApplyInbound(ctx context.Context, handler core.InboundHandler) error {
  44. p, err := getInbound(handler)
  45. if err != nil {
  46. return err
  47. }
  48. um, ok := p.(proxy.UserManager)
  49. if !ok {
  50. return newError("proxy is not a UserManager")
  51. }
  52. return um.RemoveUser(ctx, op.Email)
  53. }
  54. type handlerServer struct {
  55. s *core.Instance
  56. ihm core.InboundHandlerManager
  57. ohm core.OutboundHandlerManager
  58. }
  59. func (s *handlerServer) AddInbound(ctx context.Context, request *AddInboundRequest) (*AddInboundResponse, error) {
  60. rawHandler, err := core.CreateObject(s.s, request.Inbound)
  61. if err != nil {
  62. return nil, err
  63. }
  64. handler, ok := rawHandler.(core.InboundHandler)
  65. if !ok {
  66. return nil, newError("not an InboundHandler.")
  67. }
  68. return &AddInboundResponse{}, s.ihm.AddHandler(ctx, handler)
  69. }
  70. func (s *handlerServer) RemoveInbound(ctx context.Context, request *RemoveInboundRequest) (*RemoveInboundResponse, error) {
  71. return &RemoveInboundResponse{}, s.ihm.RemoveHandler(ctx, request.Tag)
  72. }
  73. func (s *handlerServer) AlterInbound(ctx context.Context, request *AlterInboundRequest) (*AlterInboundResponse, error) {
  74. rawOperation, err := request.Operation.GetInstance()
  75. if err != nil {
  76. return nil, newError("unknown operation").Base(err)
  77. }
  78. operation, ok := rawOperation.(InboundOperation)
  79. if !ok {
  80. return nil, newError("not an inbound operation")
  81. }
  82. handler, err := s.ihm.GetHandler(ctx, request.Tag)
  83. if err != nil {
  84. return nil, newError("failed to get handler: ", request.Tag).Base(err)
  85. }
  86. return &AlterInboundResponse{}, operation.ApplyInbound(ctx, handler)
  87. }
  88. func (s *handlerServer) AddOutbound(ctx context.Context, request *AddOutboundRequest) (*AddOutboundResponse, error) {
  89. rawHandler, err := core.CreateObject(s.s, request.Outbound)
  90. if err != nil {
  91. return nil, err
  92. }
  93. handler, ok := rawHandler.(core.OutboundHandler)
  94. if !ok {
  95. return nil, newError("not an OutboundHandler.")
  96. }
  97. return &AddOutboundResponse{}, s.ohm.AddHandler(ctx, handler)
  98. }
  99. func (s *handlerServer) RemoveOutbound(ctx context.Context, request *RemoveOutboundRequest) (*RemoveOutboundResponse, error) {
  100. return &RemoveOutboundResponse{}, s.ohm.RemoveHandler(ctx, request.Tag)
  101. }
  102. func (s *handlerServer) AlterOutbound(ctx context.Context, request *AlterOutboundRequest) (*AlterOutboundResponse, error) {
  103. rawOperation, err := request.Operation.GetInstance()
  104. if err != nil {
  105. return nil, newError("unknown operation").Base(err)
  106. }
  107. operation, ok := rawOperation.(OutboundOperation)
  108. if !ok {
  109. return nil, newError("not an outbound operation")
  110. }
  111. handler := s.ohm.GetHandler(request.Tag)
  112. return &AlterOutboundResponse{}, operation.ApplyOutbound(ctx, handler)
  113. }
  114. type service struct {
  115. v *core.Instance
  116. }
  117. func (s *service) Register(server *grpc.Server) {
  118. RegisterHandlerServiceServer(server, &handlerServer{
  119. s: s.v,
  120. ihm: s.v.InboundHandlerManager(),
  121. ohm: s.v.OutboundHandlerManager(),
  122. })
  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. }