From eaab9c9538ec217b81bf47028da733e0b21866f7 Mon Sep 17 00:00:00 2001 From: snltty <1069410172@qq.com> Date: Tue, 24 Sep 2024 00:50:43 +0800 Subject: [PATCH] sync --- .../sforward/proxy/SForwardProxyUdp.cs | 32 +++++++++++++------ version.txt | 2 +- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/linker/plugins/sforward/proxy/SForwardProxyUdp.cs b/linker/plugins/sforward/proxy/SForwardProxyUdp.cs index f5f16bc0..bb76b9e3 100644 --- a/linker/plugins/sforward/proxy/SForwardProxyUdp.cs +++ b/linker/plugins/sforward/proxy/SForwardProxyUdp.cs @@ -49,9 +49,11 @@ private async Task BindReceive(AsyncUserUdpToken token, byte bufferSize) } Memory memory = buffer.AsMemory(0, result.ReceivedBytes); + IPEndPoint source = result.RemoteEndPoint as IPEndPoint; //已经连接 - if (udpConnections.TryGetValue(result.RemoteEndPoint as IPEndPoint, out UdpTargetCache cache) && cache != null) + if (udpConnections.TryGetValue(source, out UdpTargetCache cache) && cache != null) { + cache.Update(); await token.SourceSocket.SendToAsync(memory, cache.IPEndPoint).ConfigureAwait(false); } else @@ -62,13 +64,11 @@ private async Task BindReceive(AsyncUserUdpToken token, byte bufferSize) ulong _id = memory.Slice(flagBytes.Length).ToUInt64(); if (udptcss.TryRemove(_id, out TaskCompletionSource _tcs)) { - _tcs.SetResult(result.RemoteEndPoint as IPEndPoint); + _tcs.SetResult(source); } continue; } - - IPEndPoint source = result.RemoteEndPoint as IPEndPoint; if (udpConnections.TryGetValue(source, out _)) { continue; @@ -166,6 +166,8 @@ public async Task OnConnectUdp(byte bufferSize, ulong id, IPEndPoint server, IPE Socket serviceUdp = null; buffer = new byte[(1 << bufferSize) * 1024]; IPEndPoint tempEp = new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort); + + UdpConnectedCache cache = new UdpConnectedCache { SourceSocket = socketUdp, TargetSocket = serviceUdp }; while (true) { try @@ -173,8 +175,12 @@ public async Task OnConnectUdp(byte bufferSize, ulong id, IPEndPoint server, IPE SocketReceiveFromResult result = await socketUdp.ReceiveFromAsync(buffer, tempEp).ConfigureAwait(false); if (result.ReceivedBytes == 0) { + serviceUdp?.SafeClose(); + serviceUdp?.Close(); + socketUdp?.Dispose(); break; } + cache.Update(); Memory memory = buffer.AsMemory(0, result.ReceivedBytes); if (serviceUdp == null) @@ -183,19 +189,27 @@ public async Task OnConnectUdp(byte bufferSize, ulong id, IPEndPoint server, IPE serviceUdp.WindowsUdpBug(); await serviceUdp.SendToAsync(memory, service).ConfigureAwait(false); - udpConnectds.TryAdd(id, new UdpConnectedCache { SourceSocket = socketUdp, TargetSocket = serviceUdp }); + cache.TargetSocket = serviceUdp; + udpConnectds.TryAdd(id, cache); TimerHelper.Async(async () => { - buffer = new byte[(1 << bufferSize) * 1024]; + byte[] buffer = new byte[(1 << bufferSize) * 1024]; IPEndPoint tempEp = new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort); while (true) { try { SocketReceiveFromResult result = await serviceUdp.ReceiveFromAsync(buffer, tempEp).ConfigureAwait(false); - if (result.ReceivedBytes == 0) break; + if (result.ReceivedBytes == 0) + { + serviceUdp?.SafeClose(); + serviceUdp?.Close(); + socketUdp?.Dispose(); + break; + } await socketUdp.SendToAsync(buffer.AsMemory(0, result.ReceivedBytes), server).ConfigureAwait(false); + cache.Update(); } catch (Exception ex) { @@ -266,7 +280,7 @@ public void Update() { LastTime = Environment.TickCount64; } - public bool Timeout => Environment.TickCount64 - LastTime > 60 * 60 * 1000; + public bool Timeout => Environment.TickCount64 - LastTime > 60 * 1000; } public sealed class UdpConnectedCache @@ -278,7 +292,7 @@ public void Update() { LastTime = Environment.TickCount64; } - public bool Timeout => Environment.TickCount64 - LastTime > 60*60*1000; + public bool Timeout => Environment.TickCount64 - LastTime > 60 * 1000; public void Clear() { diff --git a/version.txt b/version.txt index ec2b5052..2407cb7d 100644 --- a/version.txt +++ b/version.txt @@ -1,5 +1,5 @@ v1.4.2 -2024-09-23 17:05:56 +2024-09-24 00:50:43 1. 优化服务器代理穿透,延长UDP超时时间,优化穿透添加逻辑 2. 当提示端口被占用时,可以稍等一段时间再尝试启动穿透 3. 优化配置同步 \ No newline at end of file