2020-05-07 10:25:58 +02:00
|
|
|
package wg
|
2019-03-25 01:02:10 +01:00
|
|
|
|
|
|
|
import (
|
2019-03-28 20:27:08 +01:00
|
|
|
"hash/fnv"
|
2019-03-25 01:02:10 +01:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
"github.com/costela/wesher/common"
|
2019-07-12 11:08:14 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
|
|
)
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
type WgState struct {
|
2019-03-25 01:02:10 +01:00
|
|
|
iface string
|
2019-07-12 11:08:14 +02:00
|
|
|
client *wgctrl.Client
|
|
|
|
OverlayAddr net.IPNet
|
2019-03-25 01:02:10 +01:00
|
|
|
Port int
|
2019-07-12 11:08:14 +02:00
|
|
|
PrivKey wgtypes.Key
|
|
|
|
PubKey wgtypes.Key
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
func NewWGConfig(iface string, port int) (*WgState, error) {
|
2019-07-12 11:08:14 +02:00
|
|
|
client, err := wgctrl.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "could not instantiate wireguard client")
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 11:08:14 +02:00
|
|
|
privKey, err := wgtypes.GeneratePrivateKey()
|
2019-03-25 01:02:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
pubKey := privKey.PublicKey()
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
wgState := WgState{
|
2019-03-25 01:02:10 +01:00
|
|
|
iface: iface,
|
2019-07-12 11:08:14 +02:00
|
|
|
client: client,
|
2019-03-25 01:02:10 +01:00
|
|
|
Port: port,
|
|
|
|
PrivKey: privKey,
|
|
|
|
PubKey: pubKey,
|
|
|
|
}
|
|
|
|
return &wgState, nil
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
func (wg *WgState) AssignOverlayAddr(ipnet *net.IPNet, name string) {
|
2019-03-25 01:02:10 +01:00
|
|
|
// TODO: this is way too brittle and opaque
|
|
|
|
bits, size := ipnet.Mask.Size()
|
2019-03-29 22:15:17 +01:00
|
|
|
ip := make([]byte, len(ipnet.IP))
|
2019-03-28 20:27:08 +01:00
|
|
|
copy(ip, []byte(ipnet.IP))
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2019-03-28 20:27:08 +01:00
|
|
|
h := fnv.New128a()
|
2019-03-25 01:02:10 +01:00
|
|
|
h.Write([]byte(name))
|
|
|
|
hb := h.Sum(nil)
|
|
|
|
|
2019-03-28 20:27:08 +01:00
|
|
|
for i := 1; i <= (size-bits)/8; i++ {
|
|
|
|
ip[len(ip)-i] = hb[len(hb)-i]
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
2019-03-28 20:27:08 +01:00
|
|
|
|
2019-07-12 11:08:14 +02:00
|
|
|
wg.OverlayAddr = net.IPNet{
|
|
|
|
IP: net.IP(ip),
|
|
|
|
Mask: net.CIDRMask(size, size), // either /32 or /128, depending if ipv4 or ipv6
|
|
|
|
}
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
func (wg *WgState) DownInterface() error {
|
2019-07-12 11:08:14 +02:00
|
|
|
if _, err := wg.client.Device(wg.iface); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil // device already gone; noop
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
link, err := netlink.LinkByName(wg.iface)
|
2019-03-25 01:02:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
return netlink.LinkDel(link)
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
func (wg *WgState) SetUpInterface(nodes []common.Node) error {
|
2019-07-14 12:59:54 +02:00
|
|
|
if err := netlink.LinkAdd(&wireguard{LinkAttrs: netlink.LinkAttrs{Name: wg.iface}}); err != nil && !os.IsExist(err) {
|
|
|
|
return errors.Wrapf(err, "could not create interface %s", wg.iface)
|
2019-07-12 11:08:14 +02:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
peerCfgs, err := wg.NodesToPeerConfigs(nodes)
|
2019-07-12 11:08:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error converting received node information to wireguard format")
|
|
|
|
}
|
2019-07-29 19:20:25 +02:00
|
|
|
if err := wg.client.ConfigureDevice(wg.iface, wgtypes.Config{
|
2019-07-12 11:08:14 +02:00
|
|
|
PrivateKey: &wg.PrivKey,
|
|
|
|
ListenPort: &wg.Port,
|
|
|
|
ReplacePeers: true,
|
|
|
|
Peers: peerCfgs,
|
2019-07-29 19:20:25 +02:00
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not set wireguard configuration for %s", wg.iface)
|
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
|
|
|
|
link, err := netlink.LinkByName(wg.iface)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get link information for %s", wg.iface)
|
|
|
|
}
|
2019-07-29 19:20:25 +02:00
|
|
|
if err := netlink.AddrReplace(link, &netlink.Addr{
|
2019-07-12 11:08:14 +02:00
|
|
|
IPNet: &wg.OverlayAddr,
|
2019-07-29 19:20:25 +02:00
|
|
|
}); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not set address for %s", wg.iface)
|
|
|
|
}
|
|
|
|
// TODO: make MTU configurable?
|
|
|
|
if err := netlink.LinkSetMTU(link, 1420); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not set MTU for %s", wg.iface)
|
|
|
|
}
|
|
|
|
if err := netlink.LinkSetUp(link); err != nil {
|
|
|
|
return errors.Wrapf(err, "could not enable interface %s", wg.iface)
|
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
for _, node := range nodes {
|
|
|
|
netlink.RouteAdd(&netlink.Route{
|
|
|
|
LinkIndex: link.Attrs().Index,
|
|
|
|
Dst: &node.OverlayAddr,
|
|
|
|
Scope: netlink.SCOPE_LINK,
|
|
|
|
})
|
2019-03-27 22:48:54 +01:00
|
|
|
}
|
2019-03-25 01:02:10 +01:00
|
|
|
|
2019-07-12 11:08:14 +02:00
|
|
|
return nil
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:25:58 +02:00
|
|
|
func (wg *WgState) NodesToPeerConfigs(nodes []common.Node) ([]wgtypes.PeerConfig, error) {
|
2019-07-12 11:08:14 +02:00
|
|
|
peerCfgs := make([]wgtypes.PeerConfig, len(nodes))
|
|
|
|
for i, node := range nodes {
|
|
|
|
pubKey, err := wgtypes.ParseKey(node.PubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
peerCfgs[i] = wgtypes.PeerConfig{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
ReplaceAllowedIPs: true,
|
|
|
|
Endpoint: &net.UDPAddr{
|
|
|
|
IP: node.Addr,
|
|
|
|
Port: wg.Port,
|
|
|
|
},
|
|
|
|
AllowedIPs: []net.IPNet{
|
|
|
|
node.OverlayAddr,
|
|
|
|
},
|
|
|
|
}
|
2019-03-25 01:02:10 +01:00
|
|
|
}
|
2019-07-12 11:08:14 +02:00
|
|
|
return peerCfgs, nil
|
|
|
|
}
|