From b6bd2d33e1422d46ed253bbe1bcd3d09a251a1e3 Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Wed, 4 Mar 2020 22:50:44 +0000 Subject: [PATCH] move sync fn to sync.go --- sync.go | 24 ++++++++++++++++++++++++ up.go | 21 --------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/sync.go b/sync.go index 18209a9..19c3b1d 100644 --- a/sync.go +++ b/sync.go @@ -1,7 +1,31 @@ package dsnet +import ( + "golang.zx2c4.com/wireguard/wgctrl" + "golang.zx2c4.com/wireguard/wgctrl/wgtypes" +) + func Sync() { // TODO check device settings first conf := MustLoadDsnetConfig() ConfigureDevice(conf) } + +func ConfigureDevice(conf *DsnetConfig) { + wgConfig := wgtypes.Config{ + PrivateKey: &conf.PrivateKey.Key, + ListenPort: &conf.ListenPort, + ReplacePeers: true, + Peers: conf.GetWgPeerConfigs(), + } + + wg, err := wgctrl.New() + check(err) + defer wg.Close() + + err = wg.ConfigureDevice(conf.InterfaceName, wgConfig) + + if err != nil { + ExitFail("Could not configure device '%s' (%v)", conf.InterfaceName, err) + } +} diff --git a/up.go b/up.go index 685fb55..8824f7b 100644 --- a/up.go +++ b/up.go @@ -4,8 +4,6 @@ import ( "net" "github.com/vishvananda/netlink" - "golang.zx2c4.com/wireguard/wgctrl" - "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) func Up() { @@ -47,22 +45,3 @@ func CreateLink(conf *DsnetConfig) { ExitFail("Could not bring up device '%s' (%v)", conf.InterfaceName, err) } } - -func ConfigureDevice(conf *DsnetConfig) { - wgConfig := wgtypes.Config{ - PrivateKey: &conf.PrivateKey.Key, - ListenPort: &conf.ListenPort, - ReplacePeers: true, - Peers: conf.GetWgPeerConfigs(), - } - - wg, err := wgctrl.New() - check(err) - defer wg.Close() - - err = wg.ConfigureDevice(conf.InterfaceName, wgConfig) - - if err != nil { - ExitFail("Could not configure device '%s' (%v)", conf.InterfaceName, err) - } -}