github.com/eagleql/xray-core@v1.4.4/testing/servers/tcp/port.go (about)

     1  package tcp
     2  
     3  import (
     4  	"github.com/eagleql/xray-core/common"
     5  	"github.com/eagleql/xray-core/common/net"
     6  )
     7  
     8  // PickPort returns an unused TCP port in the system. The port returned is highly likely to be unused, but not guaranteed.
     9  func PickPort() net.Port {
    10  	listener, err := net.Listen("tcp4", "127.0.0.1:0")
    11  	common.Must(err)
    12  	defer listener.Close()
    13  
    14  	addr := listener.Addr().(*net.TCPAddr)
    15  	return net.Port(addr.Port)
    16  }