TlsOverSocks5Proxy.cs 872 B

123456789101112131415161718192021222324252627282930313233343536
  1. using Pipelines.Extensions;
  2. using Socks5.Models;
  3. using System.IO.Pipelines;
  4. using System.Net;
  5. using System.Net.Security;
  6. namespace STUN.Proxy;
  7. public class TlsOverSocks5Proxy : Socks5TcpProxy
  8. {
  9. private SslStream? _tlsStream;
  10. private readonly string _targetHost;
  11. public TlsOverSocks5Proxy(Socks5CreateOption socks5Options, string targetHost) : base(socks5Options)
  12. {
  13. _targetHost = targetHost;
  14. }
  15. public override async ValueTask<IDuplexPipe> ConnectAsync(IPEndPoint local, IPEndPoint dst, CancellationToken cancellationToken = default)
  16. {
  17. IDuplexPipe pipe = await base.ConnectAsync(local, dst, cancellationToken);
  18. _tlsStream = new SslStream(pipe.AsStream(true));
  19. await _tlsStream.AuthenticateAsClientAsync(_targetHost);
  20. return _tlsStream.AsDuplexPipe();
  21. }
  22. protected override void CloseClient()
  23. {
  24. _tlsStream?.Dispose();
  25. base.CloseClient();
  26. }
  27. }