deal with private IP separately
This commit is contained in:
parent
5cc866fe77
commit
c2aac62f27
12
add.go
12
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),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
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 }}
|
||||
|
@ -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 {
|
||||
@ -32,8 +34,8 @@ type DsnetConfig struct {
|
||||
// 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"`
|
||||
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
|
||||
}
|
||||
|
4
init.go
4
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()
|
||||
|
||||
|
@ -12,7 +12,7 @@ type DsnetReport struct {
|
||||
PrivateKey wgtypes.Key
|
||||
PublicKey wgtypes.Key
|
||||
ListenPort int
|
||||
Peers []Peer
|
||||
Peers []PeerReport
|
||||
}
|
||||
|
||||
type PeerReport struct {
|
||||
|
Loading…
Reference in New Issue
Block a user