2018-03-01 05:53:39 -05:00
|
|
|
package tcp
|
|
|
|
|
2020-12-02 10:48:32 -05:00
|
|
|
import "v2ray.com/core/common/net"
|
2018-03-01 05:53:39 -05:00
|
|
|
|
2020-12-02 10:48:32 -05:00
|
|
|
// PickPort returns an unused TCP port of the system.
|
2018-03-01 05:53:39 -05:00
|
|
|
func PickPort() net.Port {
|
2020-12-02 10:48:32 -05:00
|
|
|
listener := pickPort()
|
2018-03-01 05:53:39 -05:00
|
|
|
defer listener.Close()
|
|
|
|
|
|
|
|
addr := listener.Addr().(*net.TCPAddr)
|
|
|
|
return net.Port(addr.Port)
|
|
|
|
}
|
2020-12-02 10:48:32 -05:00
|
|
|
|
|
|
|
func pickPort() net.Listener {
|
|
|
|
listener, err := net.Listen("tcp4", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
listener = pickPort()
|
|
|
|
}
|
|
|
|
return listener
|
|
|
|
}
|