This is an automated email from the ASF dual-hosted git repository.

littlecui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new ec2581b  Fix: Can not get the IPv6 remote addr (#1150)
ec2581b is described below

commit ec2581bc5cb1d4f7bc172d7b345ae0d12cad8ca9
Author: little-cui <[email protected]>
AuthorDate: Mon Sep 6 20:42:25 2021 +0800

    Fix: Can not get the IPv6 remote addr (#1150)
---
 datasource/etcd/event/dependency_event_handler.go |  2 +-
 go.mod                                            |  4 ++--
 pkg/util/net.go                                   |  6 ++++-
 pkg/util/net_test.go                              | 27 +++++++++++++----------
 4 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/datasource/etcd/event/dependency_event_handler.go 
b/datasource/etcd/event/dependency_event_handler.go
index c16ca17..8a78baa 100644
--- a/datasource/etcd/event/dependency_event_handler.go
+++ b/datasource/etcd/event/dependency_event_handler.go
@@ -149,7 +149,7 @@ func (h *DependencyEventHandler) Handle() error {
 
        key := path.GetServiceDependencyQueueRootKey("")
        resp, err := sd.DependencyQueue().Search(context.Background(), 
etcdadpt.WithNoCache(),
-               etcdadpt.WithStrKey(key), etcdadpt.WithPrefix(), 
etcdadpt.WithAscendOrder(), etcdadpt.WithOrderByCreate())
+               etcdadpt.WithStrKey(key), etcdadpt.WithPrefix())
        if err != nil {
                return err
        }
diff --git a/go.mod b/go.mod
index e804c93..4983028 100644
--- a/go.mod
+++ b/go.mod
@@ -44,8 +44,8 @@ require (
        github.com/urfave/cli v1.22.4
        github.com/widuu/gojson v0.0.0-20170212122013-7da9d2cd949b
        go.etcd.io/etcd/api/v3 v3.5.0
-       go.etcd.io/etcd/client/v3 v3.5.0 // indirect
-       go.etcd.io/etcd/server/v3 v3.5.0 // indirect
+       go.etcd.io/etcd/client/v3 v3.5.0
+       go.etcd.io/etcd/server/v3 v3.5.0
        go.mongodb.org/mongo-driver v1.4.2
        go.uber.org/zap v1.17.0
        golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
diff --git a/pkg/util/net.go b/pkg/util/net.go
index 1ad72c6..ff88758 100644
--- a/pkg/util/net.go
+++ b/pkg/util/net.go
@@ -70,7 +70,11 @@ func GetRealIP(r *http.Request) string {
                        return ip
                }
        }
-       return strings.Split(r.RemoteAddr, ":")[0]
+       host, _, err := net.SplitHostPort(r.RemoteAddr)
+       if err != nil {
+               return ""
+       }
+       return host
 }
 
 func InetNtoIP(ipnr uint32) net.IP {
diff --git a/pkg/util/net_test.go b/pkg/util/net_test.go
index 4478475..361bf6c 100644
--- a/pkg/util/net_test.go
+++ b/pkg/util/net_test.go
@@ -17,6 +17,7 @@
 package util
 
 import (
+       "github.com/stretchr/testify/assert"
        "net/http"
        "testing"
 )
@@ -117,25 +118,27 @@ func TestGetRealIP(t *testing.T) {
        req, _ := http.NewRequest(http.MethodGet, 
"https://127.0.0.1:30100/x/?a=b&c=d#e";, nil)
        req.RemoteAddr = "127.0.0.1:30100"
        ip := GetRealIP(req)
-       if ip != "127.0.0.1" {
-               t.Fatalf("TestGetRealIP failed")
-       }
+       assert.Equal(t, "127.0.0.1", ip)
 
        req.Header.Set("X-Real-Ip", "255.255.255.255")
        ip = GetRealIP(req)
-       if ip != "127.0.0.1" {
-               t.Fatalf("TestGetRealIP failed")
-       }
+       assert.Equal(t, "127.0.0.1", ip)
 
        req.Header.Set("X-Real-Ip", "4.4.4.4")
        ip = GetRealIP(req)
-       if ip != "4.4.4.4" {
-               t.Fatalf("TestGetRealIP failed")
-       }
+       assert.Equal(t, "4.4.4.4", ip)
 
        req.Header.Set("X-Forwarded-For", "1.1.1.1, 2.2.2.2, 3.3.3.3")
        ip = GetRealIP(req)
-       if ip != "1.1.1.1" {
-               t.Fatalf("TestGetRealIP failed")
-       }
+       assert.Equal(t, "1.1.1.1", ip)
+
+       // ipv6
+       req, _ = http.NewRequest(http.MethodGet, 
"https://127.0.0.1:30100/x/?a=b&c=d#e";, nil)
+       req.RemoteAddr = "[::1]:30100"
+       ip = GetRealIP(req)
+       assert.Equal(t, "::1", ip)
+
+       req.RemoteAddr = "[2008:0:0:0:8:800:200C:417A]:30100"
+       ip = GetRealIP(req)
+       assert.Equal(t, "2008:0:0:0:8:800:200C:417A", ip)
 }

Reply via email to