dsnet/configtypes.go

174 lines
4.5 KiB
Go
Raw Normal View History

2020-02-10 20:58:13 +01:00
package dsnet
import (
2020-03-02 03:54:43 +01:00
"encoding/json"
"io/ioutil"
2020-02-10 20:58:13 +01:00
"net"
2020-03-03 23:33:48 +01:00
"os"
2020-03-04 21:30:05 +01:00
"time"
2020-03-04 00:09:54 +01:00
"github.com/go-playground/validator/v10"
2020-03-04 21:34:11 +01:00
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
2020-02-10 20:58:13 +01:00
)
// see https://github.com/WireGuard/wgctrl-go/blob/master/wgtypes/types.go for definitions
2020-02-20 20:08:07 +01:00
type PeerConfig struct {
// Used to update DNS
2020-03-02 00:08:10 +01:00
Hostname string `validate:"required,gte=1,lte=255"`
2020-03-02 20:38:00 +01:00
// username of person running this host/router
Owner string `validate:"required,gte=1,lte=255"`
2020-02-20 20:08:07 +01:00
// Description of what the host is and/or does
2020-03-02 00:08:10 +01:00
Description string `validate:"required,gte=1,lte=255"`
2020-03-03 23:30:36 +01:00
// Internal VPN IP address. Added to AllowedIPs in server config as a /32
2020-03-04 00:09:54 +01:00
IP net.IP `validate:"required`
2020-03-02 22:02:21 +01:00
PublicKey JSONKey `validate:"required,len=44"`
PrivateKey JSONKey `json:"-"` // omitted from config!
2020-03-02 22:02:21 +01:00
PresharedKey JSONKey `validate:"required,len=44"`
2020-03-03 23:30:36 +01:00
// TODO ExternalIP support (Endpoint)
//ExternalIP net.UDPAddr `validate:"required,udp4_addr"`
// TODO support routing additional networks (AllowedIPs)
2020-03-04 20:46:39 +01:00
Networks []JSONIPNet `validate:"required"`
2020-02-20 20:08:07 +01:00
}
type DsnetConfig struct {
2020-02-27 23:19:48 +01:00
// domain to append to hostnames. Relies on separate DNS server for
// resolution. Informational only.
2020-03-04 20:38:48 +01:00
ExternalIP net.IP `validate:"required"`
ListenPort int `validate:"gte=1024,lte=65535"`
Domain string `validate:"required,gte=1,lte=255"`
InterfaceName string `validate:"required,gte=1,lte=255"`
2020-03-02 20:41:36 +01:00
// IP network from which to allocate automatic sequential addresses
// Network is chosen randomly when not specified
2020-03-03 23:30:36 +01:00
Network JSONIPNet `validate:"required"`
2020-03-04 00:09:54 +01:00
IP net.IP `validate:"required"`
DNS net.IP `validate:"required"`
2020-03-01 23:03:31 +01:00
// TODO Default subnets to route via VPN
2020-03-04 00:09:54 +01:00
ReportFile string `validate:"required"`
PrivateKey JSONKey `validate:"required,len=44"`
PresharedKey JSONKey `validate:"required,len=44"`
Peers []PeerConfig `validate:"dive"`
2020-02-20 20:08:07 +01:00
}
2020-03-02 03:54:43 +01:00
func MustLoadDsnetConfig() *DsnetConfig {
raw, err := ioutil.ReadFile(CONFIG_FILE)
2020-03-03 23:33:48 +01:00
if os.IsNotExist(err) {
ExitFail("%s does not exist. `dsnet init` may be required.", CONFIG_FILE)
} else if os.IsPermission(err) {
ExitFail("%s cannot be accessed. Sudo may be required.", CONFIG_FILE)
} else {
check(err)
}
2020-03-02 03:54:43 +01:00
conf := DsnetConfig{}
err = json.Unmarshal(raw, &conf)
check(err)
2020-03-04 00:09:54 +01:00
err = validator.New().Struct(conf)
check(err)
2020-03-02 03:54:43 +01:00
return &conf
}
func (conf *DsnetConfig) MustSave() {
_json, _ := json.MarshalIndent(conf, "", " ")
err := ioutil.WriteFile(CONFIG_FILE, _json, 0600)
check(err)
}
func (conf *DsnetConfig) MustAddPeer(peer PeerConfig) {
2020-03-02 20:26:08 +01:00
// TODO validate all PeerConfig (keys etc)
for _, p := range conf.Peers {
if peer.Hostname == p.Hostname {
ExitFail("%s is not an unique hostname", peer.Hostname)
}
}
2020-03-03 23:30:36 +01:00
if conf.IPAllocated(peer.IP) {
ExitFail("%s is already allocated", peer.IP)
}
for _, peerIPNet := range peer.Networks {
2020-03-02 20:26:08 +01:00
if conf.IPAllocated(peerIPNet.IPNet.IP) {
2020-03-02 20:31:29 +01:00
ExitFail("%s is already allocated", peerIPNet)
2020-03-02 20:26:08 +01:00
}
}
conf.Peers = append(conf.Peers, peer)
}
func (conf DsnetConfig) IPAllocated(IP net.IP) bool {
2020-03-03 23:30:36 +01:00
if IP.Equal(conf.IP) {
2020-03-02 21:10:48 +01:00
return true
}
for _, peer := range conf.Peers {
2020-03-03 23:30:36 +01:00
if IP.Equal(peer.IP) {
return true
}
for _, peerIPNet := range peer.Networks {
2020-03-02 20:13:38 +01:00
if IP.Equal(peerIPNet.IPNet.IP) {
2020-03-02 20:31:29 +01:00
return true
}
}
}
2020-03-02 20:31:29 +01:00
return false
}
2020-03-02 19:44:19 +01:00
// choose a free IP for a new Peer
2020-03-02 21:11:33 +01:00
func (conf DsnetConfig) MustAllocateIP() net.IP {
2020-03-02 19:44:19 +01:00
network := conf.Network.IPNet
ones, bits := network.Mask.Size()
zeros := bits - ones
2020-03-02 20:03:00 +01:00
min := 1 // avoids network addr
max := (1 << zeros) - 2 // avoids broadcast addr + overflow
2020-03-02 19:44:19 +01:00
for i := min; i <= max; i++ {
IP := make(net.IP, len(network.IP))
copy(IP, network.IP)
// OR the host part with the network part
for j := 0; j < len(IP); j++ {
shift := (len(IP) - j - 1) * 8
IP[j] = IP[j] | byte(i>>shift)
2020-03-02 20:13:38 +01:00
}
2020-03-02 19:44:19 +01:00
2020-03-02 22:02:21 +01:00
if !conf.IPAllocated(IP) {
2020-03-02 20:29:08 +01:00
return IP
}
2020-03-02 19:44:19 +01:00
}
2020-03-02 20:29:08 +01:00
ExitFail("IP range exhausted")
return net.IP{}
2020-03-02 19:44:19 +01:00
}
2020-03-04 21:30:05 +01:00
func (conf DsnetConfig) GetWgPeerConfigs() []wgtypes.PeerConfig {
wgPeers := make([]wgtypes.PeerConfig, 0, len(conf.Peers))
2020-03-04 21:34:11 +01:00
interval := time.Second * KEEPALIVE_SECONDS
2020-03-04 21:30:05 +01:00
for _, peer := range conf.Peers {
wgPeers = append(wgPeers, wgtypes.PeerConfig{
2020-03-04 21:34:11 +01:00
PublicKey: peer.PublicKey.Key,
Remove: false,
UpdateOnly: false,
PresharedKey: &peer.PresharedKey.Key,
Endpoint: nil,
2020-03-04 21:30:05 +01:00
PersistentKeepaliveInterval: &interval,
2020-03-04 21:34:11 +01:00
ReplaceAllowedIPs: true,
2020-03-04 21:30:05 +01:00
AllowedIPs: []net.IPNet{
net.IPNet{
2020-03-04 21:34:11 +01:00
IP: peer.IP,
Mask: net.IPMask{255, 255, 255, 255},
2020-03-04 21:30:05 +01:00
},
},
})
}
return wgPeers
}