From f10f08c87d7ae05b68afd832ce25f5ae063ee44e Mon Sep 17 00:00:00 2001 From: Claire Raymond Date: Thu, 15 Oct 2015 11:42:43 +0000 Subject: [PATCH] add retry on socks and vmess inbound --- proxy/socks/socks.go | 28 ++++++++++++++++++---------- proxy/vmess/vmessin.go | 17 +++++++++++------ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index eaf0722b1..493245903 100644 --- a/proxy/socks/socks.go +++ b/proxy/socks/socks.go @@ -4,7 +4,6 @@ import ( "errors" "io" "net" - "strconv" "sync" "time" @@ -12,6 +11,7 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" + "github.com/v2ray/v2ray-core/common/retry" "github.com/v2ray/v2ray-core/proxy" jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json" "github.com/v2ray/v2ray-core/proxy/socks/protocol" @@ -37,7 +37,11 @@ func NewSocksServer(dispatcher app.PacketDispatcher, config *jsonconfig.SocksCon } func (server *SocksServer) Listen(port uint16) error { - listener, err := net.Listen("tcp", ":"+strconv.Itoa(int(port))) + listener, err := net.ListenTCP("tcp", &net.TCPAddr{ + IP: []byte{0, 0, 0, 0}, + Port: int(port), + Zone: "", + }) if err != nil { log.Error("Socks failed to listen on port %d: %v", port, err) return err @@ -50,18 +54,22 @@ func (server *SocksServer) Listen(port uint16) error { return nil } -func (server *SocksServer) AcceptConnections(listener net.Listener) { +func (server *SocksServer) AcceptConnections(listener *net.TCPListener) { for server.accepting { - connection, err := listener.Accept() - if err != nil { - log.Error("Socks failed to accept new connection %v", err) - continue - } - go server.HandleConnection(connection) + retry.Timed(100 /* times */, 100 /* ms */).On(func() error { + connection, err := listener.AcceptTCP() + if err != nil { + log.Error("Socks failed to accept new connection %v", err) + return err + } + go server.HandleConnection(connection) + return nil + }) + } } -func (server *SocksServer) HandleConnection(connection net.Conn) error { +func (server *SocksServer) HandleConnection(connection *net.TCPConn) error { defer connection.Close() reader := v2net.NewTimeOutReader(120, connection) diff --git a/proxy/vmess/vmessin.go b/proxy/vmess/vmessin.go index 444ea8e6a..485dd8694 100644 --- a/proxy/vmess/vmessin.go +++ b/proxy/vmess/vmessin.go @@ -11,6 +11,7 @@ import ( v2io "github.com/v2ray/v2ray-core/common/io" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" + "github.com/v2ray/v2ray-core/common/retry" "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/vmess/protocol" "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" @@ -53,12 +54,16 @@ func (handler *VMessInboundHandler) Listen(port uint16) error { func (handler *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error { for handler.accepting { - connection, err := listener.AcceptTCP() - if err != nil { - log.Error("Failed to accpet connection: %s", err.Error()) - continue - } - go handler.HandleConnection(connection) + retry.Timed(100 /* times */, 100 /* ms */).On(func() error { + connection, err := listener.AcceptTCP() + if err != nil { + log.Error("Failed to accpet connection: %s", err.Error()) + return err + } + go handler.HandleConnection(connection) + return nil + }) + } return nil }