Merge pull request 'Add keepalive to wesher' (#1) from keepalive into master

Reviewed-on: https://git.weko.io/resilien/wesher/pulls/1
This commit is contained in:
killian 2022-02-08 11:04:26 +01:00
commit b4a5b61255
4 changed files with 43 additions and 31 deletions

View File

@ -145,6 +145,7 @@ All options can be passed either as command-line flags or environment variables:
| `--interface DEV` | WESHER_INTERFACE | name of the wireguard interface to create and manage | `wgoverlay` |
| `--no-etc-hosts` | WESHER_NO_ETC_HOSTS | whether to skip writing hosts entries for each node in mesh | `false` |
| `--log-level LEVEL` | WESHER_LOG_LEVEL | set the verbosity (one of debug/info/warn/error) | `warn` |
| `--keepalive-interval INTERVAL` | WESHER_KEEPALIVE_INTERVAL | interval for which to send keepalive packets | `30s` |
## Running multiple clusters

View File

@ -26,6 +26,7 @@ type config struct {
NoEtcHosts bool `id:"no-etc-hosts" desc:"disable writing of entries to /etc/hosts"`
LogLevel string `id:"log-level" desc:"set the verbosity (debug/info/warn/error)" default:"warn"`
Version bool `desc:"display current version and exit"`
KeepaliveInterval string `id:"keepalive-interval" desc:"interval for which to send keepalive packets" default:"30s"`
// for easier local testing; will break etchosts entry
UseIPAsName bool `id:"ip-as-name" default:"false" opts:"hidden"`

View File

@ -39,7 +39,13 @@ func main() {
if err != nil {
logrus.WithError(err).Fatal("could not create cluster")
}
wgstate, localNode, err := wg.New(config.Interface, config.WireguardPort, config.BaseMtu, (*net.IPNet)(config.OverlayNet), cluster.LocalName)
keepaliveDuration, err := time.ParseDuration(config.KeepaliveInterval)
if err != nil {
logrus.WithError(err).Fatal("could not parse time duration for keepalive")
}
wgstate, localNode, err := wg.New(config.Interface, config.WireguardPort, config.BaseMtu, (*net.IPNet)(config.OverlayNet), cluster.LocalName, &keepaliveDuration)
if err != nil {
logrus.WithError(err).Fatal("could not instantiate wireguard controller")
}

View File

@ -4,6 +4,7 @@ import (
"hash/fnv"
"net"
"os"
"time"
"github.com/costela/wesher/common"
"github.com/pkg/errors"
@ -21,12 +22,13 @@ type State struct {
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")
@ -45,6 +47,7 @@ func New(iface string, port int, mtu int, ipnet *net.IPNet, name string) (*State
Mtu: mtu,
PrivKey: privKey,
PubKey: pubKey,
KeepaliveInterval: keepaliveInterval,
}
state.assignOverlayAddr(ipnet, name)
@ -193,6 +196,7 @@ func (s *State) nodesToPeerConfigs(nodes []common.Node) ([]wgtypes.PeerConfig, e
Port: s.Port,
},
AllowedIPs: append([]net.IPNet{node.OverlayAddr}, node.Routes...),
PersistentKeepaliveInterval: s.KeepaliveInterval,
}
}
return peerCfgs, nil