Exemple #1
0
        Task RunLoopAsync(CancellationToken cancellationToken, ITcpStream remoteStream)
        {
            var sendLoop = Task.Run(
                () => this.CopyAsync(this._clientStream, remoteStream, cancellationToken),
                cancellationToken);

            var receiveLoop = Task.Run(
                () => this.CopyAsync(remoteStream, this._clientStream, cancellationToken),
                cancellationToken);

            return(Task.WhenAll(sendLoop, receiveLoop));
        }
        public SocksConnection(
            ITcpStream stream,
            IProxyFactory proxyFactory,
            ArrayPool <byte> bufferPool,
            ILoggerFactory loggerFactory)
        {
            this._clientStream = stream ?? throw new ArgumentNullException(nameof(stream));
            this._proxyFactory = proxyFactory ?? throw new ArgumentNullException(nameof(proxyFactory));
            this._bufferPool   = bufferPool ?? throw new ArgumentNullException(nameof(bufferPool));

            this._cancellationTokenSource = new CancellationTokenSource();
            this._log = loggerFactory.CreateLogger(this.GetType().Name);
        }
Exemple #3
0
        async Task DoServerLoop()
        {
            var cancellationToken = this._cancellationTokenSource.Token;

            using (cancellationToken.Register(o => ((IDisposable)o).Dispose(), this._listener))
            {
                ITcpStream       stream     = null;
                ISocksConnection connection = null;
                try
                {
                    while (true)
                    {
                        stream = await this._listener.GetNextClientStreamAsync().ConfigureAwait(false);

                        cancellationToken.ThrowIfCancellationRequested();

                        this._log.LogDebug("Accepted connection from {0}", stream.RemoteEndPoint);
                        connection = this._connectionFactory.Create(stream);
                        this._connections.Add(connection);
                        connection.StartHandlingClient();
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                }
                catch (Exception ex) when(ex is OperationCanceledException || ex is ObjectDisposedException)
                {
                    try
                    {
                        connection?.Dispose();
                    }
                    catch
                    {
                        // ignore
                    }

                    try
                    {
                        stream?.Dispose();
                    }
                    catch
                    {
                        // ignore
                    }
                }
                catch (Exception ex)
                {
                    this._log.LogError(ex, "Unhandled exception in background listener loop");
                }
            }
        }
Exemple #4
0
        public TcpProxy(
            ITcpStream clientStream,
            EndPoint destinationEndPoint,
            ITcpConnector tcpConnector,
            ArrayPool <byte> bufferPool)
        {
            this._clientStream        = clientStream ?? throw new ArgumentNullException(nameof(clientStream));
            this._destinationEndPoint = destinationEndPoint
                                        ?? throw new ArgumentNullException(nameof(destinationEndPoint));
            this._tcpConnector = tcpConnector ?? throw new ArgumentNullException(nameof(tcpConnector));
            this._bufferPool   = bufferPool ?? throw new ArgumentNullException(nameof(bufferPool));

            if (!(destinationEndPoint is IPEndPoint) && !(destinationEndPoint is DnsEndPoint))
            {
                throw new ArgumentException(
                          "EndPoint must be either IPEndPoint or DnsEndPoint",
                          nameof(destinationEndPoint));
            }
        }
 public IProxy CreateTcpProxy(ITcpStream clientStream, EndPoint destinationEndPoint, ArrayPool <byte> bufferPool)
 {
     return(new TcpProxy(clientStream, destinationEndPoint, this._tcpConnector, bufferPool));
 }
 public FakeTcpListener(ITcpStream stagedTcpStream)
 {
     this._tcpStream = stagedTcpStream ?? throw new ArgumentNullException(nameof(stagedTcpStream));
 }
 public IProxy CreateTcpProxy(ITcpStream clientStream, EndPoint destinationEndPoint, ArrayPool <byte> bufferPool)
 {
     this.TcpProxyCreated = true;
     return(this.NextTcpProxyToReturn ?? new FakeProxy());
 }
Exemple #8
0
 public ISocksConnection Create(ITcpStream stream)
 {
     return(ActivatorUtilities.CreateInstance <SocksConnection>(this._serviceProvider, stream));
 }