From ec547d5b999693356bcb6309b02d545411261748 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 15 May 2014 11:27:30 -0700 Subject: [PATCH] command/agent: find proper private IP on Windows /cc @armon --- CHANGELOG.md | 19 +++++++++---------- command/agent/agent.go | 2 +- consul/util.go | 20 ++++++++++++++------ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11338b11d..d49e01ce9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,21 +1,20 @@ ## 0.2.1 (unreleased) -FEATURES: - -BUG FIXES: - - * Renaming "seperator" to "separator". This is the correct spelling, - but both spellings are respected for backwards compatibility. [GH-101] - IMPROVEMENTS: * Improved the URL formatting for the key/value editor in the Web UI. - Importantly, the editor now allows editing keys with dashes in the - name. [GH-119] + Importantly, the editor now allows editing keys with dashes in the + name. [GH-119] * The web UI now has cancel and delete folder actions in the key/value - editor. [GH-124], [GH-122] + editor. [GH-124], [GH-122] * Add flag to agent to write pid to a file. [GH-106] +BUG FIXES: + + * Renaming "seperator" to "separator". This is the correct spelling, + but both spellings are respected for backwards compatibility. [GH-101] + * Private IP is properly found on Windows clients. + ## 0.2.0 (May 1, 2014) FEATURES: diff --git a/command/agent/agent.go b/command/agent/agent.go index 51580c28c..e96ed228e 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -79,7 +79,7 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) { if err != nil { return nil, fmt.Errorf("Failed to get advertise address: %v", err) } - config.AdvertiseAddr = ip.IP.String() + config.AdvertiseAddr = ip.String() } agent := &Agent{ diff --git a/consul/util.go b/consul/util.go index f2c29d40d..977d38a04 100644 --- a/consul/util.go +++ b/consul/util.go @@ -106,26 +106,34 @@ func isPrivateIP(ip_str string) bool { // GetPrivateIP is used to return the first private IP address // associated with an interface on the machine -func GetPrivateIP() (*net.IPNet, error) { +func GetPrivateIP() (net.IP, error) { addresses, err := net.InterfaceAddrs() if err != nil { return nil, fmt.Errorf("Failed to get interface addresses: %v", err) } // Find private IPv4 address - for _, addr := range addresses { - ip, ok := addr.(*net.IPNet) - if !ok { + for _, rawAddr := range addresses { + var ip net.IP + switch addr := rawAddr.(type) { + case *net.IPAddr: + ip = addr.IP + case *net.IPNet: + ip = addr.IP + default: continue } - if ip.IP.To4() == nil { + + if ip.To4() == nil { continue } - if !isPrivateIP(ip.IP.String()) { + if !isPrivateIP(ip.String()) { continue } + return ip, nil } + return nil, fmt.Errorf("No private IP address found") }