/
tunnel.go
499 lines (405 loc) · 10.6 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
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"
RandomPortAddress = "127.0.0.1:0"
NoRemoteGiven = "cannot create a tunnel without at least one remote 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)
}
type SSHChannel struct {
Local string
Remote string
listener net.Listener
conn net.Conn
}
func (ch *SSHChannel) Close() {
if ch.listener != nil {
ch.listener.Close()
}
}
func (ch SSHChannel) String() string {
return fmt.Sprintf("[local=%s, remote=%s]", ch.Local, ch.Remote)
}
// Tunnel represents the ssh tunnel and the channels connecting local and
// remote endpoints.
type Tunnel struct {
// Ready tells when the Tunnel is ready to accept connections
Ready chan bool
// KeepAliveInterval is the time period used to send keep alive packets to
// the remote ssh server
KeepAliveInterval time.Duration
server *Server
channels []*SSHChannel
done chan error
client *ssh.Client
}
// New creates a new instance of Tunnel.
func New(server *Server, channels []*SSHChannel) (*Tunnel, error) {
for _, channel := range channels {
if channel.Local == "" || channel.Remote == "" {
return nil, fmt.Errorf("invalid ssh channel: local=%s, remote=%s", channel.Local, channel.Remote)
}
}
return &Tunnel{
Ready: make(chan bool, 1),
channels: channels,
server: server,
done: make(chan error),
}, nil
}
// Start creates the ssh tunnel and initialized all channels allowing data
// exchange between local and remote enpoints.
func (t *Tunnel) Start() error {
err := t.Listen()
if err != nil {
return err
}
defer func() {
for _, ch := range t.channels {
ch.Close()
}
}()
log.Debugf("tunnel: %s", t)
//connecting to ssh server
err = t.dial()
if err != nil {
return err
}
wg := &sync.WaitGroup{}
wg.Add(len(t.channels))
// wait for all ssh channels to be ready to accept connections then sends a
// single message signalling all tunnel are ready
go func(tunnel *Tunnel, waitgroup *sync.WaitGroup) {
waitgroup.Wait()
tunnel.Ready <- true
}(t, wg)
for _, ch := range t.channels {
go func(channel *SSHChannel, waitgroup *sync.WaitGroup) {
var err error
var once sync.Once
for {
once.Do(func() {
log.WithFields(log.Fields{
"local": channel.Local,
"remote": channel.Remote,
}).Info("tunnel is ready")
waitgroup.Done()
})
channel.conn, err = channel.listener.Accept()
if err != nil {
t.done <- fmt.Errorf("error while establishing new connection: %v", err)
return
}
log.WithFields(log.Fields{
"address": channel.conn.RemoteAddr(),
}).Debug("new connection")
err = t.startChannel(channel)
if err != nil {
t.done <- err
return
}
}
}(ch, wg)
}
select {
case err = <-t.done:
if t.client != nil {
t.client.Conn.Close()
}
return err
}
}
// Listen creates tcp listeners for each channel defined.
func (t *Tunnel) Listen() error {
for _, ch := range t.channels {
if ch.listener == nil {
l, err := net.Listen("tcp", ch.Local)
if err != nil {
return err
}
ch.listener = l
ch.Local = l.Addr().String()
}
}
return nil
}
func (t *Tunnel) startChannel(channel *SSHChannel) error {
if t.client == nil {
return fmt.Errorf("new channel can't be established: missing connection to the ssh server")
}
remoteConn, err := t.client.Dial("tcp", channel.Remote)
if err != nil {
return fmt.Errorf("remote dial error: %s", err)
}
go copyConn(channel.conn, remoteConn)
go copyConn(remoteConn, channel.conn)
log.WithFields(log.Fields{
"remote": channel.Remote,
"server": t.server,
}).Debug("new connection established to remote")
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("[channels:%s, server:%s]", t.channels, t.server.Address)
}
func (t *Tunnel) dial() error {
if t.client != nil {
return nil
}
c, err := sshClientConfig(*t.server)
if err != nil {
return fmt.Errorf("error generating ssh client config: %s", err)
}
t.client, err = ssh.Dial("tcp", t.server.Address, c)
if err != nil {
return fmt.Errorf("server dial error: %s", err)
}
go t.keepAlive()
log.WithFields(log.Fields{
"server": t.server,
}).Debug("new connection established to server")
return nil
}
func (t *Tunnel) keepAlive() {
ticker := time.NewTicker(t.KeepAliveInterval)
for {
select {
case <-ticker.C:
_, _, err := t.client.SendRequest("keepalive@mole", true, nil)
if err != nil {
log.Warnf("error sending keep-alive request to ssh server: %v", err)
}
}
}
}
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 expandAddress(address string) string {
if strings.HasPrefix(address, ":") {
return fmt.Sprintf("127.0.0.1%s", address)
}
return address
}
// BuildSSHChannels normalizes the given set of local and remote addresses,
// combining them to build a set of ssh channel objects.
func BuildSSHChannels(serverName string, local, remote []string) ([]*SSHChannel, error) {
// if not local and remote were given, try to find the addresses from the SSH
// configuration file.
if len(local) == 0 && len(remote) == 0 {
lf, err := getLocalForward(serverName)
if err != nil {
return nil, err
}
local = []string{lf.Local}
remote = []string{lf.Remote}
} else {
lSize := len(local)
rSize := len(remote)
if lSize > rSize {
// if there are more local than remote addresses given, the additional
// addresses must be removed.
if rSize == 0 {
return nil, fmt.Errorf(NoRemoteGiven)
}
local = local[0:rSize]
} else if lSize < rSize {
// if there are more remote than local addresses given, the missing local
// addresses should be configured as localhost with random ports.
nl := make([]string, rSize)
for i, _ := range remote {
if i < lSize {
if local[i] != "" {
nl[i] = local[i]
} else {
nl[i] = RandomPortAddress
}
} else {
nl[i] = RandomPortAddress
}
}
local = nl
}
}
for i, addr := range local {
local[i] = expandAddress(addr)
}
for i, addr := range remote {
remote[i] = expandAddress(addr)
}
channels := make([]*SSHChannel, len(remote))
for i, r := range remote {
channels[i] = &SSHChannel{Local: local[i], Remote: r}
}
return channels, nil
}
func getLocalForward(serverName string) (*LocalForward, error) {
cfg, err := NewSSHConfigFile()
if err != nil {
return nil, fmt.Errorf("error reading ssh configuration file: %v", err)
}
sh := cfg.Get(serverName)
if sh.LocalForward == nil {
return nil, fmt.Errorf("LocalForward could not be found or has invalid syntax for host %s", serverName)
}
return sh.LocalForward, nil
}