/
main.go
378 lines (334 loc) · 7.88 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
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
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"os"
"sync/atomic"
"time"
"github.com/9072997/fingerprintverifier"
"github.com/kardianos/service"
"golang.org/x/crypto/ssh"
)
type Connection struct {
Name string
Host string
Username string
KeyFile string
Key string
Password string
Fingerprint string
KeepAliveInterval Duration
MaxReconnectDelay Duration
Tunnels []*Tunnel
// internal fields
status atomic.Value
auth ssh.AuthMethod
conn *ssh.Client
listeners []net.Listener
reconnectDelay time.Duration
}
type Tunnel struct {
From Endpoint
To Endpoint
// internal fields
direction TunnelDirection
}
type Endpoint struct {
Side string
Address string
}
type TunnelDirection int
const (
UnspecifiedDirection TunnelDirection = iota
ExposedOnServer
ExposedLocally
)
type program struct {
Config []*Connection
}
func main() {
s, _ := service.New(
new(program),
&service.Config{
Name: "TunTun",
DisplayName: "TunTun",
Description: "Maintains persistent SSH tunnels.",
},
)
if len(os.Args) > 1 {
switch os.Args[1] {
case "install":
err := s.Install()
if err != nil {
panic(err)
}
return
case "uninstall":
err := s.Uninstall()
if err != nil {
panic(err)
}
return
}
}
err := s.Run()
if err != nil {
panic(err)
}
}
func (p *program) Start(s service.Service) error {
// read config file
path := GetConfigPath()
j, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read config file: %w", err)
}
err = json.Unmarshal(j, &p.Config)
if err != nil {
return fmt.Errorf("failed to parse config file: %w", err)
}
// validate config
if len(p.Config) == 0 {
return fmt.Errorf("no connections defined in config")
}
for _, c := range p.Config {
issue := c.validate()
if issue != "" {
return fmt.Errorf("invalid config: %s", issue)
}
}
// connect to SSH servers
for _, c := range p.Config {
go Handle(c)
}
return nil
}
func (p *program) Stop(s service.Service) error {
// let the os figure it out
return nil
}
func (c *Connection) setStatus(status string) {
log.Println(c.Name + ": " + status)
c.status.Store(status)
}
func (c *Connection) getStatus() string {
s := c.status.Load()
if s == nil {
return ""
}
return s.(string)
}
func (c *Connection) validate() string {
if c.Name == "" {
return "connection has no name"
}
if c.Host == "" {
return "host is empty for connection " + c.Name
}
if c.Username == "" {
return "username is empty for connection " + c.Name
}
if len(c.Tunnels) == 0 {
return "no tunnels defined for connection " + c.Name
}
if c.KeepAliveInterval.Unwrap() == 0 {
c.KeepAliveInterval.Set(time.Minute)
}
if c.MaxReconnectDelay.Unwrap() == 0 {
c.MaxReconnectDelay.Set(time.Minute)
}
// KeyFile and Key are mutually exclusive
if c.KeyFile != "" && c.Key != "" {
return "connection " + c.Name + " has both KeyFile and Key set"
}
// check that Host resolves
host, _, err := net.SplitHostPort(c.Host)
if err != nil {
// default to port 22
host = c.Host
c.Host = net.JoinHostPort(c.Host, "22")
}
addrs, _ := net.LookupHost(host)
if len(addrs) == 0 {
return "failed to resolve host " + c.Host + " for connection " + c.Name
}
// try to read key (if set)
var key []byte
if c.KeyFile != "" {
var err error
key, err = os.ReadFile(c.KeyFile)
if err != nil {
return "failed to read key file " + c.KeyFile + " for connection " + c.Name + ": " + err.Error()
}
} else if c.Key != "" {
key = []byte(c.Key)
}
// try to parse key
if key != nil {
// is there a password for the key?
var signer ssh.Signer
var err error
if c.Password == "" {
signer, err = ssh.ParsePrivateKey(key)
} else {
signer, err = ssh.ParsePrivateKeyWithPassphrase(key, []byte(c.Password))
}
if err != nil {
return "failed to parse key for connection " + c.Name + ": " + err.Error()
}
c.auth = ssh.PublicKeys(signer)
}
// if no key is set, try to use password
if c.auth == nil && c.Password != "" {
c.auth = ssh.Password(c.Password)
}
// check tunnels
for _, t := range c.Tunnels {
if t.From.Side == "" {
return c.Name + ".From.Side is empty"
}
if t.From.Address == "" {
return c.Name + ".From.Address is empty"
}
if t.To.Side == "" {
return c.Name + ".To.Side is empty"
}
if t.To.Address == "" {
return c.Name + ".To.Address is empty"
}
// tunnel must have a Local and Remote endpoint
if t.From.Side == "Local" && t.To.Side == "Remote" {
t.direction = ExposedLocally
} else if t.From.Side == "Remote" && t.To.Side == "Local" {
t.direction = ExposedOnServer
} else {
return "for connection " + c.Name + ", tunnel should be Local->Remote or Remote->Local"
}
// check that From and To are valid
_, fromPort, err := net.SplitHostPort(t.From.Address)
if err != nil {
return "for connection " + c.Name + ", tunnel From address is invalid: " + err.Error()
}
if fromPort == "" {
return "for connection " + c.Name + ", tunnel From address is missing port number"
}
_, toPort, err := net.SplitHostPort(t.To.Address)
if err != nil {
return "for connection " + c.Name + ", tunnel To address is invalid: " + err.Error()
}
if toPort == "" {
return "for connection " + c.Name + ", tunnel To address is missing port number"
}
}
return ""
}
func (c *Connection) connect() bool {
if c.conn != nil {
c.setStatus("already connected")
return true
}
issue := c.validate()
if issue != "" {
c.setStatus(issue)
return false
}
// connect to SSH server
c.setStatus("connecting")
var authMethods []ssh.AuthMethod
if c.auth != nil {
authMethods = []ssh.AuthMethod{c.auth}
}
var err error
c.conn, err = ssh.Dial("tcp", c.Host, &ssh.ClientConfig{
User: c.Username,
Auth: authMethods,
HostKeyCallback: fingerprintverifier.New(c.Fingerprint),
})
if err != nil {
c.setStatus("failed to connect to " + c.Host + ": " + err.Error())
return false
}
// establish tunnels
c.setStatus("configuring tunnels")
for _, t := range c.Tunnels {
switch t.direction {
case ExposedLocally:
l, err := net.Listen("tcp", t.From.Address)
if err != nil {
c.setStatus("failed to listen on " + t.From.Address + " (Local): " + err.Error())
continue
}
c.listeners = append(c.listeners, l)
go Forward(l, t.To.Address, c.conn.Dial, c.setStatus)
case ExposedOnServer:
r, err := c.conn.Listen("tcp", t.From.Address)
if err != nil {
c.setStatus("failed to listen on " + t.From.Address + " (Remote): " + err.Error())
continue
}
c.listeners = append(c.listeners, r)
go Forward(r, t.To.Address, net.Dial, c.setStatus)
default:
panic("invalid tunnel direction")
}
}
c.setStatus("ok")
return true
}
func (c *Connection) Close() error {
if c.conn != nil {
go c.conn.Close()
}
c.conn = nil
for _, l := range c.listeners {
go l.Close()
}
c.listeners = nil
return nil
}
func (c *Connection) IsAlive(timeout time.Duration) bool {
if c.conn == nil {
return false
}
t := time.NewTimer(timeout)
defer t.Stop()
errChan := make(chan error)
go func() {
_, _, err := c.conn.SendRequest("keepalive@openssh.com", true, nil)
errChan <- err
}()
select {
case <-t.C:
return false
case err := <-errChan:
return err == nil
}
}
func Handle(c *Connection) {
kii := c.KeepAliveInterval.Unwrap()
t := time.NewTicker(kii)
defer t.Stop()
c.reconnectDelay = time.Second
for {
if c.IsAlive(kii / 10 * 9) {
c.reconnectDelay = time.Second
<-t.C
continue
}
c.setStatus("disconnected")
// reconnect
c.Close()
success := c.connect()
if !success {
// exponential backoff
c.reconnectDelay *= 2
if c.reconnectDelay > c.MaxReconnectDelay.Unwrap() {
c.reconnectDelay = c.MaxReconnectDelay.Unwrap()
}
c.setStatus("reconnecting in " + c.reconnectDelay.String())
time.Sleep(c.reconnectDelay)
}
}
}