-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect-local-to-remote-port.go
115 lines (101 loc) · 2.59 KB
/
connect-local-to-remote-port.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
package sshtunnel
import (
"context"
"fmt"
"strconv"
"aduu.dev/utils/dash"
"aduu.dev/utils/runmanager"
"k8s.io/klog/v2"
)
type localConnection struct {
LocalPort string
RemoteAdress string
RemotePort string
SSHName string
}
func newLocalConnection(
localPort string,
remoteAdress string,
remotePort string,
sshName string,
) (con *localConnection, err error) {
if _, err = strconv.ParseInt(localPort, 10, 64); err != nil {
return nil, fmt.Errorf("local port is invalid: %w", err)
}
if _, err = strconv.ParseInt(remotePort, 10, 64); err != nil {
return nil, fmt.Errorf("remote port is invalid: %w", err)
}
return &localConnection{LocalPort: localPort,
RemoteAdress: remoteAdress,
RemotePort: remotePort,
SSHName: sshName,
}, nil
}
func connectLocalPortToRemotePort(
ctx context.Context,
d dash.Runner,
con *localConnection) (err error,
) {
return d.RunE(ctx,
dash.SplitTemplateExpand(`ssh -L 127.0.0.1:{{.LocalPort}}:{{.RemoteAdress}}:{{.RemotePort}} {{.SSHName}} -N`, con))
}
func connectRemotePortToLocalPort(
ctx context.Context,
d dash.Runner,
con *localConnection) (err error,
) {
return d.RunE(ctx,
dash.SplitTemplateExpand(`ssh -R 127.0.0.1:{{.RemotePort}}:{{.RemoteAdress}}:{{.LocalPort}} {{.SSHName}} -N`, con))
}
// ConnectLocalPortToRemotePort starts a service which connects
// the local host with the remote host on the given ports.
func ConnectLocalPortToRemotePort(
localPort string,
remoteAdress string,
remotePort string,
sshName string,
) runmanager.Service {
return func(m *runmanager.RunManager) {
con, err := newLocalConnection(localPort,
remoteAdress, remotePort,
sshName)
if err != nil {
m.ErrChan <- err
}
m.Run(&runmanager.Runner{
Run: func() error {
return connectLocalPortToRemotePort(m.Context, dash.NewRunner(), con)
},
Shutdown: func() error {
klog.InfoS("Shutting down local-to-remote-port connection")
return nil
},
})
}
}
// ConnectLocalPortToRemotePort starts a service which connects
// the remote host with the local host on the given ports.
func ConnectRemotePortToLocalPort(
remoteAdress string,
remotePort string,
localPort string,
sshName string,
) runmanager.Service {
return func(m *runmanager.RunManager) {
con, err := newLocalConnection(localPort,
remoteAdress, remotePort,
sshName)
if err != nil {
m.ErrChan <- err
}
m.Run(&runmanager.Runner{
Run: func() error {
return connectRemotePortToLocalPort(m.Context, dash.NewRunner(), con)
},
Shutdown: func() error {
klog.InfoS("Shutting down local-to-remote-port connection")
return nil
},
})
}
}