Skip to content

Commit

Permalink
fix: SOCKS5 UDP service
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Aug 24, 2021
1 parent e1ce383 commit e317674
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Shadowsocks.Protocol/LocalTcpServices/Socks5Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public async ValueTask HandleAsync(IDuplexPipe pipe, CancellationToken token = d
Port = Socks5CreateOption.Port
};
await socks5.SendReplyAsync(Socks5Reply.Succeeded, bound, token);

// wait remote close
var result = await pipe.Input.ReadAsync(token);
Report.IfNot(result.IsCompleted);
break;
}
default:
Expand Down
10 changes: 7 additions & 3 deletions Shadowsocks.Protocol/LocalUdpServices/Socks5UdpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.Threading;
using Shadowsocks.Protocol.ServersControllers;
using Shadowsocks.Protocol.UdpClients;
using Socks5.Enums;
using Socks5.Utils;
using System;
Expand Down Expand Up @@ -69,9 +70,12 @@ public async ValueTask HandleAsync(UdpReceiveResult receiveResult, UdpClient inc
_ => socks5UdpPacket.Address!.ToString()
};

var client = await _cache.GetOrCreateAsync(
receiveResult.RemoteEndPoint,
_ => _serversController.GetServerUdpAsync(target).AsTask());
if (!_cache.TryGetValue(receiveResult.RemoteEndPoint, out IUdpClient client))
{
client = await _serversController.GetServerUdpAsync(target);

_cache.Set(receiveResult.RemoteEndPoint, client, _cacheOptions);
}

_logger.LogInformation(@"Udp Send to {0} via {1}", target, client);
var sendBuffer = receiveResult.Buffer.AsMemory(3); //TODO Only support ss now
Expand Down

0 comments on commit e317674

Please sign in to comment.