Skip to content

Commit

Permalink
Create create a dailFunc in ssh package once
Browse files Browse the repository at this point in the history
[#159619865]

Signed-off-by: Jake Klein <[email protected]>
  • Loading branch information
ChunyiLyu authored and Jake Klein committed Aug 10, 2018
1 parent 84e790c commit 0b72453
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions ssh/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewConnectionWithServerAliveInterval(hostName, userName, privateKey string,
},
logger: logger,
serverAliveInterval: serverAliveInterval,
dailFunc: createDialFunc(),
}

return conn, nil
Expand All @@ -63,6 +64,7 @@ type Connection struct {
sshConfig *ssh.ClientConfig
logger Logger
serverAliveInterval time.Duration
dailFunc boshhttp.DialFunc
}

func (c Connection) Run(cmd string) (stdout, stderr []byte, exitCode int, err error) {
Expand Down Expand Up @@ -106,14 +108,7 @@ func (w *sessionClosingOnErrorWriter) Write(data []byte) (int, error) {
}

func (c Connection) getConnection() (*ssh.Client, error) {
dialFunc := net.Dial

if os.Getenv("BOSH_ALL_PROXY") != "" {
socksProxy := proxy.NewSocks5Proxy(proxy.NewHostKey(), log.New(os.Stdout, "sock5-proxy", log.LstdFlags))
dialFunc = boshhttp.SOCKS5DialFuncFromEnvironment(net.Dial, socksProxy)
}

conn, err := dialFunc("tcp", c.host)
conn, err := c.dailFunc("tcp", c.host)
if err != nil {
return nil, err
}
Expand All @@ -126,6 +121,15 @@ func (c Connection) getConnection() (*ssh.Client, error) {
return ssh.NewClient(client, chans, reqs), nil
}

func createDialFunc() boshhttp.DialFunc {
dialFunc := net.Dial
if os.Getenv("BOSH_ALL_PROXY") != "" {
socksProxy := proxy.NewSocks5Proxy(proxy.NewHostKey(), log.New(os.Stdout, "sock5-proxy", log.LstdFlags))
dialFunc = boshhttp.SOCKS5DialFuncFromEnvironment(net.Dial, socksProxy)
}
return dialFunc
}

func (c Connection) runInSession(cmd string, stdout, stderr io.Writer, stdin io.Reader) (int, error) {
connection, err := c.getConnection()
if err != nil {
Expand Down

0 comments on commit 0b72453

Please sign in to comment.