github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/app/proxyman/command/command.go (about) 1 // +build !confonly 2 3 package command 4 5 import ( 6 "context" 7 8 grpc "google.golang.org/grpc" 9 10 "v2ray.com/core" 11 "v2ray.com/core/common" 12 "v2ray.com/core/features/inbound" 13 "v2ray.com/core/features/outbound" 14 "v2ray.com/core/proxy" 15 ) 16 17 // InboundOperation is the interface for operations that applies to inbound handlers. 18 type InboundOperation interface { 19 // ApplyInbound applies this operation to the given inbound handler. 20 ApplyInbound(context.Context, inbound.Handler) error 21 } 22 23 // OutboundOperation is the interface for operations that applies to outbound handlers. 24 type OutboundOperation interface { 25 // ApplyOutbound applies this operation to the given outbound handler. 26 ApplyOutbound(context.Context, outbound.Handler) error 27 } 28 29 func getInbound(handler inbound.Handler) (proxy.Inbound, error) { 30 gi, ok := handler.(proxy.GetInbound) 31 if !ok { 32 return nil, newError("can't get inbound proxy from handler.") 33 } 34 return gi.GetInbound(), nil 35 } 36 37 // ApplyInbound implements InboundOperation. 38 func (op *AddUserOperation) ApplyInbound(ctx context.Context, handler inbound.Handler) error { 39 p, err := getInbound(handler) 40 if err != nil { 41 return err 42 } 43 um, ok := p.(proxy.UserManager) 44 if !ok { 45 return newError("proxy is not a UserManager") 46 } 47 mUser, err := op.User.ToMemoryUser() 48 if err != nil { 49 return newError("failed to parse user").Base(err) 50 } 51 return um.AddUser(ctx, mUser) 52 } 53 54 // ApplyInbound implements InboundOperation. 55 func (op *RemoveUserOperation) ApplyInbound(ctx context.Context, handler inbound.Handler) error { 56 p, err := getInbound(handler) 57 if err != nil { 58 return err 59 } 60 um, ok := p.(proxy.UserManager) 61 if !ok { 62 return newError("proxy is not a UserManager") 63 } 64 return um.RemoveUser(ctx, op.Email) 65 } 66 67 type handlerServer struct { 68 s *core.Instance 69 ihm inbound.Manager 70 ohm outbound.Manager 71 } 72 73 func (s *handlerServer) AddInbound(ctx context.Context, request *AddInboundRequest) (*AddInboundResponse, error) { 74 if err := core.AddInboundHandler(s.s, request.Inbound); err != nil { 75 return nil, err 76 } 77 78 return &AddInboundResponse{}, nil 79 } 80 81 func (s *handlerServer) RemoveInbound(ctx context.Context, request *RemoveInboundRequest) (*RemoveInboundResponse, error) { 82 return &RemoveInboundResponse{}, s.ihm.RemoveHandler(ctx, request.Tag) 83 } 84 85 func (s *handlerServer) AlterInbound(ctx context.Context, request *AlterInboundRequest) (*AlterInboundResponse, error) { 86 rawOperation, err := request.Operation.GetInstance() 87 if err != nil { 88 return nil, newError("unknown operation").Base(err) 89 } 90 operation, ok := rawOperation.(InboundOperation) 91 if !ok { 92 return nil, newError("not an inbound operation") 93 } 94 95 handler, err := s.ihm.GetHandler(ctx, request.Tag) 96 if err != nil { 97 return nil, newError("failed to get handler: ", request.Tag).Base(err) 98 } 99 100 return &AlterInboundResponse{}, operation.ApplyInbound(ctx, handler) 101 } 102 103 func (s *handlerServer) AddOutbound(ctx context.Context, request *AddOutboundRequest) (*AddOutboundResponse, error) { 104 if err := core.AddOutboundHandler(s.s, request.Outbound); err != nil { 105 return nil, err 106 } 107 return &AddOutboundResponse{}, nil 108 } 109 110 func (s *handlerServer) RemoveOutbound(ctx context.Context, request *RemoveOutboundRequest) (*RemoveOutboundResponse, error) { 111 return &RemoveOutboundResponse{}, s.ohm.RemoveHandler(ctx, request.Tag) 112 } 113 114 func (s *handlerServer) AlterOutbound(ctx context.Context, request *AlterOutboundRequest) (*AlterOutboundResponse, error) { 115 rawOperation, err := request.Operation.GetInstance() 116 if err != nil { 117 return nil, newError("unknown operation").Base(err) 118 } 119 operation, ok := rawOperation.(OutboundOperation) 120 if !ok { 121 return nil, newError("not an outbound operation") 122 } 123 124 handler := s.ohm.GetHandler(request.Tag) 125 return &AlterOutboundResponse{}, operation.ApplyOutbound(ctx, handler) 126 } 127 128 func (s *handlerServer) mustEmbedUnimplementedHandlerServiceServer() {} 129 130 type service struct { 131 v *core.Instance 132 } 133 134 func (s *service) Register(server *grpc.Server) { 135 hs := &handlerServer{ 136 s: s.v, 137 } 138 common.Must(s.v.RequireFeatures(func(im inbound.Manager, om outbound.Manager) { 139 hs.ihm = im 140 hs.ohm = om 141 })) 142 RegisterHandlerServiceServer(server, hs) 143 } 144 145 func init() { 146 common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) { 147 s := core.MustFromContext(ctx) 148 return &service{v: s}, nil 149 })) 150 }