1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 23:47:07 -05:00
This commit is contained in:
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.AddHandler(context.Background(), &CommanderOutbound{
c.ohm.AddHandler(context.Background(), &Outbound{
tag: c.config.Tag,
listener: listener,
})

View File

@ -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()
}

View File

@ -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
}

View File

@ -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
}