Skip to content

Commit

Permalink
fix(platform): ssh proxy dial target no time out (#2069)
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Ryu committed Aug 23, 2022
1 parent 49a5902 commit 11d78c8
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions pkg/util/ssh/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
package ssh

import (
"fmt"
"net"
"time"

"tkestack.io/tke/pkg/util/log"
)

type Proxy interface {
Expand All @@ -39,18 +43,37 @@ func (sj JumpServer) ProxyConn(targetAddr string) (net.Conn, func(), error) {
}
// do not use sudo in jump server
sshstruct.Sudo = false
if sshstruct.DialTimeOut == 0 {
sshstruct.DialTimeOut = time.Second
}
jumperClient, closer, err := sshstruct.newClient()
if err != nil {
return nil, nil, err
}
conn, err := jumperClient.Dial("tcp", targetAddr)
if err != nil {
closer()
return nil, nil, err
type result struct {
conn net.Conn
err error
}
return conn,
func() {
ch := make(chan result, 1)
go func() {
conn, err := jumperClient.Dial("tcp", targetAddr)
if err != nil {
closer()
},
nil
log.Errorf("proxy %s dial %s failed: %v", sj.Host, targetAddr)
} else {
log.Infof("proxy %s dial %s sucess", sj.Host, targetAddr)
}
r := result{conn: conn, err: err}
ch <- r
}()
select {
case r := <-ch:
return r.conn,
func() {
closer()
},
r.err
case <-time.After(sshstruct.DialTimeOut):
return nil, nil, fmt.Errorf("proxy %s dial %s time out in %s", sshstruct.Host, targetAddr, sshstruct.DialTimeOut.String())
}
}

0 comments on commit 11d78c8

Please sign in to comment.