From b3cf11e79fb242e3e95b23cee71dc8d98206a309 Mon Sep 17 00:00:00 2001 From: wzv5 Date: Fri, 21 Jul 2023 16:15:46 +0800 Subject: [PATCH] fix #4 --- pkg/ping/http.go | 8 ++++++-- test/pping_test.go | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/ping/http.go b/pkg/ping/http.go index 2418e81..d29331f 100644 --- a/pkg/ping/http.go +++ b/pkg/ping/http.go @@ -74,10 +74,14 @@ func (this *HttpPing) PingContext(ctx context.Context) IPingResult { return this.errorResult(err) } } + ipstr := ip.String() + if isIPv6(ip) { + ipstr = fmt.Sprintf("[%s]", ipstr) + } if port != "" { - u.Host = net.JoinHostPort(ip.String(), port) + u.Host = fmt.Sprintf("%s:%s", ipstr, port) } else { - u.Host = ip.String() + u.Host = ipstr } url2 := u.String() diff --git a/test/pping_test.go b/test/pping_test.go index 86eed71..20ac085 100644 --- a/test/pping_test.go +++ b/test/pping_test.go @@ -99,3 +99,12 @@ func TestQuic(t *testing.T) { t.Fatal(result.Error()) } } + +func TestHttpIPv6(t *testing.T) { + p := ping.NewHttpPing("GET", "https://www.qq.com/", time.Second*5) + p.IP, _ = ping.LookupIPv6("www.qq.com") + result := p.Ping() + if result.Error() != nil { + t.Fatal(result.Error()) + } +}