diff --git a/cluster.go b/cluster.go index d343852..e3dd4df 100644 --- a/cluster.go +++ b/cluster.go @@ -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 diff --git a/config.go b/config.go index e1a1c93..79e2a81 100644 --- a/config.go +++ b/config.go @@ -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 diff --git a/main.go b/main.go index 38abb78..53be385 100644 --- a/main.go +++ b/main.go @@ -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") }