1
0
mirror of https://github.com/rancher/rke.git synced 2025-05-03 13:57:21 +00:00
rke/hosts/dialer.go

54 lines
1.1 KiB
Go
Raw Normal View History

2017-10-29 09:45:21 +00:00
package hosts
import (
"fmt"
"net"
"net/http"
"golang.org/x/crypto/ssh"
)
type Dialer interface {
NewHTTPClient() (*http.Client, error)
}
type sshDialer struct {
2017-11-17 21:33:07 +00:00
host *Host
signer ssh.Signer
2017-10-29 09:45:21 +00:00
}
func (d *sshDialer) NewHTTPClient() (*http.Client, error) {
dialer := &sshDialer{
host: d.host,
signer: d.signer,
}
httpClient := &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
},
}
return httpClient, nil
}
2017-10-29 09:45:21 +00:00
func (d *sshDialer) Dial(network, addr string) (net.Conn, error) {
2017-11-28 17:45:24 +00:00
sshAddr := d.host.Address + ":22"
2017-10-29 09:45:21 +00:00
// Build SSH client configuration
2017-11-17 21:33:07 +00:00
cfg, err := makeSSHConfig(d.host.User, d.signer)
2017-10-29 09:45:21 +00:00
if err != nil {
return nil, fmt.Errorf("Error configuring SSH: %v", err)
2017-10-29 09:45:21 +00:00
}
// Establish connection with SSH server
conn, err := ssh.Dial("tcp", sshAddr, cfg)
if err != nil {
return nil, fmt.Errorf("Failed to dial ssh using address [%s]: %v", sshAddr, err)
2017-10-29 09:45:21 +00:00
}
if len(d.host.DockerSocket) == 0 {
d.host.DockerSocket = "/var/run/docker.sock"
}
remote, err := conn.Dial("unix", d.host.DockerSocket)
if err != nil {
return nil, fmt.Errorf("Failed to dial to Docker socket: %v", err)
2017-10-29 09:45:21 +00:00
}
return remote, err
}