2019-02-01 14:08:21 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
2018-02-05 17:38:24 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
grpc "google.golang.org/grpc"
|
2018-10-21 04:27:13 -04:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/inbound"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/outbound"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/proxy"
|
2018-02-05 17:38:24 -05:00
|
|
|
)
|
|
|
|
|
2018-02-08 16:53:01 -05:00
|
|
|
// InboundOperation is the interface for operations that applies to inbound handlers.
|
2018-02-05 17:38:24 -05:00
|
|
|
type InboundOperation interface {
|
2018-03-14 22:32:10 -04:00
|
|
|
// ApplyInbound applies this operation to the given inbound handler.
|
2018-10-11 15:14:53 -04:00
|
|
|
ApplyInbound(context.Context, inbound.Handler) error
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 16:53:01 -05:00
|
|
|
// OutboundOperation is the interface for operations that applies to outbound handlers.
|
2018-02-05 17:38:24 -05:00
|
|
|
type OutboundOperation interface {
|
2018-02-08 16:53:01 -05:00
|
|
|
// ApplyOutbound applies this operation to the given outbound handler.
|
2018-10-11 14:43:37 -04:00
|
|
|
ApplyOutbound(context.Context, outbound.Handler) error
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
func getInbound(handler inbound.Handler) (proxy.Inbound, error) {
|
2018-02-08 16:53:01 -05:00
|
|
|
gi, ok := handler.(proxy.GetInbound)
|
2018-02-05 17:38:24 -05:00
|
|
|
if !ok {
|
2018-02-08 16:53:01 -05:00
|
|
|
return nil, newError("can't get inbound proxy from handler.")
|
|
|
|
}
|
|
|
|
return gi.GetInbound(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInbound implements InboundOperation.
|
2018-10-11 15:14:53 -04:00
|
|
|
func (op *AddUserOperation) ApplyInbound(ctx context.Context, handler inbound.Handler) error {
|
2018-02-08 16:53:01 -05:00
|
|
|
p, err := getInbound(handler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
um, ok := p.(proxy.UserManager)
|
|
|
|
if !ok {
|
2018-04-02 03:17:36 -04:00
|
|
|
return newError("proxy is not a UserManager")
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
2018-08-26 18:11:32 -04:00
|
|
|
mUser, err := op.User.ToMemoryUser()
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to parse user").Base(err)
|
|
|
|
}
|
|
|
|
return um.AddUser(ctx, mUser)
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 16:53:01 -05:00
|
|
|
// ApplyInbound implements InboundOperation.
|
2018-10-11 15:14:53 -04:00
|
|
|
func (op *RemoveUserOperation) ApplyInbound(ctx context.Context, handler inbound.Handler) error {
|
2018-02-08 16:53:01 -05:00
|
|
|
p, err := getInbound(handler)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
um, ok := p.(proxy.UserManager)
|
|
|
|
if !ok {
|
2018-04-02 03:17:36 -04:00
|
|
|
return newError("proxy is not a UserManager")
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
2018-02-08 16:53:01 -05:00
|
|
|
return um.RemoveUser(ctx, op.Email)
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type handlerServer struct {
|
|
|
|
s *core.Instance
|
2018-10-11 15:14:53 -04:00
|
|
|
ihm inbound.Manager
|
2018-10-12 17:57:56 -04:00
|
|
|
ohm outbound.Manager
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *handlerServer) AddInbound(ctx context.Context, request *AddInboundRequest) (*AddInboundResponse, error) {
|
2018-10-21 15:27:05 -04:00
|
|
|
if err := core.AddInboundHandler(s.s, request.Inbound); err != nil {
|
2018-02-05 17:38:24 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-21 15:27:05 -04:00
|
|
|
|
|
|
|
return &AddInboundResponse{}, nil
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *handlerServer) RemoveInbound(ctx context.Context, request *RemoveInboundRequest) (*RemoveInboundResponse, error) {
|
|
|
|
return &RemoveInboundResponse{}, s.ihm.RemoveHandler(ctx, request.Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *handlerServer) AlterInbound(ctx context.Context, request *AlterInboundRequest) (*AlterInboundResponse, error) {
|
|
|
|
rawOperation, err := request.Operation.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unknown operation").Base(err)
|
|
|
|
}
|
|
|
|
operation, ok := rawOperation.(InboundOperation)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("not an inbound operation")
|
|
|
|
}
|
|
|
|
|
|
|
|
handler, err := s.ihm.GetHandler(ctx, request.Tag)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to get handler: ", request.Tag).Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &AlterInboundResponse{}, operation.ApplyInbound(ctx, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *handlerServer) AddOutbound(ctx context.Context, request *AddOutboundRequest) (*AddOutboundResponse, error) {
|
2018-10-21 15:27:05 -04:00
|
|
|
if err := core.AddOutboundHandler(s.s, request.Outbound); err != nil {
|
2018-02-05 17:38:24 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-21 15:27:05 -04:00
|
|
|
return &AddOutboundResponse{}, nil
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *handlerServer) RemoveOutbound(ctx context.Context, request *RemoveOutboundRequest) (*RemoveOutboundResponse, error) {
|
|
|
|
return &RemoveOutboundResponse{}, s.ohm.RemoveHandler(ctx, request.Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *handlerServer) AlterOutbound(ctx context.Context, request *AlterOutboundRequest) (*AlterOutboundResponse, error) {
|
|
|
|
rawOperation, err := request.Operation.GetInstance()
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("unknown operation").Base(err)
|
|
|
|
}
|
|
|
|
operation, ok := rawOperation.(OutboundOperation)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("not an outbound operation")
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := s.ohm.GetHandler(request.Tag)
|
|
|
|
return &AlterOutboundResponse{}, operation.ApplyOutbound(ctx, handler)
|
|
|
|
}
|
|
|
|
|
2020-08-24 08:10:26 -04:00
|
|
|
func (s *handlerServer) mustEmbedUnimplementedHandlerServiceServer() {}
|
|
|
|
|
2018-02-08 17:24:35 -05:00
|
|
|
type service struct {
|
|
|
|
v *core.Instance
|
2018-02-05 17:38:24 -05:00
|
|
|
}
|
|
|
|
|
2018-02-08 17:24:35 -05:00
|
|
|
func (s *service) Register(server *grpc.Server) {
|
2018-10-21 04:27:13 -04:00
|
|
|
hs := &handlerServer{
|
|
|
|
s: s.v,
|
|
|
|
}
|
2018-11-13 17:19:58 -05:00
|
|
|
common.Must(s.v.RequireFeatures(func(im inbound.Manager, om outbound.Manager) {
|
2018-10-22 05:26:22 -04:00
|
|
|
hs.ihm = im
|
|
|
|
hs.ohm = om
|
2018-11-13 17:19:58 -05:00
|
|
|
}))
|
2018-10-21 04:27:13 -04:00
|
|
|
RegisterHandlerServiceServer(server, hs)
|
2018-02-08 09:39:46 -05:00
|
|
|
}
|
2018-02-05 17:38:24 -05:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
|
2018-02-21 11:05:29 -05:00
|
|
|
s := core.MustFromContext(ctx)
|
2018-02-08 17:24:35 -05:00
|
|
|
return &service{v: s}, nil
|
2018-02-05 17:38:24 -05:00
|
|
|
}))
|
|
|
|
}
|