Code cleaning

This commit is contained in:
Zoltan Papp 2025-02-14 15:54:40 +01:00 committed by Zoltán Papp
parent 6a0f6efc18
commit ffe74365a8
2 changed files with 23 additions and 8 deletions

View File

@ -1,5 +1,16 @@
package bind package bind
import wgConn "golang.zx2c4.com/wireguard/conn" import (
"net"
wgConn "golang.zx2c4.com/wireguard/conn"
)
type Endpoint = wgConn.StdNetEndpoint type Endpoint = wgConn.StdNetEndpoint
func EndpointToUDPAddr(e Endpoint) *net.UDPAddr {
return &net.UDPAddr{
IP: e.Addr().AsSlice(),
Port: int(e.Port()),
}
}

View File

@ -16,7 +16,7 @@ import (
type ProxyBind struct { type ProxyBind struct {
Bind *bind.ICEBind Bind *bind.ICEBind
wgAddr *net.UDPAddr // wgEndpoint is a fake address that generated by the Bind.SetEndpoint based on the remote NetBird peer address
wgEndpoint *bind.Endpoint wgEndpoint *bind.Endpoint
remoteConn net.Conn remoteConn net.Conn
ctx context.Context ctx context.Context
@ -32,21 +32,25 @@ type ProxyBind struct {
// AddTurnConn adds a new connection to the bind. // AddTurnConn adds a new connection to the bind.
// endpoint is the NetBird address of the remote peer. The SetEndpoint return with the address what will be used in the // endpoint is the NetBird address of the remote peer. The SetEndpoint return with the address what will be used in the
// WireGuard configuration. // WireGuard configuration.
//
// Parameters:
// - ctx: Context is used for proxyToLocal to avoid unnecessary error messages
// - nbAddr: The NetBird UDP address of the remote peer, it required to generate fake address
// - remoteConn: The established TURN connection to the remote peer
func (p *ProxyBind) AddTurnConn(ctx context.Context, nbAddr *net.UDPAddr, remoteConn net.Conn) error { func (p *ProxyBind) AddTurnConn(ctx context.Context, nbAddr *net.UDPAddr, remoteConn net.Conn) error {
addr, err := p.Bind.SetEndpoint(nbAddr, remoteConn) fakeAddr, err := p.Bind.SetEndpoint(nbAddr, remoteConn)
if err != nil { if err != nil {
return err return err
} }
p.wgAddr = addr p.wgEndpoint = addrToEndpoint(fakeAddr)
p.wgEndpoint = addrToEndpoint(addr)
p.remoteConn = remoteConn p.remoteConn = remoteConn
p.ctx, p.cancel = context.WithCancel(ctx) p.ctx, p.cancel = context.WithCancel(ctx)
return err return err
} }
func (p *ProxyBind) EndpointAddr() *net.UDPAddr { func (p *ProxyBind) EndpointAddr() *net.UDPAddr {
return p.wgAddr return bind.EndpointToUDPAddr(*p.wgEndpoint)
} }
func (p *ProxyBind) Work() { func (p *ProxyBind) Work() {
@ -93,7 +97,7 @@ func (p *ProxyBind) close() error {
p.cancel() p.cancel()
p.Bind.RemoveEndpoint(p.wgAddr) p.Bind.RemoveEndpoint(bind.EndpointToUDPAddr(*p.wgEndpoint))
if rErr := p.remoteConn.Close(); rErr != nil && !errors.Is(rErr, net.ErrClosed) { if rErr := p.remoteConn.Close(); rErr != nil && !errors.Is(rErr, net.ErrClosed) {
return rErr return rErr