1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-04 09:17:32 -05:00
v2fly/testing/servers/tcp/port.go
2022-01-02 15:16:23 +00:00

21 lines
412 B
Go

package tcp
import "github.com/v2fly/v2ray-core/v5/common/net"
// PickPort returns an unused TCP port of the system.
func PickPort() net.Port {
listener := pickPort()
defer listener.Close()
addr := listener.Addr().(*net.TCPAddr)
return net.Port(addr.Port)
}
func pickPort() net.Listener {
listener, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
listener = pickPort()
}
return listener
}