2020-03-04 23:06:15 +01:00
|
|
|
package dsnet
|
|
|
|
|
2020-03-04 23:50:44 +01:00
|
|
|
import (
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
|
|
|
|
2020-03-04 23:12:31 +01:00
|
|
|
func Sync() {
|
2020-03-04 23:06:15 +01:00
|
|
|
// TODO check device settings first
|
|
|
|
conf := MustLoadDsnetConfig()
|
|
|
|
ConfigureDevice(conf)
|
|
|
|
}
|
2020-03-04 23:50:44 +01:00
|
|
|
|
|
|
|
func ConfigureDevice(conf *DsnetConfig) {
|
2020-03-07 19:45:50 +01:00
|
|
|
peers := conf.GetWgPeerConfigs()
|
|
|
|
|
2020-03-04 23:50:44 +01:00
|
|
|
wgConfig := wgtypes.Config{
|
2020-03-08 15:10:38 +01:00
|
|
|
PrivateKey: &conf.PrivateKey.Key,
|
|
|
|
ListenPort: &conf.ListenPort,
|
|
|
|
ReplacePeers: true,
|
2020-03-07 19:45:50 +01:00
|
|
|
Peers: peers,
|
2020-03-04 23:50:44 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 15:10:38 +01:00
|
|
|
wg, err := wgctrl.New()
|
|
|
|
check(err)
|
|
|
|
defer wg.Close()
|
|
|
|
|
2020-03-04 23:50:44 +01:00
|
|
|
err = wg.ConfigureDevice(conf.InterfaceName, wgConfig)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ExitFail("Could not configure device '%s' (%v)", conf.InterfaceName, err)
|
|
|
|
}
|
|
|
|
}
|