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"
"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()
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)
continue
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)

@ -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 {
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())
continue
return err
}
go handler.HandleConnection(connection)
return nil
})
}
return nil
}

Loading…
Cancel
Save