broken config file parsing

This commit is contained in:
Callan Bryant 2020-03-02 02:49:03 +00:00
parent 3cce9fa63e
commit 11a4ab4fc0
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
2 changed files with 18 additions and 0 deletions

3
add.go
View File

@ -3,6 +3,7 @@ package dsnet
import (
"io/ioutil"
"encoding/json"
"fmt"
)
func Add(hostname string, owner string, description string, publicKey string) {
@ -11,4 +12,6 @@ func Add(hostname string, owner string, description string, publicKey string) {
conf := DsnetConfig{}
err = json.Unmarshal(raw, &conf)
check(err)
fmt.Printf("%+v\n", conf)
}

View File

@ -4,6 +4,7 @@ import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"net"
"time"
"strings"
)
// see https://github.com/WireGuard/wgctrl-go/blob/master/wgtypes/types.go for definitions
@ -73,6 +74,13 @@ func (n JSONIPNet) MarshalJSON() ([]byte, error) {
return []byte("\"" + n.IPNet.String() + "\""), nil
}
func (n JSONIPNet) UnmarshalJSON(b []byte) error {
cidr := strings.Trim(string(b), "\"")
_, IPNet, err := net.ParseCIDR(cidr)
n.IPNet = *IPNet
return err
}
func (n *JSONIPNet) String() string {
return n.IPNet.String()
}
@ -85,6 +93,13 @@ func (k JSONKey) MarshalJSON() ([]byte, error) {
return []byte("\"" + k.Key.String() + "\""), nil
}
func (k JSONKey) UnmarshalJSON(b []byte) error {
b64Key := strings.Trim(string(b), "\"")
key, err := wgtypes.ParseKey(b64Key)
k.Key = key
return err
}
func GenerateJSONPrivateKey() JSONKey {
privateKey, err := wgtypes.GeneratePrivateKey()