working psudorandom v6 allocator
This commit is contained in:
parent
64a73d18ed
commit
19f25ff847
4
add.go
4
add.go
@ -62,8 +62,8 @@ func Add() {
|
|||||||
privateKey := GenerateJSONPrivateKey()
|
privateKey := GenerateJSONPrivateKey()
|
||||||
publicKey := privateKey.PublicKey()
|
publicKey := privateKey.PublicKey()
|
||||||
|
|
||||||
IP := conf.MustAllocateIP(conf.Network.IPNet)
|
IP := conf.MustAllocateIP()
|
||||||
IP6 := conf.MustAllocateIP(conf.Network6.IPNet)
|
IP6 := conf.MustAllocateIP6()
|
||||||
|
|
||||||
peer := PeerConfig{
|
peer := PeerConfig{
|
||||||
Owner: owner,
|
Owner: owner,
|
||||||
|
@ -2,6 +2,7 @@ package dsnet
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
@ -153,19 +154,20 @@ func (conf DsnetConfig) IPAllocated(IP net.IP) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// choose a free IP for a new Peer
|
// choose a free IPv4 for a new Peer (sequential allocation)
|
||||||
func (conf DsnetConfig) MustAllocateIP(network net.IPNet) net.IP {
|
func (conf DsnetConfig) MustAllocateIP() net.IP {
|
||||||
|
network := conf.Network.IPNet
|
||||||
ones, bits := network.Mask.Size()
|
ones, bits := network.Mask.Size()
|
||||||
zeros := bits - ones
|
zeros := bits - ones
|
||||||
|
|
||||||
// avoids network addr
|
// avoids network addr
|
||||||
min := 1
|
min := 1
|
||||||
// avoids broadcast addr + overflow. Note there is no broadcast addr with
|
// avoids broadcast addr + overflow
|
||||||
// IPv6, but I don't care about losing one when there are so many!
|
|
||||||
max := (1 << zeros) - 2
|
max := (1 << zeros) - 2
|
||||||
|
|
||||||
for i := min; i <= max; i++ {
|
for i := min; i <= max; i++ {
|
||||||
IP := make(net.IP, len(network.IP))
|
IP := make(net.IP, len(network.IP))
|
||||||
|
// dst, src!
|
||||||
copy(IP, network.IP)
|
copy(IP, network.IP)
|
||||||
|
|
||||||
// OR the host part with the network part
|
// OR the host part with the network part
|
||||||
@ -184,6 +186,37 @@ func (conf DsnetConfig) MustAllocateIP(network net.IPNet) net.IP {
|
|||||||
return net.IP{}
|
return net.IP{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// choose a free IPv6 for a new Peer (pseudorandom allocation)
|
||||||
|
func (conf DsnetConfig) MustAllocateIP6() net.IP {
|
||||||
|
network := conf.Network6.IPNet
|
||||||
|
ones, bits := network.Mask.Size()
|
||||||
|
zeros := bits - ones
|
||||||
|
|
||||||
|
rbs := make([]byte, zeros)
|
||||||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
|
||||||
|
for i := 0; i <= 10000; i++ {
|
||||||
|
rand.Read(rbs)
|
||||||
|
IP := make(net.IP, len(network.IP))
|
||||||
|
// dst, src! Copy prefix of IP
|
||||||
|
copy(IP, network.IP)
|
||||||
|
|
||||||
|
// OR the host part with the network part
|
||||||
|
for j := ones / 8; j < len(IP); j++ {
|
||||||
|
IP[j] = IP[j] | rbs[j]
|
||||||
|
fmt.Println("%d, %s", j, IP[j])
|
||||||
|
}
|
||||||
|
|
||||||
|
if !conf.IPAllocated(IP) {
|
||||||
|
return IP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ExitFail("Could not allocate random IPv6 after 10000 tries. This was highly unlikely!")
|
||||||
|
|
||||||
|
return net.IP{}
|
||||||
|
}
|
||||||
|
|
||||||
func (conf DsnetConfig) GetWgPeerConfigs() []wgtypes.PeerConfig {
|
func (conf DsnetConfig) GetWgPeerConfigs() []wgtypes.PeerConfig {
|
||||||
wgPeers := make([]wgtypes.PeerConfig, 0, len(conf.Peers))
|
wgPeers := make([]wgtypes.PeerConfig, 0, len(conf.Peers))
|
||||||
|
|
||||||
|
4
init.go
4
init.go
@ -31,8 +31,8 @@ func Init() {
|
|||||||
Networks: []JSONIPNet{},
|
Networks: []JSONIPNet{},
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.IP = conf.MustAllocateIP(conf.Network.IPNet)
|
conf.IP = conf.MustAllocateIP()
|
||||||
conf.IP6 = conf.MustAllocateIP(conf.Network6.IPNet)
|
conf.IP6 = conf.MustAllocateIP6()
|
||||||
|
|
||||||
// DNS not set by default
|
// DNS not set by default
|
||||||
//conf.DNS = IP
|
//conf.DNS = IP
|
||||||
|
Loading…
Reference in New Issue
Block a user