deal with private IP separately

This commit is contained in:
Callan Bryant 2020-03-03 22:30:36 +00:00
parent 5cc866fe77
commit c2aac62f27
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
4 changed files with 26 additions and 24 deletions

12
add.go
View File

@ -1,7 +1,6 @@
package dsnet package dsnet
import ( import (
"net"
"os" "os"
"text/template" "text/template"
) )
@ -21,14 +20,7 @@ func Add(hostname string, owner string, description string) { //, publicKey stri
PublicKey: publicKey, PublicKey: publicKey,
PrivateKey: privateKey, // omitted from server config JSON! PrivateKey: privateKey, // omitted from server config JSON!
PresharedKey: GenerateJSONKey(), PresharedKey: GenerateJSONKey(),
AllowedIPs: []JSONIPNet{
JSONIPNet{
IPNet: net.IPNet{
IP: IP, IP: IP,
Mask: net.CIDRMask(32, 32),
},
},
},
} }
conf.MustAddPeer(peer) conf.MustAddPeer(peer)
@ -38,10 +30,10 @@ func Add(hostname string, owner string, description string) { //, publicKey stri
func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) { func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) {
const peerConf = `[Interface] const peerConf = `[Interface]
Address = {{ index .Peer.AllowedIPs 0 }} Address = {{ .Peer.IP }}
PrivateKey={{ .Peer.PrivateKey.Key }} PrivateKey={{ .Peer.PrivateKey.Key }}
PresharedKey={{ .Peer.PresharedKey.Key }} PresharedKey={{ .Peer.PresharedKey.Key }}
DNS = {{ .DsnetConfig.InternalDNS }} DNS = {{ .DsnetConfig.DNS }}
[Peer] [Peer]
PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }} PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }}

View File

@ -14,13 +14,15 @@ type PeerConfig struct {
Owner string `validate:"required,gte=1,lte=255"` Owner string `validate:"required,gte=1,lte=255"`
// Description of what the host is and/or does // Description of what the host is and/or does
Description string `validate:"required,gte=1,lte=255"` 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"` PublicKey JSONKey `validate:"required,len=44"`
PrivateKey JSONKey `json:"-"` // omitted from config! PrivateKey JSONKey `json:"-"` // omitted from config!
PresharedKey JSONKey `validate:"required,len=44"` PresharedKey JSONKey `validate:"required,len=44"`
// TODO endpoint support // TODO ExternalIP support (Endpoint)
//Endpoint net.UDPAddr `validate:"required,udp4_addr"` //ExternalIP net.UDPAddr `validate:"required,udp4_addr"`
AllowedIPs []JSONIPNet `validate:"dive,required,cidr"` // TODO support routing additional networks (AllowedIPs)
Networks []JSONIPNet `validate:"dive,cidr"`
} }
type DsnetConfig struct { type DsnetConfig struct {
@ -32,8 +34,8 @@ type DsnetConfig struct {
// IP network from which to allocate automatic sequential addresses // IP network from which to allocate automatic sequential addresses
// Network is chosen randomly when not specified // Network is chosen randomly when not specified
Network JSONIPNet `validate:"required"` Network JSONIPNet `validate:"required"`
InternalIP net.IP `validate:"required,cidr"` IP net.IP `validate:"required,cidr"`
InternalDNS net.IP `validate:"required,cidr"` DNS net.IP `validate:"required,cidr"`
// TODO Default subnets to route via VPN // TODO Default subnets to route via VPN
ReportFile string `validate:"required"` ReportFile string `validate:"required"`
PrivateKey JSONKey `validate:"required,len=44"` 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) { if conf.IPAllocated(peerIPNet.IPNet.IP) {
ExitFail("%s is already allocated", peerIPNet) ExitFail("%s is already allocated", peerIPNet)
} }
@ -75,12 +81,16 @@ func (conf *DsnetConfig) MustAddPeer(peer PeerConfig) {
} }
func (conf DsnetConfig) IPAllocated(IP net.IP) bool { func (conf DsnetConfig) IPAllocated(IP net.IP) bool {
if IP.Equal(conf.InternalIP) { if IP.Equal(conf.IP) {
return true return true
} }
for _, peer := range conf.Peers { 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) { if IP.Equal(peerIPNet.IPNet.IP) {
return true return true
} }

View File

@ -26,8 +26,8 @@ func Init() {
} }
IP := conf.MustAllocateIP() IP := conf.MustAllocateIP()
conf.InternalIP = IP conf.IP = IP
conf.InternalDNS = IP conf.DNS = IP
conf.MustSave() conf.MustSave()

View File

@ -12,7 +12,7 @@ type DsnetReport struct {
PrivateKey wgtypes.Key PrivateKey wgtypes.Key
PublicKey wgtypes.Key PublicKey wgtypes.Key
ListenPort int ListenPort int
Peers []Peer Peers []PeerReport
} }
type PeerReport struct { type PeerReport struct {