-
Notifications
You must be signed in to change notification settings - Fork 233
/
wg.go
90 lines (70 loc) · 1.94 KB
/
wg.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
package wg
import (
"encoding/base64"
"encoding/hex"
"errors"
"net"
"golang.zx2c4.com/wireguard/device"
)
type Config struct {
LocalPrivateKey PrivateKey `toml:"local_private_key"`
LocalNetwork *IPNet `toml:"local_network"`
RemotePublicKey PublicKey `toml:"remote_public_key"`
RemoteNetwork *IPNet `toml:"remote_network"`
Endpoint string `toml:"endpoint"`
DNS net.IP `toml:"dns"`
KeepAlive int `toml:"keepalive"`
MTU int `toml:"mtu"`
LogLevel int `toml:"log_level"`
}
type IPNet net.IPNet
func (n *IPNet) MarshalText() ([]byte, error) {
return []byte((*net.IPNet)(n).String()), nil
}
func (n *IPNet) UnmarshalText(text []byte) error {
_, ipnet, err := net.ParseCIDR(string(text))
if err != nil {
return err
}
n.IP, n.Mask = ipnet.IP, ipnet.Mask
return nil
}
func (n *IPNet) String() string { return (*net.IPNet)(n).String() }
type PrivateKey device.NoisePrivateKey
func (pk PrivateKey) MarshalText() ([]byte, error) {
return []byte(base64.StdEncoding.EncodeToString(pk[:])), nil
}
func (pk *PrivateKey) UnmarshalText(text []byte) error {
buf, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return err
}
if len(buf) != device.NoisePrivateKeySize {
return errors.New("invalid noise private key")
}
copy(pk[:], buf)
return nil
}
func (pk PrivateKey) ToHex() string {
val := (device.NoisePrivateKey)(pk)
return hex.EncodeToString(val[:])
}
type PublicKey device.NoisePublicKey
func (pk PublicKey) MarshalText() ([]byte, error) {
return []byte(base64.StdEncoding.EncodeToString(pk[:])), nil
}
func (pk *PublicKey) UnmarshalText(text []byte) error {
buf, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return err
}
if len(buf) != device.NoisePublicKeySize {
return errors.New("invalid noise private key")
}
copy(pk[:], buf)
return nil
}
func (pk PublicKey) ToHex() string {
val := (device.NoisePublicKey)(pk)
return hex.EncodeToString(val[:])
}