Add keepalive to wesher

(cherry picked from commit 982cac3b1a69d5759eaf477785301a564b551bc5)
This commit is contained in:
Jacob McSwain
2020-11-12 14:03:31 -06:00
committed by Killian Kemps
parent fd8b63c291
commit 480cbb45f5
4 changed files with 43 additions and 31 deletions

View File

@ -4,6 +4,7 @@ import (
"hash/fnv"
"net"
"os"
"time"
"github.com/costela/wesher/common"
"github.com/pkg/errors"
@ -14,19 +15,20 @@ import (
// State holds the configured state of a Wesher Wireguard interface
type State struct {
iface string
client *wgctrl.Client
OverlayAddr net.IPNet
Port int
Mtu int
PrivKey wgtypes.Key
PubKey wgtypes.Key
iface string
client *wgctrl.Client
OverlayAddr net.IPNet
Port int
Mtu int
PrivKey wgtypes.Key
PubKey wgtypes.Key
KeepaliveInterval *time.Duration
}
// New creates a new Wesher Wireguard state
// The Wireguard keys are generated for every new interface
// The interface must later be setup using SetUpInterface
func New(iface string, port int, mtu int, ipnet *net.IPNet, name string) (*State, *common.Node, error) {
func New(iface string, port int, mtu int, ipnet *net.IPNet, name string, keepaliveInterval *time.Duration) (*State, *common.Node, error) {
client, err := wgctrl.New()
if err != nil {
return nil, nil, errors.Wrap(err, "could not instantiate wireguard client")
@ -39,12 +41,13 @@ func New(iface string, port int, mtu int, ipnet *net.IPNet, name string) (*State
pubKey := privKey.PublicKey()
state := State{
iface: iface,
client: client,
Port: port,
Mtu: mtu,
PrivKey: privKey,
PubKey: pubKey,
iface: iface,
client: client,
Port: port,
Mtu: mtu,
PrivKey: privKey,
PubKey: pubKey,
KeepaliveInterval: keepaliveInterval,
}
state.assignOverlayAddr(ipnet, name)
@ -192,7 +195,8 @@ func (s *State) nodesToPeerConfigs(nodes []common.Node) ([]wgtypes.PeerConfig, e
IP: node.Addr,
Port: s.Port,
},
AllowedIPs: append([]net.IPNet{node.OverlayAddr}, node.Routes...),
AllowedIPs: append([]net.IPNet{node.OverlayAddr}, node.Routes...),
PersistentKeepaliveInterval: s.KeepaliveInterval,
}
}
return peerCfgs, nil