1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| package udp
|
| import "github.com/v2fly/v2ray-core/v5/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
| }
|
|