-
Notifications
You must be signed in to change notification settings - Fork 5
/
ip.go
46 lines (38 loc) · 866 Bytes
/
ip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package utils
import (
"github.com/filedag-project/filedag-storage/objectservice/pkg/set"
logging "github.com/ipfs/go-log/v2"
"net"
"runtime"
)
var log = logging.Logger("utils")
// MustGetLocalIP4 returns IPv4 addresses of localhost. It panics on error.
func MustGetLocalIP4() (ipList set.StringSet) {
ipList = set.NewStringSet()
ifs, err := net.Interfaces()
if err != nil {
log.Errorf("Unable to get IP addresses of this host %v", err)
}
for _, interf := range ifs {
addrs, err := interf.Addrs()
if err != nil {
continue
}
if runtime.GOOS == "windows" && interf.Flags&net.FlagUp == 0 {
continue
}
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.To4() != nil {
ipList.Add(ip.String())
}
}
}
return ipList
}