Skip to content

Commit

Permalink
Merge pull request #116 from lxzan/dev
Browse files Browse the repository at this point in the history
fix ipv6 client connection errors
  • Loading branch information
lxzan authored Sep 22, 2024
2 parents 586e980 + aa65011 commit cfca2d0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ bench:

cover:
go test -coverprofile=./bin/cover.out --cover ./...

clean:
rm -rf ./bin/*
4 changes: 1 addition & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ func NewClient(handler Event, option *ClientOption) (*Conn, *http.Response, erro
return nil, nil, err
}

port := internal.SelectValue(URL.Port() == "", internal.SelectValue(tlsEnabled, "443", "80"), URL.Port())
hp := internal.SelectValue(URL.Hostname() == "", "127.0.0.1", URL.Hostname()) + ":" + port
c.conn, err = dialer.Dial("tcp", hp)
c.conn, err = dialer.Dial("tcp", internal.GetAddrFromURL(URL, tlsEnabled))
if err != nil {
return nil, nil, err
}
Expand Down
24 changes: 24 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"net"
"net/url"
"reflect"
"strings"
"unsafe"
Expand Down Expand Up @@ -238,3 +240,25 @@ func IsSameSlice[T comparable](a, b []T) bool {
}
return true
}

func IsIPv6(ipStr string) bool {
ip := net.ParseIP(ipStr)
if ip == nil {
return false
}
return ip.To4() == nil
}

// GetAddrFromURL 根据URL获取网络连接地址
// Get the network connection address based on the URL
func GetAddrFromURL(URL *url.URL, tlsEnabled bool) string {
port := SelectValue(URL.Port() == "", SelectValue(tlsEnabled, "443", "80"), URL.Port())
hostname := URL.Hostname()
if hostname == "" {
hostname = "127.0.0.1"
}
if IsIPv6(hostname) {
hostname = "[" + hostname + "]"
}
return hostname + ":" + port
}
33 changes: 33 additions & 0 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"hash/fnv"
"io"
"net/url"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -233,3 +234,35 @@ func TestIsSameSlice(t *testing.T) {
[]int{1, 2, 4},
))
}

func TestIsIPv6(t *testing.T) {
assert.False(t, IsIPv6("192.168.1.1"))
assert.False(t, IsIPv6("google.com"))
assert.True(t, IsIPv6("2001:0:2851:b9f0:3866:a7c6:871b:706a"))
}

func TestGetAddrFromURL(t *testing.T) {
t.Run("", func(t *testing.T) {
u, _ := url.Parse("wss://[2001:0:2851:b9f0:3866:a7c6:871b:706a]/connect")
addr := GetAddrFromURL(u, true)
assert.Equal(t, addr, "[2001:0:2851:b9f0:3866:a7c6:871b:706a]:443")
})

t.Run("", func(t *testing.T) {
u, _ := url.Parse("wss://:9443")
addr := GetAddrFromURL(u, true)
assert.Equal(t, addr, "127.0.0.1:9443")
})

t.Run("", func(t *testing.T) {
u, _ := url.Parse("wss://")
addr := GetAddrFromURL(u, true)
assert.Equal(t, addr, "127.0.0.1:443")
})

t.Run("", func(t *testing.T) {
u, _ := url.Parse("ws://google.com/connect")
addr := GetAddrFromURL(u, false)
assert.Equal(t, addr, "google.com:80")
})
}

0 comments on commit cfca2d0

Please sign in to comment.