mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-05 14:43:03 +00:00
17 lines
422 B
Go
17 lines
422 B
Go
package tcp
|
|
|
|
import (
|
|
"github.com/xtls/xray-core/v1/common"
|
|
"github.com/xtls/xray-core/v1/common/net"
|
|
)
|
|
|
|
// PickPort returns an unused TCP port in the system. The port returned is highly likely to be unused, but not guaranteed.
|
|
func PickPort() net.Port {
|
|
listener, err := net.Listen("tcp4", "127.0.0.1:0")
|
|
common.Must(err)
|
|
defer listener.Close()
|
|
|
|
addr := listener.Addr().(*net.TCPAddr)
|
|
return net.Port(addr.Port)
|
|
}
|