command.go 3.9 KB

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