1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 01:27:03 -05:00
v2fly/testing/servers/tcp/port.go
2021-02-17 04:31:50 +08:00

21 lines
412 B
Go

package tcp
import "github.com/v2fly/v2ray-core/v4/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
}