From 8f2ac50590fe04fff3625b43339376ee7d308bb7 Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Tue, 3 Mar 2020 20:25:48 +0000 Subject: [PATCH] split types into extypes --- exttypes.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ types.go | 62 ----------------------------------------------- 2 files changed, 69 insertions(+), 62 deletions(-) create mode 100644 exttypes.go diff --git a/exttypes.go b/exttypes.go new file mode 100644 index 0000000..d43c6af --- /dev/null +++ b/exttypes.go @@ -0,0 +1,69 @@ +package dsnet + +import ( + "net" + "strings" + + "golang.zx2c4.com/wireguard/wgctrl/wgtypes" +) + +type JSONIPNet struct { + IPNet net.IPNet +} + +func (n JSONIPNet) MarshalJSON() ([]byte, error) { + return []byte("\"" + n.IPNet.String() + "\""), nil +} + +func (n *JSONIPNet) UnmarshalJSON(b []byte) error { + cidr := strings.Trim(string(b), "\"") + IP, IPNet, err := net.ParseCIDR(cidr) + IPNet.IP = IP + n.IPNet = *IPNet + return err +} + +func (n *JSONIPNet) String() string { + return n.IPNet.String() +} + +type JSONKey struct { + Key wgtypes.Key +} + +func (k JSONKey) MarshalJSON() ([]byte, error) { + return []byte("\"" + k.Key.String() + "\""), nil +} + +func (k JSONKey) PublicKey() JSONKey { + return JSONKey{ + Key: k.Key.PublicKey(), + } +} + +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() + + check(err) + + return JSONKey{ + Key: privateKey, + } +} + +func GenerateJSONKey() JSONKey { + privateKey, err := wgtypes.GenerateKey() + + check(err) + + return JSONKey{ + Key: privateKey, + } +} diff --git a/types.go b/types.go index 2c3304a..f8b2b09 100644 --- a/types.go +++ b/types.go @@ -4,7 +4,6 @@ import ( "encoding/json" "io/ioutil" "net" - "strings" "time" "golang.zx2c4.com/wireguard/wgctrl/wgtypes" @@ -151,64 +150,3 @@ type Dsnet struct { ListenPort int Peers []Peer } - -type JSONIPNet struct { - IPNet net.IPNet -} - -func (n JSONIPNet) MarshalJSON() ([]byte, error) { - return []byte("\"" + n.IPNet.String() + "\""), nil -} - -func (n *JSONIPNet) UnmarshalJSON(b []byte) error { - cidr := strings.Trim(string(b), "\"") - IP, IPNet, err := net.ParseCIDR(cidr) - IPNet.IP = IP - n.IPNet = *IPNet - return err -} - -func (n *JSONIPNet) String() string { - return n.IPNet.String() -} - -type JSONKey struct { - Key wgtypes.Key -} - -func (k JSONKey) MarshalJSON() ([]byte, error) { - return []byte("\"" + k.Key.String() + "\""), nil -} - -func (k JSONKey) PublicKey() JSONKey { - return JSONKey{ - Key: k.Key.PublicKey(), - } -} - -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() - - check(err) - - return JSONKey{ - Key: privateKey, - } -} - -func GenerateJSONKey() JSONKey { - privateKey, err := wgtypes.GenerateKey() - - check(err) - - return JSONKey{ - Key: privateKey, - } -}