rocketmq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maixiao...@apache.org
Subject [rocketmq-client-go] branch master updated: fix: filter lookback interface for utils.ClientIP (#700)
Date Tue, 13 Jul 2021 05:04:43 GMT
This is an automated email from the ASF dual-hosted git repository.

maixiaohai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-client-go.git


The following commit(s) were added to refs/heads/master by this push:
     new e94bf49  fix: filter lookback interface for utils.ClientIP (#700)
e94bf49 is described below

commit e94bf49362a714350afb50536dcda5b7f5811ee9
Author: KallyDev <36319157+kallydev@users.noreply.github.com>
AuthorDate: Tue Jul 13 13:04:34 2021 +0800

    fix: filter lookback interface for utils.ClientIP (#700)
---
 internal/utils/net.go | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/internal/utils/net.go b/internal/utils/net.go
index 0dfcff8..cf35594 100644
--- a/internal/utils/net.go
+++ b/internal/utils/net.go
@@ -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
+						}
+					}
+				}
 			}
 		}
 	}

Mime
View raw message