forked from rancher/rke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialer.go
92 lines (79 loc) · 1.99 KB
/
dialer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package hosts
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/client"
"golang.org/x/crypto/ssh"
)
type dialer struct {
host *Host
}
const (
DockerAPIVersion = "1.24"
)
func (d *dialer) Dial(network, addr string) (net.Conn, error) {
sshAddr := d.host.IP + ":22"
// Build SSH client configuration
cfg, err := makeSSHConfig(d.host.User)
if err != nil {
logrus.Fatalf("Error configuring SSH: %v", err)
}
// Establish connection with SSH server
conn, err := ssh.Dial("tcp", sshAddr, cfg)
if err != nil {
logrus.Fatalf("Error establishing SSH connection: %v", err)
}
if len(d.host.DockerSocket) == 0 {
d.host.DockerSocket = "/var/run/docker.sock"
}
remote, err := conn.Dial("unix", d.host.DockerSocket)
if err != nil {
logrus.Fatalf("Error connecting to Docker socket on host [%s]: %v", d.host.Hostname, err)
}
return remote, err
}
func (h *Host) TunnelUp() error {
logrus.Infof("[ssh] Start tunnel for host [%s]", h.Hostname)
dialer := &dialer{
host: h,
}
httpClient := &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
},
}
// set Docker client
var err error
logrus.Debugf("Connecting to Docker API for host [%s]", h.Hostname)
h.DClient, err = client.NewClient("unix:///var/run/docker.sock", DockerAPIVersion, httpClient, nil)
if err != nil {
return fmt.Errorf("Can't connect to Docker for host [%s]: %v", h.Hostname, err)
}
return nil
}
func privateKeyPath() string {
return os.Getenv("HOME") + "/.ssh/id_rsa"
}
// Get private key for ssh authentication
func parsePrivateKey(keyPath string) (ssh.Signer, error) {
buff, _ := ioutil.ReadFile(keyPath)
return ssh.ParsePrivateKey(buff)
}
func makeSSHConfig(user string) (*ssh.ClientConfig, error) {
key, err := parsePrivateKey(privateKeyPath())
if err != nil {
return nil, err
}
config := ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(key),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return &config, nil
}