From 82f231d5e03227c0d10339e3fe0eb68f79556f44 Mon Sep 17 00:00:00 2001 From: samosvalishe Date: Tue, 19 May 2026 10:37:21 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20=D0=B2=D1=8B=D0=BD=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=B8=20UDP=20proxy-=D1=86=D0=B8=D0=BA=D0=BB=20=D0=B2=20?= =?UTF-8?q?internal/proxy/udp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Переносит oneDtlsConnection/oneTurnConnection и их retry-циклы из client/main.go в internal/proxy/udp, за интерфейс AuthHandler, чтобы пакет оставался вне client/internal/. UDPPacket/packetPool становятся udp.Packet/udp.Pool. Глобал globalAppCancel убран в пользу Deps.AppCancel. turnParams пока остаётся в client/main.go, т.к. vless ещё его использует; переедет с этапом 4.2. client/main.go: 1195 → 895. Wire-формат и CLI не изменены. --- client/internal/vkauth/client.go | 4 + client/main.go | 354 +++------------------------- internal/proxy/udp/udp.go | 385 +++++++++++++++++++++++++++++++ 3 files changed, 416 insertions(+), 327 deletions(-) create mode 100644 internal/proxy/udp/udp.go diff --git a/client/internal/vkauth/client.go b/client/internal/vkauth/client.go index ff9d610..ddb4949 100644 --- a/client/internal/vkauth/client.go +++ b/client/internal/vkauth/client.go @@ -176,6 +176,10 @@ func (c *Client) LockoutUntilUnix() int64 { return c.lockout.Load() } +// IsAuthError mirrors the package-level IsAuthError as a method so callers can +// program against a small interface. +func (c *Client) IsAuthError(err error) bool { return IsAuthError(err) } + // engageLockout arms the global captcha lockout for d from now. func (c *Client) engageLockout(d time.Duration) { c.lockout.Store(time.Now().Add(d).Unix()) diff --git a/client/main.go b/client/main.go index f584261..870d2c6 100644 --- a/client/main.go +++ b/client/main.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "log" - "math/rand" "net" "os" "os/signal" @@ -25,11 +24,11 @@ import ( "github.com/cacggghp/vk-turn-proxy/client/internal/vkauth" "github.com/cacggghp/vk-turn-proxy/internal/bond" "github.com/cacggghp/vk-turn-proxy/internal/dtlsdial" + udpproxy "github.com/cacggghp/vk-turn-proxy/internal/proxy/udp" "github.com/cacggghp/vk-turn-proxy/internal/stats" "github.com/cacggghp/vk-turn-proxy/internal/turnpipe" "github.com/cacggghp/vk-turn-proxy/internal/wrap" "github.com/cacggghp/vk-turn-proxy/tcputil" - "github.com/cbeuw/connutil" "github.com/xtaci/smux" ) @@ -39,7 +38,6 @@ type getCredsFunc func(ctx context.Context, link string, streamID int) (string, var ( activeLocalPeer atomic.Value connectedStreams atomic.Int32 - globalAppCancel context.CancelFunc udpDtlsDialer = &dtlsdial.Dialer{ HandshakeTimeout: 20 * time.Second, HandshakeSem: make(chan struct{}, 3), @@ -59,15 +57,6 @@ func debugf(format string, v ...any) { } } -type UDPPacket struct { - Data []byte - N int -} - -var packetPool = sync.Pool{ - New: func() any { return &UDPPacket{Data: make([]byte, 2048)} }, -} - // manualCaptchaSolver bridges the vkauth.ManualSolveFunc contract to the // local captcha bouncer (see manual_captcha.go). func manualCaptchaSolver(_ context.Context, e *captcha.Error, d net.Dialer) (string, string, error) { @@ -82,94 +71,6 @@ func manualCaptchaSolver(_ context.Context, e *captcha.Error, d net.Dialer) (str return "", "", fmt.Errorf("no redirect_uri or captcha_img") } -func dtlsFunc(ctx context.Context, conn net.PacketConn, peer *net.UDPAddr) (net.Conn, error) { - return udpDtlsDialer.Dial(ctx, conn, peer) -} - -func oneDtlsConnection(ctx context.Context, peer *net.UDPAddr, listenConn net.PacketConn, inboundChan <-chan *UDPPacket, connchan chan<- net.PacketConn, okchan chan<- struct{}, streamID int) error { - time.Sleep(time.Duration(rand.Intn(400)+100) * time.Millisecond) - - dtlsctx, dtlscancel := context.WithCancel(ctx) - defer dtlscancel() - - conn1, conn2 := connutil.AsyncPacketPipe() - go func() { - for { - select { - case <-dtlsctx.Done(): - return - case connchan <- conn2: - } - } - }() - dtlsConn, err1 := dtlsFunc(dtlsctx, conn1, peer) - if err1 != nil { - return fmt.Errorf("failed to connect DTLS: %s", err1) - } - defer func() { - if closeErr := dtlsConn.Close(); closeErr != nil { - log.Printf("[STREAM %d] failed to close DTLS connection: %s", streamID, closeErr) - } - log.Printf("[STREAM %d] Closed DTLS connection\n", streamID) - }() - log.Printf("[STREAM %d] Established DTLS connection!\n", streamID) - - if okchan != nil { - go func() { - select { - case okchan <- struct{}{}: - case <-dtlsctx.Done(): - } - }() - } - - wg := sync.WaitGroup{} - context.AfterFunc(dtlsctx, func() { - if err := dtlsConn.SetDeadline(time.Now()); err != nil { - log.Printf("[STREAM %d] Warning: SetDeadline failed: %v", streamID, err) - } - }) - - wg.Go(func() { - defer dtlscancel() - for { - select { - case <-dtlsctx.Done(): - return - case pkt := <-inboundChan: - _, _ = dtlsConn.Write(pkt.Data[:pkt.N]) - packetPool.Put(pkt) - } - } - }) - - wg.Go(func() { - defer dtlscancel() - buf := make([]byte, 1600) - for { - n, err1 := dtlsConn.Read(buf) - if err1 != nil { - return - } - - // Send back to the active WG client - if peerAddr := activeLocalPeer.Load(); peerAddr != nil { - if addr, ok := peerAddr.(net.Addr); ok { - if _, err := listenConn.WriteTo(buf[:n], addr); err != nil { - log.Printf("[STREAM %d] failed to forward packet to local peer: %v", streamID, err) - } - } - } - } - }) - - wg.Wait() - if err := dtlsConn.SetDeadline(time.Time{}); err != nil { - log.Printf("[STREAM %d] Failed to clear DTLS deadline: %s", streamID, err) - } - return nil -} - type turnParams struct { host string port string @@ -179,227 +80,8 @@ type turnParams struct { getCreds getCredsFunc } -func oneTurnConnection(ctx context.Context, turnParams *turnParams, peer *net.UDPAddr, conn2 net.PacketConn, streamID int, c chan<- error) { - time.Sleep(time.Duration(rand.Intn(400)+100) * time.Millisecond) - var err error - defer func() { c <- err }() - user, pass, urlTarget, err1 := turnParams.getCreds(ctx, turnParams.link, streamID) - if err1 != nil { - err = fmt.Errorf("failed to get TURN credentials: %s", err1) - return - } - stream, err1 := turnpipe.Open(ctx, turnpipe.Config{ - HostOverride: turnParams.host, - PortOverride: turnParams.port, - UDP: turnParams.udp, - }, peer, user, pass, urlTarget) - if err1 != nil { - if vkauth.IsAuthError(err1) { - vkAuth.HandleAuthError(streamID) - } - err = err1 - return - } - relayConn := stream.Relay - debugf("[STREAM %d] TURN server IP: %s", streamID, stream.ServerUDPAddr.IP) - - // Reset error count on successful allocation - vkAuth.ResetErrors(streamID) - - // Safely track active streams globally - connectedStreams.Add(1) - defer func() { - connectedStreams.Add(-1) - if cerr := stream.Close(); cerr != nil { - err = fmt.Errorf("failed to close TURN stream: %s", cerr) - } - }() - - if isDebug { - log.Printf("[STREAM %d] relayed-address=%s", streamID, relayConn.LocalAddr().String()) - } - - wg := sync.WaitGroup{} - turnctx, turncancel := context.WithCancel(ctx) - st := stats.New(isDebug) - go st.LogEvery(turnctx, debugf, fmt.Sprintf("[STREAM %d] TURN", streamID), "to-turn", "from-turn") - - context.AfterFunc(turnctx, func() { - if err := relayConn.SetDeadline(time.Now()); err != nil { - log.Printf("Failed to set relay deadline: %s", err) - } - // Do not set conn2 deadline (conn2 can sometimes be listenConn if direct mode is used) - }) - var internalPipeAddr atomic.Value - var wc *wrap.Conn - if len(turnParams.wrapKey) == wrap.KeyLen { - var wcErr error - wc, wcErr = wrap.NewConn(turnParams.wrapKey, false) - if wcErr != nil { - log.Printf("[STREAM %d] WRAP init failed: %v", streamID, wcErr) - turncancel() - return - } - } - - go func() { - defer turncancel() - buf := make([]byte, 1600) - // Reusable scratch buffer for wrapped wire bytes; sized once per - // stream so the hot-path TX loop performs zero allocations. - var wireBuf []byte - if wc != nil { - wireBuf = make([]byte, wrap.MaxWire(len(buf))) - } - for { - if turnctx.Err() != nil { - return - } - n, addr1, err1 := conn2.ReadFrom(buf) - if err1 != nil { - return - } - if turnctx.Err() != nil { - return - } - - internalPipeAddr.Store(addr1) - - out := buf[:n] - if wc != nil { - written, wrapErr := wc.WrapInto(wireBuf, out) - if wrapErr != nil { - log.Printf("[STREAM %d] WRAP failed: %v", streamID, wrapErr) - return - } - out = wireBuf[:written] - } - - written, err1 := relayConn.WriteTo(out, peer) - st.AddTx(written) - if err1 != nil { - return - } - } - }() - - wg.Go(func() { - defer turncancel() - readBufLen := 1600 - if wc != nil { - readBufLen = wrap.MaxWire(1600) - } - buf := make([]byte, readBufLen) - plain := make([]byte, 1600) - for { - n, _, err1 := relayConn.ReadFrom(buf) - if err1 != nil { - return - } - addr1 := internalPipeAddr.Load() - if addr1 == nil { - continue - } - - if addr, ok := addr1.(net.Addr); ok { - payload := buf[:n] - if wc != nil { - m, wrapErr := wc.Unwrap(payload, plain) - if wrapErr != nil { - log.Printf("[STREAM %d] UNWRAP failed: %v (n=%d)", streamID, wrapErr, n) - continue - } - payload = plain[:m] - } - st.AddRx(len(payload)) - if _, err := conn2.WriteTo(payload, addr); err != nil { - return - } - } - } - }) - - wg.Wait() - if err := relayConn.SetDeadline(time.Time{}); err != nil { - log.Printf("Failed to clear relay deadline: %s", err) - } -} - -func oneDtlsConnectionLoop(ctx context.Context, peer *net.UDPAddr, listenConn net.PacketConn, inboundChan <-chan *UDPPacket, connchan chan<- net.PacketConn, okchan chan<- struct{}, streamID int) { - for { - select { - case <-ctx.Done(): - return - default: - err := oneDtlsConnection(ctx, peer, listenConn, inboundChan, connchan, okchan, streamID) - if err != nil { - if time.Now().Unix() < vkAuth.LockoutUntilUnix() && strings.Contains(err.Error(), "context deadline exceeded") { - continue - } - select { - case <-ctx.Done(): - return - case <-time.After(time.Duration(10+rand.Intn(20)) * time.Second): - } - } - } - } -} - -func oneTurnConnectionLoop(ctx context.Context, turnParams *turnParams, peer *net.UDPAddr, connchan <-chan net.PacketConn, t <-chan time.Time, streamID int) { - for { - select { - case <-ctx.Done(): - return - case conn2 := <-connchan: - select { - case <-t: - case <-ctx.Done(): - return - } - c := make(chan error) - go oneTurnConnection(ctx, turnParams, peer, conn2, streamID, c) - - if err := <-c; err != nil { - if strings.Contains(err.Error(), "FATAL_CAPTCHA") { - log.Printf("[STREAM %d] Fatal manual captcha error. Shutting down application.", streamID) - if globalAppCancel != nil { - globalAppCancel() - } - return - } - if strings.Contains(err.Error(), "CAPTCHA_WAIT_REQUIRED") { - if !strings.Contains(err.Error(), "global lockout active") { - log.Printf("[STREAM %d] Backing off for 60 seconds to avoid IP ban...", streamID) - select { - case <-ctx.Done(): - return - case <-time.After(60 * time.Second): - } - } else { - lockoutEnd := vkAuth.LockoutUntilUnix() - sleepDuration := time.Until(time.Unix(lockoutEnd, 0)) - if sleepDuration < 0 { - sleepDuration = 5 * time.Second - } - select { - case <-ctx.Done(): - return - case <-time.After(sleepDuration): - } - } - } else { - log.Printf("[STREAM %d] %s", streamID, err) - time.Sleep(2 * time.Second) - } - } - } - } -} - func main() { ctx, cancel := context.WithCancel(context.Background()) - globalAppCancel = cancel defer cancel() signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT) @@ -530,7 +212,7 @@ func main() { } // Shared Worker Pool Queue for Aggregation - inboundChan := make(chan *UDPPacket, 2000) + inboundChan := make(chan *udpproxy.Packet, 2000) go func() { // Pointer-cache for the last seen local peer addr. Avoids the @@ -542,8 +224,8 @@ func main() { var lastAddr net.Addr var lastAddrStr string for { - pktIface := packetPool.Get() - pkt, ok := pktIface.(*UDPPacket) + pktIface := udpproxy.Pool.Get() + pkt, ok := pktIface.(*udpproxy.Packet) if !ok { log.Printf("packetPool returned unexpected type: %T", pktIface) continue @@ -568,7 +250,7 @@ func main() { case inboundChan <- pkt: default: // Drop the packet only if the global queue is completely full - packetPool.Put(pkt) + udpproxy.Pool.Put(pkt) } } }() @@ -580,13 +262,31 @@ func main() { log.Panicf("Direct mode not supported with dispatcher") } + udpDeps := &udpproxy.Deps{ + DTLSDialer: udpDtlsDialer, + Auth: vkAuth, + Debug: isDebug, + Debugf: debugf, + ActiveLocalPeer: &activeLocalPeer, + ConnectedStreams: &connectedStreams, + AppCancel: cancel, + } + udpParams := &udpproxy.Params{ + Host: params.host, + Port: params.port, + Link: params.link, + UDP: params.udp, + WrapKey: params.wrapKey, + GetCreds: udpproxy.GetCredsFunc(params.getCreds), + } + okchan := make(chan struct{}) connchan := make(chan net.PacketConn) wg1.Go(func() { - oneDtlsConnectionLoop(ctx, peer, listenConn, inboundChan, connchan, okchan, 1) + udpproxy.DTLSLoop(ctx, udpDeps, peer, listenConn, inboundChan, connchan, okchan, 1) }) wg1.Go(func() { - oneTurnConnectionLoop(ctx, params, peer, connchan, t, 1) + udpproxy.TURNLoop(ctx, udpDeps, udpParams, peer, connchan, t, 1) }) select { @@ -598,10 +298,10 @@ func main() { cchan := make(chan net.PacketConn) streamID := i wg1.Go(func() { - oneDtlsConnectionLoop(ctx, peer, listenConn, inboundChan, cchan, nil, streamID) + udpproxy.DTLSLoop(ctx, udpDeps, peer, listenConn, inboundChan, cchan, nil, streamID) }) wg1.Go(func() { - oneTurnConnectionLoop(ctx, params, peer, cchan, t, streamID) + udpproxy.TURNLoop(ctx, udpDeps, udpParams, peer, cchan, t, streamID) }) } diff --git a/internal/proxy/udp/udp.go b/internal/proxy/udp/udp.go new file mode 100644 index 0000000..7b14efa --- /dev/null +++ b/internal/proxy/udp/udp.go @@ -0,0 +1,385 @@ +// Package udp implements the UDP-mode proxy loop: it terminates DTLS from a +// local peer (WireGuard) and relays its packets through a per-stream TURN +// allocation back to a remote peer. It owns oneDtlsConnection / +// oneTurnConnection and their retry loops; wiring (flag parsing, listener, +// inbound dispatch) stays in main. +package udp + +import ( + "context" + "fmt" + "log" + "math/rand" + "net" + "strings" + "sync" + "sync/atomic" + "time" + + "github.com/cacggghp/vk-turn-proxy/internal/dtlsdial" + "github.com/cacggghp/vk-turn-proxy/internal/stats" + "github.com/cacggghp/vk-turn-proxy/internal/turnpipe" + "github.com/cacggghp/vk-turn-proxy/internal/wrap" + "github.com/cbeuw/connutil" +) + +// Packet is a pooled UDP datagram carried from the listener to the per-stream +// DTLS worker. N is the populated prefix of Data. +type Packet struct { + Data []byte + N int +} + +// Pool reuses Packet buffers across the inbound hot path. Buffer size matches +// the 2048-byte default the listener loop expects. +var Pool = sync.Pool{ + New: func() any { return &Packet{Data: make([]byte, 2048)} }, +} + +// GetCredsFunc resolves VK TURN credentials for a (link, streamID) pair. +// Matches vkauth.Client.GetCredentials. +type GetCredsFunc func(ctx context.Context, link string, streamID int) (string, string, string, error) + +// AuthHandler is the subset of vkauth.Client this package needs. Keeping it as +// an interface lets internal/proxy/udp stay outside client/internal/. +type AuthHandler interface { + IsAuthError(err error) bool + HandleAuthError(streamID int) bool + ResetErrors(streamID int) + LockoutUntilUnix() int64 +} + +// Params is the per-stream TURN/wrap configuration shared by the DTLS and TURN +// loops. Equivalent of the old client/main.go turnParams. +type Params struct { + Host string + Port string + Link string + UDP bool + WrapKey []byte + GetCreds GetCredsFunc +} + +// Deps groups everything the loops need from the host process. ActiveLocalPeer +// is read concurrently with the listener that writes it; ConnectedStreams is +// the live-stream counter the auth layer queries. +type Deps struct { + DTLSDialer *dtlsdial.Dialer + Auth AuthHandler + Debug bool + Debugf func(format string, v ...any) + ActiveLocalPeer *atomic.Value + ConnectedStreams *atomic.Int32 + AppCancel func() +} + +func (d *Deps) debugf(format string, v ...any) { + if d.Debugf != nil { + d.Debugf(format, v...) + } +} + +// DTLSLoop keeps a single DTLS termination alive for streamID, restarting it +// on failure with a 10-30s backoff (skipped while a captcha lockout is active +// and the prior error was a deadline). connchan is fed a fresh AsyncPacketPipe +// half on each attempt; okchan (non-nil only for stream 1) signals the first +// successful handshake. +func DTLSLoop(ctx context.Context, deps *Deps, peer *net.UDPAddr, listenConn net.PacketConn, inboundChan <-chan *Packet, connchan chan<- net.PacketConn, okchan chan<- struct{}, streamID int) { + for { + select { + case <-ctx.Done(): + return + default: + err := oneDTLS(ctx, deps, peer, listenConn, inboundChan, connchan, okchan, streamID) + if err != nil { + if time.Now().Unix() < deps.Auth.LockoutUntilUnix() && strings.Contains(err.Error(), "context deadline exceeded") { + continue + } + select { + case <-ctx.Done(): + return + case <-time.After(time.Duration(10+rand.Intn(20)) * time.Second): + } + } + } + } +} + +// TURNLoop drives the TURN allocation half. It waits for a fresh conn2 from +// the DTLS loop, throttles via t (the global 200ms tick), runs one TURN +// session, and reacts to FATAL_CAPTCHA / CAPTCHA_WAIT_REQUIRED accordingly. +func TURNLoop(ctx context.Context, deps *Deps, params *Params, peer *net.UDPAddr, connchan <-chan net.PacketConn, t <-chan time.Time, streamID int) { + for { + select { + case <-ctx.Done(): + return + case conn2 := <-connchan: + select { + case <-t: + case <-ctx.Done(): + return + } + c := make(chan error) + go oneTURN(ctx, deps, params, peer, conn2, streamID, c) + + if err := <-c; err != nil { + if strings.Contains(err.Error(), "FATAL_CAPTCHA") { + log.Printf("[STREAM %d] Fatal manual captcha error. Shutting down application.", streamID) + if deps.AppCancel != nil { + deps.AppCancel() + } + return + } + if strings.Contains(err.Error(), "CAPTCHA_WAIT_REQUIRED") { + if !strings.Contains(err.Error(), "global lockout active") { + log.Printf("[STREAM %d] Backing off for 60 seconds to avoid IP ban...", streamID) + select { + case <-ctx.Done(): + return + case <-time.After(60 * time.Second): + } + } else { + lockoutEnd := deps.Auth.LockoutUntilUnix() + sleepDuration := time.Until(time.Unix(lockoutEnd, 0)) + if sleepDuration < 0 { + sleepDuration = 5 * time.Second + } + select { + case <-ctx.Done(): + return + case <-time.After(sleepDuration): + } + } + } else { + log.Printf("[STREAM %d] %s", streamID, err) + time.Sleep(2 * time.Second) + } + } + } + } +} + +func oneDTLS(ctx context.Context, deps *Deps, peer *net.UDPAddr, listenConn net.PacketConn, inboundChan <-chan *Packet, connchan chan<- net.PacketConn, okchan chan<- struct{}, streamID int) error { + time.Sleep(time.Duration(rand.Intn(400)+100) * time.Millisecond) + + dtlsctx, dtlscancel := context.WithCancel(ctx) + defer dtlscancel() + + conn1, conn2 := connutil.AsyncPacketPipe() + go func() { + for { + select { + case <-dtlsctx.Done(): + return + case connchan <- conn2: + } + } + }() + dtlsRaw, err1 := deps.DTLSDialer.Dial(dtlsctx, conn1, peer) + if err1 != nil { + return fmt.Errorf("failed to connect DTLS: %s", err1) + } + var dtlsConn net.Conn = dtlsRaw + defer func() { + if closeErr := dtlsConn.Close(); closeErr != nil { + log.Printf("[STREAM %d] failed to close DTLS connection: %s", streamID, closeErr) + } + log.Printf("[STREAM %d] Closed DTLS connection\n", streamID) + }() + log.Printf("[STREAM %d] Established DTLS connection!\n", streamID) + + if okchan != nil { + go func() { + select { + case okchan <- struct{}{}: + case <-dtlsctx.Done(): + } + }() + } + + wg := sync.WaitGroup{} + context.AfterFunc(dtlsctx, func() { + if err := dtlsConn.SetDeadline(time.Now()); err != nil { + log.Printf("[STREAM %d] Warning: SetDeadline failed: %v", streamID, err) + } + }) + + wg.Go(func() { + defer dtlscancel() + for { + select { + case <-dtlsctx.Done(): + return + case pkt := <-inboundChan: + _, _ = dtlsConn.Write(pkt.Data[:pkt.N]) + Pool.Put(pkt) + } + } + }) + + wg.Go(func() { + defer dtlscancel() + buf := make([]byte, 1600) + for { + n, err1 := dtlsConn.Read(buf) + if err1 != nil { + return + } + + if peerAddr := deps.ActiveLocalPeer.Load(); peerAddr != nil { + if addr, ok := peerAddr.(net.Addr); ok { + if _, err := listenConn.WriteTo(buf[:n], addr); err != nil { + log.Printf("[STREAM %d] failed to forward packet to local peer: %v", streamID, err) + } + } + } + } + }) + + wg.Wait() + if err := dtlsConn.SetDeadline(time.Time{}); err != nil { + log.Printf("[STREAM %d] Failed to clear DTLS deadline: %s", streamID, err) + } + return nil +} + +func oneTURN(ctx context.Context, deps *Deps, params *Params, peer *net.UDPAddr, conn2 net.PacketConn, streamID int, c chan<- error) { + time.Sleep(time.Duration(rand.Intn(400)+100) * time.Millisecond) + var err error + defer func() { c <- err }() + user, pass, urlTarget, err1 := params.GetCreds(ctx, params.Link, streamID) + if err1 != nil { + err = fmt.Errorf("failed to get TURN credentials: %s", err1) + return + } + stream, err1 := turnpipe.Open(ctx, turnpipe.Config{ + HostOverride: params.Host, + PortOverride: params.Port, + UDP: params.UDP, + }, peer, user, pass, urlTarget) + if err1 != nil { + if deps.Auth.IsAuthError(err1) { + deps.Auth.HandleAuthError(streamID) + } + err = err1 + return + } + relayConn := stream.Relay + deps.debugf("[STREAM %d] TURN server IP: %s", streamID, stream.ServerUDPAddr.IP) + + deps.Auth.ResetErrors(streamID) + + deps.ConnectedStreams.Add(1) + defer func() { + deps.ConnectedStreams.Add(-1) + if cerr := stream.Close(); cerr != nil { + err = fmt.Errorf("failed to close TURN stream: %s", cerr) + } + }() + + if deps.Debug { + log.Printf("[STREAM %d] relayed-address=%s", streamID, relayConn.LocalAddr().String()) + } + + wg := sync.WaitGroup{} + turnctx, turncancel := context.WithCancel(ctx) + st := stats.New(deps.Debug) + go st.LogEvery(turnctx, deps.debugf, fmt.Sprintf("[STREAM %d] TURN", streamID), "to-turn", "from-turn") + + context.AfterFunc(turnctx, func() { + if err := relayConn.SetDeadline(time.Now()); err != nil { + log.Printf("Failed to set relay deadline: %s", err) + } + }) + var internalPipeAddr atomic.Value + var wc *wrap.Conn + if len(params.WrapKey) == wrap.KeyLen { + var wcErr error + wc, wcErr = wrap.NewConn(params.WrapKey, false) + if wcErr != nil { + log.Printf("[STREAM %d] WRAP init failed: %v", streamID, wcErr) + turncancel() + return + } + } + + go func() { + defer turncancel() + buf := make([]byte, 1600) + var wireBuf []byte + if wc != nil { + wireBuf = make([]byte, wrap.MaxWire(len(buf))) + } + for { + if turnctx.Err() != nil { + return + } + n, addr1, err1 := conn2.ReadFrom(buf) + if err1 != nil { + return + } + if turnctx.Err() != nil { + return + } + + internalPipeAddr.Store(addr1) + + out := buf[:n] + if wc != nil { + written, wrapErr := wc.WrapInto(wireBuf, out) + if wrapErr != nil { + log.Printf("[STREAM %d] WRAP failed: %v", streamID, wrapErr) + return + } + out = wireBuf[:written] + } + + written, err1 := relayConn.WriteTo(out, peer) + st.AddTx(written) + if err1 != nil { + return + } + } + }() + + wg.Go(func() { + defer turncancel() + readBufLen := 1600 + if wc != nil { + readBufLen = wrap.MaxWire(1600) + } + buf := make([]byte, readBufLen) + plain := make([]byte, 1600) + for { + n, _, err1 := relayConn.ReadFrom(buf) + if err1 != nil { + return + } + addr1 := internalPipeAddr.Load() + if addr1 == nil { + continue + } + + if addr, ok := addr1.(net.Addr); ok { + payload := buf[:n] + if wc != nil { + m, wrapErr := wc.Unwrap(payload, plain) + if wrapErr != nil { + log.Printf("[STREAM %d] UNWRAP failed: %v (n=%d)", streamID, wrapErr, n) + continue + } + payload = plain[:m] + } + st.AddRx(len(payload)) + if _, err := conn2.WriteTo(payload, addr); err != nil { + return + } + } + } + }) + + wg.Wait() + if err := relayConn.SetDeadline(time.Time{}); err != nil { + log.Printf("Failed to clear relay deadline: %s", err) + } +}