move sync fn to sync.go

This commit is contained in:
Callan Bryant 2020-03-04 22:50:44 +00:00
parent 5a71debc59
commit b6bd2d33e1
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
2 changed files with 24 additions and 21 deletions

24
sync.go
View File

@ -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)
}
}

21
up.go
View File

@ -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)
}
}