From c29f1c8cf02fb91742f9b09a1bc235a30a0dc47f Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sun, 21 Oct 2018 21:27:05 +0200 Subject: [PATCH] merge duplicated code --- app/proxyman/command/command.go | 19 +++-------- v2ray.go | 58 ++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/app/proxyman/command/command.go b/app/proxyman/command/command.go index d3d8ffc8..14df7fcc 100755 --- a/app/proxyman/command/command.go +++ b/app/proxyman/command/command.go @@ -70,15 +70,11 @@ type handlerServer struct { } func (s *handlerServer) AddInbound(ctx context.Context, request *AddInboundRequest) (*AddInboundResponse, error) { - rawHandler, err := core.CreateObject(s.s, request.Inbound) - if err != nil { + if err := core.AddInboundHandler(s.s, request.Inbound); err != nil { return nil, err } - handler, ok := rawHandler.(inbound.Handler) - if !ok { - return nil, newError("not an InboundHandler.") - } - return &AddInboundResponse{}, s.ihm.AddHandler(ctx, handler) + + return &AddInboundResponse{}, nil } func (s *handlerServer) RemoveInbound(ctx context.Context, request *RemoveInboundRequest) (*RemoveInboundResponse, error) { @@ -104,15 +100,10 @@ func (s *handlerServer) AlterInbound(ctx context.Context, request *AlterInboundR } func (s *handlerServer) AddOutbound(ctx context.Context, request *AddOutboundRequest) (*AddOutboundResponse, error) { - rawHandler, err := core.CreateObject(s.s, request.Outbound) - if err != nil { + if err := core.AddOutboundHandler(s.s, request.Outbound); err != nil { return nil, err } - handler, ok := rawHandler.(outbound.Handler) - if !ok { - return nil, newError("not an OutboundHandler.") - } - return &AddOutboundResponse{}, s.ohm.AddHandler(ctx, handler) + return &AddOutboundResponse{}, nil } func (s *handlerServer) RemoveOutbound(ctx context.Context, request *RemoveOutboundRequest) (*RemoveOutboundResponse, error) { diff --git a/v2ray.go b/v2ray.go index ce72d010..e52bd238 100755 --- a/v2ray.go +++ b/v2ray.go @@ -60,22 +60,25 @@ type Instance struct { running bool } -func addInboundHandlers(server *Instance, configs []*InboundHandlerConfig) error { - if len(configs) == 0 { - return nil +func AddInboundHandler(server *Instance, config *InboundHandlerConfig) error { + inboundManager := server.GetFeature(inbound.ManagerType()).(inbound.Manager) + rawHandler, err := CreateObject(server, config) + if err != nil { + return err + } + handler, ok := rawHandler.(inbound.Handler) + if !ok { + return newError("not an InboundHandler") } + if err := inboundManager.AddHandler(context.Background(), handler); err != nil { + return err + } + return nil +} - inboundManager := server.GetFeature(inbound.ManagerType()).(inbound.Manager) +func addInboundHandlers(server *Instance, configs []*InboundHandlerConfig) error { for _, inboundConfig := range configs { - rawHandler, err := CreateObject(server, inboundConfig) - if err != nil { - return err - } - handler, ok := rawHandler.(inbound.Handler) - if !ok { - return newError("not an InboundHandler") - } - if err := inboundManager.AddHandler(context.Background(), handler); err != nil { + if err := AddInboundHandler(server, inboundConfig); err != nil { return err } } @@ -83,22 +86,25 @@ func addInboundHandlers(server *Instance, configs []*InboundHandlerConfig) error return nil } -func addOutboundHandlers(server *Instance, configs []*OutboundHandlerConfig) error { - if len(configs) == 0 { - return nil +func AddOutboundHandler(server *Instance, config *OutboundHandlerConfig) error { + outboundManager := server.GetFeature(outbound.ManagerType()).(outbound.Manager) + rawHandler, err := CreateObject(server, config) + if err != nil { + return err + } + handler, ok := rawHandler.(outbound.Handler) + if !ok { + return newError("not an OutboundHandler") } + if err := outboundManager.AddHandler(context.Background(), handler); err != nil { + return err + } + return nil +} - outboundManager := server.GetFeature(outbound.ManagerType()).(outbound.Manager) +func addOutboundHandlers(server *Instance, configs []*OutboundHandlerConfig) error { for _, outboundConfig := range configs { - rawHandler, err := CreateObject(server, outboundConfig) - if err != nil { - return err - } - handler, ok := rawHandler.(outbound.Handler) - if !ok { - return newError("not an OutboundHandler") - } - if err := outboundManager.AddHandler(context.Background(), handler); err != nil { + if err := AddOutboundHandler(server, outboundConfig); err != nil { return err } }