Skip to content

Commit 0580ba9

Browse files
committed
update bugfix. netpipes timeout.
1 parent 3e93b9d commit 0580ba9

File tree

2 files changed

+54
-59
lines changed

2 files changed

+54
-59
lines changed

connect.go

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,23 @@ func (c *Connect) CreateClient(host, port, user string, authMethods []ssh.AuthMe
151151
defer cancel()
152152

153153
// Dial to host:port
154-
netConn, err := c.ProxyDialer.DialContext(ctx, "tcp", uri)
155-
if err != nil {
156-
return
154+
netConn, cerr := c.ProxyDialer.DialContext(ctx, "tcp", uri)
155+
if cerr != nil {
156+
return cerr
157157
}
158158

159+
// Set deadline
160+
netConn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
161+
159162
// Create new ssh connect
160-
sshCon, channel, req, err := ssh.NewClientConn(netConn, uri, config)
161-
if err != nil {
162-
return
163+
sshCon, channel, req, cerr := ssh.NewClientConn(netConn, uri, config)
164+
if cerr != nil {
165+
return cerr
163166
}
164167

168+
// Reet deadline
169+
netConn.SetDeadline(time.Time{})
170+
165171
// Create *ssh.Client
166172
c.Client = ssh.NewClient(sshCon, channel, req)
167173

@@ -172,40 +178,36 @@ func (c *Connect) CreateClient(host, port, user string, authMethods []ssh.AuthMe
172178
func (c *Connect) CreateSession() (session *ssh.Session, err error) {
173179
// Create session
174180
session, err = c.Client.NewSession()
175-
176181
return
177182
}
178183

179184
// SendKeepAlive send packet to session.
180185
// TODO(blacknon): Interval及びMaxを設定できるようにする(v0.1.1)
181186
func (c *Connect) SendKeepAlive(session *ssh.Session) {
182187
// keep alive interval (default 30 sec)
183-
interval := 30
188+
interval := 1
184189
if c.SendKeepAliveInterval > 0 {
185190
interval = c.SendKeepAliveInterval
186191
}
187192

188-
// keep alive max (default 5)
189-
max := 5
190-
if c.SendKeepAliveMax > 0 {
191-
max = c.SendKeepAliveMax
192-
}
193-
194-
// keep alive counter
195-
i := 0
196193
for {
197-
// Send keep alive packet
198-
_, err := session.SendRequest("keepalive", true, nil)
199-
// _, _, err := c.Client.SendRequest("keepalive", true, nil)
200-
if err == nil {
201-
i = 0
202-
} else {
203-
i += 1
204-
}
194+
// timeout channel
195+
tc := make(chan bool, 1)
196+
197+
go func() {
198+
// Send keep alive packet
199+
_, err := session.SendRequest("keepalive", true, nil)
200+
if err == nil {
201+
tc <- true
202+
}
203+
}()
205204

206-
// check counter
207-
if max <= i {
205+
select {
206+
case <-tc:
207+
case <-time.After(time.Duration(c.ConnectTimeout) * time.Second):
208208
session.Close()
209+
c.Client.Close()
210+
log.Println("keepalive timeout")
209211
return
210212
}
211213

proxy.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,31 @@ type ProxyDialer interface {
2323
}
2424

2525
type ContextDialer struct {
26-
dialer proxy.Dialer
26+
Dialer proxy.Dialer
27+
}
28+
29+
func (c *ContextDialer) GetDialer() proxy.Dialer {
30+
return c.Dialer
2731
}
2832

2933
func (c *ContextDialer) Dial(network, addr string) (net.Conn, error) {
30-
return c.dialer.Dial(network, addr)
34+
return c.Dialer.Dial(network, addr)
3135
}
3236

3337
func (c *ContextDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
38+
// Simply call the DialContext method if supported
39+
if dialerCtx, ok := c.Dialer.(interface {
40+
DialContext(context.Context, string, string) (net.Conn, error)
41+
}); ok {
42+
return dialerCtx.DialContext(ctx, network, addr)
43+
}
44+
45+
// Fallback if DialContext is not supported
3446
connChan := make(chan net.Conn, 1)
3547
errChan := make(chan error, 1)
3648

3749
go func() {
38-
conn, err := c.dialer.Dial(network, addr)
50+
conn, err := c.Dialer.Dial(network, addr)
3951
if err != nil {
4052
errChan <- err
4153
return
@@ -96,7 +108,7 @@ func (p *Proxy) CreateProxyDialer() (proxyContextDialer ProxyDialer, err error)
96108
proxyDialer, err = p.CreateProxyCommandProxyDialer()
97109
}
98110

99-
proxyContextDialer = &ContextDialer{dialer: proxyDialer}
111+
proxyContextDialer = &ContextDialer{Dialer: proxyDialer}
100112

101113
return
102114
}
@@ -158,6 +170,8 @@ func (p *Proxy) CreateProxyCommandProxyDialer() (proxyDialer proxy.Dialer, err e
158170

159171
type NetPipe struct {
160172
Command string
173+
ctx context.Context
174+
Cmd *exec.Cmd
161175
}
162176

163177
func (n *NetPipe) Dial(network, addr string) (con net.Conn, err error) {
@@ -167,15 +181,15 @@ func (n *NetPipe) Dial(network, addr string) (con net.Conn, err error) {
167181
// Create net.Pipe(), and set proxyCommand
168182
con, srv := net.Pipe()
169183

170-
cmd := exec.Command("sh", "-c", n.Command)
184+
n.Cmd = exec.Command("sh", "-c", n.Command)
171185

172186
// setup FD
173-
cmd.Stdin = srv
174-
cmd.Stdout = srv
175-
cmd.Stderr = os.Stderr
187+
n.Cmd.Stdin = srv
188+
n.Cmd.Stdout = srv
189+
n.Cmd.Stderr = os.Stderr
176190

177-
// run proxyCommand
178-
err = cmd.Start()
191+
// Start the command
192+
err = n.Cmd.Start()
179193

180194
return
181195
}
@@ -190,6 +204,7 @@ func (n *NetPipe) DialContext(ctx context.Context, network, addr string) (con ne
190204
errChan <- err
191205
return
192206
}
207+
193208
connChan <- conn
194209
}()
195210

@@ -199,6 +214,7 @@ func (n *NetPipe) DialContext(ctx context.Context, network, addr string) (con ne
199214
case err := <-errChan:
200215
return nil, err
201216
case <-ctx.Done():
217+
n.Cmd.Process.Kill()
202218
return nil, ctx.Err()
203219
}
204220
}
@@ -258,29 +274,6 @@ func (s *httpProxy) Dial(network, addr string) (net.Conn, error) {
258274
return c, nil
259275
}
260276

261-
func (s *httpProxy) DialContext(ctx context.Context, network, addr string) (con net.Conn, err error) {
262-
connChan := make(chan net.Conn, 1)
263-
errChan := make(chan error, 1)
264-
265-
go func() {
266-
conn, err := s.Dial(network, addr)
267-
if err != nil {
268-
errChan <- err
269-
return
270-
}
271-
connChan <- conn
272-
}()
273-
274-
select {
275-
case conn := <-connChan:
276-
return conn, nil
277-
case err := <-errChan:
278-
return nil, err
279-
case <-ctx.Done():
280-
return nil, ctx.Err()
281-
}
282-
}
283-
284277
// newHttpProxy
285278
func newHttpProxy(uri *url.URL, forward proxy.Dialer) (proxy.Dialer, error) {
286279
s := new(httpProxy)

0 commit comments

Comments
 (0)