Skip to content

Commit ba7a4cf

Browse files
authored
Merge pull request #39 from blacknon/develop
debug.
2 parents c6ec827 + 5966156 commit ba7a4cf

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

connect.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package sshlib
66

77
import (
8+
"context"
89
"io"
910
"log"
1011
"net"
@@ -32,7 +33,7 @@ type Connect struct {
3233
Stderr io.Writer
3334

3435
// ProxyDialer
35-
ProxyDialer proxy.Dialer
36+
ProxyDialer proxy.ContextDialer
3637

3738
// Connect timeout second.
3839
ConnectTimeout int
@@ -146,8 +147,11 @@ func (c *Connect) CreateClient(host, port, user string, authMethods []ssh.AuthMe
146147
c.ProxyDialer = proxy.Direct
147148
}
148149

150+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
151+
defer cancel()
152+
149153
// Dial to host:port
150-
netConn, err := c.ProxyDialer.Dial("tcp", uri)
154+
netConn, err := c.ProxyDialer.DialContext(ctx, "tcp", uri)
151155
if err != nil {
152156
return
153157
}

go.mod

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ require (
1010
github.com/lunixbochs/vtclean v1.0.0
1111
github.com/miekg/pkcs11 v1.1.1
1212
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
13-
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
14-
golang.org/x/net v0.0.0-20220526153639-5463443f8c37
15-
golang.org/x/sys v0.19.0
16-
golang.org/x/term v0.19.0
13+
golang.org/x/crypto v0.26.0
14+
golang.org/x/net v0.28.0
15+
golang.org/x/sys v0.23.0
16+
golang.org/x/term v0.23.0
1717
)
1818

1919
require (

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,27 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
3737
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
3838
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e h1:T8NU3HyQ8ClP4SEE+KbFlg6n0NhuTsN4MyznaarGsZM=
3939
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
40+
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
41+
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
4042
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
4143
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
4244
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 h1:lUkvobShwKsOesNfWWlCS5q7fnbG1MEliIzwu886fn8=
4345
golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
46+
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
47+
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
4448
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
4549
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
4650
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4751
golang.org/x/sys v0.0.0-20200219091948-cb0a6d8edb6c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4852
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4953
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
5054
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
55+
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
56+
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
5157
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
5258
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
59+
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
60+
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
5361
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5462
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
5563
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

proxy.go

+47
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package sshlib
66

77
import (
88
"bufio"
9+
"context"
910
"fmt"
1011
"net"
1112
"net/http"
@@ -140,6 +141,29 @@ func (n *NetPipe) Dial(network, addr string) (con net.Conn, err error) {
140141
return
141142
}
142143

144+
func (n *NetPipe) DialContext(ctx context.Context, network, addr string) (con net.Conn, err error) {
145+
connChan := make(chan net.Conn, 1)
146+
errChan := make(chan error, 1)
147+
148+
go func() {
149+
conn, err := n.Dial(network, addr)
150+
if err != nil {
151+
errChan <- err
152+
return
153+
}
154+
connChan <- conn
155+
}()
156+
157+
select {
158+
case conn := <-connChan:
159+
return conn, nil
160+
case err := <-errChan:
161+
return nil, err
162+
case <-ctx.Done():
163+
return nil, ctx.Err()
164+
}
165+
}
166+
143167
type httpProxy struct {
144168
host string
145169
haveAuth bool
@@ -195,6 +219,29 @@ func (s *httpProxy) Dial(network, addr string) (net.Conn, error) {
195219
return c, nil
196220
}
197221

222+
func (s *httpProxy) DialContext(ctx context.Context, network, addr string) (con net.Conn, err error) {
223+
connChan := make(chan net.Conn, 1)
224+
errChan := make(chan error, 1)
225+
226+
go func() {
227+
conn, err := s.Dial(network, addr)
228+
if err != nil {
229+
errChan <- err
230+
return
231+
}
232+
connChan <- conn
233+
}()
234+
235+
select {
236+
case conn := <-connChan:
237+
return conn, nil
238+
case err := <-errChan:
239+
return nil, err
240+
case <-ctx.Done():
241+
return nil, ctx.Err()
242+
}
243+
}
244+
198245
// newHttpProxy
199246
func newHttpProxy(uri *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
200247
s := new(httpProxy)

0 commit comments

Comments
 (0)