Skip to content

Commit

Permalink
sync
Browse files Browse the repository at this point in the history
  • Loading branch information
snltty committed Sep 23, 2024
1 parent ad111da commit eaab9c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
32 changes: 23 additions & 9 deletions linker/plugins/sforward/proxy/SForwardProxyUdp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ private async Task BindReceive(AsyncUserUdpToken token, byte bufferSize)
}
Memory<byte> 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
Expand All @@ -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<IPEndPoint> _tcs))
{
_tcs.SetResult(result.RemoteEndPoint as IPEndPoint);
_tcs.SetResult(source);
}
continue;
}


IPEndPoint source = result.RemoteEndPoint as IPEndPoint;
if (udpConnections.TryGetValue(source, out _))
{
continue;
Expand Down Expand Up @@ -166,15 +166,21 @@ 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
{
SocketReceiveFromResult result = await socketUdp.ReceiveFromAsync(buffer, tempEp).ConfigureAwait(false);
if (result.ReceivedBytes == 0)
{
serviceUdp?.SafeClose();
serviceUdp?.Close();
socketUdp?.Dispose();
break;
}
cache.Update();

Memory<byte> memory = buffer.AsMemory(0, result.ReceivedBytes);
if (serviceUdp == null)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
v1.4.2
2024-09-23 17:05:56
2024-09-24 00:50:43
1. 优化服务器代理穿透,延长UDP超时时间,优化穿透添加逻辑
2. 当提示端口被占用时,可以稍等一段时间再尝试启动穿透
3. 优化配置同步

0 comments on commit eaab9c9

Please sign in to comment.