2017-11-14 22:45:07 +00:00
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
)
|
|
|
|
|
2018-03-01 10:53:39 +00:00
|
|
|
// PickPort returns an unused UDP port in the system. The port returned is highly likely to be unused, but not guaranteed.
|
2017-11-14 22:45:07 +00:00
|
|
|
func PickPort() net.Port {
|
|
|
|
conn, err := net.ListenUDP("udp4", &net.UDPAddr{
|
|
|
|
IP: net.LocalHostIP.IP(),
|
|
|
|
Port: 0,
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
addr := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
return net.Port(addr.Port)
|
|
|
|
}
|