command/agent: find proper private IP on Windows

/cc @armon
This commit is contained in:
Mitchell Hashimoto 2014-05-15 11:27:30 -07:00
parent 03a98ed1d7
commit 78b7c57ba3
3 changed files with 24 additions and 17 deletions

View File

@ -1,21 +1,20 @@
## 0.2.1 (unreleased) ## 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: IMPROVEMENTS:
* Improved the URL formatting for the key/value editor in the Web UI. * Improved the URL formatting for the key/value editor in the Web UI.
Importantly, the editor now allows editing keys with dashes in the Importantly, the editor now allows editing keys with dashes in the
name. [GH-119] name. [GH-119]
* The web UI now has cancel and delete folder actions in the key/value * 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] * 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) ## 0.2.0 (May 1, 2014)
FEATURES: FEATURES:

View File

@ -79,7 +79,7 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to get advertise address: %v", err) return nil, fmt.Errorf("Failed to get advertise address: %v", err)
} }
config.AdvertiseAddr = ip.IP.String() config.AdvertiseAddr = ip.String()
} }
agent := &Agent{ agent := &Agent{

View File

@ -106,26 +106,34 @@ func isPrivateIP(ip_str string) bool {
// GetPrivateIP is used to return the first private IP address // GetPrivateIP is used to return the first private IP address
// associated with an interface on the machine // associated with an interface on the machine
func GetPrivateIP() (*net.IPNet, error) { func GetPrivateIP() (net.IP, error) {
addresses, err := net.InterfaceAddrs() addresses, err := net.InterfaceAddrs()
if err != nil { if err != nil {
return nil, fmt.Errorf("Failed to get interface addresses: %v", err) return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
} }
// Find private IPv4 address // Find private IPv4 address
for _, addr := range addresses { for _, rawAddr := range addresses {
ip, ok := addr.(*net.IPNet) var ip net.IP
if !ok { switch addr := rawAddr.(type) {
case *net.IPAddr:
ip = addr.IP
case *net.IPNet:
ip = addr.IP
default:
continue continue
} }
if ip.IP.To4() == nil {
if ip.To4() == nil {
continue continue
} }
if !isPrivateIP(ip.IP.String()) { if !isPrivateIP(ip.String()) {
continue continue
} }
return ip, nil return ip, nil
} }
return nil, fmt.Errorf("No private IP address found") return nil, fmt.Errorf("No private IP address found")
} }