-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
287 lines (243 loc) · 6.41 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"net"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/aduermael/crypto/ssh"
"github.com/spf13/cobra"
)
var (
// path to private key or empty to use default
sshIdentityFile = ""
// open a bash session by default, but different option can be used
shell = "bash"
// proxy mode (don't start shell session)
proxyMode = false
// verbose mode (debug logs)
verbose = false
)
func main() {
rootCmd := &cobra.Command{
Use: "docker-tunnel [user@]host",
Short: "Docker-tunnel connects you to a remote Docker host through an SSH tunnel",
Run: func(cmd *cobra.Command, args []string) {
if verbose {
logLevel = logLevelDebug
}
if len(args) != 1 {
cmd.Usage()
return
}
sshClient, err := sshConnect(args[0], sshIdentityFile)
if err != nil {
printFatal(err)
}
defer sshClient.Close()
if proxyMode {
printDebug("proxy mode")
ln, err := net.Listen("tcp", ":2375")
if err != nil {
printFatal(err)
}
print("listening on port 2375...")
for {
conn, err := ln.Accept()
if err != nil {
printFatal(err)
}
go handleProxyConnection(conn, sshClient)
}
}
// proxyMode == false
// open shell session, connected to remote Docker host
printDebug("shell mode")
socketPath := tmpSocketPath()
printDebug("socket path:", socketPath)
ln, err := net.Listen("unix", socketPath)
if err != nil {
printFatal(err)
}
defer os.RemoveAll(socketPath)
// listen in background
go func() {
for {
conn, err := ln.Accept()
if err != nil {
printFatal(err)
}
printDebug("handle socket connection")
go handleProxyConnection(conn, sshClient)
}
}()
os.Setenv("PS1", "🐳 $ ")
os.Setenv("DOCKER_HOST", "unix://"+socketPath)
sh := exec.Command(shell)
sh.Stdout = os.Stdout
sh.Stderr = os.Stderr
sh.Stdin = os.Stdin
_ = sh.Run()
},
}
rootCmd.Flags().StringVarP(&sshIdentityFile, "sshid", "i", "", "path to private key")
rootCmd.Flags().StringVarP(&shell, "shell", "s", "bash", "shell to open session")
rootCmd.Flags().BoolVarP(&proxyMode, "proxy", "p", false, "proxy mode (don't start shell session)")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode (debug logs)")
if err := rootCmd.Execute(); err != nil {
printFatal(err.Error())
}
}
func tmpSocketPath() string {
randBytes := make([]byte, 16)
rand.Read(randBytes)
return filepath.Join(os.TempDir(), "docker-"+hex.EncodeToString(randBytes)+".sock")
}
func sshConnect(userAtHost, privateKeyPath string) (*ssh.Client, error) {
// root user by default
user := "root"
host := ""
userAndHost := strings.SplitN(userAtHost, "@", 2)
if len(userAndHost) == 1 {
host = userAndHost[0]
} else {
user = userAndHost[0]
host = userAndHost[1]
}
printDebug("user:", user)
u, err := url.Parse(host)
if err != nil {
return nil, fmt.Errorf("ssh connection can't be established: %s", err)
}
if u.Scheme == "" {
u.Scheme = "tcp"
}
authMethod, err := authMethodPublicKeys(privateKeyPath)
if err != nil {
printFatal(err)
}
config := &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{authMethod},
}
addr := u.Host + u.Path
parts := strings.Split(addr, ":")
lastPart := parts[len(parts)-1]
_, err = strconv.Atoi(lastPart)
// port is required, used 22 by default
if err != nil {
addr += ":22"
}
printDebug("address:", u.Scheme+"://"+addr)
sshClientConn, err := ssh.Dial(u.Scheme, addr, config)
if err != nil {
return nil, fmt.Errorf("ssh connection can't be established: %s", err)
}
printDebug("ssh connection established")
return sshClientConn, nil
}
func handleProxyConnection(conn net.Conn, sshClient *ssh.Client) {
err := forward(conn, sshClient, "unix:///var/run/docker.sock")
if err != nil {
printError(os.Stderr, "can't forward connection:", err.Error())
}
}
func forward(conn net.Conn, sshClient *ssh.Client, remoteAddr string) error {
// parse OpenSSH version, 6.7 is the minimum required
reOpenSSH := regexp.MustCompile("OpenSSH_[.0-9]+")
reOpenSSHVersion := regexp.MustCompile("[.0-9]+")
match := reOpenSSH.Find(sshClient.ServerVersion())
openSSHVersionStr := string(reOpenSSHVersion.Find(match))
openSSHVersion, err := strconv.ParseFloat(openSSHVersionStr, 64)
if err != nil {
return errors.New("can't parse server OpenSSH version")
}
if openSSHVersion < 6.7 {
return errors.New("OpenSSH 6.7 minimum required on server side")
}
// remote addr
u, err := url.Parse(remoteAddr)
if err != nil {
return fmt.Errorf("can't parse remote address: %s", remoteAddr)
}
addr := filepath.Join(u.Host, u.Path)
sshConn, err := sshClient.Dial(u.Scheme, addr)
if err != nil {
return fmt.Errorf("can't connect to %s (from remote)", remoteAddr)
}
chan1 := make(chan struct{})
chan2 := make(chan struct{})
chan3 := make(chan struct{})
var o sync.Once
closeChan2 := func() {
close(chan2)
}
// Copy conn.Reader to sshConn.Writer
go func() {
printDebug("copy: read from client conn, write to server conn")
_, err := io.Copy(sshConn, conn)
if err != nil {
printFatal(err)
}
close(chan1)
if c, ok := sshConn.(ssh.Channel); ok {
if err := c.CloseWrite(); err != nil {
printError("can't close sshConn writer")
} else {
printDebug("closed sshConn writer")
}
}
for {
printDebug("can't read from client anymore, trying to write...")
_, err := conn.Write(make([]byte, 0))
if err != nil {
printDebug("can't write, closing both connections")
o.Do(closeChan2)
break
}
time.Sleep(500 * time.Millisecond)
}
}()
// Copy sshConn.Reader to localConn.Writer
go func() {
printDebug("copy: read from server conn, write to client conn")
_, err := io.Copy(conn, sshConn)
if err != nil {
printFatal(err)
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
if err := tcpConn.CloseWrite(); err != nil {
printError("can't close TCPconn writer")
} else {
printDebug("closed TCPconn writer")
}
} else if unixConn, ok := conn.(*net.UnixConn); ok {
if err := unixConn.CloseWrite(); err != nil {
printError("can't close UnixConn writer")
} else {
printDebug("closed UnixConn writer")
}
} else {
printDebug("can't close conn writer")
}
o.Do(closeChan2)
close(chan3)
}()
<-chan1
<-chan2
conn.Close()
sshConn.Close()
<-chan3
printDebug("closed socket connection")
return nil
}