support additional networks

This commit is contained in:
Callan Bryant 2020-03-08 20:48:10 +00:00
parent cf467c346b
commit d250f2b23b
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
3 changed files with 13 additions and 1 deletions

11
add.go
View File

@ -3,6 +3,7 @@ package dsnet
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"text/template" "text/template"
"time" "time"
) )
@ -49,6 +50,13 @@ func Add() {
} }
func PrintPeerCfg(peer PeerConfig, conf *DsnetConfig) { 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] const peerConf = `[Interface]
Address = {{ .Peer.IP }} Address = {{ .Peer.IP }}
PrivateKey={{ .Peer.PrivateKey.Key }} PrivateKey={{ .Peer.PrivateKey.Key }}
@ -58,7 +66,7 @@ DNS = {{ .DsnetConfig.DNS }}
PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }} PublicKey={{ .DsnetConfig.PrivateKey.PublicKey.Key }}
PresharedKey={{ .Peer.PresharedKey.Key }} PresharedKey={{ .Peer.PresharedKey.Key }}
Endpoint={{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }} Endpoint={{ .DsnetConfig.ExternalIP }}:{{ .DsnetConfig.ListenPort }}
AllowedIPs={{ .DsnetConfig.Network }} AllowedIPs={{ .AllowedIPs }}
PersistentKeepalive={{ .Keepalive }} PersistentKeepalive={{ .Keepalive }}
` `
@ -67,6 +75,7 @@ PersistentKeepalive={{ .Keepalive }}
"Peer": peer, "Peer": peer,
"DsnetConfig": conf, "DsnetConfig": conf,
"Keepalive": time.Duration(KEEPALIVE).Seconds(), "Keepalive": time.Duration(KEEPALIVE).Seconds(),
"AllowedIPs": strings.Join(allowedIPsStr, ","),
}) })
check(err) check(err)
} }

View File

@ -45,6 +45,8 @@ type DsnetConfig struct {
ReportFile string `validate:"required"` ReportFile string `validate:"required"`
PrivateKey JSONKey `validate:"required,len=44"` PrivateKey JSONKey `validate:"required,len=44"`
Peers []PeerConfig `validate:"dive"` Peers []PeerConfig `validate:"dive"`
// extra networks available, will be added to AllowedIPs
Networks []JSONIPNet `validate:"required"`
} }
func MustLoadDsnetConfig() *DsnetConfig { func MustLoadDsnetConfig() *DsnetConfig {

View File

@ -27,6 +27,7 @@ func Init() {
ReportFile: DEFAULT_REPORT_FILE, ReportFile: DEFAULT_REPORT_FILE,
ExternalIP: getExternalIP(), ExternalIP: getExternalIP(),
InterfaceName: DEFAULT_INTERFACE_NAME, InterfaceName: DEFAULT_INTERFACE_NAME,
Networks: []JSONIPNet{},
} }
IP := conf.MustAllocateIP() IP := conf.MustAllocateIP()