2017-11-14 17:45:07 -05:00
|
|
|
package udp
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
import "github.com/v2fly/v2ray-core/v5/common/net"
|
2017-11-14 17:45:07 -05:00
|
|
|
|
2020-12-02 10:48:32 -05:00
|
|
|
// PickPort returns an unused UDP port of the system.
|
2017-11-14 17:45:07 -05:00
|
|
|
func PickPort() net.Port {
|
2020-12-02 10:48:32 -05:00
|
|
|
conn := pickPort()
|
2017-11-14 17:45:07 -05:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
addr := conn.LocalAddr().(*net.UDPAddr)
|
|
|
|
return net.Port(addr.Port)
|
|
|
|
}
|
2020-12-02 10:48:32 -05:00
|
|
|
|
|
|
|
func pickPort() *net.UDPConn {
|
|
|
|
conn, err := net.ListenUDP("udp4", &net.UDPAddr{
|
|
|
|
IP: net.LocalHostIP.IP(),
|
|
|
|
Port: 0,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
conn = pickPort()
|
|
|
|
}
|
|
|
|
return conn
|
|
|
|
}
|