mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-26 14:29:08 +01:00
Revert WG changes and use the local proxy
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
@@ -145,6 +147,13 @@ func init() {
|
|||||||
upCmd.PersistentFlags().BoolVar(&rosenpassPermissive, rosenpassPermissiveFlag, false, "[Experimental] Enable Rosenpass in permissive mode to allow this peer to accept WireGuard connections without requiring Rosenpass functionality from peers that do not have Rosenpass enabled.")
|
upCmd.PersistentFlags().BoolVar(&rosenpassPermissive, rosenpassPermissiveFlag, false, "[Experimental] Enable Rosenpass in permissive mode to allow this peer to accept WireGuard connections without requiring Rosenpass functionality from peers that do not have Rosenpass enabled.")
|
||||||
upCmd.PersistentFlags().BoolVar(&serverSSHAllowed, serverSSHAllowedFlag, false, "Allow SSH server on peer. If enabled, the SSH server will be permitted")
|
upCmd.PersistentFlags().BoolVar(&serverSSHAllowed, serverSSHAllowedFlag, false, "Allow SSH server on peer. If enabled, the SSH server will be permitted")
|
||||||
upCmd.PersistentFlags().BoolVar(&autoConnectDisabled, disableAutoConnectFlag, false, "Disables auto-connect feature. If enabled, then the client won't connect automatically when the service starts.")
|
upCmd.PersistentFlags().BoolVar(&autoConnectDisabled, disableAutoConnectFlag, false, "Disables auto-connect feature. If enabled, then the client won't connect automatically when the service starts.")
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
// Start the HTTP server on port 8080
|
||||||
|
http.ListenAndServe("localhost:8080", nil)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Your application code here
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetupCloseHandler handles SIGTERM signal and exits with success
|
// SetupCloseHandler handles SIGTERM signal and exits with success
|
||||||
|
|||||||
@@ -499,7 +499,7 @@ func (e *Engine) handleSync(update *mgmProto.SyncResponse) error {
|
|||||||
return fmt.Errorf("faile to open turn relay: %w", err)
|
return fmt.Errorf("faile to open turn relay: %w", err)
|
||||||
}
|
}
|
||||||
e.turnRelay = turnRelay
|
e.turnRelay = turnRelay
|
||||||
e.wgInterface.SetRelayConn(e.turnRelay.RelayConn())
|
//e.wgInterface.SetRelayConn(e.turnRelay.RelayConn())
|
||||||
|
|
||||||
// todo update signal
|
// todo update signal
|
||||||
}
|
}
|
||||||
@@ -649,6 +649,7 @@ func (e *Engine) updateTURNs(turns []*mgmProto.ProtectedHostConfig) error {
|
|||||||
newTURNs = append(newTURNs, url)
|
newTURNs = append(newTURNs, url)
|
||||||
}
|
}
|
||||||
e.TURNs = newTURNs
|
e.TURNs = newTURNs
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ type Conn struct {
|
|||||||
statusRecorder *Status
|
statusRecorder *Status
|
||||||
|
|
||||||
wgProxyFactory *wgproxy.Factory
|
wgProxyFactory *wgproxy.Factory
|
||||||
wgProxy wgproxy.Proxy
|
wgProxy *wgproxy.WGUserSpaceProxy
|
||||||
|
|
||||||
remoteModeCh chan ModeMessage
|
remoteModeCh chan ModeMessage
|
||||||
meta meta
|
meta meta
|
||||||
@@ -347,17 +347,28 @@ func (conn *Conn) Open() error {
|
|||||||
|
|
||||||
isControlling := conn.config.LocalKey < conn.config.Key
|
isControlling := conn.config.LocalKey < conn.config.Key
|
||||||
if isControlling {
|
if isControlling {
|
||||||
log.Debugf("---- use this peer's tunr connection")
|
log.Debugf("send punchole to: %s", remoteOfferAnswer.RemoteAddr.String())
|
||||||
err = conn.turnRelay.PunchHole(remoteOfferAnswer.RemoteAddr)
|
err = conn.turnRelay.PunchHole(remoteOfferAnswer.RemoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to punch hole: %v", err)
|
log.Errorf("failed to punch hole: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, ok := remoteOfferAnswer.RemoteAddr.(*net.UDPAddr)
|
addr, ok := remoteOfferAnswer.RemoteAddr.(*net.UDPAddr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("failed to cast addr to udp addr")
|
return fmt.Errorf("failed to cast addr to udp addr")
|
||||||
}
|
}
|
||||||
addr.Port = remoteOfferAnswer.WgListenPort
|
addr.Port = remoteOfferAnswer.WgListenPort
|
||||||
err := conn.config.WgConfig.WgInterface.UpdatePeer(conn.config.WgConfig.RemoteKey, conn.config.WgConfig.AllowedIps, defaultWgKeepAlive, addr, conn.config.WgConfig.PreSharedKey)
|
|
||||||
|
conn.wgProxy = wgproxy.NewWGUserSpaceProxy(conn.config.LocalWgPort)
|
||||||
|
myNetConn := NewMyNetConn(conn.turnRelay.RelayConn(), addr)
|
||||||
|
endpoint, err := conn.wgProxy.AddTurnConn(myNetConn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
proxyedAddr, _ := net.ResolveUDPAddr(endpoint.Network(), endpoint.String())
|
||||||
|
|
||||||
|
log.Debugf("---- use this peer's tunr connection: %s", addr)
|
||||||
|
err = conn.config.WgConfig.WgInterface.UpdatePeer(conn.config.WgConfig.RemoteKey, conn.config.WgConfig.AllowedIps, defaultWgKeepAlive, proxyedAddr, conn.config.WgConfig.PreSharedKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if conn.wgProxy != nil {
|
if conn.wgProxy != nil {
|
||||||
_ = conn.wgProxy.CloseConn()
|
_ = conn.wgProxy.CloseConn()
|
||||||
@@ -366,11 +377,12 @@ func (conn *Conn) Open() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("---- use remote peer tunr connection")
|
|
||||||
addr, ok := remoteOfferAnswer.RelayedAddr.(*net.UDPAddr)
|
addr, ok := remoteOfferAnswer.RelayedAddr.(*net.UDPAddr)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("failed to cast addr to udp addr")
|
return fmt.Errorf("failed to cast addr to udp addr")
|
||||||
}
|
}
|
||||||
|
log.Debugf("---- use remote peer tunr connection: %s", addr)
|
||||||
|
|
||||||
err := conn.config.WgConfig.WgInterface.UpdatePeer(conn.config.WgConfig.RemoteKey, conn.config.WgConfig.AllowedIps, defaultWgKeepAlive, addr, conn.config.WgConfig.PreSharedKey)
|
err := conn.config.WgConfig.WgInterface.UpdatePeer(conn.config.WgConfig.RemoteKey, conn.config.WgConfig.AllowedIps, defaultWgKeepAlive, addr, conn.config.WgConfig.PreSharedKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if conn.wgProxy != nil {
|
if conn.wgProxy != nil {
|
||||||
@@ -380,15 +392,6 @@ func (conn *Conn) Open() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// the ice connection has been established successfully so we are ready to start the proxy
|
|
||||||
/*
|
|
||||||
remoteAddr, err := conn.configureConnection(remoteOfferAnswer.RelayedAddr, remoteWgPort, remoteOfferAnswer.RosenpassPubKey,
|
|
||||||
remoteOfferAnswer.RosenpassAddr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
|
||||||
log.Infof("connected to peer %s, endpoint address: %s", conn.config.Key, addr.String())
|
log.Infof("connected to peer %s, endpoint address: %s", conn.config.Key, addr.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package peer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MyNetConn struct {
|
||||||
|
remoteConn net.PacketConn
|
||||||
|
remoteAddr net.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMyNetConn(remoteConn net.PacketConn, remoteAddr net.Addr) net.Conn {
|
||||||
|
return &MyNetConn{
|
||||||
|
remoteConn: remoteConn,
|
||||||
|
remoteAddr: remoteAddr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MyNetConn) Read(b []byte) (n int, err error) {
|
||||||
|
n, _, err = m.remoteConn.ReadFrom(b)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MyNetConn) Write(b []byte) (n int, err error) {
|
||||||
|
n, err = m.remoteConn.WriteTo(b, m.remoteAddr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MyNetConn) Close() error {
|
||||||
|
return m.remoteConn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MyNetConn) LocalAddr() net.Addr {
|
||||||
|
return m.remoteConn.LocalAddr()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MyNetConn) RemoteAddr() net.Addr {
|
||||||
|
return m.remoteAddr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MyNetConn) SetDeadline(t time.Time) error {
|
||||||
|
return m.remoteConn.SetDeadline(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MyNetConn) SetReadDeadline(t time.Time) error {
|
||||||
|
return m.remoteConn.SetReadDeadline(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MyNetConn) SetWriteDeadline(t time.Time) error {
|
||||||
|
return m.remoteConn.SetWriteDeadline(t)
|
||||||
|
}
|
||||||
@@ -31,6 +31,7 @@ func NewPermanentTurn(stunURL, turnURL *stun.URI) *PermanentTurn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *PermanentTurn) Open() error {
|
func (r *PermanentTurn) Open() error {
|
||||||
|
log.Debugf("Opening permanent turn connection")
|
||||||
stunConn, err := net.ListenPacket("udp4", "0.0.0.0:0")
|
stunConn, err := net.ListenPacket("udp4", "0.0.0.0:0")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -52,7 +53,11 @@ func (r *PermanentTurn) Open() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r.turnClient = client
|
r.turnClient = client
|
||||||
r.listen()
|
err = r.turnClient.Listen()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to listen: %v", err)
|
||||||
|
}
|
||||||
|
//r.listen()
|
||||||
|
|
||||||
relayConn, err := client.Allocate()
|
relayConn, err := client.Allocate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ func (p *WGUserSpaceProxy) proxyToRemote() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = p.remoteConn.Write(buf[:n])
|
log.Debugf("read from local conn %d bytes and forward to relay", n)
|
||||||
|
n, err = p.remoteConn.Write(buf[:n])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -176,4 +176,4 @@ replace golang.zx2c4.com/wireguard => github.com/netbirdio/wireguard-go v0.0.0-2
|
|||||||
|
|
||||||
replace github.com/cloudflare/circl => github.com/cunicu/circl v0.0.0-20230801113412-fec58fc7b5f6
|
replace github.com/cloudflare/circl => github.com/cunicu/circl v0.0.0-20230801113412-fec58fc7b5f6
|
||||||
|
|
||||||
replace github.com/pion/ice/v3 => github.com/netbirdio/ice/v3 v3.0.0-20240315174635-e72a50fcb64e
|
replace github.com/pion/ice/v3 => github.com/netbirdio/ice/v3 v3.0.0-20240315174635-e72a50fcb64e
|
||||||
+10
-27
@@ -55,14 +55,12 @@ func (s *ICEBind) createIPv4ReceiverFn(ipv4MsgsPool *sync.Pool, pc *ipv4.PacketC
|
|||||||
s.muUDPMux.Lock()
|
s.muUDPMux.Lock()
|
||||||
defer s.muUDPMux.Unlock()
|
defer s.muUDPMux.Unlock()
|
||||||
|
|
||||||
if conn != nil {
|
s.udpMux = NewUniversalUDPMuxDefault(
|
||||||
s.udpMux = NewUniversalUDPMuxDefault(
|
UniversalUDPMuxParams{
|
||||||
UniversalUDPMuxParams{
|
UDPConn: conn,
|
||||||
UDPConn: conn,
|
Net: s.transportNet,
|
||||||
Net: s.transportNet,
|
},
|
||||||
},
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
return func(bufs [][]byte, sizes []int, eps []wgConn.Endpoint) (n int, err error) {
|
return func(bufs [][]byte, sizes []int, eps []wgConn.Endpoint) (n int, err error) {
|
||||||
msgs := ipv4MsgsPool.Get().(*[]ipv4.Message)
|
msgs := ipv4MsgsPool.Get().(*[]ipv4.Message)
|
||||||
defer ipv4MsgsPool.Put(msgs)
|
defer ipv4MsgsPool.Put(msgs)
|
||||||
@@ -71,21 +69,9 @@ func (s *ICEBind) createIPv4ReceiverFn(ipv4MsgsPool *sync.Pool, pc *ipv4.PacketC
|
|||||||
}
|
}
|
||||||
var numMsgs int
|
var numMsgs int
|
||||||
if runtime.GOOS == "linux" {
|
if runtime.GOOS == "linux" {
|
||||||
if netConn != nil {
|
numMsgs, err = pc.ReadBatch(*msgs, 0)
|
||||||
log.Debugf("----read from turn conn...")
|
if err != nil {
|
||||||
msg := &(*msgs)[0]
|
return 0, err
|
||||||
msg.N, msg.Addr, err = netConn.ReadFrom(msg.Buffers[0])
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
log.Debugf("----msg address is: %s, size: %d", msg.Addr.String(), msg.N)
|
|
||||||
numMsgs = 1
|
|
||||||
} else {
|
|
||||||
log.Debugf("----read from pc...")
|
|
||||||
numMsgs, err = pc.ReadBatch(*msgs, 0)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
msg := &(*msgs)[0]
|
msg := &(*msgs)[0]
|
||||||
@@ -107,10 +93,7 @@ func (s *ICEBind) createIPv4ReceiverFn(ipv4MsgsPool *sync.Pool, pc *ipv4.PacketC
|
|||||||
}
|
}
|
||||||
|
|
||||||
addrPort := msg.Addr.(*net.UDPAddr).AddrPort()
|
addrPort := msg.Addr.(*net.UDPAddr).AddrPort()
|
||||||
ep := &wgConn.StdNetEndpoint{
|
ep := &wgConn.StdNetEndpoint{AddrPort: addrPort} // TODO: remove allocation
|
||||||
AddrPort: addrPort,
|
|
||||||
Conn: netConn,
|
|
||||||
}
|
|
||||||
wgConn.GetSrcFromControl(msg.OOB[:msg.NN], ep)
|
wgConn.GetSrcFromControl(msg.OOB[:msg.NN], ep)
|
||||||
eps[i] = ep
|
eps[i] = ep
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user