mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-30 05:56:54 -05:00
21 lines
396 B
Go
21 lines
396 B
Go
package tcp
|
|
|
|
import "v2ray.com/core/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
|
|
}
|