add option for binding to interface
this effectively still bind to an address, but resolves it based on the interface instead of explicitly.
This commit is contained in:
parent
6c5389e683
commit
2edf641c46
|
@ -132,7 +132,8 @@ All options can be passed either as command-line flags or environment variables:
|
||||||
| `--cluster-key KEY` | WESHER_CLUSTER_KEY | shared key for cluster membership; must be 32 bytes base64 encoded; will be generated if not provided | autogenerated/loaded |
|
| `--cluster-key KEY` | WESHER_CLUSTER_KEY | shared key for cluster membership; must be 32 bytes base64 encoded; will be generated if not provided | autogenerated/loaded |
|
||||||
| `--join HOST,...` | WESHER_JOIN | 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 HOST,...` | WESHER_JOIN | 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 | |
|
||||||
| `--init` | WESHER_INIT | whether to explicitly (re)initialize the cluster; any known state from previous runs will be forgotten | `false` |
|
| `--init` | WESHER_INIT | whether to explicitly (re)initialize the cluster; any known state from previous runs will be forgotten | `false` |
|
||||||
| `--bind-addr ADDR` | WESHER_BIND_ADDR | IP address to bind to for cluster membership | autodetected |
|
| `--bind-addr ADDR` | WESHER_BIND_ADDR | IP address to bind to for cluster membership (cannot be used with --bind-iface) | autodetected |
|
||||||
|
| `--bind-iface IFACE` | WESHER_BIND_IFACE | Interface to bind to for cluster membership (cannot be used with --bind-addr)| |
|
||||||
| `--cluster-port PORT` | WESHER_CLUSTER_PORT | port used for membership gossip traffic (both TCP and UDP); must be the same across cluster | `7946` |
|
| `--cluster-port PORT` | WESHER_CLUSTER_PORT | port used for membership gossip traffic (both TCP and UDP); must be the same across cluster | `7946` |
|
||||||
| `--wireguard-port PORT` | WESHER_WIREGUARD_PORT | port used for wireguard traffic (UDP); must be the same across cluster | `51820` |
|
| `--wireguard-port PORT` | WESHER_WIREGUARD_PORT | port used for wireguard traffic (UDP); must be the same across cluster | `51820` |
|
||||||
| `--overlay-net ADDR/MASK` | WESHER_OVERLAY_NET | the network in which to allocate addresses for the overlay mesh network (CIDR format); smaller networks increase the chance of IP collision | `10.0.0.0/8` |
|
| `--overlay-net ADDR/MASK` | WESHER_OVERLAY_NET | the network in which to allocate addresses for the overlay mesh network (CIDR format); smaller networks increase the chance of IP collision | `10.0.0.0/8` |
|
||||||
|
|
20
cluster.go
20
cluster.go
|
@ -64,10 +64,28 @@ func newCluster(config *config, wg *wgState) (*cluster, error) {
|
||||||
}
|
}
|
||||||
state.ClusterKey = clusterKey
|
state.ClusterKey = clusterKey
|
||||||
|
|
||||||
|
// we check for mutual exclusion in config.go
|
||||||
|
bindAddr := config.BindAddr
|
||||||
|
if config.BindIface != "" {
|
||||||
|
iface, err := net.InterfaceByName(config.BindIface)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
addrs, err := iface.Addrs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(addrs) > 0 {
|
||||||
|
if addr, ok := addrs[0].(*net.IPNet); ok {
|
||||||
|
bindAddr = addr.IP.String()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mlConfig := memberlist.DefaultWANConfig()
|
mlConfig := memberlist.DefaultWANConfig()
|
||||||
mlConfig.LogOutput = logrus.StandardLogger().WriterLevel(logrus.DebugLevel)
|
mlConfig.LogOutput = logrus.StandardLogger().WriterLevel(logrus.DebugLevel)
|
||||||
mlConfig.SecretKey = clusterKey
|
mlConfig.SecretKey = clusterKey
|
||||||
mlConfig.BindAddr = config.BindAddr
|
mlConfig.BindAddr = bindAddr
|
||||||
mlConfig.BindPort = config.ClusterPort
|
mlConfig.BindPort = config.ClusterPort
|
||||||
mlConfig.AdvertisePort = config.ClusterPort
|
mlConfig.AdvertisePort = config.ClusterPort
|
||||||
if config.UseIPAsName && config.BindAddr != "0.0.0.0" {
|
if config.UseIPAsName && config.BindAddr != "0.0.0.0" {
|
||||||
|
|
|
@ -14,7 +14,8 @@ 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."`
|
||||||
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"`
|
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)"`
|
||||||
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"`
|
||||||
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"`
|
||||||
|
@ -44,7 +45,9 @@ func loadConfig() (*config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: this is a workaround for memberlist refusing to listen on public IPs if BindAddr==0.0.0.0
|
// FIXME: this is a workaround for memberlist refusing to listen on public IPs if BindAddr==0.0.0.0
|
||||||
if config.BindAddr == "" {
|
if config.BindAddr != "" && config.BindIface != "" {
|
||||||
|
return nil, fmt.Errorf("setting both bind address and bind interface is not supported")
|
||||||
|
} else if config.BindAddr == "" && config.BindIface == "" {
|
||||||
detectedBindAddr, err := sockaddr.GetPublicIP()
|
detectedBindAddr, err := sockaddr.GetPublicIP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue