keksvpn/pkg/wgfunc/device.go

69 lines
1.1 KiB
Go
Raw Normal View History

2022-02-27 04:08:06 +00:00
package wgfunc
import "github.com/vishvananda/netlink"
type Device struct {
name string
iPRanges []string
wgDev *netlink.GenericLink
}
func (s *Device) createNetworkDevice() (error) {
la := netlink.NewLinkAttrs()
la.Name = s.name
wgDev := &netlink.GenericLink{
LinkAttrs: la,
LinkType: "wireguard",
}
err := netlink.LinkAdd(wgDev)
if err != nil {
return err
}
s.wgDev = wgDev
return nil
}
func (s *Device) upNetworkDevice() (error) {
err := netlink.LinkSetUp(s.wgDev) //todo: test
if err != nil {
return err
}
return nil
}
func (s *Device) removeNetworkDevice() (error) {
la := netlink.NewLinkAttrs()
la.Name = s.name
wgDev := &netlink.GenericLink{
LinkAttrs: la,
LinkType: "wireguard",
}
err := netlink.LinkDel(wgDev)
return err
}
func (s *WireGuard) addIPToNetwork() (error) {
var ips []*netlink.Addr
for _, iprange := range s.iPRanges {
ip, err := netlink.ParseAddr(iprange)
if err != nil {
return err
}
ips = append(ips, ip)
}
for _, ip := range ips {
err := netlink.AddrAdd(s.wgDev, ip)
if err != nil { //ToDo: Check how to test this case
return err
}
}
return nil
}