protected async override Task CreateConnectionAsync(TcpClient client, string destinationHost, int destinationPort, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(destinationHost)) { throw new ArgumentException(null, nameof(destinationHost)); } if (!PortHelper.ValidateTcpPort(destinationPort)) { throw new ArgumentOutOfRangeException(nameof(destinationPort)); } if (client == null || !client.Connected) { throw new SocketException(); } try { await RequestConnectionAsync(client.GetStream(), CommandConnect, destinationHost, destinationPort, cancellationToken) .ConfigureAwait(false); } catch (Exception ex) { client.Close(); if (ex is IOException || ex is SocketException) { throw new ProxyException("Error while working with proxy", ex); } throw; } }
/// /// <inheritdoc/> protected async override Task CreateConnectionAsync(TcpClient client, string destinationHost, int destinationPort, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(destinationHost)) { throw new ArgumentException(null, nameof(destinationHost)); } if (!PortHelper.ValidateTcpPort(destinationPort)) { throw new ArgumentOutOfRangeException(nameof(destinationPort)); } if (client == null || !client.Connected) { throw new SocketException(); } HttpStatusCode statusCode; try { var nStream = client.GetStream(); await RequestConnectionAsync(nStream, destinationHost, destinationPort, cancellationToken).ConfigureAwait(false); statusCode = await ReceiveResponseAsync(nStream, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { client.Close(); if (ex is IOException || ex is SocketException) { throw new ProxyException("Error while working with proxy", ex); } throw; } if (statusCode != HttpStatusCode.OK) { client.Close(); throw new ProxyException("The proxy didn't reply with 200 OK"); } }
protected override Task CreateConnectionAsync(TcpClient client, string destinationHost, int destinationPort, CancellationToken cancellationToken = default) { if (string.IsNullOrEmpty(destinationHost)) { throw new ArgumentException(null, nameof(destinationHost)); } if (!PortHelper.ValidateTcpPort(destinationPort)) { throw new ArgumentOutOfRangeException(nameof(destinationPort)); } if (client == null || !client.Connected) { throw new SocketException(); } return(Task.CompletedTask); }