From 016df73cfd7de0e57395d06f9a00ac2a590cef03 Mon Sep 17 00:00:00 2001 From: Dingjun Date: Sat, 16 Sep 2017 09:44:08 +0800 Subject: [PATCH] add timeout for dial --- client.go | 6 ++++-- obfssh/proxy.go | 6 +++--- obfssh/ssh.go | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 74800ea..6b506d9 100644 --- a/client.go +++ b/client.go @@ -84,7 +84,8 @@ func (cc *Client) Close() { l.Close() } //Log(DEBUG, "close ssh connection") - //cc.sshConn.Close() + cc.sshConn.Close() + cc.conn.Close() } // RunCmd run a single command on server @@ -290,7 +291,8 @@ func (cc *Client) keepAlive(interval time.Duration, maxCount int) { if count >= maxCount { cc.err = fmt.Errorf("keep alive detects connection hang up") Log(ERROR, "keep alive hit max count, exit") - cc.sshConn.Close() + //cc.sshConn.Close() + //cc.conn.Close() // send exit signal close(cc.ch) return diff --git a/obfssh/proxy.go b/obfssh/proxy.go index bfbdeb6..6c5a803 100644 --- a/obfssh/proxy.go +++ b/obfssh/proxy.go @@ -147,7 +147,7 @@ func httpProxyHandshake(c net.Conn, host string, port int) (net.Conn, error) { } func dialHTTPProxy(host string, port int, p proxy) (net.Conn, error) { - c, err := net.Dial("tcp", net.JoinHostPort(p.Host, fmt.Sprintf("%d", p.Port))) + c, err := dialer.Dial("tcp", net.JoinHostPort(p.Host, fmt.Sprintf("%d", p.Port))) if err != nil { return nil, err } @@ -171,7 +171,7 @@ func dialHTTPSProxy(host string, port int, p proxy) (net.Conn, error) { InsecureSkipVerify: p.Insecure, } - c, err := tls.Dial("tcp", net.JoinHostPort(p.Host, fmt.Sprintf("%d", p.Port)), tlsconfig) + c, err := tls.DialWithDialer(dialer, "tcp", net.JoinHostPort(p.Host, fmt.Sprintf("%d", p.Port)), tlsconfig) if err != nil { return nil, err } @@ -190,7 +190,7 @@ func dialHTTPSProxy(host string, port int, p proxy) (net.Conn, error) { } func dialSocks5Proxy(host string, port int, p proxy) (net.Conn, error) { - c, err := net.Dial("tcp", net.JoinHostPort(p.Host, fmt.Sprintf("%d", p.Port))) + c, err := dialer.Dial("tcp", net.JoinHostPort(p.Host, fmt.Sprintf("%d", p.Port))) if err != nil { return nil, err } diff --git a/obfssh/ssh.go b/obfssh/ssh.go index 156a593..b9db9be 100644 --- a/obfssh/ssh.go +++ b/obfssh/ssh.go @@ -17,6 +17,8 @@ import ( "time" ) +var dialer = &net.Dialer{Timeout: 10 * time.Second} + func main() { var configfile string var cfg config @@ -185,7 +187,7 @@ func main() { err = fmt.Errorf("unsupported scheme: %s", cfg.Proxy.Scheme) } } else { - c, err = net.Dial("tcp", rhost) + c, err = dialer.Dial("tcp", rhost) } if err != nil {