From 75a7e9c7f5210a9c6e828e2b66b28b4d654f07f7 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Wed, 4 Apr 2018 21:32:40 +0200 Subject: [PATCH] comments --- app/commander/commander.go | 2 +- app/commander/outbound.go | 25 ++++++++++++++----------- app/proxyman/inbound/inbound.go | 4 +++- app/router/router.go | 5 +++++ 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/app/commander/commander.go b/app/commander/commander.go index 4a8c80ba7..fef554953 100644 --- a/app/commander/commander.go +++ b/app/commander/commander.go @@ -74,7 +74,7 @@ func (c *Commander) Start() error { }() c.ohm.RemoveHandler(context.Background(), c.config.Tag) - c.ohm.AddHandler(context.Background(), &CommanderOutbound{ + c.ohm.AddHandler(context.Background(), &Outbound{ tag: c.config.Tag, listener: listener, }) diff --git a/app/commander/outbound.go b/app/commander/outbound.go index c55db7478..25e476db1 100644 --- a/app/commander/outbound.go +++ b/app/commander/outbound.go @@ -5,6 +5,7 @@ import ( "net" "sync" + "v2ray.com/core/common" "v2ray.com/core/common/signal" "v2ray.com/core/transport/ray" ) @@ -24,6 +25,7 @@ func (l *OutboundListener) add(conn net.Conn) { } } +// Accept implements net.Listener. func (l *OutboundListener) Accept() (net.Conn, error) { select { case <-l.done.C(): @@ -33,8 +35,9 @@ func (l *OutboundListener) Accept() (net.Conn, error) { } } +// Close implement net.Listener. func (l *OutboundListener) Close() error { - l.done.Close() + common.Must(l.done.Close()) L: for { select { @@ -47,6 +50,7 @@ L: return nil } +// Addr implements net.Listener. func (l *OutboundListener) Addr() net.Addr { return &net.TCPAddr{ IP: net.IP{0, 0, 0, 0}, @@ -54,8 +58,8 @@ func (l *OutboundListener) Addr() net.Addr { } } -// CommanderOutbound is a core.OutboundHandler that handles gRPC connections. -type CommanderOutbound struct { +// Outbound is a core.OutboundHandler that handles gRPC connections. +type Outbound struct { tag string listener *OutboundListener access sync.RWMutex @@ -63,7 +67,7 @@ type CommanderOutbound struct { } // Dispatch implements core.OutboundHandler. -func (co *CommanderOutbound) Dispatch(ctx context.Context, r ray.OutboundRay) { +func (co *Outbound) Dispatch(ctx context.Context, r ray.OutboundRay) { co.access.RLock() if co.closed { @@ -81,12 +85,12 @@ func (co *CommanderOutbound) Dispatch(ctx context.Context, r ray.OutboundRay) { } // Tag implements core.OutboundHandler. -func (co *CommanderOutbound) Tag() string { +func (co *Outbound) Tag() string { return co.tag } // Start implements common.Runnable. -func (co *CommanderOutbound) Start() error { +func (co *Outbound) Start() error { co.access.Lock() co.closed = false co.access.Unlock() @@ -94,11 +98,10 @@ func (co *CommanderOutbound) Start() error { } // Close implements common.Closable. -func (co *CommanderOutbound) Close() error { +func (co *Outbound) Close() error { co.access.Lock() - co.closed = true - co.listener.Close() - co.access.Unlock() + defer co.access.Unlock() - return nil + co.closed = true + return co.listener.Close() } diff --git a/app/proxyman/inbound/inbound.go b/app/proxyman/inbound/inbound.go index 39d339608..084d25b5b 100644 --- a/app/proxyman/inbound/inbound.go +++ b/app/proxyman/inbound/inbound.go @@ -72,7 +72,9 @@ func (m *Manager) RemoveHandler(ctx context.Context, tag string) error { defer m.access.Unlock() if handler, found := m.taggedHandlers[tag]; found { - handler.Close() + if err := handler.Close(); err != nil { + newError("failed to close handler ", tag).Base(err).AtWarning().WithContext(ctx).WriteToLog() + } delete(m.taggedHandlers, tag) return nil } diff --git a/app/router/router.go b/app/router/router.go index 4267064c5..fb30b419c 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -11,12 +11,14 @@ import ( "v2ray.com/core/proxy" ) +// Router is an implementation of core.Router. type Router struct { domainStrategy Config_DomainStrategy rules []Rule dns core.DNSClient } +// NewRouter creates a new Router based on the given config. func NewRouter(ctx context.Context, config *Config) (*Router, error) { v := core.MustFromContext(ctx) r := &Router{ @@ -68,6 +70,7 @@ func (r *ipResolver) Resolve() []net.Address { return r.ip } +// PickRoute implements core.Router. func (r *Router) PickRoute(ctx context.Context) (string, error) { resolver := &ipResolver{ dns: r.dns, @@ -106,10 +109,12 @@ func (r *Router) PickRoute(ctx context.Context) (string, error) { return "", core.ErrNoClue } +// Start implements common.Runnable. func (*Router) Start() error { return nil } +// Close implements common.Closable. func (*Router) Close() error { return nil }