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 c54f53eaf7
commit ec547d5b99
3 changed files with 24 additions and 17 deletions

View File

@ -1,12 +1,5 @@
## 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.
@ -16,6 +9,12 @@ IMPROVEMENTS:
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:

View File

@ -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{

View File

@ -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")
}