dsnet/types.go

205 lines
4.6 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-02 03:49:03 +01:00
"strings"
2020-03-02 03:54:43 +01:00
"time"
"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-02-20 20:08:07 +01:00
2020-03-02 00:08:10 +01:00
PublicKey JSONKey `validate:"required,len=44"`
PresharedKey JSONKey `validate:"required,len=44"`
Endpoint net.UDPAddr `validate:"required,udp4_addr"`
2020-03-02 20:13:38 +01:00
AllowedIPs []JSONIPNet `validate:"dive,required,cidr"`
2020-02-20 20:08:07 +01:00
}
2020-02-10 20:58:13 +01:00
type Peer struct {
2020-02-20 20:08:07 +01:00
// username of person running this host/router
Owner string
// Used to update DNS
Hostname string
// Description of what the host is and/or does
2020-02-10 20:58:13 +01:00
Description string
2020-02-20 20:08:07 +01:00
// whether last heartbeat/rxdata was received (50% margin)
Online bool
// if no data for x days, consider revoking access
Expired bool
2020-03-02 00:08:10 +01:00
PublicKey wgtypes.Key
PresharedKey wgtypes.Key
Endpoint *net.UDPAddr
2020-02-10 20:58:13 +01:00
LastHandshakeTime time.Time
2020-03-02 00:08:10 +01:00
ReceiveBytes int64
TransmitBytes int64
AllowedIPs []net.IPNet
2020-02-10 20:58:13 +01:00
}
2020-02-20 20:08:07 +01:00
type DsnetConfig struct {
2020-03-02 00:08:10 +01:00
PrivateKey JSONKey `validate:"required,len=44"`
PresharedKey JSONKey `validate:"required,len=44"`
ListenPort int `validate:"gte=1024,lte=65535"`
Peers []PeerConfig
2020-02-27 23:19:48 +01:00
// IP network from which to allocate automatic sequential addresses
2020-02-27 23:31:44 +01:00
// Network is chosen randomly when not specified
2020-03-02 00:08:10 +01:00
Network JSONIPNet `validate:"required"`
2020-02-27 23:19:48 +01:00
// domain to append to hostnames. Relies on separate DNS server for
// resolution. Informational only.
2020-03-02 00:08:10 +01:00
Domain string `validate:"required,gte=1,lte=255"`
2020-03-01 23:03:31 +01:00
// TODO Default subnets to route via VPN
2020-03-02 00:17:01 +01:00
ReportFile string `validate:"required"`
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)
check(err)
conf := DsnetConfig{}
err = json.Unmarshal(raw, &conf)
check(err)
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)
}
}
for _, peerIPNet := range peer.AllowedIPs {
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 {
for _, peer := range conf.Peers {
for _, peerIPNet := range peer.AllowedIPs {
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 20:29:08 +01:00
func (conf DsnetConfig) MustChooseIP() 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 20:27:54 +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-02-20 20:08:07 +01:00
type Dsnet struct {
2020-03-02 00:08:10 +01:00
Name string
2020-02-28 00:22:32 +01:00
PrivateKey wgtypes.Key
2020-03-02 00:08:10 +01:00
PublicKey wgtypes.Key
2020-02-20 20:08:07 +01:00
ListenPort int
2020-03-02 00:08:10 +01:00
Peers []Peer
2020-02-20 20:08:07 +01:00
}
2020-03-01 23:03:31 +01:00
2020-03-01 23:29:11 +01:00
type JSONIPNet struct {
2020-03-01 23:58:21 +01:00
IPNet net.IPNet
2020-03-01 23:03:31 +01:00
}
2020-03-01 23:29:11 +01:00
func (n JSONIPNet) MarshalJSON() ([]byte, error) {
2020-03-01 23:58:21 +01:00
return []byte("\"" + n.IPNet.String() + "\""), nil
2020-03-01 23:03:31 +01:00
}
2020-03-02 03:49:44 +01:00
func (n *JSONIPNet) UnmarshalJSON(b []byte) error {
2020-03-02 03:49:03 +01:00
cidr := strings.Trim(string(b), "\"")
2020-03-02 20:38:00 +01:00
IP, IPNet, err := net.ParseCIDR(cidr)
IPNet.IP = IP
2020-03-02 03:49:03 +01:00
n.IPNet = *IPNet
return err
}
2020-03-01 23:29:11 +01:00
func (n *JSONIPNet) String() string {
2020-03-01 23:58:21 +01:00
return n.IPNet.String()
}
type JSONKey struct {
Key wgtypes.Key
}
func (k JSONKey) MarshalJSON() ([]byte, error) {
return []byte("\"" + k.Key.String() + "\""), nil
}
func (k JSONKey) PublicKey() JSONKey {
return JSONKey{
Key: k.Key.PublicKey(),
}
}
2020-03-02 03:49:44 +01:00
func (k *JSONKey) UnmarshalJSON(b []byte) error {
2020-03-02 03:49:03 +01:00
b64Key := strings.Trim(string(b), "\"")
key, err := wgtypes.ParseKey(b64Key)
k.Key = key
return err
}
2020-03-01 23:58:21 +01:00
func GenerateJSONPrivateKey() JSONKey {
privateKey, err := wgtypes.GeneratePrivateKey()
2020-03-02 00:48:02 +01:00
check(err)
2020-03-01 23:58:21 +01:00
return JSONKey{
Key: privateKey,
}
}
func GenerateJSONKey() JSONKey {
privateKey, err := wgtypes.GenerateKey()
2020-03-02 00:48:02 +01:00
check(err)
2020-03-01 23:58:21 +01:00
return JSONKey{
Key: privateKey,
}
2020-03-01 23:03:31 +01:00
}