-
Notifications
You must be signed in to change notification settings - Fork 0
/
autotunnel.go
919 lines (804 loc) · 20.4 KB
/
autotunnel.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
package autotunnel
import (
"context"
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"os/user"
"sync"
"sync/atomic"
"syscall"
"time"
autotunnelconfig "github.com/kamaln7/autotunnel/internal/config"
"github.com/kevinburke/ssh_config"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
type AutoTunnel struct {
ctx context.Context
cancel context.CancelFunc
config *autotunnelconfig.Config
ll *logrus.Logger
monitor *NetworkInterfaceMonitor
connections map[string]*connection
tunnels []*tunnel
wg sync.WaitGroup
}
type connection struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
host *host
proxy Proxy
ll *logrus.Entry
dialTimeout time.Duration
inactiveTimeout time.Duration
paused bool
pausedLock sync.Mutex
cl *ssh.Client
clMutex sync.RWMutex
active bool
lastError string
currentConnections uint32
inactiveTimer *time.Timer
connectionsMutex sync.RWMutex
isProxy bool
}
type tunnel struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
name string
connection *connection
localPort string
remotePort string
ll *logrus.Entry
dialTimeout time.Duration
listener net.Listener
lastError string
currentConnections uint32
}
type commandProxy struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
ll *logrus.Entry
command []string
inactiveTimeout time.Duration
active bool
process *os.Process
conn net.Conn
mtx sync.RWMutex
inactiveTimer *time.Timer
currentConnections uint32
}
type Proxy interface {
Dial(ctx context.Context, ll *logrus.Entry, n, addr string) (net.Conn, error)
Active() bool
TrackConnection()
TrackConnectionClosed()
}
func New(ctx context.Context, c *autotunnelconfig.Config, ll *logrus.Logger) (*AutoTunnel, error) {
at := &AutoTunnel{
config: c,
ll: ll,
connections: make(map[string]*connection),
}
at.ctx, at.cancel = context.WithCancel(ctx)
hosts := make(map[string]*host)
for i, t := range at.config.Tunnels {
ll := ll.WithField("tunnel_index", i)
if t.Name == "" {
m := "tunnel missing name"
ll.Error(m)
return nil, fmt.Errorf("parsing tunnel [%d]: %s", i, m)
} else {
ll = ll.WithField("tunnel_name", t.Name)
}
if t.Host == "" {
m := "tunnel missing host"
ll.Error(m)
return nil, fmt.Errorf("parsing tunnel %s: %s", t.Name, m)
}
if t.LocalPort == "" && t.RemotePort == "" {
m := "tunnel requires at least one of remote_port or local_port"
ll.Error(m)
return nil, fmt.Errorf("parsing tunnel %s: %s", t.Name, m)
}
if t.LocalPort == "" && t.RemotePort != "" {
t.LocalPort = t.RemotePort
} else if t.RemotePort == "" && t.LocalPort != "" {
t.RemotePort = t.LocalPort
}
hosts[t.Host] = &host{}
if t.JumpHost != "" {
hosts[t.JumpHost] = &host{}
}
}
for alias, host := range hosts {
host.alias = alias
hostname := ssh_config.Get(alias, "Hostname")
if hostname == "" {
hostname = alias
}
port := ssh_config.Get(alias, "Port")
if port == "" {
port = "22"
}
username := ssh_config.Get(alias, "User")
if username == "" {
u, err := user.Current()
if err != nil {
return nil, err
}
username = u.Username
}
host.hostport = fmt.Sprintf("%s:%s", hostname, port)
host.username = username
conn := &connection{
host: host,
ll: at.ll.WithFields(logrus.Fields{
"host": alias,
"dial_timeout": at.config.DialTimeout,
}),
wg: at.wg,
dialTimeout: at.config.DialTimeout,
inactiveTimeout: at.config.InactiveTimeout,
}
conn.ctx, conn.cancel = context.WithCancel(at.ctx)
at.connections[alias] = conn
}
for _, t := range at.config.Tunnels {
if at.connections[t.Host].proxy != nil {
continue
}
var proxy Proxy
if t.JumpHost != "" {
p := at.connections[t.JumpHost]
if !p.isProxy {
p.isProxy = true
p.ll = p.ll.WithField("proxy_type", "jump_host")
}
proxy = p
} else if len(t.JumpCommand) > 0 {
ll.WithField("jump_command", t.JumpCommand).Trace("creating command proxy")
p := &commandProxy{
ll: at.ll.WithFields(logrus.Fields{
"jump_command": t.JumpCommand,
"proxy_type": "jump_command",
}),
command: t.JumpCommand,
inactiveTimeout: at.config.InactiveTimeout,
wg: at.wg,
}
p.ctx, p.cancel = context.WithCancel(at.ctx)
proxy = p
}
at.connections[t.Host].proxy = proxy
}
for _, t := range at.config.Tunnels {
tt := &tunnel{
name: t.Name,
localPort: t.LocalPort,
remotePort: t.RemotePort,
connection: at.connections[t.Host],
ll: at.ll.WithFields(logrus.Fields{
"tunnel": t.Name,
"local_port": t.LocalPort,
"remote_port": t.RemotePort,
"ssh_dial_timeout": at.config.DialTimeout,
}),
wg: at.wg,
dialTimeout: at.config.DialTimeout,
}
tt.ctx, tt.cancel = context.WithCancel(at.ctx)
at.tunnels = append(at.tunnels, tt)
}
if c.InterfaceName != "" {
at.monitor = NewNetworkInterfaceMonitor(at.ctx, ll, time.Minute, c.InterfaceName)
}
return at, nil
}
type host struct {
alias string
hostport string
username string
}
func (at *AutoTunnel) Start() error {
for _, c := range at.connections {
go c.init()
}
for _, t := range at.tunnels {
go t.start()
}
if at.monitor != nil {
ll := at.ll.WithField("network_interface", at.monitor.Name)
ll.Info("configuring interface monitor")
_, err := at.monitor.Start()
if err != nil {
ll.WithError(err).Error("interface monitor returned error")
}
upChan, downChan := at.monitor.Chans()
go func() {
for {
select {
case <-at.ctx.Done():
return
case <-upChan:
ll.Info("network interface reported up, unpausing connections")
for _, c := range at.connections {
go c.unpause()
}
case <-downChan:
ll.Info("network interface reported down, pausing connections")
for _, c := range at.connections {
go c.pause()
}
}
}
}()
}
<-at.ctx.Done()
for _, t := range at.tunnels {
t.stop()
}
for _, c := range at.connections {
c.shutdown()
}
err := at.ctx.Err()
at.ll.WithError(err).Info("shutting down")
at.wg.Wait()
return err
}
func (t *tunnel) start() {
t.wg.Add(1)
t.ll.Info("starting listener")
listener, err := net.Listen("tcp", "127.0.0.1:"+t.localPort)
if err != nil {
t.lastError = err.Error()
t.ll.WithError(err).Error("starting listener")
return
}
t.listener = listener
t.accept()
}
func (t *tunnel) stop() {
t.ll.Info("stopping listener")
t.cancel()
if t.listener != nil {
t.listener.Close()
t.listener = nil
t.wg.Done()
}
}
func (t *tunnel) accept() {
for t.ctx.Err() == nil {
conn, err := t.listener.Accept()
if err != nil {
if errors.Is(err, net.ErrClosed) {
return
}
t.lastError = err.Error()
t.ll.WithError(err).Error("accepting connection")
}
ll := t.ll.WithField("local_address", conn.RemoteAddr().String())
ll.Trace("accepted connection")
go t.forward(ll, conn)
}
}
func (t *tunnel) trackConnection() {
atomic.AddUint32(&t.currentConnections, 1)
t.connection.TrackConnection()
}
func (t *tunnel) trackConnectionClosed() {
defer atomic.AddUint32(&t.currentConnections, ^uint32(0))
defer t.connection.TrackConnectionClosed()
}
func (t *tunnel) forward(ll *logrus.Entry, conn net.Conn) {
var remoteConn net.Conn
defer func() {
go conn.Close()
if remoteConn != nil {
go remoteConn.Close()
}
}()
if t.connection.Paused() {
ll.Trace("rejecting connection because ssh connection is paused")
return
}
go t.trackConnection()
defer func() {
go t.trackConnectionClosed()
}()
ll.Trace("forwarding connection")
errChan := make(chan error)
go func() {
<-t.ctx.Done()
errChan <- t.ctx.Err()
}()
go func() {
ll.Trace("getting ssh client")
sshClCtx := t.ctx
if t.dialTimeout != 0 {
var sshClCancel context.CancelFunc
sshClCtx, sshClCancel = context.WithTimeout(sshClCtx, t.dialTimeout)
defer sshClCancel()
}
sshClChan := make(chan *ssh.Client)
go func() {
sshClChan <- t.connection.client()
}()
var (
cl *ssh.Client
err error
)
select {
case cl = <-sshClChan:
case <-sshClCtx.Done():
errChan <- fmt.Errorf("getting ssh client: %w", sshClCtx.Err())
return
}
if cl == nil {
errChan <- fmt.Errorf("failed to get ssh client")
return
}
ll.Trace("dialing remote connection")
remoteConn, err = cl.Dial("tcp", "0.0.0.0:"+t.remotePort)
if err != nil {
ll.WithError(err).Error("dialing remote connection")
errChan <- fmt.Errorf("dialing remote connection: %w", err)
return
}
ll.Trace("proxying connection")
go func() {
_, err = io.Copy(remoteConn, conn)
if err != nil {
err = fmt.Errorf("local->remote copy error: %w", err)
}
errChan <- err
}()
go func() {
_, err = io.Copy(conn, remoteConn)
if err != nil {
err = fmt.Errorf("remote->local copy error: %w", err)
}
errChan <- err
}()
}()
err := <-errChan
if err != nil {
ll.WithError(err).Error("connection error")
}
ll.Trace("connection closed")
}
func (c *connection) start() {
c.clMutex.Lock()
defer c.clMutex.Unlock()
c.wg.Add(1)
if c.cl != nil {
// another goroutine has already established a client
return
}
if err := c.ctx.Err(); err != nil {
c.ll.WithError(err).Error("connection is closed")
return
}
sshAgent, err := sshAgentAuthMethod()
if err != nil {
c.ll.WithError(err).Error("error getting ssh agent")
return
}
// var hostKeyCallback ssh.HostKeyCallback
// if khc, err := knownhosts.New(filepath.Join(os.Getenv("HOME"), ".ssh", "known_hosts")); err != nil {
// c.ll.WithError(err).Warn("error getting known_hosts verifier; disabling host key checks")
// hostKeyCallback = ssh.InsecureIgnoreHostKey()
// } else {
// // knownhosts's callback can't handle hostnames without ports, so we add it in ourselves if it's missing
// hostKeyCallback = func(hostname string, remote net.Addr, key ssh.PublicKey) error {
// if _, _, err := net.SplitHostPort(hostname); err != nil {
// hostname = c.host.hostport
// }
// c.ll.WithField("hostname", hostname).Debug("YEET")
// return khc(hostname, remote, key)
// }
// }
// I can't figure out how to fix this error & I don't have the time
// ssh: handshake failed: knownhosts: SplitHostPort(pipe): address pipe: missing port in address
sshConfig := &ssh.ClientConfig{
User: c.host.username,
Auth: []ssh.AuthMethod{sshAgent},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: c.dialTimeout,
}
var conn net.Conn
ctx := c.ctx
if c.dialTimeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(c.ctx, c.dialTimeout)
defer cancel()
}
ll := c.ll
if c.proxy != nil {
conn, err = c.proxy.Dial(ctx, ll, "tcp", c.host.hostport)
} else {
ll.Debug("dialing ssh remote")
d := net.Dialer{Timeout: c.dialTimeout}
conn, err = d.DialContext(ctx, "tcp", c.host.hostport)
}
if err != nil {
c.lastError = err.Error()
ll.WithError(err).Error("dialing ssh remote")
return
}
ncc, chans, reqs, err := ssh.NewClientConn(conn, c.host.hostport, sshConfig)
if err != nil {
c.lastError = err.Error()
ll.WithError(err).Error("creating ssh client")
return
}
c.cl = ssh.NewClient(ncc, chans, reqs)
c.active = true
go func() {
err := c.cl.Wait()
ll.WithError(err).Debug("ssh client closed")
c.wg.Done()
c.close()
}()
go func() {
t := time.NewTicker(time.Minute)
defer t.Stop()
for c.ctx.Err() == nil {
select {
case <-t.C:
c.clMutex.RLock()
if c.cl == nil {
c.ll.Debug("ssh client closed. stopping heartbeat loop")
c.clMutex.RUnlock()
return
}
deadline := time.Now().Add(time.Minute).Add(5 * time.Second)
err := conn.SetDeadline(deadline)
if err != nil {
c.ll.WithError(err).Debug("setting ssh heartbeat deadline")
c.clMutex.RUnlock()
return
}
_, _, err = c.cl.SendRequest("keepalive@openssh.com", true, nil)
if err != nil {
c.ll.WithError(err).Info("ssh heartbeat failed. closing connection")
go c.close()
}
_ = conn.SetDeadline(time.Time{})
c.clMutex.RUnlock()
case <-c.ctx.Done():
}
}
c.ll.WithError(c.ctx.Err()).Debug("stopping ssh heartbeat due to error")
}()
}
func (c *connection) init() {
go func() {
<-c.ctx.Done()
err := c.ctx.Err()
ll := c.ll
if err != nil {
ll = ll.WithError(err)
}
ll.Info("closing connection")
c.close()
}()
}
func (c *connection) client() *ssh.Client {
c.pausedLock.Lock()
defer c.pausedLock.Unlock()
if c.paused {
return nil
}
c.clMutex.RLock()
if c.cl != nil {
c.clMutex.RUnlock()
if c.proxy != nil && !c.proxy.Active() {
c.ll.Debug("tried to get client from an active connection with an inactive proxy. closing connection & starting a new one")
c.close()
} else {
return c.cl
}
} else {
c.clMutex.RUnlock()
}
c.start()
c.clMutex.RLock()
defer c.clMutex.RUnlock()
return c.cl
}
func (c *connection) shutdown() {
c.cancel()
}
func (c *connection) close() {
c.clMutex.Lock()
defer c.clMutex.Unlock()
if c.cl == nil {
c.ll.Trace("already closed")
return
}
c.active = false
c.ll.Debug("closing ssh client")
c.cl.Close()
c.cl = nil
}
func (c *connection) pause() {
c.pausedLock.Lock()
defer c.pausedLock.Unlock()
if c.paused {
return
}
c.paused = true
c.close()
}
func (c *connection) unpause() {
c.pausedLock.Lock()
defer c.pausedLock.Unlock()
c.paused = false
}
func (c *connection) TrackConnection() {
if c.proxy != nil {
go c.proxy.TrackConnection()
}
c.connectionsMutex.Lock()
defer c.connectionsMutex.Unlock()
c.currentConnections++
if c.inactiveTimer != nil {
c.ll.WithField("inactive_timeout", c.inactiveTimeout).Debug("got connection, interrupting inactive timer")
c.inactiveTimer.Stop()
c.inactiveTimer = nil
}
}
func (c *connection) TrackConnectionClosed() {
if c.proxy != nil {
go c.proxy.TrackConnectionClosed()
}
c.connectionsMutex.Lock()
defer c.connectionsMutex.Unlock()
if c.currentConnections > 0 {
c.currentConnections--
}
if c.currentConnections == 0 && c.inactiveTimeout != 0 {
c.ll.WithField("inactive_timeout", c.inactiveTimeout).Debug("all active connections were closed, ssh connection will be closed if it remains inactive")
c.inactiveTimer = time.AfterFunc(c.inactiveTimeout, func() {
c.connectionsMutex.RLock()
defer c.connectionsMutex.RUnlock()
if c.currentConnections == 0 {
if c.isProxy {
// give any child connections a little time to shut down cleanly on the other side
time.Sleep(2 * time.Second)
}
c.ll.WithField("inactive_timeout", c.inactiveTimeout).Debug("ssh connection remained inactive, closing")
go c.close()
}
c.inactiveTimer = nil
})
}
}
func (c *connection) Paused() bool {
c.pausedLock.Lock()
defer c.pausedLock.Unlock()
return c.paused
}
func sshAgentAuthMethod() (ssh.AuthMethod, error) {
sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
return nil, err
}
return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers), nil
}
type TunnelStatus struct {
Name string
LocalPort string
Connected bool
ActiveConnections uint32
LastError string
Paused bool
}
func (at *AutoTunnel) Status() (status []TunnelStatus) {
for _, t := range at.tunnels {
var lastErr string
if t.lastError != "" {
lastErr = t.lastError
} else if t.connection.lastError != "" {
lastErr = t.connection.lastError
}
status = append(status, TunnelStatus{
Name: t.name,
LocalPort: t.localPort,
LastError: lastErr,
Connected: t.connection.active,
ActiveConnections: t.currentConnections,
Paused: t.connection.paused,
})
}
return
}
type NetworkInterfaceMonitor struct {
ctx context.Context
Name string
interval time.Duration
ll *logrus.Logger
lastState *bool
upChan, downChan chan struct{}
}
func NewNetworkInterfaceMonitor(ctx context.Context, ll *logrus.Logger, interval time.Duration, name string) *NetworkInterfaceMonitor {
return &NetworkInterfaceMonitor{
ctx: ctx,
ll: ll,
Name: name,
interval: interval,
upChan: make(chan struct{}, 1),
downChan: make(chan struct{}, 1),
}
}
func (c *NetworkInterfaceMonitor) Chans() (chan struct{}, chan struct{}) {
return c.upChan, c.downChan
}
func (c *NetworkInterfaceMonitor) Start() (bool, error) {
go func() {
t := time.NewTicker(c.interval)
for c.ctx.Err() == nil {
select {
case <-t.C:
_, _ = c.check()
case <-c.ctx.Done():
return
}
}
}()
return c.check()
}
func (c *NetworkInterfaceMonitor) check() (bool, error) {
i, err := net.InterfaceByName(c.Name)
if err != nil {
go c.sendState(false)
return false, err
}
up := i.Flags&net.FlagUp != 0
if c.lastState == nil {
c.sendState(up)
c.lastState = &up
} else if *c.lastState != up {
c.sendState(up)
*c.lastState = up
}
return up, nil
}
func (c *NetworkInterfaceMonitor) sendState(up bool) {
if up {
c.upChan <- struct{}{}
} else {
c.downChan <- struct{}{}
}
}
func (c *connection) Active() bool {
return c.active
}
func (c *connection) Dial(ctx context.Context, ll *logrus.Entry, n, addr string) (net.Conn, error) {
ll = ll.WithField("proxy", c.host.alias)
ll.Debug("getting proxy client")
cl := c.client()
if cl == nil {
c.lastError = "failed to get proxy ssh client"
ll.Error("failed to get proxy ssh client")
return nil, errors.New("failed to get proxy ssh client")
}
ll.Debug("dialing ssh remote through proxy")
return cl.Dial(n, addr)
}
func (c *commandProxy) Active() bool {
return c.active
}
func (c *commandProxy) Dial(ctx context.Context, ll *logrus.Entry, n, addr string) (net.Conn, error) {
c.mtx.RLock()
if c.conn != nil {
c.ll.Trace("Dial(): not starting new proxy command; using existing conn")
c.mtx.RUnlock()
return c.conn, nil
}
c.mtx.RUnlock()
c.ll.Trace("Dial(): starting new proxy command")
c.start()
c.mtx.RLock()
defer c.mtx.RUnlock()
return c.conn, nil
}
func (c *commandProxy) TrackConnection() {
c.mtx.Lock()
defer c.mtx.Unlock()
c.currentConnections++
if c.inactiveTimer != nil {
c.ll.WithField("inactive_timeout", c.inactiveTimeout).Debug("got connection, interrupting inactive timer")
c.inactiveTimer.Stop()
c.inactiveTimer = nil
}
}
func (c *commandProxy) TrackConnectionClosed() {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.currentConnections > 0 {
c.currentConnections--
}
if c.currentConnections == 0 && c.inactiveTimeout != 0 {
c.ll.WithField("inactive_timeout", c.inactiveTimeout).Debug("all active connections were closed, proxy command will be closed if it remains inactive")
c.inactiveTimer = time.AfterFunc(c.inactiveTimeout, func() {
c.mtx.RLock()
defer c.mtx.RUnlock()
if c.currentConnections == 0 {
c.ll.WithField("inactive_timeout", c.inactiveTimeout).Debug("proxy command remained inactive, closing")
go c.close()
}
c.inactiveTimer = nil
})
}
}
func (c *commandProxy) start() {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.process != nil && c.process.Pid > 0 {
// another goroutine has already established a client
c.ll.WithField("pid", c.process.Pid).Debug("not starting new command proxy process; using existing one instead")
return
}
if err := c.ctx.Err(); err != nil {
c.ll.WithError(c.ctx.Err()).Error("not starting new command proxy process; context is closed")
return
}
p1, p2 := net.Pipe()
var (
name string
args []string
)
if len(c.command) > 0 {
name = c.command[0]
}
c.ll.WithField("command", c.command).Debug("starting new commandProxy process")
if len(c.command) > 1 {
args = c.command[1:]
}
cmd := exec.CommandContext(c.ctx, name, args...)
cmd.Stdin = p1
cmd.Stdout = p1
cmd.Stderr = c.ll.Writer()
err := cmd.Start()
if err != nil {
c.ll.WithError(err).Error("can't start proxy command")
return
}
c.active = true
ll := c.ll.WithField("pid", cmd.Process.Pid)
c.process = cmd.Process
c.wg.Add(1)
go func() {
err := cmd.Wait()
ll.WithError(err).Debug("proxy command exited")
c.close()
c.wg.Done()
}()
c.conn = p2
}
func (c *commandProxy) close() {
c.mtx.Lock()
defer c.mtx.Unlock()
c.active = false
if c.process == nil {
c.ll.Trace("no running proxy process")
c.conn = nil
return
}
c.ll.Debug("terminating proxy process")
_ = c.process.Signal(syscall.SIGINT)
time.Sleep(250 * time.Millisecond)
_ = c.process.Kill()
c.conn = nil
c.process = nil
}