mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-19 16:56:39 +00:00
[relay] Feature/relay integration (#2244)
This update adds new relay integration for NetBird clients. The new relay is based on web sockets and listens on a single port. - Adds new relay implementation with websocket with single port relaying mechanism - refactor peer connection logic, allowing upgrade and downgrade from/to P2P connection - peer connections are faster since it connects first to relay and then upgrades to P2P - maintains compatibility with old clients by not using the new relay - updates infrastructure scripts with new relay service
This commit is contained in:
72
relay/testec2/tun/proxy.go
Normal file
72
relay/testec2/tun/proxy.go
Normal file
@@ -0,0 +1,72 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package tun
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync/atomic"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Proxy struct {
|
||||
Device *Device
|
||||
PConn net.PacketConn
|
||||
DstAddr net.Addr
|
||||
shutdownFlag atomic.Bool
|
||||
}
|
||||
|
||||
func (p *Proxy) Start() {
|
||||
go p.readFromDevice()
|
||||
go p.readFromConn()
|
||||
}
|
||||
|
||||
func (p *Proxy) Close() {
|
||||
p.shutdownFlag.Store(true)
|
||||
}
|
||||
|
||||
func (p *Proxy) readFromDevice() {
|
||||
buf := make([]byte, 1500)
|
||||
for {
|
||||
n, err := p.Device.Read(buf)
|
||||
if err != nil {
|
||||
if p.shutdownFlag.Load() {
|
||||
return
|
||||
}
|
||||
log.Errorf("failed to read from device: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = p.PConn.WriteTo(buf[:n], p.DstAddr)
|
||||
if err != nil {
|
||||
if p.shutdownFlag.Load() {
|
||||
return
|
||||
}
|
||||
log.Errorf("failed to write to conn: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Proxy) readFromConn() {
|
||||
buf := make([]byte, 1500)
|
||||
for {
|
||||
n, _, err := p.PConn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
if p.shutdownFlag.Load() {
|
||||
return
|
||||
}
|
||||
log.Errorf("failed to read from conn: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = p.Device.Write(buf[:n])
|
||||
if err != nil {
|
||||
if p.shutdownFlag.Load() {
|
||||
return
|
||||
}
|
||||
log.Errorf("failed to write to device: %s", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user