diff --git a/testing/scenarios/command_test.go b/testing/scenarios/command_test.go index b15431f6..c3d0844b 100644 --- a/testing/scenarios/command_test.go +++ b/testing/scenarios/command_test.go @@ -37,8 +37,8 @@ func TestCommanderRemoveHandler(t *testing.T) { assert(err, IsNil) defer tcpServer.Close() - clientPort := pickPort() - cmdPort := pickPort() + clientPort := tcp.PickPort() + cmdPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&commander.Config{ @@ -150,8 +150,8 @@ func TestCommanderAddRemoveUser(t *testing.T) { u1 := protocol.NewID(uuid.New()) u2 := protocol.NewID(uuid.New()) - cmdPort := pickPort() - serverPort := pickPort() + cmdPort := tcp.PickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&commander.Config{ @@ -219,7 +219,7 @@ func TestCommanderAddRemoveUser(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&policy.Config{ diff --git a/testing/scenarios/common.go b/testing/scenarios/common.go index ac6155bb..bfc8c03c 100644 --- a/testing/scenarios/common.go +++ b/testing/scenarios/common.go @@ -22,15 +22,6 @@ import ( "v2ray.com/core/common/serial" ) -func pickPort() net.Port { - listener, err := net.Listen("tcp4", "127.0.0.1:0") - common.Must(err) - defer listener.Close() - - addr := listener.Addr().(*net.TCPAddr) - return net.Port(addr.Port) -} - func xor(b []byte) []byte { r := make([]byte, len(b)) for i, v := range b { diff --git a/testing/scenarios/dns_test.go b/testing/scenarios/dns_test.go index 5f168211..bb9e92ac 100644 --- a/testing/scenarios/dns_test.go +++ b/testing/scenarios/dns_test.go @@ -28,7 +28,7 @@ func TestResolveIP(t *testing.T) { assert(err, IsNil) defer tcpServer.Close() - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&dns.Config{ diff --git a/testing/scenarios/dokodemo_test.go b/testing/scenarios/dokodemo_test.go index 2a821f35..5148280d 100644 --- a/testing/scenarios/dokodemo_test.go +++ b/testing/scenarios/dokodemo_test.go @@ -32,7 +32,7 @@ func TestDokodemoTCP(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -64,7 +64,7 @@ func TestDokodemoTCP(t *testing.T) { }, } - clientPort := uint32(pickPort()) + clientPort := uint32(tcp.PickPort()) clientPortRange := uint32(5) clientConfig := &core.Config{ App: []*serial.TypedMessage{ @@ -145,7 +145,7 @@ func TestDokodemoUDP(t *testing.T) { defer udpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -171,7 +171,7 @@ func TestDokodemoUDP(t *testing.T) { }, } - clientPort := uint32(pickPort()) + clientPort := uint32(tcp.PickPort()) clientPortRange := uint32(5) clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ diff --git a/testing/scenarios/feature_test.go b/testing/scenarios/feature_test.go index 6549417b..84aaaccd 100644 --- a/testing/scenarios/feature_test.go +++ b/testing/scenarios/feature_test.go @@ -46,7 +46,7 @@ func TestPassiveConnection(t *testing.T) { assert(err, IsNil) defer tcpServer.Close() - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -117,7 +117,7 @@ func TestProxy(t *testing.T) { defer tcpServer.Close() serverUserID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -144,7 +144,7 @@ func TestProxy(t *testing.T) { } proxyUserID := protocol.NewID(uuid.New()) - proxyPort := pickPort() + proxyPort := tcp.PickPort() proxyConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -170,7 +170,7 @@ func TestProxy(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -265,7 +265,7 @@ func TestProxyOverKCP(t *testing.T) { defer tcpServer.Close() serverUserID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -295,7 +295,7 @@ func TestProxyOverKCP(t *testing.T) { } proxyUserID := protocol.NewID(uuid.New()) - proxyPort := pickPort() + proxyPort := tcp.PickPort() proxyConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -326,7 +326,7 @@ func TestProxyOverKCP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -430,8 +430,8 @@ func TestBlackhole(t *testing.T) { assert(err, IsNil) defer tcpServer2.Close() - serverPort := pickPort() - serverPort2 := pickPort() + serverPort := tcp.PickPort() + serverPort2 := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -521,7 +521,7 @@ func TestForward(t *testing.T) { assert(err, IsNil) defer tcpServer.Close() - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -587,7 +587,7 @@ func TestUDPConnection(t *testing.T) { assert(err, IsNil) defer udpServer.Close() - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -663,8 +663,8 @@ func TestUDPConnection(t *testing.T) { func TestDomainSniffing(t *testing.T) { assert := With(t) - sniffingPort := pickPort() - httpPort := pickPort() + sniffingPort := tcp.PickPort() + httpPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -764,7 +764,7 @@ func TestDialV2Ray(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ diff --git a/testing/scenarios/http_test.go b/testing/scenarios/http_test.go index 942718e3..e490c4a4 100644 --- a/testing/scenarios/http_test.go +++ b/testing/scenarios/http_test.go @@ -26,7 +26,7 @@ import ( func TestHttpConformance(t *testing.T) { assert := With(t) - httpServerPort := pickPort() + httpServerPort := tcp.PickPort() httpServer := &v2httptest.Server{ Port: httpServerPort, PathHandler: make(map[string]http.HandlerFunc), @@ -35,7 +35,7 @@ func TestHttpConformance(t *testing.T) { assert(err, IsNil) defer httpServer.Close() - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -90,7 +90,7 @@ func TestHttpConnectMethod(t *testing.T) { assert(err, IsNil) defer tcpServer.Close() - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -146,7 +146,7 @@ func TestHttpConnectMethod(t *testing.T) { func TestHttpPost(t *testing.T) { assert := With(t) - httpServerPort := pickPort() + httpServerPort := tcp.PickPort() httpServer := &v2httptest.Server{ Port: httpServerPort, PathHandler: map[string]http.HandlerFunc{ @@ -169,7 +169,7 @@ func TestHttpPost(t *testing.T) { assert(err, IsNil) defer httpServer.Close() - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -226,7 +226,7 @@ func setProxyBasicAuth(req *http.Request, user, pass string) { func TestHttpBasicAuth(t *testing.T) { assert := With(t) - httpServerPort := pickPort() + httpServerPort := tcp.PickPort() httpServer := &v2httptest.Server{ Port: httpServerPort, PathHandler: make(map[string]http.HandlerFunc), @@ -235,7 +235,7 @@ func TestHttpBasicAuth(t *testing.T) { assert(err, IsNil) defer httpServer.Close() - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { diff --git a/testing/scenarios/policy_test.go b/testing/scenarios/policy_test.go index 9e8b7ce7..93e4de3e 100644 --- a/testing/scenarios/policy_test.go +++ b/testing/scenarios/policy_test.go @@ -17,6 +17,7 @@ import ( "v2ray.com/core/proxy/vmess" "v2ray.com/core/proxy/vmess/inbound" "v2ray.com/core/proxy/vmess/outbound" + "v2ray.com/core/testing/servers/tcp" . "v2ray.com/ext/assert" ) @@ -49,7 +50,7 @@ func TestVMessClosing(t *testing.T) { dest := net.DestinationFromAddr(tcpServer.Addr()) userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&policy.Config{ @@ -88,7 +89,7 @@ func TestVMessClosing(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&policy.Config{ diff --git a/testing/scenarios/shadowsocks_test.go b/testing/scenarios/shadowsocks_test.go index 7f827bb9..74ed835b 100644 --- a/testing/scenarios/shadowsocks_test.go +++ b/testing/scenarios/shadowsocks_test.go @@ -41,7 +41,7 @@ func TestShadowsocksAES256TCP(t *testing.T) { Ota: shadowsocks.Account_Enabled, }) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -70,7 +70,7 @@ func TestShadowsocksAES256TCP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -159,7 +159,7 @@ func TestShadowsocksAES128UDP(t *testing.T) { Ota: shadowsocks.Account_Enabled, }) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -189,7 +189,7 @@ func TestShadowsocksAES128UDP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -278,7 +278,7 @@ func TestShadowsocksChacha20TCP(t *testing.T) { Ota: shadowsocks.Account_Enabled, }) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -307,7 +307,7 @@ func TestShadowsocksChacha20TCP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -395,7 +395,7 @@ func TestShadowsocksAES256GCMTCP(t *testing.T) { CipherType: shadowsocks.CipherType_AES_256_GCM, }) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -424,7 +424,7 @@ func TestShadowsocksAES256GCMTCP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -512,7 +512,7 @@ func TestShadowsocksAES128GCMUDP(t *testing.T) { CipherType: shadowsocks.CipherType_AES_128_GCM, }) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -542,7 +542,7 @@ func TestShadowsocksAES128GCMUDP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -630,7 +630,7 @@ func TestShadowsocksAES256GCMConformance(t *testing.T) { CipherType: shadowsocks.CipherType_AES_256_GCM, }) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -708,7 +708,7 @@ func TestShadowsocksChacha20Poly1305UDPConformance(t *testing.T) { CipherType: shadowsocks.CipherType_CHACHA20_POLY1305, }) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -791,7 +791,7 @@ func TestShadowsocksChacha20Conformance(t *testing.T) { Ota: shadowsocks.Account_Disabled, }) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ diff --git a/testing/scenarios/socks_test.go b/testing/scenarios/socks_test.go index 04ebe966..4bbaddb2 100644 --- a/testing/scenarios/socks_test.go +++ b/testing/scenarios/socks_test.go @@ -28,7 +28,7 @@ func TestSocksBridgeTCP(t *testing.T) { assert(err, IsNil) defer tcpServer.Close() - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -53,7 +53,7 @@ func TestSocksBridgeTCP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -125,7 +125,7 @@ func TestSocksBridageUDP(t *testing.T) { assert(err, IsNil) defer udpServer.Close() - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -150,7 +150,7 @@ func TestSocksBridageUDP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -222,8 +222,8 @@ func TestSocksConformance(t *testing.T) { assert(err, IsNil) defer tcpServer.Close() - authPort := pickPort() - noAuthPort := pickPort() + authPort := tcp.PickPort() + noAuthPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { diff --git a/testing/scenarios/tls_test.go b/testing/scenarios/tls_test.go index c6d5505d..392dbcdb 100644 --- a/testing/scenarios/tls_test.go +++ b/testing/scenarios/tls_test.go @@ -36,7 +36,7 @@ func TestSimpleTLSConnection(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -70,7 +70,7 @@ func TestSimpleTLSConnection(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -185,7 +185,7 @@ func TestTLSOverKCP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -266,7 +266,7 @@ func TestTLSOverWebSocket(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -301,7 +301,7 @@ func TestTLSOverWebSocket(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { diff --git a/testing/scenarios/transport_test.go b/testing/scenarios/transport_test.go index 253cd280..0ce1462c 100644 --- a/testing/scenarios/transport_test.go +++ b/testing/scenarios/transport_test.go @@ -33,7 +33,7 @@ func TestHttpConnectionHeader(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { @@ -69,7 +69,7 @@ func TestHttpConnectionHeader(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ Inbound: []*core.InboundHandlerConfig{ { diff --git a/testing/scenarios/vmess_test.go b/testing/scenarios/vmess_test.go index c392d91f..9b746baa 100644 --- a/testing/scenarios/vmess_test.go +++ b/testing/scenarios/vmess_test.go @@ -36,7 +36,7 @@ func TestVMessDynamicPort(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -91,7 +91,7 @@ func TestVMessDynamicPort(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -171,7 +171,7 @@ func TestVMessGCM(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -204,7 +204,7 @@ func TestVMessGCM(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -294,7 +294,7 @@ func TestVMessGCMUDP(t *testing.T) { defer udpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -327,7 +327,7 @@ func TestVMessGCMUDP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -427,7 +427,7 @@ func TestVMessChacha20(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -460,7 +460,7 @@ func TestVMessChacha20(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -550,7 +550,7 @@ func TestVMessNone(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -583,7 +583,7 @@ func TestVMessNone(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -711,7 +711,7 @@ func TestVMessKCP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -808,7 +808,7 @@ func TestVMessIPv6(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -841,7 +841,7 @@ func TestVMessIPv6(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -923,7 +923,7 @@ func TestVMessGCMMux(t *testing.T) { defer tcpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -956,7 +956,7 @@ func TestVMessGCMMux(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -1065,7 +1065,7 @@ func TestVMessGCMMuxUDP(t *testing.T) { defer udpServer.Close() userID := protocol.NewID(uuid.New()) - serverPort := pickPort() + serverPort := tcp.PickPort() serverConfig := &core.Config{ App: []*serial.TypedMessage{ serial.ToTypedMessage(&log.Config{ @@ -1098,7 +1098,7 @@ func TestVMessGCMMuxUDP(t *testing.T) { }, } - clientPort := pickPort() + clientPort := tcp.PickPort() clientUDPPort := udp.PickPort() clientConfig := &core.Config{ App: []*serial.TypedMessage{ diff --git a/testing/servers/tcp/port.go b/testing/servers/tcp/port.go new file mode 100644 index 00000000..cf4729d2 --- /dev/null +++ b/testing/servers/tcp/port.go @@ -0,0 +1,16 @@ +package tcp + +import ( + "v2ray.com/core/common" + "v2ray.com/core/common/net" +) + +// PickPort returns an unused TCP port in the system. The port returned is highly likely to be unused, but not guaranteed. +func PickPort() net.Port { + listener, err := net.Listen("tcp4", "127.0.0.1:0") + common.Must(err) + defer listener.Close() + + addr := listener.Addr().(*net.TCPAddr) + return net.Port(addr.Port) +} diff --git a/testing/servers/udp/port.go b/testing/servers/udp/port.go index 0c77c4cf..75da5df4 100644 --- a/testing/servers/udp/port.go +++ b/testing/servers/udp/port.go @@ -5,6 +5,7 @@ import ( "v2ray.com/core/common/net" ) +// PickPort returns an unused UDP port in the system. The port returned is highly likely to be unused, but not guaranteed. func PickPort() net.Port { conn, err := net.ListenUDP("udp4", &net.UDPAddr{ IP: net.LocalHostIP.IP(),