Xray-core/testing/servers/tcp/port.go

17 lines
416 B
Go
Raw Normal View History

2020-11-25 11:01:53 +00:00
package tcp
import (
2020-12-04 01:36:16 +00:00
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/net"
2020-11-25 11:01:53 +00:00
)
// 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)
}