separate link creation / device configuration

This commit is contained in:
Callan Bryant 2020-03-04 22:01:30 +00:00
parent ccc45f8368
commit f00e344a72
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
2 changed files with 20 additions and 5 deletions

View File

@ -85,6 +85,18 @@ func (conf *DsnetConfig) MustAddPeer(peer PeerConfig) {
} }
} }
for _, p := range conf.Peers {
if peer.PublicKey.Key == p.PublicKey.Key {
ExitFail("%s is not an unique public key", peer.Hostname)
}
}
for _, p := range conf.Peers {
if peer.PresharedKey.Key == p.PresharedKey.Key {
ExitFail("%s is not an unique preshared key", peer.Hostname)
}
}
if conf.IPAllocated(peer.IP) { if conf.IPAllocated(peer.IP) {
ExitFail("%s is already allocated", peer.IP) ExitFail("%s is already allocated", peer.IP)
} }

13
up.go
View File

@ -10,10 +10,11 @@ import (
func Up() { func Up() {
conf := MustLoadDsnetConfig() conf := MustLoadDsnetConfig()
CreateInterface(conf) CreateLink(conf)
ConfigureDevice(conf)
} }
func CreateInterface(conf *DsnetConfig) { func CreateLink(conf *DsnetConfig) {
linkAttrs := netlink.NewLinkAttrs() linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = conf.InterfaceName linkAttrs.Name = conf.InterfaceName
@ -39,6 +40,11 @@ func CreateInterface(conf *DsnetConfig) {
ExitFail("Could not add addr %s to interface %s", addr.IP, err) ExitFail("Could not add addr %s to interface %s", addr.IP, err)
} }
// bring up interface (UNKNOWN state instead of UP, a wireguard quirk)
err = netlink.LinkSetUp(link)
}
func ConfigureDevice(conf *DsnetConfig) {
wgConfig := wgtypes.Config{ wgConfig := wgtypes.Config{
PrivateKey: &conf.PrivateKey.Key, PrivateKey: &conf.PrivateKey.Key,
ListenPort: &conf.ListenPort, ListenPort: &conf.ListenPort,
@ -56,9 +62,6 @@ func CreateInterface(conf *DsnetConfig) {
ExitFail("Could not configure device '%s' (%v)", conf.InterfaceName, err) ExitFail("Could not configure device '%s' (%v)", conf.InterfaceName, err)
} }
// bring up interface (UNKNOWN state, a wireguard thing)
err = netlink.LinkSetUp(link)
if err != nil { if err != nil {
ExitFail("Could not bring up device '%s' (%v)", conf.InterfaceName, err) ExitFail("Could not bring up device '%s' (%v)", conf.InterfaceName, err)
} }