pull/1019/head
Darien Raymond 2018-04-04 21:32:40 +02:00
parent 90c6113dfc
commit 75a7e9c7f5
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 23 additions and 13 deletions

View File

@ -74,7 +74,7 @@ func (c *Commander) Start() error {
}() }()
c.ohm.RemoveHandler(context.Background(), c.config.Tag) c.ohm.RemoveHandler(context.Background(), c.config.Tag)
c.ohm.AddHandler(context.Background(), &CommanderOutbound{ c.ohm.AddHandler(context.Background(), &Outbound{
tag: c.config.Tag, tag: c.config.Tag,
listener: listener, listener: listener,
}) })

View File

@ -5,6 +5,7 @@ import (
"net" "net"
"sync" "sync"
"v2ray.com/core/common"
"v2ray.com/core/common/signal" "v2ray.com/core/common/signal"
"v2ray.com/core/transport/ray" "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) { func (l *OutboundListener) Accept() (net.Conn, error) {
select { select {
case <-l.done.C(): case <-l.done.C():
@ -33,8 +35,9 @@ func (l *OutboundListener) Accept() (net.Conn, error) {
} }
} }
// Close implement net.Listener.
func (l *OutboundListener) Close() error { func (l *OutboundListener) Close() error {
l.done.Close() common.Must(l.done.Close())
L: L:
for { for {
select { select {
@ -47,6 +50,7 @@ L:
return nil return nil
} }
// Addr implements net.Listener.
func (l *OutboundListener) Addr() net.Addr { func (l *OutboundListener) Addr() net.Addr {
return &net.TCPAddr{ return &net.TCPAddr{
IP: net.IP{0, 0, 0, 0}, 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. // Outbound is a core.OutboundHandler that handles gRPC connections.
type CommanderOutbound struct { type Outbound struct {
tag string tag string
listener *OutboundListener listener *OutboundListener
access sync.RWMutex access sync.RWMutex
@ -63,7 +67,7 @@ type CommanderOutbound struct {
} }
// Dispatch implements core.OutboundHandler. // 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() co.access.RLock()
if co.closed { if co.closed {
@ -81,12 +85,12 @@ func (co *CommanderOutbound) Dispatch(ctx context.Context, r ray.OutboundRay) {
} }
// Tag implements core.OutboundHandler. // Tag implements core.OutboundHandler.
func (co *CommanderOutbound) Tag() string { func (co *Outbound) Tag() string {
return co.tag return co.tag
} }
// Start implements common.Runnable. // Start implements common.Runnable.
func (co *CommanderOutbound) Start() error { func (co *Outbound) Start() error {
co.access.Lock() co.access.Lock()
co.closed = false co.closed = false
co.access.Unlock() co.access.Unlock()
@ -94,11 +98,10 @@ func (co *CommanderOutbound) Start() error {
} }
// Close implements common.Closable. // Close implements common.Closable.
func (co *CommanderOutbound) Close() error { func (co *Outbound) Close() error {
co.access.Lock() co.access.Lock()
co.closed = true defer co.access.Unlock()
co.listener.Close()
co.access.Unlock()
return nil co.closed = true
return co.listener.Close()
} }

View File

@ -72,7 +72,9 @@ func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
defer m.access.Unlock() defer m.access.Unlock()
if handler, found := m.taggedHandlers[tag]; found { 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) delete(m.taggedHandlers, tag)
return nil return nil
} }

View File

@ -11,12 +11,14 @@ import (
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
) )
// Router is an implementation of core.Router.
type Router struct { type Router struct {
domainStrategy Config_DomainStrategy domainStrategy Config_DomainStrategy
rules []Rule rules []Rule
dns core.DNSClient dns core.DNSClient
} }
// NewRouter creates a new Router based on the given config.
func NewRouter(ctx context.Context, config *Config) (*Router, error) { func NewRouter(ctx context.Context, config *Config) (*Router, error) {
v := core.MustFromContext(ctx) v := core.MustFromContext(ctx)
r := &Router{ r := &Router{
@ -68,6 +70,7 @@ func (r *ipResolver) Resolve() []net.Address {
return r.ip return r.ip
} }
// PickRoute implements core.Router.
func (r *Router) PickRoute(ctx context.Context) (string, error) { func (r *Router) PickRoute(ctx context.Context) (string, error) {
resolver := &ipResolver{ resolver := &ipResolver{
dns: r.dns, dns: r.dns,
@ -106,10 +109,12 @@ func (r *Router) PickRoute(ctx context.Context) (string, error) {
return "", core.ErrNoClue return "", core.ErrNoClue
} }
// Start implements common.Runnable.
func (*Router) Start() error { func (*Router) Start() error {
return nil return nil
} }
// Close implements common.Closable.
func (*Router) Close() error { func (*Router) Close() error {
return nil return nil
} }