mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-16 09:26:21 -05:00
21 lines
412 B
Go
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
|
|
}
|