-
Notifications
You must be signed in to change notification settings - Fork 241
/
state.go
79 lines (67 loc) · 2.3 KB
/
state.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
package wg
import (
"fmt"
"net"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/internal/logger"
"github.com/superfly/flyctl/terminal"
"golang.zx2c4.com/wireguard/device"
)
type WireGuardState struct {
Org string `json:"org"`
Name string `json:"name"`
Region string `json:"region"`
LocalPublic string `json:"localprivate"`
LocalPrivate string `json:"localpublic"`
DNS string `json:"dns"`
Peer api.CreatedWireGuardPeer `json:"peer"`
}
// BUG(tqbf): Obviously all this needs to go, and I should just
// make my code conform to the marshal/unmarshal protocol wireguard-go
// uses, but in the service of landing this feature, I'm just going
// to apply a layer of spackle for now.
func (s *WireGuardState) TunnelConfig() *Config {
skey := PrivateKey{}
if err := skey.UnmarshalText([]byte(s.LocalPrivate)); err != nil {
panic(fmt.Sprintf("martian local private key: %s", err))
}
pkey := PublicKey{}
if err := pkey.UnmarshalText([]byte(s.Peer.Pubkey)); err != nil {
panic(fmt.Sprintf("martian local public key: %s", err))
}
_, lnet, err := net.ParseCIDR(fmt.Sprintf("%s/120", s.Peer.Peerip))
if err != nil {
panic(fmt.Sprintf("martian local public: %s/120: %s", s.Peer.Peerip, err))
}
raddr := net.ParseIP(s.Peer.Peerip).To16()
for i := 6; i < 16; i++ {
raddr[i] = 0
}
// BUG(tqbf): for now, we never manage tunnels for different
// organizations, and while this comment is eating more space
// than the code I'd need to do this right, it's more fun to
// type, so we just hardcode.
_, rnet, _ := net.ParseCIDR(fmt.Sprintf("%s/48", raddr))
raddr[15] = 3
dns := net.ParseIP(raddr.String())
// BUG(tqbf): I think this dance just because these needed to
// parse for Ben's TOML code.
wgl := IPNet(*lnet)
wgr := IPNet(*rnet)
var wgLogLevel int
switch terminal.GetLogLevel() {
case logger.Debug:
wgLogLevel = device.LogLevelVerbose
case logger.Info | logger.Warn | logger.Error:
wgLogLevel = device.LogLevelError
}
return &Config{
LocalPrivateKey: skey,
LocalNetwork: &wgl,
RemotePublicKey: pkey,
RemoteNetwork: &wgr,
Endpoint: s.Peer.Endpointip + ":51820",
DNS: dns,
LogLevel: wgLogLevel,
}
}