diff --git a/command/agent/agent.go b/command/agent/agent.go index b9b4157541..5d8b663b02 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -305,7 +305,7 @@ func (a *Agent) consulConfig() (*consul.Config, error) { } a.config.AdvertiseAddr = ipStr - case a.config.BindAddr != "0.0.0.0" && a.config.BindAddr != "" && a.config.BindAddr != "[::]": + case a.config.BindAddr != "" && !isAddrANY(a.config.BindAddr): a.config.AdvertiseAddr = a.config.BindAddr default: diff --git a/command/agent/command.go b/command/agent/command.go index 199f057b93..682a52474b 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -371,12 +371,12 @@ func (c *Command) readConfig() *Config { return nil } - if config.AdvertiseAddr == "0.0.0.0" || config.AdvertiseAddr == "::" || config.AdvertiseAddr == "[::]" { + if isAddrANY(config.AdvertiseAddr) { c.UI.Error("Advertise address cannot be " + config.AdvertiseAddr) return nil } - if config.AdvertiseAddrWan == "0.0.0.0" || config.AdvertiseAddrWan == "::" || config.AdvertiseAddrWan == "[::]" { + if isAddrANY(config.AdvertiseAddrWan) { c.UI.Error("Advertise WAN address cannot be " + config.AdvertiseAddrWan) return nil } diff --git a/command/agent/config.go b/command/agent/config.go index 2e07a10d86..3329ef0c8a 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -1968,3 +1968,23 @@ func (d dirEnts) Less(i, j int) bool { func (d dirEnts) Swap(i, j int) { d[i], d[j] = d[j], d[i] } + +// isAddrANY checks if the given ip address is an IPv4 or IPv6 ANY address. ip +// can be either a *net.IP or a string. It panics on another type. +func isAddrANY(ip interface{}) bool { + if ip == nil { + return false + } + var ips string + switch x := ip.(type) { + case net.IP: + ips = x.String() + case *net.IP: + ips = x.String() + case string: + ips = x + default: + panic(fmt.Sprintf("invalid type: %T", ip)) + } + return ips == "0.0.0.0" || ips == "::" || ips == "[::]" +}