From 456167d22a3465da59af208e94361536b2ef2a8f Mon Sep 17 00:00:00 2001 From: Jason Peng Date: Fri, 18 Feb 2022 10:27:09 +0800 Subject: [PATCH] Imp: add host util func (#85) --- net/net.go | 39 +++++++++++++++++++++++++++++++++++++-- net/net_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/net/net.go b/net/net.go index ee3871d..907e750 100644 --- a/net/net.go +++ b/net/net.go @@ -131,7 +131,7 @@ func isValidNetworkInterface(face net.Interface) bool { return true } -// refer from https://github.com/facebookarchive/grace/blob/master/gracenet/net.go#L180 +// IsSameAddr refer from https://github.com/facebookarchive/grace/blob/master/gracenet/net.go#L180 func IsSameAddr(addr1, addr2 net.Addr) bool { if addr1.Network() != addr2.Network() { return false @@ -170,7 +170,7 @@ func ListenOnTCPRandomPort(ip string) (*net.TCPListener, error) { return net.ListenTCP("tcp4", &localAddr) } -// ListenOnUDPRandomPort a udp endpoint listening on a random port +// ListenOnUDPRandomPort an udp endpoint listening on a random port func ListenOnUDPRandomPort(ip string) (*net.UDPConn, error) { localAddr := net.UDPAddr{ IP: net.IPv4zero, @@ -310,3 +310,38 @@ func getNumOfIPSegment(ipSegment string, isIpv4 bool) int { ipSeg, _ := strconv.ParseInt(ipSegment, 0, 16) return int(ipSeg) } + +// HostAddress composes an ip:port style address. It's opposite function is net.SplitHostPort. +func HostAddress(host string, port int) string { + return net.JoinHostPort(host, strconv.Itoa(port)) +} + +// WSHostAddress return a ws hostAddress +func WSHostAddress(host string, port int, path string) string { + return "ws://" + net.JoinHostPort(host, strconv.Itoa(port)) + path +} + +// WSSHostAddress return a wss hostAddress +func WSSHostAddress(host string, port int, path string) string { + return "wss://" + net.JoinHostPort(host, strconv.Itoa(port)) + path +} + +// HostAddress2 return a hostAddress +func HostAddress2(host string, port string) string { + return net.JoinHostPort(host, port) +} + +// WSHostAddress2 return a ws hostAddress +func WSHostAddress2(host string, port string, path string) string { + return "ws://" + net.JoinHostPort(host, port) + path +} + +// WSSHostAddress2 return a wss hostAddress +func WSSHostAddress2(host string, port string, path string) string { + return "wss://" + net.JoinHostPort(host, port) + path +} + +// HostPort return host, port, err +func HostPort(addr string) (string, string, error) { + return net.SplitHostPort(addr) +} diff --git a/net/net_test.go b/net/net_test.go index 6ad1ec3..b21afd9 100644 --- a/net/net_test.go +++ b/net/net_test.go @@ -129,3 +129,35 @@ func TestMatchIpIpv6Range(t *testing.T) { assert.True(t, MatchIP("234e:0:4567:0:0:0:3d:1-2", "234e:0:4567:0:0:0:3d:1", "8080")) assert.False(t, MatchIP("234e:0:4567:0:0:0:3d:1-2", "234e:0:4567:0:0:0:3d:3", "8080")) } + +func TestHostAddress(t *testing.T) { + assert.Equal(t, "127.0.0.1:8080", HostAddress("127.0.0.1", 8080)) +} + +func TestWSHostAddress(t *testing.T) { + assert.Equal(t, "ws://127.0.0.1:8080ws", WSHostAddress("127.0.0.1", 8080, "ws")) +} + +func TestWSSHostAddress(t *testing.T) { + assert.Equal(t, "wss://127.0.0.1:8080wss", WSSHostAddress("127.0.0.1", 8080, "wss")) +} + +func TestHostAddress2(t *testing.T) { + assert.Equal(t, "127.0.0.1:8080", HostAddress2("127.0.0.1", "8080")) +} + +func TestWSHostAddress2(t *testing.T) { + assert.Equal(t, "ws://127.0.0.1:8080ws", WSHostAddress2("127.0.0.1", "8080", "ws")) +} + +func TestWSSHostAddress2(t *testing.T) { + assert.Equal(t, "wss://127.0.0.1:8080wss", WSSHostAddress2("127.0.0.1", "8080", "wss")) + assert.False(t, WSSHostAddress2("127.0.0.1", "8080", "wss") == "wss://127.0.0.1:8081wss") +} + +func TestHostPort(t *testing.T) { + host, port, err := HostPort("127.0.0.1:8080") + assert.Equal(t, "127.0.0.1", host) + assert.Equal(t, "8080", port) + assert.Nil(t, err) +}