dsnet/init.go

50 lines
963 B
Go
Raw Normal View History

2020-02-28 00:22:32 +01:00
package dsnet
import (
2020-03-01 21:54:33 +01:00
"net"
2020-03-01 22:39:05 +01:00
"math/rand"
"fmt"
"time"
2020-03-01 22:48:24 +01:00
"encoding/json"
2020-02-28 00:22:32 +01:00
2020-03-01 21:54:33 +01:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
//"github.com/mikioh/ipaddr"
2020-02-28 00:22:32 +01:00
)
func Init() {
2020-03-01 22:39:05 +01:00
// TODO check errors
privateKey, _ := wgtypes.GeneratePrivateKey()
presharedKey, _ := wgtypes.GenerateKey()
conf := DsnetConfig{
PrivateKey: privateKey,
PresharedKey: presharedKey,
ListenPort: DEFAULT_LISTEN_PORT,
Network: getRandomNetwork(),
2020-03-01 22:50:06 +01:00
Peers: make([]PeerConfig,0),
2020-03-01 22:39:05 +01:00
Domain: "dsnet",
2020-03-01 21:54:33 +01:00
}
2020-03-01 22:39:05 +01:00
2020-03-01 23:03:31 +01:00
//fmt.Println(conf.Network.String())
//fmt.Printf("%-+v/n",conf)
2020-03-01 22:48:24 +01:00
_json, _ := json.MarshalIndent(conf, "", " ")
fmt.Println(string(_json))
2020-03-01 21:54:33 +01:00
}
2020-03-01 22:39:05 +01:00
// get a random /22 subnet on 10.0.0.0 (1023 hosts) (or /24?)
// TODO also the 20 bit block and 16 bit block?
2020-03-01 23:29:11 +01:00
func getRandomNetwork() JSONIPNet {
2020-03-01 22:39:05 +01:00
rbs := make([]byte, 2)
rand.Seed(time.Now().UTC().UnixNano())
rand.Read(rbs)
2020-03-01 23:29:11 +01:00
return JSONIPNet{
2020-03-01 23:03:31 +01:00
net.IPNet {
net.IP{10,rbs[0],rbs[1]<<2,0},
net.IPMask{255,255,252,0},
},
2020-03-01 21:54:33 +01:00
}
2020-02-28 00:22:32 +01:00
}