Skip to content

Commit

Permalink
fix UDP port forwarding failures
Browse files Browse the repository at this point in the history
  • Loading branch information
yoli799480165 committed Jul 2, 2024
1 parent cbe9b0f commit 06275e6
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 119 deletions.
20 changes: 15 additions & 5 deletions src/Chaldea.Fate.RhoAias/ClientHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private void CreateForwarder(string requestId, Proxy proxy)
_logger.LogInformation($"CreateForwarder: {proxy.LocalIP}:{proxy.LocalPort}");
var cancellationToken = CancellationToken.None;
using (var serverStream = await CreateRemote(requestId, cancellationToken))
using (var localStream = await CreateLocal(requestId, proxy.LocalIP, proxy.LocalPort, cancellationToken))
using (var localStream = await CreateLocal(proxy.Type, proxy.LocalIP, proxy.LocalPort, cancellationToken))
{
if (proxy.Compressed)
{
Expand All @@ -191,10 +191,20 @@ private void CreateForwarder(string requestId, Proxy proxy)
});
}

private async Task<Stream> CreateLocal(string requestId, string localIp, int port, CancellationToken cancellationToken)
private async Task<Stream> CreateLocal(ProxyType type, string localIp, int port, CancellationToken cancellationToken)
{
var socket = await ConnectAsync(localIp, port);
return new NetworkStream(socket, true) { ReadTimeout = 1000 * 60 * 10 };
if (type == ProxyType.UDP)
{
_logger.LogInformation($"Create socket: {localIp}:{port}");
var client = new UdpClient();
client.Connect(localIp, port);
return client.GetStream();
}
else
{
var socket = await ConnectAsync(localIp, port);
return new NetworkStream(socket, true) { ReadTimeout = 1000 * 60 * 10 };
}
}

private async Task<Stream> CreateRemote(string requestId, CancellationToken cancellationToken)
Expand Down Expand Up @@ -252,4 +262,4 @@ private async Task<Socket> ConnectAsync(string host, int port)

return null;
}
}
}
55 changes: 2 additions & 53 deletions src/Chaldea.Fate.RhoAias/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Buffers;
using System.Security.Claims;
using System.Security.Claims;

namespace Chaldea.Fate.RhoAias;

Expand All @@ -15,54 +14,4 @@ public static Guid UserId(this ClaimsPrincipal user)

return Guid.Empty;
}

public static async Task CopyToAsync(this Stream source, Stream destination, bool flush, CancellationToken cancellationToken)
{
const int DefaultCopyBufferSize = 81920;
var bufferSize = DefaultCopyBufferSize;
if (source.CanSeek)
{
var length = source.Length;
var position = source.Position;
if (length <= position)
{
bufferSize = 1;
}
else
{
var remaining = length - position;
if (remaining > 0)
{
bufferSize = (int)Math.Min(bufferSize, remaining);
}
}
}

ArgumentNullException.ThrowIfNull(destination);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(bufferSize);
if (!destination.CanWrite)
{
if (destination.CanRead)
{
throw new Exception();
}

throw new Exception();
}

var buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
try
{
int bytesRead;
while ((bytesRead = await source.ReadAsync(new Memory<byte>(buffer), cancellationToken).ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(new ReadOnlyMemory<byte>(buffer, 0, bytesRead), cancellationToken).ConfigureAwait(false);
if (flush) await destination.FlushAsync(cancellationToken).ConfigureAwait(false);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
}
}
Loading

0 comments on commit 06275e6

Please sign in to comment.