|
|
@ -31,26 +31,30 @@ var quicConfig = &quic.Config{ |
|
|
|
} |
|
|
|
|
|
|
|
type connManager struct { |
|
|
|
connIPv4Once sync.Once |
|
|
|
connIPv4 net.PacketConn |
|
|
|
mutex sync.Mutex |
|
|
|
|
|
|
|
connIPv6Once sync.Once |
|
|
|
connIPv6 net.PacketConn |
|
|
|
connIPv4 net.PacketConn |
|
|
|
connIPv6 net.PacketConn |
|
|
|
} |
|
|
|
|
|
|
|
func (c *connManager) GetConnForAddr(network string) (net.PacketConn, error) { |
|
|
|
c.mutex.Lock() |
|
|
|
defer c.mutex.Unlock() |
|
|
|
|
|
|
|
switch network { |
|
|
|
case "udp4": |
|
|
|
if c.connIPv4 != nil { |
|
|
|
return c.connIPv4, nil |
|
|
|
} |
|
|
|
var err error |
|
|
|
c.connIPv4Once.Do(func() { |
|
|
|
c.connIPv4, err = c.createConn(network, "0.0.0.0:0") |
|
|
|
}) |
|
|
|
c.connIPv4, err = c.createConn(network, "0.0.0.0:0") |
|
|
|
return c.connIPv4, err |
|
|
|
case "udp6": |
|
|
|
if c.connIPv6 != nil { |
|
|
|
return c.connIPv6, nil |
|
|
|
} |
|
|
|
var err error |
|
|
|
c.connIPv6Once.Do(func() { |
|
|
|
c.connIPv6, err = c.createConn(network, ":0") |
|
|
|
}) |
|
|
|
c.connIPv6, err = c.createConn(network, ":0") |
|
|
|
return c.connIPv6, err |
|
|
|
default: |
|
|
|
return nil, fmt.Errorf("unsupported network: %s", network) |
|
|
|