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:
		| @@ -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` | | | `--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` | | | `--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` | | | `--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 | ## Running multiple clusters | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										31
									
								
								config.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								config.go
									
									
									
									
									
								
							| @@ -11,21 +11,22 @@ import ( | |||||||
| ) | ) | ||||||
|  |  | ||||||
| type config struct { | type config struct { | ||||||
| 	ClusterKey    []byte   `id:"cluster-key" desc:"shared key for cluster membership; must be 32 bytes base64 encoded; will be generated if not provided"` | 	ClusterKey        []byte   `id:"cluster-key" desc:"shared key for cluster membership; must be 32 bytes base64 encoded; will be generated if not provided"` | ||||||
| 	Join          []string `desc:"comma separated list of hostnames or IP addresses to existing cluster members; if not provided, will attempt resuming any known state or otherwise wait for further members."` | 	Join              []string `desc:"comma separated list of hostnames or IP addresses to existing cluster members; if not provided, will attempt resuming any known state or otherwise wait for further members."` | ||||||
| 	Rejoin        int      `desc:"interval at which join nodes are joined again if away, 0 disables rejoining altogether" default:"0"` | 	Rejoin            int      `desc:"interval at which join nodes are joined again if away, 0 disables rejoining altogether" default:"0"` | ||||||
| 	Init          bool     `desc:"whether to explicitly (re)initialize the cluster; any known state from previous runs will be forgotten"` | 	Init              bool     `desc:"whether to explicitly (re)initialize the cluster; any known state from previous runs will be forgotten"` | ||||||
| 	BindAddr      string   `id:"bind-addr" desc:"IP address to bind to for cluster membership traffic (cannot be used with --bind-iface)"` | 	BindAddr          string   `id:"bind-addr" desc:"IP address to bind to for cluster membership traffic (cannot be used with --bind-iface)"` | ||||||
| 	BindIface     string   `id:"bind-iface" desc:"Interface to bind to for cluster membership traffic (cannot be used with --bind-addr)"` | 	BindIface         string   `id:"bind-iface" desc:"Interface to bind to for cluster membership traffic (cannot be used with --bind-addr)"` | ||||||
| 	ClusterPort   int      `id:"cluster-port" desc:"port used for membership gossip traffic (both TCP and UDP); must be the same across cluster" default:"7946"` | 	ClusterPort       int      `id:"cluster-port" desc:"port used for membership gossip traffic (both TCP and UDP); must be the same across cluster" default:"7946"` | ||||||
| 	WireguardPort int      `id:"wireguard-port" desc:"port used for wireguard traffic (UDP); must be the same across cluster" default:"51820"` | 	WireguardPort     int      `id:"wireguard-port" desc:"port used for wireguard traffic (UDP); must be the same across cluster" default:"51820"` | ||||||
| 	BaseMtu       int      `id:"mtu" desc:"MTU of the underlying network, taking intermediary hops into account" default:"1500"` | 	BaseMtu           int      `id:"mtu" desc:"MTU of the underlying network, taking intermediary hops into account" default:"1500"` | ||||||
| 	OverlayNet    *network `id:"overlay-net" desc:"the network in which to allocate addresses for the overlay mesh network (CIDR format); smaller networks increase the chance of IP collision" default:"10.0.0.0/8"` | 	OverlayNet        *network `id:"overlay-net" desc:"the network in which to allocate addresses for the overlay mesh network (CIDR format); smaller networks increase the chance of IP collision" default:"10.0.0.0/8"` | ||||||
| 	RoutedNet     *network `id:"routed-net" desc:"network used to filter routes that nodes are allowed to announce (CIDR format)" default:"0.0.0.0/32"` | 	RoutedNet         *network `id:"routed-net" desc:"network used to filter routes that nodes are allowed to announce (CIDR format)" default:"0.0.0.0/32"` | ||||||
| 	Interface     string   `desc:"name of the wireguard interface to create and manage" default:"wgoverlay"` | 	Interface         string   `desc:"name of the wireguard interface to create and manage" default:"wgoverlay"` | ||||||
| 	NoEtcHosts    bool     `id:"no-etc-hosts" desc:"disable writing of entries to /etc/hosts"` | 	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"` | 	LogLevel          string   `id:"log-level" desc:"set the verbosity (debug/info/warn/error)" default:"warn"` | ||||||
| 	Version       bool     `desc:"display current version and exit"` | 	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 | 	// for easier local testing; will break etchosts entry | ||||||
| 	UseIPAsName bool `id:"ip-as-name" default:"false" opts:"hidden"` | 	UseIPAsName bool `id:"ip-as-name" default:"false" opts:"hidden"` | ||||||
|   | |||||||
							
								
								
									
										8
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								main.go
									
									
									
									
									
								
							| @@ -39,7 +39,13 @@ func main() { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		logrus.WithError(err).Fatal("could not create cluster") | 		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 { | 	if err != nil { | ||||||
| 		logrus.WithError(err).Fatal("could not instantiate wireguard controller") | 		logrus.WithError(err).Fatal("could not instantiate wireguard controller") | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ import ( | |||||||
| 	"hash/fnv" | 	"hash/fnv" | ||||||
| 	"net" | 	"net" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/costela/wesher/common" | 	"github.com/costela/wesher/common" | ||||||
| 	"github.com/pkg/errors" | 	"github.com/pkg/errors" | ||||||
| @@ -14,19 +15,20 @@ import ( | |||||||
|  |  | ||||||
| // State holds the configured state of a Wesher Wireguard interface | // State holds the configured state of a Wesher Wireguard interface | ||||||
| type State struct { | type State struct { | ||||||
| 	iface       string | 	iface             string | ||||||
| 	client      *wgctrl.Client | 	client            *wgctrl.Client | ||||||
| 	OverlayAddr net.IPNet | 	OverlayAddr       net.IPNet | ||||||
| 	Port        int | 	Port              int | ||||||
| 	Mtu         int | 	Mtu               int | ||||||
| 	PrivKey     wgtypes.Key | 	PrivKey           wgtypes.Key | ||||||
| 	PubKey      wgtypes.Key | 	PubKey            wgtypes.Key | ||||||
|  | 	KeepaliveInterval *time.Duration | ||||||
| } | } | ||||||
|  |  | ||||||
| // New creates a new Wesher Wireguard state | // New creates a new Wesher Wireguard state | ||||||
| // The Wireguard keys are generated for every new interface | // The Wireguard keys are generated for every new interface | ||||||
| // The interface must later be setup using SetUpInterface | // 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() | 	client, err := wgctrl.New() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, nil, errors.Wrap(err, "could not instantiate wireguard client") | 		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() | 	pubKey := privKey.PublicKey() | ||||||
|  |  | ||||||
| 	state := State{ | 	state := State{ | ||||||
| 		iface:   iface, | 		iface:             iface, | ||||||
| 		client:  client, | 		client:            client, | ||||||
| 		Port:    port, | 		Port:              port, | ||||||
| 		Mtu:     mtu, | 		Mtu:               mtu, | ||||||
| 		PrivKey: privKey, | 		PrivKey:           privKey, | ||||||
| 		PubKey:  pubKey, | 		PubKey:            pubKey, | ||||||
|  | 		KeepaliveInterval: keepaliveInterval, | ||||||
| 	} | 	} | ||||||
| 	state.assignOverlayAddr(ipnet, name) | 	state.assignOverlayAddr(ipnet, name) | ||||||
|  |  | ||||||
| @@ -192,7 +195,8 @@ func (s *State) nodesToPeerConfigs(nodes []common.Node) ([]wgtypes.PeerConfig, e | |||||||
| 				IP:   node.Addr, | 				IP:   node.Addr, | ||||||
| 				Port: s.Port, | 				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 | 	return peerCfgs, nil | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user