|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net.Sockets; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.IO; |
| 8 | +using System.Net.Security; |
| 9 | +using Titanium.Web.Proxy.Helpers; |
| 10 | +using System.Threading; |
| 11 | +using Titanium.Web.Proxy.Extensions; |
| 12 | +using Titanium.Web.Proxy.Models; |
| 13 | +using System.Security.Authentication; |
| 14 | + |
| 15 | +namespace Titanium.Web.Proxy.Network |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// A class that manages Tcp Connection to server used by this proxy server |
| 19 | + /// </summary> |
| 20 | + internal class TcpConnectionFactory |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// Get a TcpConnection to the specified host, port optionally HTTPS and a particular HTTP version |
| 24 | + /// </summary> |
| 25 | + /// <param name="hostname"></param> |
| 26 | + /// <param name="port"></param> |
| 27 | + /// <param name="isHttps"></param> |
| 28 | + /// <param name="version"></param> |
| 29 | + /// <returns></returns> |
| 30 | + internal async Task<TcpConnection> GetClient(string hostname, int port, bool isHttps, Version version, |
| 31 | + ExternalProxy upStreamHttpProxy, ExternalProxy upStreamHttpsProxy, int bufferSize, SslProtocols supportedSslProtocols, |
| 32 | + int connectionTimeOutSeconds, |
| 33 | + RemoteCertificateValidationCallback remoteCertificateValidationCallBack, |
| 34 | + LocalCertificateSelectionCallback localCertificateSelectionCallback) |
| 35 | + { |
| 36 | + //not in cache so create and return |
| 37 | + return await CreateClient(hostname, port, isHttps, version, connectionTimeOutSeconds, upStreamHttpProxy, upStreamHttpsProxy, bufferSize, supportedSslProtocols, |
| 38 | + remoteCertificateValidationCallBack, localCertificateSelectionCallback); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Create connection to a particular host/port optionally with SSL and a particular HTTP version |
| 45 | + /// </summary> |
| 46 | + /// <param name="hostname"></param> |
| 47 | + /// <param name="port"></param> |
| 48 | + /// <param name="isHttps"></param> |
| 49 | + /// <param name="version"></param> |
| 50 | + /// <returns></returns> |
| 51 | + private async Task<TcpConnection> CreateClient(string hostname, int port, bool isHttps, Version version, int connectionTimeOutSeconds, |
| 52 | + ExternalProxy upStreamHttpProxy, ExternalProxy upStreamHttpsProxy, int bufferSize, SslProtocols supportedSslProtocols, |
| 53 | + RemoteCertificateValidationCallback remoteCertificateValidationCallBack, LocalCertificateSelectionCallback localCertificateSelectionCallback) |
| 54 | + { |
| 55 | + TcpClient client; |
| 56 | + Stream stream; |
| 57 | + |
| 58 | + if (isHttps) |
| 59 | + { |
| 60 | + SslStream sslStream = null; |
| 61 | + |
| 62 | + //If this proxy uses another external proxy then create a tunnel request for HTTPS connections |
| 63 | + if (upStreamHttpsProxy != null) |
| 64 | + { |
| 65 | + client = new TcpClient(upStreamHttpsProxy.HostName, upStreamHttpsProxy.Port); |
| 66 | + stream = client.GetStream(); |
| 67 | + |
| 68 | + using (var writer = new StreamWriter(stream, Encoding.ASCII, bufferSize, true)) |
| 69 | + { |
| 70 | + await writer.WriteLineAsync(string.Format("CONNECT {0}:{1} {2}", hostname, port, version)); |
| 71 | + await writer.WriteLineAsync(string.Format("Host: {0}:{1}", hostname, port)); |
| 72 | + await writer.WriteLineAsync("Connection: Keep-Alive"); |
| 73 | + await writer.WriteLineAsync(); |
| 74 | + await writer.FlushAsync(); |
| 75 | + writer.Close(); |
| 76 | + } |
| 77 | + |
| 78 | + using (var reader = new CustomBinaryReader(stream)) |
| 79 | + { |
| 80 | + var result = await reader.ReadLineAsync(); |
| 81 | + |
| 82 | + if (!result.ToLower().Contains("200 connection established")) |
| 83 | + { |
| 84 | + throw new Exception("Upstream proxy failed to create a secure tunnel"); |
| 85 | + } |
| 86 | + |
| 87 | + await reader.ReadAllLinesAsync(); |
| 88 | + } |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + client = new TcpClient(hostname, port); |
| 93 | + stream = client.GetStream(); |
| 94 | + } |
| 95 | + |
| 96 | + try |
| 97 | + { |
| 98 | + sslStream = new SslStream(stream, true, remoteCertificateValidationCallBack, |
| 99 | + localCertificateSelectionCallback); |
| 100 | + |
| 101 | + await sslStream.AuthenticateAsClientAsync(hostname, null, supportedSslProtocols, false); |
| 102 | + |
| 103 | + stream = sslStream; |
| 104 | + } |
| 105 | + catch |
| 106 | + { |
| 107 | + if (sslStream != null) |
| 108 | + { |
| 109 | + sslStream.Dispose(); |
| 110 | + } |
| 111 | + |
| 112 | + throw; |
| 113 | + } |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + if (upStreamHttpProxy != null) |
| 118 | + { |
| 119 | + client = new TcpClient(upStreamHttpProxy.HostName, upStreamHttpProxy.Port); |
| 120 | + stream = client.GetStream(); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + client = new TcpClient(hostname, port); |
| 125 | + stream = client.GetStream(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + client.ReceiveTimeout = connectionTimeOutSeconds * 1000; |
| 130 | + client.SendTimeout = connectionTimeOutSeconds * 1000; |
| 131 | + |
| 132 | + return new TcpConnection() |
| 133 | + { |
| 134 | + HostName = hostname, |
| 135 | + port = port, |
| 136 | + IsHttps = isHttps, |
| 137 | + TcpClient = client, |
| 138 | + StreamReader = new CustomBinaryReader(stream), |
| 139 | + Stream = stream, |
| 140 | + Version = version |
| 141 | + }; |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + } |
| 146 | +} |
0 commit comments