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

14
add.go
View File

@ -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 }}

View File

@ -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
}

View File

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

View File

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