This repository has been archived by the owner on Jan 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
97 lines (72 loc) · 2.4 KB
/
ssh.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
93
94
95
96
97
// Provides functions to handle ssh tunneling
package main
import (
"io"
"log"
"net"
"os"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
//SSHAgent leverages the local ssh-agent process to authenticate
func SSHAgent() ssh.AuthMethod {
if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
}
return nil
}
//Opens local port on host machine and listens for connections
func establishLocalListener(port string) net.Listener {
listener, err := net.Listen("tcp", "localhost:"+port)
if err != nil {
log.Print(err)
listener.Close()
}
return listener
}
//Establish ssh connection to remote machine
func connectToRemote(username string, hostname string) *ssh.Client {
sshConfig := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{SSHAgent()},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
remoteConnection, err := ssh.Dial("tcp", hostname+":"+"22", sshConfig)
if err != nil {
log.Fatalf("remote connectioned failed: please check valid ssh key is loaded and access to the server is permitted\n%v", err)
}
return remoteConnection
}
//Establish socket connection on remote machine
func connectToSocket(remoteConnection *ssh.Client, socketPath string) *net.Conn {
socketConnection, err := remoteConnection.Dial("unix", socketPath)
if err != nil {
log.Fatal("Cannot connect to socket successfully. Check permissions and make sure process is running.")
}
return &socketConnection
}
//Allow two way communication between two connections
func copyConnectionData(writer, reader net.Conn) {
_, err := io.Copy(writer, reader)
if err != nil {
log.Print(err)
}
}
//Establish local, remote, socket connections and then allow communication
func establishTunnel(username string, hostname string, localPort string, done chan bool) {
listener := establishLocalListener(localPort)
remoteConnection := connectToRemote(username, hostname)
socketConnection := connectToSocket(remoteConnection, "/var/run/docker.sock")
done <- true
defer remoteConnection.Close()
defer listener.Close()
for {
localConnection, err := listener.Accept()
if err != nil {
log.Println(err)
}
go copyConnectionData(localConnection, *socketConnection)
go copyConnectionData(*socketConnection, localConnection)
socketConnection = connectToSocket(remoteConnection, "/var/run/docker.sock")
}
}