dsnet/init.go

47 lines
953 B
Go
Raw Normal View History

2020-02-28 00:22:32 +01:00
package dsnet
import (
2020-03-02 00:08:10 +01:00
"encoding/json"
2020-03-01 22:39:05 +01:00
"fmt"
2020-03-02 00:08:10 +01:00
"math/rand"
"net"
2020-03-01 22:39:05 +01:00
"time"
2020-03-02 00:52:20 +01:00
"io/ioutil"
2020-03-01 21:54:33 +01:00
//"github.com/mikioh/ipaddr"
2020-02-28 00:22:32 +01:00
)
func Init() {
2020-03-01 23:58:21 +01:00
privateKey := GenerateJSONPrivateKey()
presharedKey := GenerateJSONKey()
2020-03-01 22:39:05 +01:00
conf := DsnetConfig{
2020-03-02 00:08:10 +01:00
PrivateKey: privateKey,
2020-03-01 22:39:05 +01:00
PresharedKey: presharedKey,
2020-03-02 00:08:10 +01:00
ListenPort: DEFAULT_LISTEN_PORT,
Network: getRandomNetwork(),
Peers: make([]PeerConfig, 0),
Domain: "dsnet",
2020-03-02 00:17:01 +01:00
ReportFile: DEFAULT_REPORT_FILE,
2020-03-01 21:54:33 +01:00
}
2020-03-01 22:39:05 +01:00
2020-03-01 22:48:24 +01:00
_json, _ := json.MarshalIndent(conf, "", " ")
2020-03-02 00:52:20 +01:00
err := ioutil.WriteFile(CONFIG_FILE, _json, 0600)
check(err)
2020-03-01 22:48:24 +01:00
2020-03-02 00:52:20 +01:00
fmt.Printf("Config written to %s. Please edit.", CONFIG_FILE)
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?)
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-02 00:17:01 +01:00
IPNet: net.IPNet{
2020-03-02 00:08:10 +01:00
net.IP{10, rbs[0], rbs[1] << 2, 0},
net.IPMask{255, 255, 252, 0},
2020-03-01 23:03:31 +01:00
},
2020-03-01 21:54:33 +01:00
}
2020-02-28 00:22:32 +01:00
}