From d250f2b23bf922fa955c085522b2b36b0fe97996 Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Sun, 8 Mar 2020 20:48:10 +0000 Subject: [PATCH] support additional networks --- add.go | 11 ++++++++++- configtypes.go | 2 ++ init.go | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/add.go b/add.go index 3d305fd..1944f51 100644 --- a/add.go +++ b/add.go @@ -3,6 +3,7 @@ package dsnet import ( "fmt" "os" + "strings" "text/template" "time" ) @@ -49,6 +50,13 @@ func Add() { } func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) { + allowedIPsStr := make([]string, len(conf.Networks)+1) + allowedIPsStr[0] = conf.Network.String() + + for i, net := range conf.Networks { + allowedIPsStr[i+1] = net.String() + } + const peerConf = `[Interface] Address = {{ .Peer.IP }} PrivateKey={{ .Peer.PrivateKey.Key }} @@ -58,7 +66,7 @@ DNS = {{ .DsnetConfig.DNS }} PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }} PresharedKey={{ .Peer.PresharedKey.Key }} Endpoint={{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }} -AllowedIPs={{ .DsnetConfig.Network }} +AllowedIPs={{ .AllowedIPs }} PersistentKeepalive={{ .Keepalive }} ` @@ -67,6 +75,7 @@ PersistentKeepalive={{ .Keepalive }} "Peer": peer, "DsnetConfig": conf, "Keepalive": time.Duration(KEEPALIVE).Seconds(), + "AllowedIPs": strings.Join(allowedIPsStr, ","), }) check(err) } diff --git a/configtypes.go b/configtypes.go index ec0aa30..937953c 100644 --- a/configtypes.go +++ b/configtypes.go @@ -45,6 +45,8 @@ type DsnetConfig struct { ReportFile string `validate:"required"` PrivateKey JSONKey `validate:"required,len=44"` Peers []PeerConfig `validate:"dive"` + // extra networks available, will be added to AllowedIPs + Networks []JSONIPNet `validate:"required"` } func MustLoadDsnetConfig() *DsnetConfig { diff --git a/init.go b/init.go index 211859f..ef73d1f 100644 --- a/init.go +++ b/init.go @@ -27,6 +27,7 @@ func Init() { ReportFile: DEFAULT_REPORT_FILE, ExternalIP: getExternalIP(), InterfaceName: DEFAULT_INTERFACE_NAME, + Networks: []JSONIPNet{}, } IP := conf.MustAllocateIP()