1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00
v2fly/testing/servers/udp/port.go

24 lines
440 B
Go
Raw Normal View History

2017-11-14 17:45:07 -05:00
package udp
2021-02-16 15:31:50 -05:00
import "github.com/v2fly/v2ray-core/v4/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
}