2021-05-01 12:45:37 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/vishvananda/netlink"
|
2021-06-06 00:40:44 +02:00
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
|
|
|
|
2021-05-01 12:45:37 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2021-05-15 12:05:15 +02:00
|
|
|
//const (
|
|
|
|
// interfacePrefix = "wg"
|
|
|
|
//)
|
2021-05-01 12:45:37 +02:00
|
|
|
|
|
|
|
// assignAddr Adds IP address to the tunnel interface
|
2021-06-06 00:40:44 +02:00
|
|
|
func assignAddr(address string, tunDevice tun.Device) error {
|
|
|
|
var err error
|
2021-05-01 12:45:37 +02:00
|
|
|
attrs := netlink.NewLinkAttrs()
|
2021-06-06 00:40:44 +02:00
|
|
|
attrs.Name, err = tunDevice.Name()
|
2021-05-01 12:45:37 +02:00
|
|
|
|
|
|
|
link := wgLink{
|
|
|
|
attrs: &attrs,
|
|
|
|
}
|
|
|
|
|
2021-06-06 00:40:44 +02:00
|
|
|
log.Debugf("adding address %s to interface: %s", address, attrs.Name)
|
2021-05-01 12:45:37 +02:00
|
|
|
addr, _ := netlink.ParseAddr(address)
|
2021-06-06 00:40:44 +02:00
|
|
|
err = netlink.AddrAdd(&link, addr)
|
2021-05-01 12:45:37 +02:00
|
|
|
if os.IsExist(err) {
|
2021-06-06 00:40:44 +02:00
|
|
|
log.Infof("interface %s already has the address: %s", attrs.Name, address)
|
2021-05-01 12:45:37 +02:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// On linux, the link must be brought up
|
|
|
|
err = netlink.LinkSetUp(&link)
|
|
|
|
return err
|
|
|
|
}
|
2021-06-06 00:40:44 +02:00
|
|
|
|
|
|
|
type wgLink struct {
|
|
|
|
attrs *netlink.LinkAttrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attrs returns the Wireguard's default attributes
|
|
|
|
func (w *wgLink) Attrs() *netlink.LinkAttrs {
|
|
|
|
return w.attrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the interface type
|
|
|
|
func (w *wgLink) Type() string {
|
|
|
|
return "wireguard"
|
|
|
|
}
|