split types into extypes

This commit is contained in:
Callan Bryant 2020-03-03 20:25:48 +00:00
parent 06d1642b16
commit 8f2ac50590
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
2 changed files with 69 additions and 62 deletions

69
exttypes.go Normal file
View File

@ -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,
}
}

View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net" "net"
"strings"
"time" "time"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes" "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
@ -151,64 +150,3 @@ type Dsnet struct {
ListenPort int ListenPort int
Peers []Peer 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,
}
}