Skip to content

Commit

Permalink
Imp: add host util func (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lvnszn authored Feb 18, 2022
1 parent a2ef9b8 commit 456167d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
39 changes: 37 additions & 2 deletions net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
32 changes: 32 additions & 0 deletions net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 456167d

Please sign in to comment.