forked from mdlayher/wireguard_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
45 lines (37 loc) · 1.03 KB
/
parse.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
package wireguardexporter
import (
"fmt"
"io"
"github.com/BurntSushi/toml"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
// file is the TOML mapping of public keys to peer names.
type file struct {
Peers []struct {
PublicKey string `toml:"public_key"`
Name string `toml:"name"`
} `toml:"peer"`
}
// ParsePeers parses a TOML mapping of peer public keys to friendly names.
func ParsePeers(r io.Reader) (map[string]string, error) {
var f file
md, err := toml.DecodeReader(r, &f)
if err != nil {
return nil, err
}
if u := md.Undecoded(); len(u) > 0 {
return nil, fmt.Errorf("unrecognized keys: %s", u)
}
peers := make(map[string]string)
for _, p := range f.Peers {
// Each peer must have a valid public key and a name set.
if _, err := wgtypes.ParseKey(p.PublicKey); err != nil {
return nil, fmt.Errorf("invalid public key %q: %v", p.PublicKey, err)
}
if p.Name == "" {
return nil, fmt.Errorf("no name set for peer with public key %q", p.PublicKey)
}
peers[p.PublicKey] = p.Name
}
return peers, nil
}