Skip to content

Commit

Permalink
fix: filter lookback interface for utils.ClientIP (#700)
Browse files Browse the repository at this point in the history
  • Loading branch information
kallydev authored Jul 13, 2021
1 parent 8612bf7 commit e94bf49
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions internal/utils/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@ func init() {
}

func ClientIP4() ([]byte, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, errors.New("unexpected IP address")
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ip4 := ipnet.IP.To4(); ip4 != nil {
return ip4, nil
if ifaces, err := net.Interfaces(); err == nil && ifaces != nil {
for _, iface := range ifaces {
if iface.Flags&net.FlagLoopback != 0 || iface.Flags&net.FlagUp == 0 {
continue
}
if addrs, err := iface.Addrs(); err == nil && addrs != nil {
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ip4 := ipnet.IP.To4(); ip4 != nil {
return ip4, nil
}
}
}
}
}
}
Expand Down

0 comments on commit e94bf49

Please sign in to comment.