mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-16 07:16:38 +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.
50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
//go:build linux && !android
|
|
|
|
package wgproxy
|
|
|
|
import (
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/netbirdio/netbird/client/iface/wgproxy/ebpf"
|
|
udpProxy "github.com/netbirdio/netbird/client/iface/wgproxy/udp"
|
|
)
|
|
|
|
type KernelFactory struct {
|
|
wgPort int
|
|
mtu uint16
|
|
|
|
ebpfProxy *ebpf.WGEBPFProxy
|
|
}
|
|
|
|
func NewKernelFactory(wgPort int, mtu uint16) *KernelFactory {
|
|
f := &KernelFactory{
|
|
wgPort: wgPort,
|
|
mtu: mtu,
|
|
}
|
|
|
|
ebpfProxy := ebpf.NewWGEBPFProxy(wgPort, mtu)
|
|
if err := ebpfProxy.Listen(); err != nil {
|
|
log.Infof("WireGuard Proxy Factory will produce UDP proxy")
|
|
log.Warnf("failed to initialize ebpf proxy, fallback to user space proxy: %s", err)
|
|
return f
|
|
}
|
|
log.Infof("WireGuard Proxy Factory will produce eBPF proxy")
|
|
f.ebpfProxy = ebpfProxy
|
|
return f
|
|
}
|
|
|
|
func (w *KernelFactory) GetProxy() Proxy {
|
|
if w.ebpfProxy == nil {
|
|
return udpProxy.NewWGUDPProxy(w.wgPort, w.mtu)
|
|
}
|
|
|
|
return ebpf.NewProxyWrapper(w.ebpfProxy)
|
|
}
|
|
|
|
func (w *KernelFactory) Free() error {
|
|
if w.ebpfProxy == nil {
|
|
return nil
|
|
}
|
|
return w.ebpfProxy.Free()
|
|
}
|