dsnet/up.go

65 lines
1.3 KiB
Go
Raw Normal View History

2020-03-04 20:38:48 +01:00
package dsnet
import (
"net"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/wgctrl"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func Up() {
conf := MustLoadDsnetConfig()
CreateInterface(conf)
}
func CreateInterface(conf *DsnetConfig) {
linkAttrs := netlink.NewLinkAttrs()
linkAttrs.Name = conf.InterfaceName
2020-03-04 20:56:57 +01:00
link := &netlink.GenericLink{
2020-03-04 20:38:48 +01:00
LinkAttrs: linkAttrs,
LinkType: "wireguard",
}
2020-03-04 20:56:57 +01:00
err := netlink.LinkAdd(link)
2020-03-04 20:38:48 +01:00
if err != nil {
2020-03-04 20:56:57 +01:00
ExitFail("Could not add interface '%s' (%v)", linkAttrs.Name, err)
2020-03-04 20:38:48 +01:00
}
addr := &netlink.Addr{
IPNet: &net.IPNet{
IP: conf.IP,
Mask: conf.Network.IPNet.Mask,
},
}
2020-03-04 20:56:57 +01:00
err = netlink.AddrAdd(link, addr)
2020-03-04 20:38:48 +01:00
if err != nil {
ExitFail("Could not add addr %s to interface %s", addr.IP, err)
}
2020-03-04 21:30:05 +01:00
wgConfig := wgtypes.Config{
2020-03-04 20:38:48 +01:00
PrivateKey: &conf.PrivateKey.Key,
ListenPort: &conf.ListenPort,
2020-03-04 21:30:05 +01:00
ReplacePeers: true,
Peers: conf.GetWgPeerConfigs(),
2020-03-04 20:38:48 +01:00
}
wg, err := wgctrl.New()
check(err)
2020-03-04 21:30:05 +01:00
err = wg.ConfigureDevice(linkAttrs.Name, wgConfig)
2020-03-04 20:38:48 +01:00
if err != nil {
ExitFail("Could not configure device '%s' (%v)", linkAttrs.Name, err)
}
2020-03-04 20:56:57 +01:00
// bring up interface (needs conf first)
//err = netlink.LinkSetUp(link)
//
//if err != nil {
// ExitFail("Could not bring up device '%s' (%v)", linkAttrs.Name, err)
//}
2020-03-04 20:38:48 +01:00
}