-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2544 from typhoonzero/pserver_etcd
Pserver etcd registration
- Loading branch information
Showing
6 changed files
with
221 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package networkhelper | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
) | ||
|
||
// GetExternalIP returns the ip address of local network interface, not the | ||
// loopback device. | ||
func GetExternalIP() (string, error) { | ||
ifaces, err := net.Interfaces() | ||
if err != nil { | ||
return "", err | ||
} | ||
for _, iface := range ifaces { | ||
if iface.Flags&net.FlagUp == 0 { | ||
continue // interface down | ||
} | ||
if iface.Flags&net.FlagLoopback != 0 { | ||
continue // loopback interface | ||
} | ||
addrs, err := iface.Addrs() | ||
if err != nil { | ||
return "", err | ||
} | ||
for _, addr := range addrs { | ||
var ip net.IP | ||
switch v := addr.(type) { | ||
case *net.IPNet: | ||
ip = v.IP | ||
case *net.IPAddr: | ||
ip = v.IP | ||
} | ||
if ip == nil || ip.IsLoopback() { | ||
continue | ||
} | ||
ip = ip.To4() | ||
if ip == nil { | ||
continue // not an ipv4 address | ||
} | ||
return ip.String(), nil | ||
} | ||
} | ||
return "", errors.New("are you connected to the network?") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package networkhelper | ||
|
||
import "testing" | ||
|
||
func TestGetIP(t *testing.T) { | ||
_, err := GetExternalIP() | ||
if err != nil { | ||
t.Errorf("GetExternalIP returns error : %v\n", err) | ||
} | ||
} |