mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 09:36:34 -05:00
24 lines
440 B
Go
24 lines
440 B
Go
package udp
|
|
|
|
import "github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
// PickPort returns an unused UDP port of the system.
|
|
func PickPort() net.Port {
|
|
conn := pickPort()
|
|
defer conn.Close()
|
|
|
|
addr := conn.LocalAddr().(*net.UDPAddr)
|
|
return net.Port(addr.Port)
|
|
}
|
|
|
|
func pickPort() *net.UDPConn {
|
|
conn, err := net.ListenUDP("udp4", &net.UDPAddr{
|
|
IP: net.LocalHostIP.IP(),
|
|
Port: 0,
|
|
})
|
|
if err != nil {
|
|
conn = pickPort()
|
|
}
|
|
return conn
|
|
}
|