Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter loopback ip #153

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,31 @@ var localIP = ""

func LocalIP() string {
if localIP == "" {
addrs, err := net.InterfaceAddrs()
netInterfaces, err := net.Interfaces()
if err != nil {
logger.Errorf("get InterfaceAddress failed,err:%+v", err)
logger.Errorf("get Interfaces failed,err:%+v", err)
return ""
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
localIP = ipnet.IP.String()
logger.Infof("InitLocalIp, LocalIp:%s", localIP)
break

for i := 0; i < len(netInterfaces); i++ {
if ((netInterfaces[i].Flags & net.FlagUp) != 0) && ((netInterfaces[i].Flags & net.FlagLoopback) == 0) {
addrs, err := netInterfaces[i].Addrs()
if err != nil {
logger.Errorf("get InterfaceAddress failed,err:%+v", err)
return ""
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
localIP = ipnet.IP.String()
break
}
}
}
}

if len(localIP) > 0 {
logger.Infof("Local IP:%s", localIP)
}
}
return localIP
}
Expand Down