add timeout for Connectors
This commit is contained in:
parent
99a08048a0
commit
8d16b2d0b5
25
.dockerignore
Normal file
25
.dockerignore
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Folders
|
||||||
|
_obj
|
||||||
|
_test
|
||||||
|
release
|
||||||
|
debian
|
||||||
|
docs
|
||||||
|
|
||||||
|
*.exe
|
||||||
|
*.test
|
||||||
|
|
||||||
|
*.bak
|
||||||
|
|
||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
LICENSE
|
||||||
|
VERSION
|
||||||
|
README.md
|
||||||
|
Changelog.md
|
||||||
|
Makefile
|
||||||
|
docker-compose.yml
|
7
chain.go
7
chain.go
@ -136,8 +136,13 @@ func (c *Chain) dialWithOptions(addr string, options *ChainOptions) (net.Conn, e
|
|||||||
|
|
||||||
ipAddr := c.resolve(addr, options.Resolver, options.Hosts)
|
ipAddr := c.resolve(addr, options.Resolver, options.Hosts)
|
||||||
|
|
||||||
|
timeout := options.Timeout
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = DialTimeout
|
||||||
|
}
|
||||||
|
|
||||||
if route.IsEmpty() {
|
if route.IsEmpty() {
|
||||||
return net.DialTimeout("tcp", ipAddr, options.Timeout)
|
return net.DialTimeout("tcp", ipAddr, timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := route.getConn()
|
conn, err := route.getConn()
|
||||||
|
10
client.go
10
client.go
@ -236,7 +236,8 @@ func QUICConfigHandshakeOption(config *QUICConfig) HandshakeOption {
|
|||||||
|
|
||||||
// ConnectOptions describes the options for Connector.Connect.
|
// ConnectOptions describes the options for Connector.Connect.
|
||||||
type ConnectOptions struct {
|
type ConnectOptions struct {
|
||||||
Addr string
|
Addr string
|
||||||
|
Timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConnectOption allows a common way to set ConnectOptions.
|
// ConnectOption allows a common way to set ConnectOptions.
|
||||||
@ -248,3 +249,10 @@ func AddrConnectOption(addr string) ConnectOption {
|
|||||||
opts.Addr = addr
|
opts.Addr = addr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TimeoutConnectOption specifies the timeout for connecting to target.
|
||||||
|
func TimeoutConnectOption(timeout time.Duration) ConnectOption {
|
||||||
|
return func(opts *ConnectOptions) {
|
||||||
|
opts.Timeout = timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -8,11 +8,15 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// SetLogger(&LogLogger{})
|
// SetLogger(&LogLogger{})
|
||||||
// Debug = true
|
// Debug = true
|
||||||
|
DialTimeout = 500 * time.Millisecond
|
||||||
|
HandshakeTimeout = 500 * time.Millisecond
|
||||||
|
ConnectTimeout = 500 * time.Millisecond
|
||||||
|
|
||||||
cert, err := GenCertificate()
|
cert, err := GenCertificate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
15
forward.go
15
forward.go
@ -714,6 +714,8 @@ func (l *tcpRemoteForwardListener) listenLoop() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tempDelay = 0
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case l.connChan <- conn:
|
case l.connChan <- conn:
|
||||||
default:
|
default:
|
||||||
@ -774,7 +776,7 @@ func (l *tcpRemoteForwardListener) muxAccept() (conn net.Conn, err error) {
|
|||||||
return cc, nil
|
return cc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *tcpRemoteForwardListener) getSession() (*muxSession, error) {
|
func (l *tcpRemoteForwardListener) getSession() (s *muxSession, err error) {
|
||||||
l.sessionMux.Lock()
|
l.sessionMux.Lock()
|
||||||
defer l.sessionMux.Unlock()
|
defer l.sessionMux.Unlock()
|
||||||
|
|
||||||
@ -787,6 +789,15 @@ func (l *tcpRemoteForwardListener) getSession() (*muxSession, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func(c net.Conn) {
|
||||||
|
if err != nil {
|
||||||
|
c.Close()
|
||||||
|
}
|
||||||
|
}(conn)
|
||||||
|
|
||||||
|
conn.SetDeadline(time.Now().Add(HandshakeTimeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
conn, err = socks5Handshake(conn, l.chain.LastNode().User)
|
conn, err = socks5Handshake(conn, l.chain.LastNode().User)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -797,13 +808,11 @@ func (l *tcpRemoteForwardListener) getSession() (*muxSession, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.SetReadDeadline(time.Now().Add(ReadTimeout))
|
|
||||||
rep, err := gosocks5.ReadReply(conn)
|
rep, err := gosocks5.ReadReply(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Log("[rtcp] SOCKS5 BIND reply: ", err)
|
log.Log("[rtcp] SOCKS5 BIND reply: ", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn.SetReadDeadline(time.Time{})
|
|
||||||
if rep.Rep != gosocks5.Succeeded {
|
if rep.Rep != gosocks5.Succeeded {
|
||||||
log.Logf("[rtcp] bind on %s failure", l.addr)
|
log.Logf("[rtcp] bind on %s failure", l.addr)
|
||||||
return nil, fmt.Errorf("Bind on %s failure", l.addr.String())
|
return nil, fmt.Errorf("Bind on %s failure", l.addr.String())
|
||||||
|
6
gost.go
6
gost.go
@ -53,10 +53,12 @@ var (
|
|||||||
DialTimeout = 5 * time.Second
|
DialTimeout = 5 * time.Second
|
||||||
// HandshakeTimeout is the timeout of handshake.
|
// HandshakeTimeout is the timeout of handshake.
|
||||||
HandshakeTimeout = 5 * time.Second
|
HandshakeTimeout = 5 * time.Second
|
||||||
|
// ConnectTimeout is the timeout for connect.
|
||||||
|
ConnectTimeout = 5 * time.Second
|
||||||
// ReadTimeout is the timeout for reading.
|
// ReadTimeout is the timeout for reading.
|
||||||
ReadTimeout = 5 * time.Second
|
ReadTimeout = 10 * time.Second
|
||||||
// WriteTimeout is the timeout for writing.
|
// WriteTimeout is the timeout for writing.
|
||||||
WriteTimeout = 5 * time.Second
|
WriteTimeout = 10 * time.Second
|
||||||
// PingTimeout is the timeout for pinging.
|
// PingTimeout is the timeout for pinging.
|
||||||
PingTimeout = 30 * time.Second
|
PingTimeout = 30 * time.Second
|
||||||
// PingRetries is the reties of ping.
|
// PingRetries is the reties of ping.
|
||||||
|
13
http.go
13
http.go
@ -28,6 +28,19 @@ func HTTPConnector(user *url.Userinfo) Connector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
func (c *httpConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
||||||
|
opts := &ConnectOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := opts.Timeout
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = ConnectTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.SetDeadline(time.Now().Add(timeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
req := &http.Request{
|
req := &http.Request{
|
||||||
Method: http.MethodConnect,
|
Method: http.MethodConnect,
|
||||||
URL: &url.URL{Host: addr},
|
URL: &url.URL{Host: addr},
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// proxyConn obtains a connection to the proxy server.
|
// proxyConn obtains a connection to the proxy server.
|
||||||
@ -77,8 +76,8 @@ func proxyRoundtrip(client *Client, server *Server, targetURL string, data []byt
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.SetDeadline(time.Now().Add(500 * time.Millisecond))
|
// conn.SetDeadline(time.Now().Add(500 * time.Millisecond))
|
||||||
defer conn.SetDeadline(time.Time{})
|
// defer conn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
conn, err = client.Connect(conn, u.Host)
|
conn, err = client.Connect(conn, u.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
69
socks.go
69
socks.go
@ -205,6 +205,19 @@ func SOCKS5Connector(user *url.Userinfo) Connector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *socks5Connector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
func (c *socks5Connector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
||||||
|
opts := &ConnectOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := opts.Timeout
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = ConnectTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.SetDeadline(time.Now().Add(timeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
selector := &clientSelector{
|
selector := &clientSelector{
|
||||||
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
User: c.User,
|
User: c.User,
|
||||||
@ -266,6 +279,19 @@ func SOCKS5BindConnector(user *url.Userinfo) Connector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *socks5BindConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
func (c *socks5BindConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
||||||
|
opts := &ConnectOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := opts.Timeout
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = ConnectTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.SetDeadline(time.Now().Add(timeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
cc, err := socks5Handshake(conn, c.User)
|
cc, err := socks5Handshake(conn, c.User)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -292,12 +318,10 @@ func (c *socks5BindConnector) Connect(conn net.Conn, addr string, options ...Con
|
|||||||
log.Log("[socks5] bind\n", req)
|
log.Log("[socks5] bind\n", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.SetReadDeadline(time.Now().Add(ReadTimeout))
|
|
||||||
reply, err := gosocks5.ReadReply(conn)
|
reply, err := gosocks5.ReadReply(conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn.SetReadDeadline(time.Time{})
|
|
||||||
|
|
||||||
if Debug {
|
if Debug {
|
||||||
log.Log("[socks5] bind\n", reply)
|
log.Log("[socks5] bind\n", reply)
|
||||||
@ -327,6 +351,19 @@ func SOCKS5UDPConnector(user *url.Userinfo) Connector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *socks5UDPConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
func (c *socks5UDPConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
||||||
|
opts := &ConnectOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := opts.Timeout
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = ConnectTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.SetDeadline(time.Now().Add(timeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
cc, err := socks5Handshake(conn, c.User)
|
cc, err := socks5Handshake(conn, c.User)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -388,6 +425,19 @@ func SOCKS4Connector() Connector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *socks4Connector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
func (c *socks4Connector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
||||||
|
opts := &ConnectOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := opts.Timeout
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = ConnectTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.SetDeadline(time.Now().Add(timeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
taddr, err := net.ResolveTCPAddr("tcp4", addr)
|
taddr, err := net.ResolveTCPAddr("tcp4", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -435,6 +485,19 @@ func SOCKS4AConnector() Connector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *socks4aConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
func (c *socks4aConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
||||||
|
opts := &ConnectOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := opts.Timeout
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = ConnectTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.SetDeadline(time.Now().Add(timeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
host, port, err := net.SplitHostPort(addr)
|
host, port, err := net.SplitHostPort(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -1645,7 +1708,7 @@ type socks5UDPConn struct {
|
|||||||
func (c *socks5UDPConn) Read(b []byte) (int, error) {
|
func (c *socks5UDPConn) Read(b []byte) (int, error) {
|
||||||
data := mPool.Get().([]byte)
|
data := mPool.Get().([]byte)
|
||||||
defer mPool.Put(data)
|
defer mPool.Put(data)
|
||||||
|
|
||||||
n, err := c.UDPConn.Read(data)
|
n, err := c.UDPConn.Read(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
13
ss.go
13
ss.go
@ -68,6 +68,19 @@ func ShadowConnector(cipher *url.Userinfo) Connector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *shadowConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
func (c *shadowConnector) Connect(conn net.Conn, addr string, options ...ConnectOption) (net.Conn, error) {
|
||||||
|
opts := &ConnectOptions{}
|
||||||
|
for _, option := range options {
|
||||||
|
option(opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout := opts.Timeout
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = ConnectTimeout
|
||||||
|
}
|
||||||
|
|
||||||
|
conn.SetDeadline(time.Now().Add(timeout))
|
||||||
|
defer conn.SetDeadline(time.Time{})
|
||||||
|
|
||||||
rawaddr, err := ss.RawAddr(addr)
|
rawaddr, err := ss.RawAddr(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user