2013-07-14 07:22:41 -04:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
2015-06-17 16:10:42 -04:00
|
|
|
"fmt"
|
2013-07-14 07:22:41 -04:00
|
|
|
"net"
|
2013-07-14 07:31:51 -04:00
|
|
|
"time"
|
2015-06-17 16:10:42 -04:00
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
2013-07-14 07:22:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectFunc is a convenience method for returning a function
|
|
|
|
// that just uses net.Dial to communicate with the remote end that
|
|
|
|
// is suitable for use with the SSH communicator configuration.
|
2013-08-22 14:53:31 -04:00
|
|
|
func ConnectFunc(network, addr string) func() (net.Conn, error) {
|
2013-07-14 07:22:41 -04:00
|
|
|
return func() (net.Conn, error) {
|
2014-02-21 17:45:40 -05:00
|
|
|
c, err := net.DialTimeout(network, addr, 15*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tcpConn, ok := c.(*net.TCPConn); ok {
|
|
|
|
tcpConn.SetKeepAlive(true)
|
2014-09-04 14:24:01 -04:00
|
|
|
tcpConn.SetKeepAlivePeriod(5 * time.Second)
|
2014-02-21 17:45:40 -05:00
|
|
|
}
|
2014-02-21 17:51:33 -05:00
|
|
|
|
|
|
|
return c, nil
|
2013-07-14 07:22:41 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-17 16:10:42 -04:00
|
|
|
|
|
|
|
// BastionConnectFunc is a convenience method for returning a function
|
|
|
|
// that connects to a host over a bastion connection.
|
|
|
|
func BastionConnectFunc(
|
|
|
|
bProto string,
|
|
|
|
bAddr string,
|
|
|
|
bConf *ssh.ClientConfig,
|
|
|
|
proto string,
|
|
|
|
addr string) func() (net.Conn, error) {
|
|
|
|
return func() (net.Conn, error) {
|
|
|
|
// Connect to the bastion
|
|
|
|
bastion, err := ssh.Dial(bProto, bAddr, bConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error connecting to bastion: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect through to the end host
|
|
|
|
conn, err := bastion.Dial(proto, addr)
|
|
|
|
if err != nil {
|
|
|
|
bastion.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap it up so we close both things properly
|
|
|
|
return &bastionConn{
|
|
|
|
Conn: conn,
|
|
|
|
Bastion: bastion,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bastionConn struct {
|
|
|
|
net.Conn
|
|
|
|
Bastion *ssh.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *bastionConn) Close() error {
|
|
|
|
c.Conn.Close()
|
|
|
|
return c.Bastion.Close()
|
|
|
|
}
|