embed IPNet for MarshalJSON

This commit is contained in:
Callan Bryant 2020-03-01 22:03:31 +00:00
parent be849964e7
commit 099324b7f8
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
2 changed files with 22 additions and 7 deletions

10
init.go
View File

@ -25,8 +25,8 @@ func Init() {
Domain: "dsnet", Domain: "dsnet",
} }
fmt.Println(conf.Network.String()) //fmt.Println(conf.Network.String())
fmt.Printf("%-+v/n",conf) //fmt.Printf("%-+v/n",conf)
_json, _ := json.MarshalIndent(conf, "", " ") _json, _ := json.MarshalIndent(conf, "", " ")
@ -35,13 +35,15 @@ func Init() {
// get a random /22 subnet on 10.0.0.0 (1023 hosts) (or /24?) // get a random /22 subnet on 10.0.0.0 (1023 hosts) (or /24?)
// TODO also the 20 bit block and 16 bit block? // TODO also the 20 bit block and 16 bit block?
func getRandomNetwork() net.IPNet { func getRandomNetwork() IPNet {
rbs := make([]byte, 2) rbs := make([]byte, 2)
rand.Seed(time.Now().UTC().UnixNano()) rand.Seed(time.Now().UTC().UnixNano())
rand.Read(rbs) rand.Read(rbs)
return net.IPNet { return IPNet{
net.IPNet {
net.IP{10,rbs[0],rbs[1]<<2,0}, net.IP{10,rbs[0],rbs[1]<<2,0},
net.IPMask{255,255,252,0}, net.IPMask{255,255,252,0},
},
} }
} }

View File

@ -49,10 +49,11 @@ type DsnetConfig struct {
Peers []PeerConfig Peers []PeerConfig
// IP network from which to allocate automatic sequential addresses // IP network from which to allocate automatic sequential addresses
// Network is chosen randomly when not specified // Network is chosen randomly when not specified
Network net.IPNet `validate:"required"` Network IPNet `validate:"required"`
// domain to append to hostnames. Relies on separate DNS server for // domain to append to hostnames. Relies on separate DNS server for
// resolution. Informational only. // resolution. Informational only.
Domain string `validate:"required,gte=1,lte=255"` Domain string `validate:"required,gte=1,lte=255"`
// TODO Default subnets to route via VPN
} }
type Dsnet struct { type Dsnet struct {
@ -62,3 +63,15 @@ type Dsnet struct {
ListenPort int ListenPort int
Peers []Peer Peers []Peer
} }
type IPNet struct {
ipNet net.IPNet
}
func (n IPNet) MarshalJSON() ([]byte, error) {
return []byte("\"" + n.ipNet.String() + "\""), nil
}
func (n *IPNet) String() string {
return n.ipNet.String()
}