Move the bindAddress computation to config parsing

This commit is contained in:
kaiyou 2020-05-07 10:13:23 +02:00 committed by Leo Antunes
parent 9fdab65237
commit bce8a0451a
3 changed files with 19 additions and 28 deletions

View File

@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"os"
"path"
"time"
@ -33,7 +32,7 @@ type cluster struct {
const statePath = "/var/lib/wesher/state.json"
func newCluster(init bool, clusterKey []byte, bindAddr string, bindIface string, bindPort int, useIPAsName bool, getMeta func(int) []byte) (*cluster, error) {
func newCluster(init bool, clusterKey []byte, bindAddr string, bindPort int, useIPAsName bool, getMeta func(int) []byte) (*cluster, error) {
state := &ClusterState{}
if !init {
loadState(state)
@ -44,11 +43,6 @@ func newCluster(init bool, clusterKey []byte, bindAddr string, bindIface string,
return nil, err
}
bindAddr, err = computeBindAddr(bindAddr, bindIface)
if err != nil {
return nil, err
}
mlConfig := memberlist.DefaultWANConfig()
mlConfig.LogOutput = logrus.StandardLogger().WriterLevel(logrus.DebugLevel)
mlConfig.SecretKey = clusterKey
@ -175,25 +169,6 @@ func computeClusterKey(state *ClusterState, clusterKey []byte) ([]byte, error) {
return clusterKey, nil
}
func computeBindAddr(bindAddr string, bindIface string) (string, error) {
if bindIface != "" {
iface, err := net.InterfaceByName(bindIface)
if err != nil {
return "", err
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
if len(addrs) > 0 {
if addr, ok := addrs[0].(*net.IPNet); ok {
bindAddr = addr.IP.String()
}
}
}
return bindAddr, nil
}
func (c *cluster) saveState() error {
if err := os.MkdirAll(path.Dir(statePath), 0700); err != nil {
return err

View File

@ -44,10 +44,26 @@ func loadConfig() (*config, error) {
return nil, fmt.Errorf("unsupported overlay network size; net mask must be multiple of 8, got %d", bits)
}
// FIXME: this is a workaround for memberlist refusing to listen on public IPs if BindAddr==0.0.0.0
if config.BindAddr != "" && config.BindIface != "" {
return nil, fmt.Errorf("setting both bind address and bind interface is not supported")
} else if config.BindIface != "" {
// Compute the actual bind address based on the provided interface
iface, err := net.InterfaceByName(config.BindIface)
if err != nil {
return nil, fmt.Errorf("cannot find interface %s", config.BindIface)
}
addrs, err := iface.Addrs()
if err != nil {
return nil, fmt.Errorf("no available ip address on interface %s", config.BindIface)
}
if len(addrs) > 0 {
if addr, ok := addrs[0].(*net.IPNet); ok {
config.BindAddr = addr.IP.String()
}
}
} else if config.BindAddr == "" && config.BindIface == "" {
// FIXME: this is a workaround for memberlist refusing to listen on public IPs if BindAddr==0.0.0.0
detectedBindAddr, err := sockaddr.GetPublicIP()
if err != nil {
return nil, err

View File

@ -42,7 +42,7 @@ func main() {
}, limit)
}
cluster, err := newCluster(config.Init, config.ClusterKey, config.BindAddr, config.BindIface, config.ClusterPort, config.UseIPAsName, getMeta)
cluster, err := newCluster(config.Init, config.ClusterKey, config.BindAddr, config.ClusterPort, config.UseIPAsName, getMeta)
if err != nil {
logrus.WithError(err).Fatal("could not create cluster")
}