-
Notifications
You must be signed in to change notification settings - Fork 97
/
tunnel.go
383 lines (308 loc) · 7.87 KB
/
tunnel.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package tunnel
import (
"fmt"
"io"
"net"
"os"
"path/filepath"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
const (
HostMissing = "server host has to be provided as part of the server address"
)
// Server holds the SSH Server attributes used for the client to connect to it.
type Server struct {
Name string
Address string
User string
Key *PemKey
// Insecure is a flag to indicate if the host keys should be validated.
Insecure bool
}
// NewServer creates a new instance of Server using $HOME/.ssh/config to
// resolve the missing connection attributes (e.g. user, hostname, port and
// key) required to connect to the remote server, if any.
func NewServer(user, address, key string) (*Server, error) {
var host string
var hostname string
var port string
host = address
if strings.Contains(host, ":") {
args := strings.Split(host, ":")
host = args[0]
port = args[1]
}
c, err := NewSSHConfigFile()
if err != nil {
return nil, fmt.Errorf("error accessing %s: %v", host, err)
}
h := c.Get(host)
hostname = reconcileHostname(host, h.Hostname)
port = reconcilePort(port, h.Port)
user = reconcileUser(user, h.User)
key = reconcileKey(key, h.Key)
if host == "" {
return nil, fmt.Errorf(HostMissing)
}
if hostname == "" {
return nil, fmt.Errorf("no server hostname (ip) could be found for server %s", host)
}
if port == "" {
port = "22"
}
if user == "" {
return nil, fmt.Errorf("no user could be found for server %s", host)
}
if key == "" {
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("could not obtain user home directory: %v", err)
}
key = filepath.Join(home, ".ssh", "id_rsa")
}
pk, err := NewPemKey(key, "")
if err != nil {
return nil, fmt.Errorf("error while reading key %s: %v", key, err)
}
return &Server{
Name: host,
Address: fmt.Sprintf("%s:%s", hostname, port),
User: user,
Key: pk,
}, nil
}
// String provided a string representation of a Server.
func (s Server) String() string {
return fmt.Sprintf("[name=%s, address=%s, user=%s]", s.Name, s.Address, s.User)
}
// Tunnel represents the ssh tunnel used to forward a local connection to a
// a remote endpoint through a ssh server.
type Tunnel struct {
// Ready tells when the Tunnel is ready to accept connections
Ready chan bool
local string
server *Server
remote string
done chan error
client *ssh.Client
listener net.Listener
}
// New creates a new instance of Tunnel.
func New(localAddress string, server *Server, remoteAddress string) *Tunnel {
cfg, err := NewSSHConfigFile()
if err != nil {
log.Warningf("error to read ssh config: %v", err)
}
sh := cfg.Get(server.Name)
localAddress = reconcileLocal(localAddress, sh.LocalForward.Local)
remoteAddress = reconcileRemote(remoteAddress, sh.LocalForward.Remote)
return &Tunnel{
Ready: make(chan bool, 1),
local: localAddress,
server: server,
remote: remoteAddress,
done: make(chan error),
}
}
// Start creates a new ssh tunnel, allowing data exchange between the local and
// remote endpoints.
func (t *Tunnel) Start() error {
var once sync.Once
_, err := t.Listen()
if err != nil {
return err
}
defer t.listener.Close()
log.Debugf("tunnel: %s", t)
log.WithFields(log.Fields{
"local_address": t.local,
}).Info("listening on local address")
go func(t *Tunnel) {
for {
once.Do(func() {
t.Ready <- true
})
conn, err := t.listener.Accept()
if err != nil {
t.done <- fmt.Errorf("error while establishing new connection: %v", err)
return
}
log.WithFields(log.Fields{
"address": conn.RemoteAddr(),
}).Debug("new connection")
err = t.forward(conn)
if err != nil {
t.done <- err
return
}
}
}(t)
select {
case err = <-t.done:
if t.client != nil {
t.client.Conn.Close()
}
return err
}
}
// Listen binds the local address configured on Tunnel.
func (t *Tunnel) Listen() (net.Listener, error) {
if t.listener != nil {
return t.listener, nil
}
local, err := net.Listen("tcp", t.local)
if err != nil {
return nil, err
}
t.listener = local
t.local = local.Addr().String()
return t.listener, nil
}
func (t *Tunnel) forward(localConn net.Conn) error {
remoteConn, err := t.proxy()
if err != nil {
return err
}
go copyConn(localConn, remoteConn)
go copyConn(remoteConn, localConn)
return nil
}
// Stop cancels the tunnel, closing all connections.
func (t Tunnel) Stop() {
t.done <- nil
}
// String returns a string representation of a Tunnel.
func (t Tunnel) String() string {
return fmt.Sprintf("[local:%s, server:%s, remote:%s]", t.local, t.server.Address, t.remote)
}
func (t *Tunnel) proxy() (net.Conn, error) {
c, err := sshClientConfig(*t.server)
if err != nil {
return nil, fmt.Errorf("error generating ssh client config: %s", err)
}
if t.client == nil {
t.client, err = ssh.Dial("tcp", t.server.Address, c)
if err != nil {
return nil, fmt.Errorf("server dial error: %s", err)
}
log.WithFields(log.Fields{
"server": t.server,
}).Debug("new connection established to server")
}
remoteConn, err := t.client.Dial("tcp", t.remote)
if err != nil {
return nil, fmt.Errorf("remote dial error: %s", err)
}
log.WithFields(log.Fields{
"remote": t.remote,
"server": t.server,
}).Debug("new connection established to remote")
return remoteConn, nil
}
func sshClientConfig(server Server) (*ssh.ClientConfig, error) {
signer, err := server.Key.Parse()
if err != nil {
return nil, err
}
clb, err := knownHostsCallback(server.Insecure)
if err != nil {
return nil, err
}
return &ssh.ClientConfig{
User: server.User,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: clb,
Timeout: 3 * time.Second,
}, nil
}
func copyConn(writer, reader net.Conn) {
_, err := io.Copy(writer, reader)
if err != nil {
log.Errorf("%v", err)
}
}
func knownHostsCallback(insecure bool) (ssh.HostKeyCallback, error) {
var clb func(hostname string, remote net.Addr, key ssh.PublicKey) error
if insecure {
clb = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
}
} else {
var err error
home, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("could not obtain user home directory :%v", err)
}
knownHostFile := filepath.Join(home, ".ssh", "known_hosts")
log.Debugf("known_hosts file used: %s", knownHostFile)
clb, err = knownhosts.New(knownHostFile)
if err != nil {
return nil, fmt.Errorf("error while parsing 'known_hosts' file: %s: %v", knownHostFile, err)
}
}
return clb, nil
}
func reconcileHostname(givenHostname, resolvedHostname string) string {
if resolvedHostname != "" {
return resolvedHostname
}
if resolvedHostname == "" && givenHostname != "" {
return givenHostname
}
return ""
}
func reconcilePort(givenPort, resolvedPort string) string {
if givenPort != "" {
return givenPort
}
if givenPort == "" && resolvedPort != "" {
return resolvedPort
}
return ""
}
func reconcileUser(givenUser, resolvedUser string) string {
if givenUser != "" {
return givenUser
}
if givenUser == "" && resolvedUser != "" {
return resolvedUser
}
return ""
}
func reconcileKey(givenKey, resolvedKey string) string {
if givenKey != "" {
return givenKey
}
if givenKey == "" && resolvedKey != "" {
return resolvedKey
}
return ""
}
func reconcileLocal(givenLocal, resolvedLocal string) string {
if givenLocal == "" && resolvedLocal != "" {
return resolvedLocal
}
if givenLocal == "" {
return "127.0.0.1:0"
}
if strings.HasPrefix(givenLocal, ":") {
return fmt.Sprintf("127.0.0.1%s", givenLocal)
}
return givenLocal
}
func reconcileRemote(givenRemote, resolvedRemote string) string {
if givenRemote == "" && resolvedRemote != "" {
return resolvedRemote
}
if strings.HasPrefix(givenRemote, ":") {
return fmt.Sprintf("127.0.0.1%s", givenRemote)
}
return givenRemote
}