/* Copyright © 2002, The KPD-Team All rights reserved. http://www.mentalis.org/ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Neither the name of the KPD-Team, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ using System; using System.Net; using System.Net.Sockets; using System.Text; namespace Org.Mentalis.Network.ProxySocket { /// /// Implements the HTTPS (CONNECT) protocol. /// internal sealed class HttpsHandler : SocksHandler { /// /// Initializes a new HttpsHandler instance. /// /// The socket connection with the proxy server. /// server is null. public HttpsHandler(Socket server) : this(server, "") {} /// /// Initializes a new HttpsHandler instance. /// /// The socket connection with the proxy server. /// The username to use. /// server -or- user is null. public HttpsHandler(Socket server, string user) : this(server, user, "") {} /// /// Initializes a new HttpsHandler instance. /// /// The socket connection with the proxy server. /// The username to use. /// The password to use. /// server -or- user -or- pass is null. public HttpsHandler(Socket server, string user, string pass) : base(server, user) { Password = pass; } /// /// Creates an array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. /// /// An array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. private byte[] GetConnectBytes(string host, int port) { StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Format("CONNECT {0}:{1} HTTP/1.1", host, port)); sb.AppendLine(string.Format("Host: {0}:{1}", host, port)); if (!string.IsNullOrEmpty(Username)) { string auth = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", Username, Password))); sb.AppendLine(string.Format("Proxy-Authorization: Basic {0}", auth)); } sb.AppendLine(); byte[] buffer = Encoding.ASCII.GetBytes(sb.ToString()); return buffer; } /// /// Verifies that proxy server successfully connected to requested host /// /// Input data array private void VerifyConnectHeader(byte[] buffer) { string header = Encoding.ASCII.GetString(buffer); if ((!header.StartsWith("HTTP/1.1 ", StringComparison.OrdinalIgnoreCase) && !header.StartsWith("HTTP/1.0 ", StringComparison.OrdinalIgnoreCase)) || !header.EndsWith(" ")) throw new ProtocolViolationException(); string code = header.Substring(9, 3); if (code != "200") throw new ProxyException("Invalid HTTP status. Code: " + code); } /// /// Starts negotiating with the SOCKS server. /// /// The IPEndPoint to connect to. /// remoteEP is null. /// The proxy rejected the request. /// An operating system error occurs while accessing the Socket. /// The Socket has been closed. /// The proxy server uses an invalid protocol. public override void Negotiate(IPEndPoint remoteEP) { if (remoteEP == null) throw new ArgumentNullException(); Negotiate(remoteEP.Address.ToString(), remoteEP.Port); } /// /// Starts negotiating with the SOCKS server. /// /// The host to connect to. /// The port to connect to. /// host is null. /// port is invalid. /// The proxy rejected the request. /// An operating system error occurs while accessing the Socket. /// The Socket has been closed. /// The proxy server uses an invalid protocol. public override void Negotiate(string host, int port) { if (host == null) throw new ArgumentNullException(); if (port <= 0 || port > 65535 || host.Length > 255) throw new ArgumentException(); byte[] buffer = GetConnectBytes(host, port); if (Server.Send(buffer, 0, buffer.Length, SocketFlags.None) < buffer.Length) { throw new SocketException(10054); } buffer = ReadBytes(13); VerifyConnectHeader(buffer); // Read bytes 1 by 1 until we reach "\r\n\r\n" int receivedNewlineChars = 0; buffer = new byte[1]; while (receivedNewlineChars < 4) { int recv = Server.Receive(buffer, 0, 1, SocketFlags.None); if (recv == 0) { throw new SocketException(10054); } byte b = buffer[0]; if (b == (receivedNewlineChars % 2 == 0 ? '\r' : '\n')) receivedNewlineChars++; else receivedNewlineChars = b == '\r' ? 1 : 0; } } /// /// Starts negotiating asynchronously with the HTTPS server. /// /// An IPEndPoint that represents the remote device. /// The method to call when the negotiation is complete. /// The IPEndPoint of the HTTPS proxy server. /// An IAsyncProxyResult that references the asynchronous connection. public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, IPEndPoint proxyEndPoint) { return BeginNegotiate(remoteEP.Address.ToString(), remoteEP.Port, callback, proxyEndPoint); } /// /// Starts negotiating asynchronously with the HTTPS server. /// /// The host to connect to. /// The port to connect to. /// The method to call when the negotiation is complete. /// The IPEndPoint of the HTTPS proxy server. /// An IAsyncProxyResult that references the asynchronous connection. public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, IPEndPoint proxyEndPoint) { ProtocolComplete = callback; Buffer = GetConnectBytes(host, port); Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); AsyncResult = new IAsyncProxyResult(); return AsyncResult; } /// /// Called when the socket is connected to the remote server. /// /// Stores state information for this asynchronous operation as well as any user-defined data. private void OnConnect(IAsyncResult ar) { try { Server.EndConnect(ar); } catch (Exception e) { ProtocolComplete(e); return; } try { Server.BeginSend(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnConnectSent), null); } catch (Exception e) { ProtocolComplete(e); } } /// /// Called when the connect request bytes have been sent. /// /// Stores state information for this asynchronous operation as well as any user-defined data. private void OnConnectSent(IAsyncResult ar) { try { HandleEndSend(ar, Buffer.Length); Buffer = new byte[13]; Received = 0; Server.BeginReceive(Buffer, 0, 13, SocketFlags.None, new AsyncCallback(this.OnConnectReceive), Server); } catch (Exception e) { ProtocolComplete(e); } } /// /// Called when an connect reply has been received. /// /// Stores state information for this asynchronous operation as well as any user-defined data. private void OnConnectReceive(IAsyncResult ar) { try { HandleEndReceive(ar); } catch (Exception e) { ProtocolComplete(e); return; } try { if (Received < 13) { Server.BeginReceive(Buffer, Received, 13 - Received, SocketFlags.None, new AsyncCallback(this.OnConnectReceive), Server); } else { VerifyConnectHeader(Buffer); ReadUntilHeadersEnd(true); } } catch (Exception e) { ProtocolComplete(e); } } /// /// Reads socket buffer byte by byte until we reach "\r\n\r\n". /// /// private void ReadUntilHeadersEnd(bool readFirstByte) { while (Server.Available > 0 && m_receivedNewlineChars < 4) { if (!readFirstByte) readFirstByte = false; else { int recv = Server.Receive(Buffer, 0, 1, SocketFlags.None); if (recv == 0) throw new SocketException(10054); } if (Buffer[0] == (m_receivedNewlineChars % 2 == 0 ? '\r' : '\n')) m_receivedNewlineChars++; else m_receivedNewlineChars = Buffer[0] == '\r' ? 1 : 0; } if (m_receivedNewlineChars == 4) { ProtocolComplete(null); } else { Server.BeginReceive(Buffer, 0, 1, SocketFlags.None, new AsyncCallback(this.OnEndHeadersReceive), Server); } } // I think we should never reach this function in practice // But let's define it just in case /// /// Called when additional headers have been received. /// /// Stores state information for this asynchronous operation as well as any user-defined data. private void OnEndHeadersReceive(IAsyncResult ar) { try { HandleEndReceive(ar); ReadUntilHeadersEnd(false); } catch (Exception e) { ProtocolComplete(e); } } /// /// Gets or sets the password to use when authenticating with the HTTPS server. /// /// The password to use when authenticating with the HTTPS server. private string Password { get { return m_Password; } set { if (value == null) throw new ArgumentNullException(); m_Password = value; } } // private variables /// Holds the value of the Password property. private string m_Password; /// Holds the count of newline characters received. private int m_receivedNewlineChars; } }