mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-16 15:26:40 +00:00
The Relayed connection setup is optimistic. It does not have any confirmation of an established end-to-end connection. Peers start sending WireGuard handshake packets immediately after the successful offer-answer handshake. Meanwhile, for successful P2P connection negotiation, we change the WireGuard endpoint address, but this change does not trigger new handshake initiation. Because the peer switched from Relayed connection to P2P, the packets from the Relay server are dropped and must wait for the next WireGuard handshake via P2P. To avoid this scenario, the relayed WireGuard proxy no longer drops the packets. Instead, it rewrites the source address to the new P2P endpoint and continues forwarding the packets. We still have one corner case: if the Relayed server negotiation chooses a server that has not been used before. In this case, one side of the peer connection will be slower to reach the Relay server, and the Relay server will drop the handshake packet. If everything goes well we should see exactly 5 seconds improvements between the WireGuard configuration time and the handshake time.
102 lines
2.2 KiB
Go
102 lines
2.2 KiB
Go
//go:build linux && !android
|
|
|
|
package udp
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
|
|
"github.com/google/gopacket"
|
|
"github.com/google/gopacket/layers"
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/netbirdio/netbird/client/iface/wgproxy/rawsocket"
|
|
)
|
|
|
|
var (
|
|
serializeOpts = gopacket.SerializeOptions{
|
|
ComputeChecksums: true,
|
|
FixLengths: true,
|
|
}
|
|
|
|
localHostNetIPAddr = &net.IPAddr{
|
|
IP: net.ParseIP("127.0.0.1"),
|
|
}
|
|
)
|
|
|
|
type SrcFaker struct {
|
|
srcAddr *net.UDPAddr
|
|
|
|
rawSocket net.PacketConn
|
|
ipH gopacket.SerializableLayer
|
|
udpH gopacket.SerializableLayer
|
|
layerBuffer gopacket.SerializeBuffer
|
|
}
|
|
|
|
func NewSrcFaker(dstPort int, srcAddr *net.UDPAddr) (*SrcFaker, error) {
|
|
rawSocket, err := rawsocket.PrepareSenderRawSocket()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ipH, udpH, err := prepareHeaders(dstPort, srcAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
f := &SrcFaker{
|
|
srcAddr: srcAddr,
|
|
rawSocket: rawSocket,
|
|
ipH: ipH,
|
|
udpH: udpH,
|
|
layerBuffer: gopacket.NewSerializeBuffer(),
|
|
}
|
|
|
|
return f, nil
|
|
}
|
|
|
|
func (f *SrcFaker) Close() error {
|
|
return f.rawSocket.Close()
|
|
}
|
|
|
|
func (f *SrcFaker) SendPkg(data []byte) (int, error) {
|
|
defer func() {
|
|
if err := f.layerBuffer.Clear(); err != nil {
|
|
log.Errorf("failed to clear layer buffer: %s", err)
|
|
}
|
|
}()
|
|
|
|
payload := gopacket.Payload(data)
|
|
|
|
err := gopacket.SerializeLayers(f.layerBuffer, serializeOpts, f.ipH, f.udpH, payload)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("serialize layers: %w", err)
|
|
}
|
|
n, err := f.rawSocket.WriteTo(f.layerBuffer.Bytes(), localHostNetIPAddr)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("write to raw conn: %w", err)
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
func prepareHeaders(dstPort int, srcAddr *net.UDPAddr) (gopacket.SerializableLayer, gopacket.SerializableLayer, error) {
|
|
ipH := &layers.IPv4{
|
|
DstIP: net.ParseIP("127.0.0.1"),
|
|
SrcIP: srcAddr.IP,
|
|
Version: 4,
|
|
TTL: 64,
|
|
Protocol: layers.IPProtocolUDP,
|
|
}
|
|
udpH := &layers.UDP{
|
|
SrcPort: layers.UDPPort(srcAddr.Port),
|
|
DstPort: layers.UDPPort(dstPort), // dst is the localhost WireGuard port
|
|
}
|
|
|
|
err := udpH.SetNetworkLayerForChecksum(ipH)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("set network layer for checksum: %w", err)
|
|
}
|
|
|
|
return ipH, udpH, nil
|
|
}
|