add retry on socks and vmess inbound

pull/45/head
Claire Raymond 9 years ago
parent 0acfe57b0c
commit f10f08c87d

@ -4,7 +4,6 @@ import (
"errors" "errors"
"io" "io"
"net" "net"
"strconv"
"sync" "sync"
"time" "time"
@ -12,6 +11,7 @@ import (
"github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" 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"
jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json" jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json"
"github.com/v2ray/v2ray-core/proxy/socks/protocol" "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 { 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 { if err != nil {
log.Error("Socks failed to listen on port %d: %v", port, err) log.Error("Socks failed to listen on port %d: %v", port, err)
return err return err
@ -50,18 +54,22 @@ func (server *SocksServer) Listen(port uint16) error {
return nil return nil
} }
func (server *SocksServer) AcceptConnections(listener net.Listener) { func (server *SocksServer) AcceptConnections(listener *net.TCPListener) {
for server.accepting { for server.accepting {
connection, err := listener.Accept() retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
if err != nil { connection, err := listener.AcceptTCP()
log.Error("Socks failed to accept new connection %v", err) if err != nil {
continue log.Error("Socks failed to accept new connection %v", err)
} return err
go server.HandleConnection(connection) }
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() defer connection.Close()
reader := v2net.NewTimeOutReader(120, connection) reader := v2net.NewTimeOutReader(120, connection)

@ -11,6 +11,7 @@ import (
v2io "github.com/v2ray/v2ray-core/common/io" v2io "github.com/v2ray/v2ray-core/common/io"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" 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"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol" "github.com/v2ray/v2ray-core/proxy/vmess/protocol"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" "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 { func (handler *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error {
for handler.accepting { for handler.accepting {
connection, err := listener.AcceptTCP() retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
if err != nil { connection, err := listener.AcceptTCP()
log.Error("Failed to accpet connection: %s", err.Error()) if err != nil {
continue log.Error("Failed to accpet connection: %s", err.Error())
} return err
go handler.HandleConnection(connection) }
go handler.HandleConnection(connection)
return nil
})
} }
return nil return nil
} }

Loading…
Cancel
Save