-
Notifications
You must be signed in to change notification settings - Fork 4
/
client.go
630 lines (562 loc) · 17.9 KB
/
client.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
package internal
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/jpillora/backoff"
"github.com/robfig/cron/v3"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
)
var cViper *viper.Viper
type state struct {
State string `json:"state"`
DesiredState string `json:"desiredstate"`
ClientVersion string `json:"clientversion"`
Error string `json:"error"`
GatewayHost string `json:"gatewayhost"`
GatewayPort string `json:"gatewayport"`
GatewayPublicKey string `json:"gatewaypublickey"`
OriginalDefaultGatewayDev string `json:"originaldefaultgatewaydevice"`
OriginalDefaultGatewayIP string `json:"originaldefaultgatewayip"`
YggdrasilInterface string `json:"yggdrasilinterface"`
ClientIP string `json:"clientip"`
ClientNetMask int `json:"clientnetmask"`
ClientGateway string `json:"clientgateway"`
LeaseExpires time.Time `json:"leaseexpires"`
PeerRoutes map[string]yggPeerRoute `json:"peerroutes"`
}
type yggPeerRoute struct {
DefaultGatewayIP string `json:"defaultgatewayip"`
DefaultGatewayDev string `json:"defaultgatewaydevice"`
}
type errorOutput struct {
Error string `json:"error"`
}
func logAndExit(message string, exitcode int) {
Output := errorOutput{
Error: message,
}
text := "Error: " + message + "\n"
if cViper.GetBool("Json") {
tmp, err := json.Marshal(Output)
if err != nil {
Fatal(err)
}
text = string(tmp)
}
fmt.Println(text)
os.Exit(exitcode)
}
func clientUsage(fs *flag.FlagSet) {
fmt.Fprintf(os.Stderr, `
autoygg-client is a tool to register an Yggdrasil node with a gateway for internet egress.
Options:
`)
fs.PrintDefaults()
fmt.Fprintln(os.Stderr, "")
}
func doRequestWorker(fs *flag.FlagSet, verb string, action string, gatewayHost string, gatewayPort string, i info) (response []byte, err error) {
validActions := map[string]bool{
"register": true, // register and request a lease
"renew": true, // renew an existing lease
"release": true, // release an existing lease
}
if !validActions[action] {
err = errors.New("Invalid action: " + action)
// Invalid action is a fatal error, abort here
handleError(err, cViper, true)
}
var r registration
r.PublicKey, err = getSelfPublicKey()
if err != nil {
return
}
// Only send ClientName, ClientEmail and ClientPhone when registration is required
if i.RequireRegistration {
r.ClientName = cViper.GetString("clientname")
r.ClientEmail = cViper.GetString("clientemail")
r.ClientPhone = cViper.GetString("clientphone")
}
r.ClientVersion = version
req, err := json.Marshal(r)
if err != nil {
return
}
// One idle connection (for up to 90 seconds) is more than enough
client := http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConnsPerHost: 1,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
var resp *http.Response
if verb == "post" {
resp, err = client.Post("http://["+gatewayHost+"]:"+gatewayPort+"/"+action, "application/json", bytes.NewBuffer(req))
} else {
resp, err = client.Get("http://[" + gatewayHost + "]:" + gatewayPort + "/" + action)
}
if err != nil {
return
}
defer resp.Body.Close()
response, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return
}
func clientSetupRoutes(clientIP string, clientNetMask int, clientGateway string, publicKey string, defaultGatewayIP string, defaultGatewayDev string, State state) (newState state, err error) {
newState = State
newState.Error = ""
newState.OriginalDefaultGatewayDev = defaultGatewayDev
newState.OriginalDefaultGatewayIP = defaultGatewayIP
log.Printf("Create GRE tunnel")
err = addClientTunnel(cViper, "autoygg", clientIP, clientGateway, clientNetMask, State.GatewayHost)
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
newState.State = "disconnected"
saveState(newState)
return
}
// Make sure we route traffic to our Yggdrasil peer(s) to the wan default gateway
log.Printf("Get Yggdrasil peers")
peers, err := yggdrasilPeers()
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}
for _, p := range peers {
// ip ro add <peer_ip> via <wan_gw> dev <wan_dev>
log.Printf("Add Yggdrasil peer route for %s via %s", p, defaultGatewayIP)
var change bool
change, err = addPeerRoute(p, defaultGatewayIP, defaultGatewayDev)
handleError(err, cViper, false)
if err != nil {
// If we can't add a route for all yggdrasil peers, something is really wrong and we should abort.
// Because if we change the default gateway, we will be cutting ourselves off from the internet.
newState.Error += err.Error() + "\n"
saveState(newState)
return
}
if change {
if newState.PeerRoutes == nil {
newState.PeerRoutes = make(map[string]yggPeerRoute)
}
newState.PeerRoutes[p] = yggPeerRoute{DefaultGatewayIP: defaultGatewayIP, DefaultGatewayDev: defaultGatewayDev}
}
}
log.Printf("Add default gateway pointing at %s", clientGateway)
err = addDefaultGateway(clientGateway)
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}
newState.State = "connected"
saveState(newState)
// FIXME TODO:
// * replace default route, test connectivity, if fail, rollback?
return
}
func clientTearDownRoutes(clientIP string, clientNetMask int, clientGateway string, publicKey string, State state) (newState state, err error) {
newState = State
newState.Error = ""
log.Printf("Remove default gateway pointing at %s", clientGateway)
err = removeDefaultGateway(State.OriginalDefaultGatewayIP)
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}
log.Printf("Get Yggdrasil peers from state file")
handleError(nil, cViper, false)
for p := range State.PeerRoutes {
log.Printf("Remove Yggdrasil peer route for %s", p)
var change bool
change, err = removePeerRoute(p)
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}
if change {
delete(newState.PeerRoutes, p)
}
}
log.Printf("Remove GRE tunnel")
err = removeClientTunnel(cViper, "autoygg")
handleError(err, cViper, false)
if err != nil {
newState.Error += err.Error() + "\n"
}
saveState(newState)
return
}
func clientLoadConfig(path string) {
config := "client"
if cViper.Get("CONFIG") != nil {
config = cViper.Get("CONFIG").(string)
}
// Load the main config file
cViper.SetConfigType("yaml")
cViper.SetConfigName(config)
if path == "" {
cViper.AddConfigPath("/etc/autoygg/")
cViper.AddConfigPath("$HOME/.autoygg")
cViper.AddConfigPath(".")
} else {
// For testing
cViper.AddConfigPath(path)
}
err := cViper.ReadInConfig()
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// The client config file is optional
err = nil
} else if err != nil {
Fatal(fmt.Sprintln("Fatal error reading config file:", err.Error()))
}
}
func clientCreateFlagSet(args []string) (fs *flag.FlagSet) {
fs = flag.NewFlagSet("Autoygg", flag.ContinueOnError)
fs.Usage = func() { clientUsage(fs) }
fs.Bool("daemon", true, "Run in daemon mode. The client will automatically renew its lease before it expires.")
fs.String("gatewayHost", "", "Yggdrasil IP address of the gateway host")
fs.String("gatewayPort", "8080", "port of the gateway daemon")
fs.String("defaultGatewayIP", "", "LAN default gateway IP address (autodiscovered by default)")
fs.String("defaultGatewayDev", "", "LAN default gateway device (autodiscovered by default)")
fs.String("yggdrasilInterface", "tun0", "Yggdrasil tunnel interface")
fs.String("action", "register", "action (register/renew/release)")
fs.String("clientName", "", "your name (optional)")
fs.String("clientEmail", "", "your e-mail (optional)")
fs.String("clientPhone", "", "your phone number (optional)")
fs.Bool("debug", false, "debug output")
fs.Bool("quiet", false, "suppress non-error output")
fs.Bool("dumpConfig", false, "dump the configuration that would be used by autoygg-client and exit")
fs.Bool("json", false, "dump the configuration in json format, rather than yaml (only relevant when used with --dumpConfig)")
fs.Bool("complete", false, "dump the complete configuration (default false, only relevant when used with --dumpConfig)")
fs.Bool("useConfig", false, "read configuration from stdin")
fs.Bool("useUCI", false, "read configuration by executing 'autoygguci get'")
fs.Bool("state", false, "print current state in json format")
fs.Bool("help", false, "print usage and exit")
fs.Bool("version", false, "print version and exit")
err := fs.Parse(args)
if err != nil {
Fatal(err)
}
viperLoadSharedDefaults(cViper)
err = cViper.BindPFlags(fs)
if err != nil {
Fatal(err)
}
return
}
func renewLease(fs *flag.FlagSet, State state) (newState state) {
_, newState, _ = doRequest(fs, "renew", cViper.GetString("GatewayHost"), cViper.GetString("GatewayPort"), State)
return
}
func doInfoRequest(fs *flag.FlagSet, gatewayHost string, gatewayPort string) (i info, err error) {
// Reduce the connection timeout to half a second
// One idle connection for 90 seconds is more than enough
client := http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 500 * time.Millisecond,
KeepAlive: 30 * time.Second,
}).Dial,
ForceAttemptHTTP2: true,
MaxIdleConnsPerHost: 1,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
resp, err := client.Get("http://[" + gatewayHost + "]:" + gatewayPort + "/info")
if err != nil {
return
}
defer resp.Body.Close()
response, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = json.Unmarshal(response, &i)
return
}
func doRequest(fs *flag.FlagSet, action string, gatewayHost string, gatewayPort string, State state) (r registration, newState state, err error) {
newState = State
// Do an info request to know if registration is required
i, err := handleInfoWorker(fs)
if err != nil {
handleError(err, cViper, false)
return
}
verb := "post"
log.Printf("Send `" + action + "` request to autoygg")
response, err := doRequestWorker(fs, verb, action, gatewayHost, gatewayPort, i)
if err != nil {
handleError(err, cViper, false)
return
}
debug("Raw server response:\n\n%s\n\n", string(response))
err = json.Unmarshal(response, &r)
handleError(err, cViper, false)
if err != nil {
// Only abort when we are not trying to release a lease
newState.Error = err.Error()
if cViper.GetString("Action") != "release" {
saveState(newState)
return
}
}
if r.Error == "" && action == "register" {
newState.State = "connected"
newState.Error = ""
newState.GatewayHost = gatewayHost
newState.GatewayPort = gatewayPort
newState.GatewayPublicKey = r.GatewayPublicKey
newState.YggdrasilInterface = cViper.GetString("YggdrasilInterface")
newState.ClientIP = r.ClientIP
newState.ClientNetMask = r.ClientNetMask
newState.ClientGateway = r.ClientGateway
newState.LeaseExpires = r.LeaseExpires
} else if action == "release" {
// Errors while releasing can just be ignored from our local state perspective
newState.State = "disconnected"
} else if r.Error != "" {
newState.Error = r.Error
}
saveState(newState)
return
}
func loadState(origState state) (State state, err error) {
// FIXME add mutex
State = origState
path := cViper.GetString("StateDir") + "/client-state.json"
stateFile, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
// no state file, this is often normal, reset err to nil
debug("State file not found at %s", path)
err = nil
}
return
}
err = json.Unmarshal([]byte(stateFile), &State)
return
}
func saveState(State state) {
// FIXME add mutex
debug("Saving client state")
jsonState, err := json.Marshal(State)
if err != nil {
debug(err.Error())
return
}
path := cViper.GetString("StateDir") + "/client-state.json"
err = os.MkdirAll(cViper.GetString("StateDir"), os.ModePerm)
if err != nil {
debug(err.Error())
return
}
err = ioutil.WriteFile(path, jsonState, 0644)
if err != nil {
debug(err.Error())
}
}
func clientValidateConfig() (fs *flag.FlagSet) {
fs = clientCreateFlagSet(os.Args[1:])
if cViper.GetBool("UseConfig") {
cViper.SetConfigType("yaml")
cViper.SetConfigName("client")
// Read the configuration from stdin.
err := cViper.ReadConfig(os.Stdin)
if err != nil {
Fatal(err)
}
} else if cViper.GetBool("UseUCI") {
cViper.SetConfigType("yaml")
cViper.SetConfigName("client")
// Read the configuration by executing `autoygguci get`. Used on openwrt.
out, err := command(cViper.GetString("Shell"), cViper.GetString("ShellCommandArg"), "autoygguci get").Output()
if err != nil {
Fatal(err)
}
err = cViper.ReadConfig(bytes.NewBuffer(out))
if err != nil {
Fatal(err)
}
} else {
clientLoadConfig("")
}
if cViper.GetBool("Debug") {
debug = debugLog.Printf
}
if cViper.GetBool("State") || cViper.GetString("Action") == "info" {
// These arguments imply json output
cViper.Set("Json", true)
}
if cViper.GetBool("Help") {
clientUsage(fs)
os.Exit(0)
}
if cViper.GetBool("Version") {
fmt.Println(version)
os.Exit(0)
}
if cViper.GetBool("DumpConfig") {
fmt.Print(dumpConfiguration(cViper, "client"))
os.Exit(0)
}
return
}
func handleInfoWorker(fs *flag.FlagSet) (i info, err error) {
i, err = doInfoRequest(fs, cViper.GetString("GatewayHost"), cViper.GetString("GatewayPort"))
if err != nil {
if os.IsTimeout(err) {
err = fmt.Errorf("Timeout: could not connect to gateway at %s", cViper.GetString("GatewayHost"))
}
}
return
}
func handleInfo(fs *flag.FlagSet, i info) {
infoJSON, err := json.MarshalIndent(i, "", " ")
if err != nil {
logAndExit(err.Error(), 1)
}
fmt.Printf("%s\n", infoJSON)
os.Exit(0)
}
// ClientMain is the main() function for the client program
func ClientMain() {
cViper = viper.New()
setupLogWriters(cViper, true)
fs := clientValidateConfig()
// Make sure we have a version of yggdrasil that is recent enough
legacy, yggVersion, err := legacyYggdrasil()
if err != nil {
Fatal(err)
}
if legacy {
err = fmt.Errorf("The detected version of yggdrasil (%s) is too old, it is not supported by this version of autoygg.\nPlease upgrade yggdrasil to version 0.4.0 or later, or downgrade autoygg to v0.2.2", yggVersion)
Fatal(err)
}
var State state
State, err = loadState(State)
if err != nil {
logAndExit(err.Error(), 1)
}
State.ClientVersion = version
if cViper.GetBool("State") {
json, err := json.MarshalIndent(State, "", " ")
if err != nil {
logAndExit(fmt.Sprintf("Error: %s", err), 1)
}
fmt.Printf("%s\n", json)
os.Exit(0)
}
if cViper.GetString("GatewayHost") == "" {
logAndExit("GatewayHost is not defined", 0)
}
if cViper.GetString("Action") == "" {
logAndExit("Action is not defined", 0)
}
if cViper.GetString("Action") == "info" {
i, err := handleInfoWorker(fs)
// if the 'info' request failed bail out here
if err != nil {
logAndExit(err.Error(), 1)
}
handleInfo(fs, i)
} else {
if cViper.GetString("Action") == "register" || cViper.GetString("Action") == "renew" {
State.DesiredState = "connected"
} else if cViper.GetString("Action") == "release" {
State.DesiredState = "disconnected"
State, err = clientTearDownRoutes(State.ClientIP, State.ClientNetMask, State.ClientGateway, State.GatewayPublicKey, State)
if err != nil {
Fatal(err)
}
}
}
b := &backoff.Backoff{
Min: 100 * time.Millisecond,
Max: 10 * time.Second,
Factor: 2,
Jitter: true,
}
var r registration
for {
r, State, err = doRequest(fs, cViper.GetString("Action"), cViper.GetString("GatewayHost"), cViper.GetString("GatewayPort"), State)
if err != nil && cViper.GetBool("Daemon") {
d := b.Duration()
time.Sleep(d)
continue
} else {
break
}
}
if r.Error != "" {
logAndExit(r.Error, 1)
}
if err != nil {
logAndExit(err.Error(), 1)
}
if cViper.GetString("Action") == "register" {
gatewayDev := cViper.GetString("DefaultGatewayDev")
gatewayIP := cViper.GetString("DefaultGatewayIP")
if gatewayIP == "" {
YggdrasilInterface := State.YggdrasilInterface
if YggdrasilInterface == "" {
YggdrasilInterface = cViper.GetString("YggdrasilInterface")
}
tmpDev, tmpIP, err := DiscoverLocalGateway(YggdrasilInterface)
if err != nil {
Fatal(err)
}
gatewayIP = tmpIP.String()
gatewayDev = tmpDev
debug("Detected gatewayIP %s via gatewayDev %s\n", gatewayIP, gatewayDev)
}
State, err = clientSetupRoutes(r.ClientIP, r.ClientNetMask, r.ClientGateway, r.GatewayPublicKey, gatewayIP, gatewayDev, State)
if err != nil {
Fatal(err)
}
}
if cViper.GetBool("Daemon") && cViper.GetString("Action") == "register" {
log.Printf("Set up cron job to renew lease every 30 minutes")
c := cron.New()
_, err := c.AddFunc("CRON_TZ=UTC */30 * * * *", func() {
State = renewLease(fs, State)
})
handleError(err, cViper, false)
if err != nil {
Fatal("Couldn't set up cron job!")
}
go c.Start()
cancelChan := make(chan os.Signal, 1)
signal.Notify(cancelChan, os.Interrupt, syscall.SIGTERM)
sig := <-cancelChan
fmt.Fprint(os.Stderr, "\r") // Overwrite any ^C that may have been printed on the screen
debug("Caught signal %v\n", sig)
State.DesiredState = "disconnected"
State, _ = clientTearDownRoutes(r.ClientIP, r.ClientNetMask, r.ClientGateway, r.GatewayPublicKey, State)
_, _, _ = doRequest(fs, "release", cViper.GetString("GatewayHost"), cViper.GetString("GatewayPort"), State)
}
}