| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Net; |
| | | 5 | | using System.Net.Sockets; |
| | | 6 | | using System.Text.RegularExpressions; |
| | | 7 | | |
| | | 8 | | using Renci.SshNet.Abstractions; |
| | | 9 | | using Renci.SshNet.Common; |
| | | 10 | | |
| | | 11 | | namespace Renci.SshNet.Connection |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Establishes a tunnel via an HTTP proxy server. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// <list type="table"> |
| | | 18 | | /// <listheader> |
| | | 19 | | /// <term>Specification</term> |
| | | 20 | | /// <description>URL</description> |
| | | 21 | | /// </listheader> |
| | | 22 | | /// <item> |
| | | 23 | | /// <term>HTTP CONNECT method</term> |
| | | 24 | | /// <description>https://tools.ietf.org/html/rfc7231#section-4.3.6</description> |
| | | 25 | | /// </item> |
| | | 26 | | /// <item> |
| | | 27 | | /// <term>HTTP Authentication: Basic and Digest Access Authentication</term> |
| | | 28 | | /// <description>https://tools.ietf.org/html/rfc2617</description> |
| | | 29 | | /// </item> |
| | | 30 | | /// </list> |
| | | 31 | | /// </remarks> |
| | | 32 | | internal sealed class HttpConnector : ProxyConnector |
| | | 33 | | { |
| | | 34 | | public HttpConnector(ISocketFactory socketFactory) |
| | 222 | 35 | | : base(socketFactory) |
| | 222 | 36 | | { |
| | 222 | 37 | | } |
| | | 38 | | |
| | | 39 | | protected override void HandleProxyConnect(IConnectionInfo connectionInfo, Socket socket) |
| | 189 | 40 | | { |
| | 189 | 41 | | var httpResponseRe = new Regex(@"HTTP/(?<version>\d[.]\d) (?<statusCode>\d{3}) (?<reasonPhrase>.+)$"); |
| | 189 | 42 | | var httpHeaderRe = new Regex(@"(?<fieldName>[^\[\]()<>@,;:\""/?={} \t]+):(?<fieldValue>.+)?"); |
| | | 43 | | |
| | 189 | 44 | | SocketAbstraction.Send(socket, SshData.Ascii.GetBytes(string.Format(CultureInfo.InvariantCulture, |
| | 189 | 45 | | "CONNECT {0}:{1} HTTP/1.0\r\n", |
| | 189 | 46 | | connectionInfo.Host, |
| | 189 | 47 | | connectionInfo.Port))); |
| | | 48 | | |
| | | 49 | | // Send proxy authorization if specified |
| | 189 | 50 | | if (!string.IsNullOrEmpty(connectionInfo.ProxyUsername)) |
| | 159 | 51 | | { |
| | 159 | 52 | | var authorization = string.Format(CultureInfo.InvariantCulture, |
| | 159 | 53 | | "Proxy-Authorization: Basic {0}\r\n", |
| | 159 | 54 | | Convert.ToBase64String(SshData.Ascii.GetBytes($"{connectionInfo.ProxyU |
| | 159 | 55 | | SocketAbstraction.Send(socket, SshData.Ascii.GetBytes(authorization)); |
| | 159 | 56 | | } |
| | | 57 | | |
| | 189 | 58 | | SocketAbstraction.Send(socket, SshData.Ascii.GetBytes("\r\n")); |
| | | 59 | | |
| | 189 | 60 | | HttpStatusCode? statusCode = null; |
| | 189 | 61 | | var contentLength = 0; |
| | | 62 | | |
| | 660 | 63 | | while (true) |
| | 660 | 64 | | { |
| | 660 | 65 | | var response = SocketReadLine(socket, connectionInfo.Timeout); |
| | 645 | 66 | | if (response is null) |
| | 24 | 67 | | { |
| | | 68 | | // server shut down socket |
| | 24 | 69 | | break; |
| | | 70 | | } |
| | | 71 | | |
| | 621 | 72 | | if (statusCode is null) |
| | 312 | 73 | | { |
| | 312 | 74 | | var statusMatch = httpResponseRe.Match(response); |
| | 312 | 75 | | if (statusMatch.Success) |
| | 150 | 76 | | { |
| | 150 | 77 | | var httpStatusCode = statusMatch.Result("${statusCode}"); |
| | 150 | 78 | | statusCode = (HttpStatusCode) int.Parse(httpStatusCode, CultureInfo.InvariantCulture); |
| | 150 | 79 | | if (statusCode != HttpStatusCode.OK) |
| | 12 | 80 | | { |
| | 12 | 81 | | throw new ProxyException($"HTTP: Status code {httpStatusCode}, \"{statusMatch.Result("${reas |
| | | 82 | | } |
| | 138 | 83 | | } |
| | | 84 | | |
| | 300 | 85 | | continue; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | // continue on parsing message headers coming from the server |
| | 309 | 89 | | var headerMatch = httpHeaderRe.Match(response); |
| | 309 | 90 | | if (headerMatch.Success) |
| | 171 | 91 | | { |
| | 171 | 92 | | var fieldName = headerMatch.Result("${fieldName}"); |
| | 171 | 93 | | if (fieldName.Equals("Content-Length", StringComparison.OrdinalIgnoreCase)) |
| | 33 | 94 | | { |
| | 33 | 95 | | contentLength = int.Parse(headerMatch.Result("${fieldValue}"), CultureInfo.InvariantCulture); |
| | 33 | 96 | | } |
| | | 97 | | |
| | 171 | 98 | | continue; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | // check if we've reached the CRLF which separates request line and headers from the message body |
| | 138 | 102 | | if (response.Length == 0) |
| | 138 | 103 | | { |
| | | 104 | | // read response body if specified |
| | 138 | 105 | | if (contentLength > 0) |
| | 33 | 106 | | { |
| | 33 | 107 | | var contentBody = new byte[contentLength]; |
| | 33 | 108 | | _ = SocketRead(socket, contentBody, 0, contentLength, connectionInfo.Timeout); |
| | 15 | 109 | | } |
| | | 110 | | |
| | 120 | 111 | | break; |
| | | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | |
| | 144 | 115 | | if (statusCode is null) |
| | 24 | 116 | | { |
| | 24 | 117 | | throw new ProxyException("HTTP response does not contain status line."); |
| | | 118 | | } |
| | 120 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Performs a blocking read on the socket until a line is read. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="socket">The <see cref="Socket"/> to read from.</param> |
| | | 125 | | /// <param name="readTimeout">A <see cref="TimeSpan"/> that represents the time to wait until a line is read.</p |
| | | 126 | | /// <exception cref="SshOperationTimeoutException">The read has timed-out.</exception> |
| | | 127 | | /// <exception cref="SocketException">An error occurred when trying to access the socket.</exception> |
| | | 128 | | /// <returns> |
| | | 129 | | /// The line read from the socket, or <see langword="null"/> when the remote server has shutdown and all data ha |
| | | 130 | | /// </returns> |
| | | 131 | | private static string SocketReadLine(Socket socket, TimeSpan readTimeout) |
| | 660 | 132 | | { |
| | 660 | 133 | | var encoding = SshData.Ascii; |
| | 660 | 134 | | var buffer = new List<byte>(); |
| | 660 | 135 | | var data = new byte[1]; |
| | | 136 | | |
| | | 137 | | // read data one byte at a time to find end of line and leave any unhandled information in the buffer |
| | | 138 | | // to be processed by subsequent invocations |
| | | 139 | | do |
| | 10350 | 140 | | { |
| | 10350 | 141 | | var bytesRead = SocketAbstraction.Read(socket, data, 0, data.Length, readTimeout); |
| | 10335 | 142 | | if (bytesRead == 0) |
| | 24 | 143 | | { |
| | | 144 | | // the remote server shut down the socket |
| | 24 | 145 | | break; |
| | | 146 | | } |
| | | 147 | | |
| | 10311 | 148 | | var b = data[0]; |
| | 10311 | 149 | | buffer.Add(b); |
| | | 150 | | |
| | 10311 | 151 | | if (b == Session.LineFeed && buffer.Count > 1 && buffer[buffer.Count - 2] == Session.CarriageReturn) |
| | 621 | 152 | | { |
| | | 153 | | // Return line without CRLF |
| | 621 | 154 | | return encoding.GetString(buffer.ToArray(), 0, buffer.Count - 2); |
| | | 155 | | } |
| | 9690 | 156 | | } |
| | 9690 | 157 | | while (true); |
| | | 158 | | |
| | 24 | 159 | | if (buffer.Count == 0) |
| | 24 | 160 | | { |
| | 24 | 161 | | return null; |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | return encoding.GetString(buffer.ToArray(), 0, buffer.Count); |
| | 645 | 165 | | } |
| | | 166 | | } |
| | | 167 | | } |