2021-08-03 09:04:02 +00:00
|
|
|
package connection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-08-03 07:09:56 +00:00
|
|
|
"fmt"
|
2021-08-03 09:04:02 +00:00
|
|
|
"net"
|
2022-09-02 16:29:50 +00:00
|
|
|
"net/netip"
|
2023-12-04 09:49:00 +00:00
|
|
|
"runtime"
|
2022-10-12 16:01:25 +00:00
|
|
|
"sync"
|
2021-08-03 09:04:02 +00:00
|
|
|
|
2023-05-06 00:42:41 +00:00
|
|
|
"github.com/quic-go/quic-go"
|
2021-08-03 09:04:02 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-08-03 07:09:56 +00:00
|
|
|
)
|
|
|
|
|
2022-10-12 16:01:25 +00:00
|
|
|
var (
|
|
|
|
portForConnIndex = make(map[uint8]int, 0)
|
|
|
|
portMapMutex sync.Mutex
|
|
|
|
)
|
|
|
|
|
2024-10-24 18:42:02 +00:00
|
|
|
func DialQuic(
|
2023-05-06 00:42:41 +00:00
|
|
|
ctx context.Context,
|
2021-08-03 09:04:02 +00:00
|
|
|
quicConfig *quic.Config,
|
2024-10-24 18:42:02 +00:00
|
|
|
tlsConfig *tls.Config,
|
2024-10-18 21:38:05 +00:00
|
|
|
edgeAddr netip.AddrPort,
|
2023-02-28 16:11:42 +00:00
|
|
|
localAddr net.IP,
|
2022-10-12 16:01:25 +00:00
|
|
|
connIndex uint8,
|
2022-01-04 19:00:44 +00:00
|
|
|
logger *zerolog.Logger,
|
2024-10-24 18:42:02 +00:00
|
|
|
) (quic.Connection, error) {
|
2024-10-18 21:38:05 +00:00
|
|
|
udpConn, err := createUDPConnForConnIndex(connIndex, localAddr, edgeAddr, logger)
|
2022-10-12 16:01:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-10-24 18:42:02 +00:00
|
|
|
conn, err := quic.Dial(ctx, udpConn, net.UDPAddrFromAddrPort(edgeAddr), tlsConfig, quicConfig)
|
2021-08-03 09:04:02 +00:00
|
|
|
if err != nil {
|
2022-11-29 15:13:34 +00:00
|
|
|
// close the udp server socket in case of error connecting to the edge
|
|
|
|
udpConn.Close()
|
2022-05-20 21:51:36 +00:00
|
|
|
return nil, &EdgeQuicDialError{Cause: err}
|
2021-08-03 09:04:02 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 16:01:25 +00:00
|
|
|
// wrap the session, so that the UDPConn is closed after session is closed.
|
2024-10-24 18:42:02 +00:00
|
|
|
conn = &wrapCloseableConnQuicConnection{
|
|
|
|
conn,
|
2022-10-12 16:01:25 +00:00
|
|
|
udpConn,
|
|
|
|
}
|
2024-10-24 18:42:02 +00:00
|
|
|
return conn, nil
|
2022-09-13 13:00:54 +00:00
|
|
|
}
|
2022-10-12 16:01:25 +00:00
|
|
|
|
2024-10-18 21:38:05 +00:00
|
|
|
func createUDPConnForConnIndex(connIndex uint8, localIP net.IP, edgeIP netip.AddrPort, logger *zerolog.Logger) (*net.UDPConn, error) {
|
2022-10-12 16:01:25 +00:00
|
|
|
portMapMutex.Lock()
|
|
|
|
defer portMapMutex.Unlock()
|
|
|
|
|
2023-12-04 09:49:00 +00:00
|
|
|
listenNetwork := "udp"
|
2024-10-18 21:38:05 +00:00
|
|
|
// https://github.com/quic-go/quic-go/issues/3793 DF bit cannot be set for dual stack listener ("udp") on macOS,
|
|
|
|
// to set the DF bit properly, the network string needs to be specific to the IP family.
|
2023-12-04 09:49:00 +00:00
|
|
|
if runtime.GOOS == "darwin" {
|
2024-10-18 21:38:05 +00:00
|
|
|
if edgeIP.Addr().Is4() {
|
2023-12-04 09:49:00 +00:00
|
|
|
listenNetwork = "udp4"
|
|
|
|
} else {
|
|
|
|
listenNetwork = "udp6"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 16:01:25 +00:00
|
|
|
// if port was not set yet, it will be zero, so bind will randomly allocate one.
|
|
|
|
if port, ok := portForConnIndex[connIndex]; ok {
|
2023-12-04 09:49:00 +00:00
|
|
|
udpConn, err := net.ListenUDP(listenNetwork, &net.UDPAddr{IP: localIP, Port: port})
|
2022-10-12 16:01:25 +00:00
|
|
|
// if there wasn't an error, or if port was 0 (independently of error or not, just return)
|
|
|
|
if err == nil {
|
|
|
|
return udpConn, nil
|
|
|
|
} else {
|
|
|
|
logger.Debug().Err(err).Msgf("Unable to reuse port %d for connIndex %d. Falling back to random allocation.", port, connIndex)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we reached here, then there was an error or port as not been allocated it.
|
2023-12-04 09:49:00 +00:00
|
|
|
udpConn, err := net.ListenUDP(listenNetwork, &net.UDPAddr{IP: localIP, Port: 0})
|
2022-10-12 16:01:25 +00:00
|
|
|
if err == nil {
|
|
|
|
udpAddr, ok := (udpConn.LocalAddr()).(*net.UDPAddr)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unable to cast to udpConn")
|
|
|
|
}
|
|
|
|
portForConnIndex[connIndex] = udpAddr.Port
|
|
|
|
} else {
|
|
|
|
delete(portForConnIndex, connIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
return udpConn, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type wrapCloseableConnQuicConnection struct {
|
|
|
|
quic.Connection
|
|
|
|
udpConn *net.UDPConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapCloseableConnQuicConnection) CloseWithError(errorCode quic.ApplicationErrorCode, reason string) error {
|
|
|
|
err := w.Connection.CloseWithError(errorCode, reason)
|
|
|
|
w.udpConn.Close()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|