From c2aac62f273f2afaaf2c7fb28e33bf5db2302159 Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Tue, 3 Mar 2020 22:30:36 +0000 Subject: [PATCH] deal with private IP separately --- add.go | 14 +++----------- configtypes.go | 30 ++++++++++++++++++++---------- init.go | 4 ++-- reporttypes.go | 2 +- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/add.go b/add.go index ddd34ef..b0bb4e1 100644 --- a/add.go +++ b/add.go @@ -1,7 +1,6 @@ package dsnet import ( - "net" "os" "text/template" ) @@ -21,14 +20,7 @@ func Add(hostname string, owner string, description string) { //, publicKey stri PublicKey: publicKey, PrivateKey: privateKey, // omitted from server config JSON! PresharedKey: GenerateJSONKey(), - AllowedIPs: []JSONIPNet{ - JSONIPNet{ - IPNet: net.IPNet{ - IP: IP, - Mask: net.CIDRMask(32, 32), - }, - }, - }, + IP: IP, } conf.MustAddPeer(peer) @@ -38,10 +30,10 @@ func Add(hostname string, owner string, description string) { //, publicKey stri func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) { const peerConf = `[Interface] -Address = {{ index .Peer.AllowedIPs 0 }} +Address = {{ .Peer.IP }} PrivateKey={{ .Peer.PrivateKey.Key }} PresharedKey={{ .Peer.PresharedKey.Key }} -DNS = {{ .DsnetConfig.InternalDNS }} +DNS = {{ .DsnetConfig.DNS }} [Peer] PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }} diff --git a/configtypes.go b/configtypes.go index e929a18..37cd42b 100644 --- a/configtypes.go +++ b/configtypes.go @@ -14,13 +14,15 @@ type PeerConfig struct { Owner string `validate:"required,gte=1,lte=255"` // Description of what the host is and/or does Description string `validate:"required,gte=1,lte=255"` - + // Internal VPN IP address. Added to AllowedIPs in server config as a /32 + IP net.IP `validate:"required,ip` PublicKey JSONKey `validate:"required,len=44"` PrivateKey JSONKey `json:"-"` // omitted from config! PresharedKey JSONKey `validate:"required,len=44"` - // TODO endpoint support - //Endpoint net.UDPAddr `validate:"required,udp4_addr"` - AllowedIPs []JSONIPNet `validate:"dive,required,cidr"` + // TODO ExternalIP support (Endpoint) + //ExternalIP net.UDPAddr `validate:"required,udp4_addr"` + // TODO support routing additional networks (AllowedIPs) + Networks []JSONIPNet `validate:"dive,cidr"` } type DsnetConfig struct { @@ -31,9 +33,9 @@ type DsnetConfig struct { Domain string `validate:"required,gte=1,lte=255"` // IP network from which to allocate automatic sequential addresses // Network is chosen randomly when not specified - Network JSONIPNet `validate:"required"` - InternalIP net.IP `validate:"required,cidr"` - InternalDNS net.IP `validate:"required,cidr"` + Network JSONIPNet `validate:"required"` + IP net.IP `validate:"required,cidr"` + DNS net.IP `validate:"required,cidr"` // TODO Default subnets to route via VPN ReportFile string `validate:"required"` PrivateKey JSONKey `validate:"required,len=44"` @@ -65,7 +67,11 @@ func (conf *DsnetConfig) MustAddPeer(peer PeerConfig) { } } - for _, peerIPNet := range peer.AllowedIPs { + if conf.IPAllocated(peer.IP) { + ExitFail("%s is already allocated", peer.IP) + } + + for _, peerIPNet := range peer.Networks { if conf.IPAllocated(peerIPNet.IPNet.IP) { ExitFail("%s is already allocated", peerIPNet) } @@ -75,12 +81,16 @@ func (conf *DsnetConfig) MustAddPeer(peer PeerConfig) { } func (conf DsnetConfig) IPAllocated(IP net.IP) bool { - if IP.Equal(conf.InternalIP) { + if IP.Equal(conf.IP) { return true } for _, peer := range conf.Peers { - for _, peerIPNet := range peer.AllowedIPs { + if IP.Equal(peer.IP) { + return true + } + + for _, peerIPNet := range peer.Networks { if IP.Equal(peerIPNet.IPNet.IP) { return true } diff --git a/init.go b/init.go index 3915712..3844dc1 100644 --- a/init.go +++ b/init.go @@ -26,8 +26,8 @@ func Init() { } IP := conf.MustAllocateIP() - conf.InternalIP = IP - conf.InternalDNS = IP + conf.IP = IP + conf.DNS = IP conf.MustSave() diff --git a/reporttypes.go b/reporttypes.go index e2371b6..4edc62f 100644 --- a/reporttypes.go +++ b/reporttypes.go @@ -12,7 +12,7 @@ type DsnetReport struct { PrivateKey wgtypes.Key PublicKey wgtypes.Key ListenPort int - Peers []Peer + Peers []PeerReport } type PeerReport struct {