mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-25 17:43:38 +01:00
0c039274a4
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
73 lines
1.2 KiB
Go
73 lines
1.2 KiB
Go
//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
|
|
}
|
|
}
|
|
}
|