diff --git a/examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs b/examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs index f7ffeb7b9..9837de64e 100644 --- a/examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs +++ b/examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs @@ -90,8 +90,12 @@ public void StartProxy() //}; //proxyServer.AddEndPoint(transparentEndPoint); - //proxyServer.UpStreamHttpProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 }; - //proxyServer.UpStreamHttpsProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 }; + //proxyServer.UpStreamHttpProxy = new ExternalProxy("localhost", 8888); + //proxyServer.UpStreamHttpsProxy = new ExternalProxy("localhost", 8888); + + // SOCKS proxy + //proxyServer.UpStreamHttpProxy = new ExternalProxy("46.63.0.17", 4145) { ProxyType = ExternalProxyType.Socks4 }; + //proxyServer.UpStreamHttpsProxy = new ExternalProxy("46.63.0.17", 4145) { ProxyType = ExternalProxyType.Socks4 }; foreach (var endPoint in proxyServer.ProxyEndPoints) { diff --git a/src/Titanium.Web.Proxy/Extensions/TcpExtensions.cs b/src/Titanium.Web.Proxy/Extensions/TcpExtensions.cs index a4d07e3ba..4e7a41aea 100644 --- a/src/Titanium.Web.Proxy/Extensions/TcpExtensions.cs +++ b/src/Titanium.Web.Proxy/Extensions/TcpExtensions.cs @@ -5,24 +5,6 @@ namespace Titanium.Web.Proxy.Extensions { internal static class TcpExtensions { - internal static void CloseSocket(this TcpClient tcpClient) - { - if (tcpClient == null) - { - return; - } - - try - { - tcpClient.Close(); - } - catch - { - // ignored - } - } - - /// /// Check if a TcpClient is good to be used. /// This only checks if send is working so local socket is still connected. @@ -30,13 +12,11 @@ internal static void CloseSocket(this TcpClient tcpClient) /// So in our case we should retry with new connection from pool if first read after getting the connection fails. /// https://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected(v=vs.110).aspx /// - /// + /// /// - internal static bool IsGoodConnection(this TcpClient client) + internal static bool IsGoodConnection(this Socket socket) { - var socket = client.Client; - - if (!client.Connected || !socket.Connected) + if (!socket.Connected) { return false; } diff --git a/src/Titanium.Web.Proxy/Http/HttpWebClient.cs b/src/Titanium.Web.Proxy/Http/HttpWebClient.cs index 092728095..b027c3d44 100644 --- a/src/Titanium.Web.Proxy/Http/HttpWebClient.cs +++ b/src/Titanium.Web.Proxy/Http/HttpWebClient.cs @@ -2,6 +2,7 @@ using System.Net; using System.Threading; using System.Threading.Tasks; +using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Network.Tcp; namespace Titanium.Web.Proxy.Http @@ -103,10 +104,14 @@ internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTranspar { var upstreamProxy = Connection.UpStreamProxy; - bool useUpstreamProxy = upstreamProxy != null && Connection.IsHttps == false; + bool useUpstreamProxy = upstreamProxy != null && upstreamProxy.ProxyType == ExternalProxyType.Http && + !Connection.IsHttps; var serverStream = Connection.Stream; + string? upstreamProxyUserName = null; + string? upstreamProxyPassword = null; + string url; if (!useUpstreamProxy || isTransparent) { @@ -115,19 +120,13 @@ internal async Task SendRequest(bool enable100ContinueBehaviour, bool isTranspar else { url = Request.RequestUri.ToString(); - } - - string? upstreamProxyUserName = null; - string? upstreamProxyPassword = null; - // Send Authentication to Upstream proxy if needed - if (!isTransparent && upstreamProxy != null - && Connection.IsHttps == false - && !string.IsNullOrEmpty(upstreamProxy.UserName) - && upstreamProxy.Password != null) - { - upstreamProxyUserName = upstreamProxy.UserName; - upstreamProxyPassword = upstreamProxy.Password; + // Send Authentication to Upstream proxy if needed + if (!string.IsNullOrEmpty(upstreamProxy!.UserName) && upstreamProxy.Password != null) + { + upstreamProxyUserName = upstreamProxy.UserName; + upstreamProxyPassword = upstreamProxy.Password; + } } // prepare the request & headers diff --git a/src/Titanium.Web.Proxy/Models/ExternalProxy.cs b/src/Titanium.Web.Proxy/Models/ExternalProxy.cs index 70ae99caf..3f09aee62 100644 --- a/src/Titanium.Web.Proxy/Models/ExternalProxy.cs +++ b/src/Titanium.Web.Proxy/Models/ExternalProxy.cs @@ -25,6 +25,8 @@ public class ExternalProxy : IExternalProxy /// public bool BypassLocalhost { get; set; } + public ExternalProxyType ProxyType { get; set; } + /// /// Username. /// @@ -111,4 +113,16 @@ public override string ToString() return $"{HostName}:{Port}"; } } + + public enum ExternalProxyType + { + /// A HTTP/HTTPS proxy server. + Http, + + /// A SOCKS4[A] proxy server. + Socks4, + + /// A SOCKS5 proxy server. + Socks5 + } } diff --git a/src/Titanium.Web.Proxy/Models/IExternalProxy.cs b/src/Titanium.Web.Proxy/Models/IExternalProxy.cs index 7eb877859..cbfe6e41c 100644 --- a/src/Titanium.Web.Proxy/Models/IExternalProxy.cs +++ b/src/Titanium.Web.Proxy/Models/IExternalProxy.cs @@ -12,6 +12,8 @@ public interface IExternalProxy /// bool BypassLocalhost { get; set; } + ExternalProxyType ProxyType { get; set; } + /// /// Username. /// diff --git a/src/Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs b/src/Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs index 9e351a522..7e55681a8 100644 --- a/src/Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs +++ b/src/Titanium.Web.Proxy/Network/Tcp/TcpClientConnection.cs @@ -18,9 +18,9 @@ internal class TcpClientConnection : IDisposable { public object ClientUserData { get; set; } - internal TcpClientConnection(ProxyServer proxyServer, TcpClient tcpClient) + internal TcpClientConnection(ProxyServer proxyServer, Socket tcpClientSocket) { - this.tcpClient = tcpClient; + this.tcpClientSocket = tcpClientSocket; this.proxyServer = proxyServer; this.proxyServer.UpdateClientConnectionCount(true); } @@ -29,21 +29,21 @@ internal TcpClientConnection(ProxyServer proxyServer, TcpClient tcpClient) public Guid Id { get; } = Guid.NewGuid(); - public EndPoint LocalEndPoint => tcpClient.Client.LocalEndPoint; + public EndPoint LocalEndPoint => tcpClientSocket.LocalEndPoint; - public EndPoint RemoteEndPoint => tcpClient.Client.RemoteEndPoint; + public EndPoint RemoteEndPoint => tcpClientSocket.RemoteEndPoint; internal SslProtocols SslProtocol { get; set; } internal SslApplicationProtocol NegotiatedApplicationProtocol { get; set; } - private readonly TcpClient tcpClient; + private readonly Socket tcpClientSocket; private int? processId; public Stream GetStream() { - return tcpClient.GetStream(); + return new NetworkStream(tcpClientSocket, true); } public int GetProcessId(ProxyEndPoint endPoint) @@ -86,7 +86,15 @@ public void Dispose() // This way we can push tcp Time_Wait to client side when possible. await Task.Delay(1000); proxyServer.UpdateClientConnectionCount(false); - tcpClient.CloseSocket(); + + try + { + tcpClientSocket.Close(); + } + catch + { + // ignore + } }); } } diff --git a/src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs b/src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs index 62d9e935e..c22fa8506 100644 --- a/src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs +++ b/src/Titanium.Web.Proxy/Network/Tcp/TcpConnectionFactory.cs @@ -15,6 +15,7 @@ using Titanium.Web.Proxy.Helpers; using Titanium.Web.Proxy.Http; using Titanium.Web.Proxy.Models; +using Titanium.Web.Proxy.ProxySocket; namespace Titanium.Web.Proxy.Network.Tcp { @@ -85,6 +86,8 @@ internal string GetConnectionCacheKey(string remoteHostName, int remotePort, cacheKeyBuilder.Append(externalProxy.HostName); cacheKeyBuilder.Append("-"); cacheKeyBuilder.Append(externalProxy.Port); + cacheKeyBuilder.Append("-"); + cacheKeyBuilder.Append(externalProxy.ProxyType); if (externalProxy.UseDefaultCredentials) { @@ -114,7 +117,7 @@ internal async Task GetConnectionCacheKey(ProxyServer server, SessionEve applicationProtocols = new List { applicationProtocol }; } - IExternalProxy? customUpStreamProxy = session.CustomUpStreamProxy; + var customUpStreamProxy = session.CustomUpStreamProxy; bool isHttps = session.IsHttps; if (customUpStreamProxy == null && server.GetCustomUpStreamProxyFunc != null) @@ -125,12 +128,10 @@ internal async Task GetConnectionCacheKey(ProxyServer server, SessionEve session.CustomUpStreamProxyUsed = customUpStreamProxy; var uri = session.HttpClient.Request.RequestUri; - return GetConnectionCacheKey( - uri.Host, - uri.Port, - isHttps, applicationProtocols, - session.HttpClient.UpStreamEndPoint ?? server.UpStreamEndPoint, - customUpStreamProxy ?? (isHttps ? server.UpStreamHttpsProxy : server.UpStreamHttpProxy)); + var upStreamEndPoint = session.HttpClient.UpStreamEndPoint ?? server.UpStreamEndPoint; + var upStreamProxy = customUpStreamProxy ?? (isHttps ? server.UpStreamHttpsProxy : server.UpStreamHttpProxy); + return GetConnectionCacheKey(uri.Host, uri.Port, isHttps, applicationProtocols, upStreamEndPoint, + upStreamProxy); } @@ -169,7 +170,7 @@ internal Task GetServerConnection(ProxyServer proxyServer, internal async Task GetServerConnection(ProxyServer proxyServer, SessionEventArgsBase session, bool isConnect, List? applicationProtocols, bool noCache, CancellationToken cancellationToken) { - IExternalProxy? customUpStreamProxy = session.CustomUpStreamProxy; + var customUpStreamProxy = session.CustomUpStreamProxy; bool isHttps = session.IsHttps; if (customUpStreamProxy == null && proxyServer.GetCustomUpStreamProxyFunc != null) @@ -204,13 +205,10 @@ internal async Task GetServerConnection(ProxyServer proxySe port = uri.Port; } - return await GetServerConnection( - proxyServer, host, port, - session.HttpClient.Request.HttpVersion, - isHttps, applicationProtocols, isConnect, - session, session.HttpClient.UpStreamEndPoint ?? proxyServer.UpStreamEndPoint, - customUpStreamProxy ?? (isHttps ? proxyServer.UpStreamHttpsProxy : proxyServer.UpStreamHttpProxy), - noCache, cancellationToken); + var upStreamEndPoint = session.HttpClient.UpStreamEndPoint ?? proxyServer.UpStreamEndPoint; + var upStreamProxy = customUpStreamProxy ?? (isHttps ? proxyServer.UpStreamHttpsProxy : proxyServer.UpStreamHttpProxy); + return await GetServerConnection(proxyServer, host, port, session.HttpClient.Request.HttpVersion, isHttps, + applicationProtocols, isConnect, session, upStreamEndPoint, upStreamProxy, noCache, cancellationToken); } /// @@ -249,7 +247,7 @@ internal async Task GetServerConnection(ProxyServer proxySe if (existingConnections.TryDequeue(out var recentConnection)) { if (recentConnection.LastAccess > cutOff - && recentConnection.TcpClient.IsGoodConnection()) + && recentConnection.TcpSocket.IsGoodConnection()) { return recentConnection; } @@ -323,7 +321,7 @@ private async Task createServerConnection(string remoteHost externalProxy = null; } - TcpClient? tcpClient = null; + Socket? tcpServerSocket = null; HttpServerStream? stream = null; SslApplicationProtocol negotiatedApplicationProtocol = default; @@ -334,8 +332,15 @@ private async Task createServerConnection(string remoteHost retry: try { - string hostname = externalProxy != null ? externalProxy.HostName : remoteHostName; - int port = externalProxy?.Port ?? remotePort; + bool socks = externalProxy != null && externalProxy.ProxyType != ExternalProxyType.Http; + string hostname = remoteHostName; + int port = remotePort; + + if (externalProxy != null && externalProxy.ProxyType == ExternalProxyType.Http) + { + hostname = externalProxy.HostName; + port = externalProxy.Port; + } var ipAddresses = await Dns.GetHostAddressesAsync(hostname); if (ipAddresses == null || ipAddresses.Length == 0) @@ -356,28 +361,46 @@ private async Task createServerConnection(string remoteHost try { var ipAddress = ipAddresses[i]; - if (upStreamEndPoint == null) + var addressFamily = upStreamEndPoint?.AddressFamily ?? ipAddress.AddressFamily; + + if (socks) { - tcpClient = new TcpClient(ipAddress.AddressFamily); + var proxySocket = new ProxySocket.ProxySocket(addressFamily, SocketType.Stream, ProtocolType.Tcp); + proxySocket.ProxyType = externalProxy!.ProxyType == ExternalProxyType.Socks4 + ? ProxyTypes.Socks4 + : ProxyTypes.Socks5; + + var proxyIpAddresses = await Dns.GetHostAddressesAsync(externalProxy.HostName); + proxySocket.ProxyEndPoint = new IPEndPoint(proxyIpAddresses[0], externalProxy.Port); + if (!string.IsNullOrEmpty(externalProxy.UserName) && externalProxy.Password != null) + { + proxySocket.ProxyUser = externalProxy.UserName; + proxySocket.ProxyPass = externalProxy.Password; + } + + tcpServerSocket = proxySocket; } else { - tcpClient = new TcpClient(upStreamEndPoint); + tcpServerSocket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp); } - tcpClient.NoDelay = proxyServer.NoDelay; - tcpClient.ReceiveTimeout = proxyServer.ConnectionTimeOutSeconds * 1000; - tcpClient.SendTimeout = proxyServer.ConnectionTimeOutSeconds * 1000; - tcpClient.LingerState = new LingerOption(true, proxyServer.TcpTimeWaitSeconds); + tcpServerSocket.NoDelay = proxyServer.NoDelay; + tcpServerSocket.ReceiveTimeout = proxyServer.ConnectionTimeOutSeconds * 1000; + tcpServerSocket.SendTimeout = proxyServer.ConnectionTimeOutSeconds * 1000; + tcpServerSocket.LingerState = new LingerOption(true, proxyServer.TcpTimeWaitSeconds); if (proxyServer.ReuseSocket && RunTime.IsSocketReuseAvailable) { - tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); + tcpServerSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); } - var connectTask = tcpClient.ConnectAsync(ipAddress, port); - await Task.WhenAny(connectTask, Task.Delay(proxyServer.ConnectTimeOutSeconds * 1000)); - if (!connectTask.IsCompleted || !tcpClient.Connected) + var connectTask = socks + ? ProxySocketConnectionTaskFactory.CreateTask((ProxySocket.ProxySocket)tcpServerSocket, ipAddress, port) + : SocketConnectionTaskFactory.CreateTask(tcpServerSocket, ipAddress, port); + + await Task.WhenAny(connectTask, Task.Delay(proxyServer.ConnectTimeOutSeconds * 1000, cancellationToken)); + if (!connectTask.IsCompleted || !tcpServerSocket.Connected) { // here we can just do some cleanup and let the loop continue since // we will either get a connection or wind up with a null tcpClient @@ -393,11 +416,11 @@ private async Task createServerConnection(string remoteHost try { #if NET45 - tcpClient?.Close(); + tcpServerSocket?.Close(); #else - tcpClient?.Dispose(); + tcpServerSocket?.Dispose(); #endif - tcpClient = null; + tcpServerSocket = null; } catch { @@ -414,15 +437,15 @@ private async Task createServerConnection(string remoteHost // dispose the current TcpClient and try the next address lastException = e; #if NET45 - tcpClient?.Close(); + tcpServerSocket?.Close(); #else - tcpClient?.Dispose(); + tcpServerSocket?.Dispose(); #endif - tcpClient = null; + tcpServerSocket = null; } } - if (tcpClient == null) + if (tcpServerSocket == null) { if (sessionArgs != null && proxyServer.CustomUpStreamProxyFailureFunc != null) { @@ -443,11 +466,11 @@ private async Task createServerConnection(string remoteHost sessionArgs.TimeLine["Connection Established"] = DateTime.Now; } - await proxyServer.InvokeServerConnectionCreateEvent(tcpClient); + await proxyServer.InvokeServerConnectionCreateEvent(tcpServerSocket); - stream = new HttpServerStream(tcpClient.GetStream(), proxyServer.BufferPool, cancellationToken); + stream = new HttpServerStream(new NetworkStream(tcpServerSocket, true), proxyServer.BufferPool, cancellationToken); - if (externalProxy != null && (isConnect || isHttps)) + if ((externalProxy != null && externalProxy.ProxyType == ExternalProxyType.Http) && (isConnect || isHttps)) { var authority = $"{remoteHostName}:{remotePort}".GetByteString(); var connectRequest = new ConnectRequest(authority) @@ -511,7 +534,7 @@ private async Task createServerConnection(string remoteHost catch (IOException ex) when (ex.HResult == unchecked((int)0x80131620) && retry && enabledSslProtocols >= SslProtocols.Tls11) { stream?.Dispose(); - tcpClient?.Close(); + tcpServerSocket?.Close(); enabledSslProtocols = SslProtocols.Tls; retry = false; @@ -520,11 +543,11 @@ private async Task createServerConnection(string remoteHost catch (Exception) { stream?.Dispose(); - tcpClient?.Close(); + tcpServerSocket?.Close(); throw; } - return new TcpServerConnection(proxyServer, tcpClient, stream, remoteHostName, remotePort, isHttps, + return new TcpServerConnection(proxyServer, tcpServerSocket, stream, remoteHostName, remotePort, isHttps, negotiatedApplicationProtocol, httpVersion, externalProxy, upStreamEndPoint, cacheKey); } @@ -698,5 +721,43 @@ public void Dispose() } } } + + static class SocketConnectionTaskFactory + { + static IAsyncResult beginConnect(IPAddress address, int port, AsyncCallback requestCallback, + object state) + { + return ((Socket)state).BeginConnect(address, port, requestCallback, state); + } + + static void endConnect(IAsyncResult asyncResult) + { + ((Socket)asyncResult.AsyncState).EndConnect(asyncResult); + } + + public static Task CreateTask(Socket socket, IPAddress ipAddress, int port) + { + return Task.Factory.FromAsync(beginConnect, endConnect, ipAddress, port, state: socket); + } + } + + static class ProxySocketConnectionTaskFactory + { + static IAsyncResult beginConnect(IPAddress address, int port, AsyncCallback requestCallback, + object state) + { + return ((ProxySocket.ProxySocket)state).BeginConnect(address, port, requestCallback, state); + } + + static void endConnect(IAsyncResult asyncResult) + { + ((ProxySocket.ProxySocket)asyncResult.AsyncState).EndConnect(asyncResult); + } + + public static Task CreateTask(ProxySocket.ProxySocket socket, IPAddress ipAddress, int port) + { + return Task.Factory.FromAsync(beginConnect, endConnect, ipAddress, port, state: socket); + } + } } } diff --git a/src/Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs b/src/Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs index 1031eecbd..fd67928ba 100644 --- a/src/Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs +++ b/src/Titanium.Web.Proxy/Network/Tcp/TcpServerConnection.cs @@ -16,11 +16,11 @@ internal class TcpServerConnection : IDisposable { public Guid Id { get; } = Guid.NewGuid(); - internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient, HttpServerStream stream, + internal TcpServerConnection(ProxyServer proxyServer, Socket tcpSocket, HttpServerStream stream, string hostName, int port, bool isHttps, SslApplicationProtocol negotiatedApplicationProtocol, Version version, IExternalProxy? upStreamProxy, IPEndPoint? upStreamEndPoint, string cacheKey) { - TcpClient = tcpClient; + TcpSocket = tcpSocket; LastAccess = DateTime.Now; this.proxyServer = proxyServer; this.proxyServer.UpdateServerConnectionCount(true); @@ -63,7 +63,7 @@ internal TcpServerConnection(ProxyServer proxyServer, TcpClient tcpClient, HttpS /// /// The TcpClient. /// - internal TcpClient TcpClient { get; } + internal Socket TcpSocket { get; } /// /// Used to write lines to server @@ -98,7 +98,15 @@ public void Dispose() await Task.Delay(1000); proxyServer.UpdateServerConnectionCount(false); Stream.Dispose(); - TcpClient.CloseSocket(); + + try + { + TcpSocket.Close(); + } + catch + { + // ignore + } }); } diff --git a/src/Titanium.Web.Proxy/ProxyServer.cs b/src/Titanium.Web.Proxy/ProxyServer.cs index 23ddedd40..d1f4a37a3 100644 --- a/src/Titanium.Web.Proxy/ProxyServer.cs +++ b/src/Titanium.Web.Proxy/ProxyServer.cs @@ -359,12 +359,12 @@ public ExceptionHandler ExceptionFunc /// /// Customize TcpClient used for client connection upon create. /// - public event AsyncEventHandler? OnClientConnectionCreate; + public event AsyncEventHandler? OnClientConnectionCreate; /// /// Customize TcpClient used for server connection upon create. /// - public event AsyncEventHandler? OnServerConnectionCreate; + public event AsyncEventHandler? OnServerConnectionCreate; /// /// Customize the minimum ThreadPool size (increase it on a server) @@ -733,12 +733,12 @@ private void onAcceptConnection(IAsyncResult asyn) { var endPoint = (ProxyEndPoint)asyn.AsyncState; - TcpClient? tcpClient = null; + Socket? tcpClient = null; try { // based on end point type call appropriate request handlers - tcpClient = endPoint.Listener!.EndAcceptTcpClient(asyn); + tcpClient = endPoint.Listener!.EndAcceptSocket(asyn); tcpClient.NoDelay = NoDelay; } catch (ObjectDisposedException) @@ -784,19 +784,19 @@ private void setThreadPoolMinThread(int workerThreads) /// /// Handle the client. /// - /// The client. + /// The client socket. /// The proxy endpoint. /// The task. - private async Task handleClient(TcpClient tcpClient, ProxyEndPoint endPoint) + private async Task handleClient(Socket tcpClientSocket, ProxyEndPoint endPoint) { - tcpClient.ReceiveTimeout = ConnectionTimeOutSeconds * 1000; - tcpClient.SendTimeout = ConnectionTimeOutSeconds * 1000; + tcpClientSocket.ReceiveTimeout = ConnectionTimeOutSeconds * 1000; + tcpClientSocket.SendTimeout = ConnectionTimeOutSeconds * 1000; - tcpClient.LingerState = new LingerOption(true, TcpTimeWaitSeconds); + tcpClientSocket.LingerState = new LingerOption(true, TcpTimeWaitSeconds); - await InvokeClientConnectionCreateEvent(tcpClient); + await InvokeClientConnectionCreateEvent(tcpClientSocket); - using (var clientConnection = new TcpClientConnection(this, tcpClient)) + using (var clientConnection = new TcpClientConnection(this, tcpClientSocket)) { if (endPoint is TransparentProxyEndPoint tep) { @@ -867,28 +867,28 @@ internal void UpdateServerConnectionCount(bool increment) /// /// Invoke client tcp connection events if subscribed by API user. /// - /// The TcpClient object. + /// The TcpClient object. /// - internal async Task InvokeClientConnectionCreateEvent(TcpClient client) + internal async Task InvokeClientConnectionCreateEvent(Socket clientSocket) { // client connection created if (OnClientConnectionCreate != null) { - await OnClientConnectionCreate.InvokeAsync(this, client, ExceptionFunc); + await OnClientConnectionCreate.InvokeAsync(this, clientSocket, ExceptionFunc); } } /// /// Invoke server tcp connection events if subscribed by API user. /// - /// The TcpClient object. + /// The Socket object. /// - internal async Task InvokeServerConnectionCreateEvent(TcpClient client) + internal async Task InvokeServerConnectionCreateEvent(Socket serverSocket) { // server connection created if (OnServerConnectionCreate != null) { - await OnServerConnectionCreate.InvokeAsync(this, client, ExceptionFunc); + await OnServerConnectionCreate.InvokeAsync(this, serverSocket, ExceptionFunc); } } diff --git a/src/Titanium.Web.Proxy/ProxySocket/Authentication/AuthMethod.cs b/src/Titanium.Web.Proxy/ProxySocket/Authentication/AuthMethod.cs new file mode 100644 index 000000000..4ead58437 --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/Authentication/AuthMethod.cs @@ -0,0 +1,134 @@ +/* + 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; + +namespace Titanium.Web.Proxy.ProxySocket.Authentication +{ + /// + /// Implements a SOCKS authentication scheme. + /// + /// This is an abstract class; it must be inherited. + internal abstract class AuthMethod + { + /// + /// Initializes an AuthMethod instance. + /// + /// The socket connection with the proxy server. + public AuthMethod(Socket server) + { + Server = server; + } + + /// + /// Authenticates the user. + /// + /// Authentication with the proxy server failed. + /// The proxy server uses an invalid protocol. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + public abstract void Authenticate(); + + /// + /// Authenticates the user asynchronously. + /// + /// The method to call when the authentication is complete. + /// Authentication with the proxy server failed. + /// The proxy server uses an invalid protocol. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + public abstract void BeginAuthenticate(HandShakeComplete callback); + + /// + /// Gets or sets the socket connection with the proxy server. + /// + /// The socket connection with the proxy server. + protected Socket Server + { + get + { + return _server; + } + set + { + if (value == null) + throw new ArgumentNullException(); + _server = value; + } + } + + /// + /// Gets or sets a byt array that can be used to store data. + /// + /// A byte array to store data. + protected byte[] Buffer + { + get + { + return _buffer; + } + set + { + _buffer = value; + } + } + + /// + /// Gets or sets the number of bytes that have been received from the remote proxy server. + /// + /// An integer that holds the number of bytes that have been received from the remote proxy server. + protected int Received + { + get + { + return _received; + } + set + { + _received = value; + } + } + + // private variables + /// Holds the value of the Buffer property. + private byte[] _buffer; + + /// Holds the value of the Server property. + private Socket _server; + + /// Holds the address of the method to call when the proxy has authenticated the client. + protected HandShakeComplete CallBack; + + /// Holds the value of the Received property. + private int _received; + } +} diff --git a/src/Titanium.Web.Proxy/ProxySocket/Authentication/AuthNone.cs b/src/Titanium.Web.Proxy/ProxySocket/Authentication/AuthNone.cs new file mode 100644 index 000000000..94df7d57b --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/Authentication/AuthNone.cs @@ -0,0 +1,65 @@ +/* + 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.Sockets; + +namespace Titanium.Web.Proxy.ProxySocket.Authentication +{ + /// + /// This class implements the 'No Authentication' scheme. + /// + internal sealed class AuthNone : AuthMethod + { + /// + /// Initializes an AuthNone instance. + /// + /// The socket connection with the proxy server. + public AuthNone(Socket server) : base(server) { } + + /// + /// Authenticates the user. + /// + public override void Authenticate() + { + return; // Do Nothing + } + + /// + /// Authenticates the user asynchronously. + /// + /// The method to call when the authentication is complete. + /// This method immediately calls the callback method. + public override void BeginAuthenticate(HandShakeComplete callback) + { + callback(null); + } + } +} diff --git a/src/Titanium.Web.Proxy/ProxySocket/Authentication/AuthUserPass.cs b/src/Titanium.Web.Proxy/ProxySocket/Authentication/AuthUserPass.cs new file mode 100644 index 000000000..fed804d21 --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/Authentication/AuthUserPass.cs @@ -0,0 +1,204 @@ +/* + 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.Sockets; +using System.Text; + +namespace Titanium.Web.Proxy.ProxySocket.Authentication +{ + /// + /// This class implements the 'username/password authentication' scheme. + /// + internal sealed class AuthUserPass : AuthMethod + { + /// + /// Initializes a new AuthUserPass instance. + /// + /// The socket connection with the proxy server. + /// The username to use. + /// The password to use. + /// user -or- pass is null. + public AuthUserPass(Socket server, string user, string pass) : base(server) + { + Username = user; + Password = pass; + } + + /// + /// Creates an array of bytes that has to be sent if the user wants to authenticate with the username/password authentication scheme. + /// + /// An array of bytes that has to be sent if the user wants to authenticate with the username/password authentication scheme. + private byte[] GetAuthenticationBytes() + { + byte[] buffer = new byte[3 + Username.Length + Password.Length]; + buffer[0] = 1; + buffer[1] = (byte)Username.Length; + Array.Copy(Encoding.ASCII.GetBytes(Username), 0, buffer, 2, Username.Length); + buffer[Username.Length + 2] = (byte)Password.Length; + Array.Copy(Encoding.ASCII.GetBytes(Password), 0, buffer, Username.Length + 3, Password.Length); + return buffer; + } + + private int GetAuthenticationLength() + { + return 3 + Username.Length + Password.Length; + } + + /// + /// Starts the authentication process. + /// + public override void Authenticate() + { + if (Server.Send(GetAuthenticationBytes()) < GetAuthenticationLength()) + { + throw new SocketException(10054); + } + + ; + byte[] buffer = new byte[2]; + int received = 0; + while (received != 2) + { + int recv = Server.Receive(buffer, received, 2 - received, SocketFlags.None); + if (recv == 0) + throw new SocketException(10054); + received += recv; + } + + if (buffer[1] != 0) + { + Server.Close(); + throw new ProxyException("Username/password combination rejected."); + } + + return; + } + + /// + /// Starts the asynchronous authentication process. + /// + /// The method to call when the authentication is complete. + public override void BeginAuthenticate(HandShakeComplete callback) + { + CallBack = callback; + Server.BeginSend(GetAuthenticationBytes(), 0, GetAuthenticationLength(), SocketFlags.None, + new AsyncCallback(this.OnSent), Server); + return; + } + + /// + /// Called when the authentication bytes have been sent. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnSent(IAsyncResult ar) + { + try + { + if (Server.EndSend(ar) < GetAuthenticationLength()) + throw new SocketException(10054); + Buffer = new byte[2]; + Server.BeginReceive(Buffer, 0, 2, SocketFlags.None, new AsyncCallback(this.OnReceive), Server); + } + catch (Exception e) + { + CallBack(e); + } + } + + /// + /// Called when the socket received an authentication reply. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnReceive(IAsyncResult ar) + { + try + { + int recv = Server.EndReceive(ar); + if (recv <= 0) + throw new SocketException(10054); + Received += recv; + if (Received == Buffer.Length) + if (Buffer[1] == 0) + CallBack(null); + else + throw new ProxyException("Username/password combination not accepted."); + else + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, + new AsyncCallback(this.OnReceive), Server); + } + catch (Exception e) + { + CallBack(e); + } + } + + /// + /// Gets or sets the username to use when authenticating with the proxy server. + /// + /// The username to use when authenticating with the proxy server. + /// The specified value is null. + private string Username + { + get + { + return _username; + } + set + { + _username = value ?? throw new ArgumentNullException(); + } + } + + /// + /// Gets or sets the password to use when authenticating with the proxy server. + /// + /// The password to use when authenticating with the proxy server. + /// The specified value is null. + private string Password + { + get + { + return _password; + } + set + { + _password = value ?? throw new ArgumentNullException(); + } + } + + // private variables + /// Holds the value of the Username property. + private string _username; + + /// Holds the value of the Password property. + private string _password; + } +} diff --git a/src/Titanium.Web.Proxy/ProxySocket/HttpsHandler.cs b/src/Titanium.Web.Proxy/ProxySocket/HttpsHandler.cs new file mode 100644 index 000000000..3ed82a0f1 --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/HttpsHandler.cs @@ -0,0 +1,355 @@ +/* + 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 Titanium.Web.Proxy.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 && _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] == (_receivedNewlineChars % 2 == 0 ? '\r' : '\n')) + _receivedNewlineChars++; + else + _receivedNewlineChars = Buffer[0] == '\r' ? 1 : 0; + } + + if (_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 _password; + } + set + { + _password = value ?? throw new ArgumentNullException(); + } + } + + // private variables + /// Holds the value of the Password property. + private string _password; + + /// Holds the count of newline characters received. + private int _receivedNewlineChars; + } +} diff --git a/src/Titanium.Web.Proxy/ProxySocket/IAsyncProxyResult.cs b/src/Titanium.Web.Proxy/ProxySocket/IAsyncProxyResult.cs new file mode 100644 index 000000000..5d95ba3cc --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/IAsyncProxyResult.cs @@ -0,0 +1,116 @@ +/* + 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.Threading; + +namespace Titanium.Web.Proxy.ProxySocket +{ + /// + /// A class that implements the IAsyncResult interface. Objects from this class are returned by the BeginConnect method of the ProxySocket class. + /// + internal class IAsyncProxyResult : IAsyncResult + { + /// Initializes the internal variables of this object + /// An object that contains state information for this request. + internal IAsyncProxyResult(object stateObject = null) + { + _stateObject = stateObject; + _completed = false; + if (_waitHandle != null) + _waitHandle.Reset(); + + } + + /// Initializes the internal variables of this object + internal void Reset() + { + _stateObject = null; + _completed = true; + if (_waitHandle != null) + _waitHandle.Set(); + } + + /// Gets a value that indicates whether the server has completed processing the call. It is illegal for the server to use any client supplied resources outside of the agreed upon sharing semantics after it sets the IsCompleted property to "true". Thus, it is safe for the client to destroy the resources after IsCompleted property returns "true". + /// A boolean that indicates whether the server has completed processing the call. + public bool IsCompleted + { + get + { + return _completed; + } + } + + /// Gets a value that indicates whether the BeginXXXX call has been completed synchronously. If this is detected in the AsyncCallback delegate, it is probable that the thread that called BeginInvoke is the current thread. + /// Returns false. + public bool CompletedSynchronously + { + get + { + return false; + } + } + + /// Gets an object that was passed as the state parameter of the BeginXXXX method call. + /// The object that was passed as the state parameter of the BeginXXXX method call. + public object AsyncState + { + get + { + return _stateObject; + } + } + + /// + /// The AsyncWaitHandle property returns the WaitHandle that can use to perform a WaitHandle.WaitOne or WaitAny or WaitAll. The object which implements IAsyncResult need not derive from the System.WaitHandle classes directly. The WaitHandle wraps its underlying synchronization primitive and should be signaled after the call is completed. This enables the client to wait for the call to complete instead polling. The Runtime supplies a number of waitable objects that mirror Win32 synchronization primitives e.g. ManualResetEvent, AutoResetEvent and Mutex. + /// WaitHandle supplies methods that support waiting for such synchronization objects to become signaled with "any" or "all" semantics i.e. WaitHandle.WaitOne, WaitAny and WaitAll. Such methods are context aware to avoid deadlocks. The AsyncWaitHandle can be allocated eagerly or on demand. It is the choice of the IAsyncResult implementer. + /// + /// The WaitHandle associated with this asynchronous result. + public WaitHandle AsyncWaitHandle + { + get + { + if (_waitHandle == null) + _waitHandle = new ManualResetEvent(false); + return _waitHandle; + } + } + + // private variables + /// Used internally to represent the state of the asynchronous request + private bool _completed; + + /// Holds the value of the StateObject property. + private object _stateObject; + + /// Holds the value of the WaitHandle property. + private ManualResetEvent _waitHandle; + } +} diff --git a/src/Titanium.Web.Proxy/ProxySocket/ProxyException.cs b/src/Titanium.Web.Proxy/ProxySocket/ProxyException.cs new file mode 100644 index 000000000..14d6798d9 --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/ProxyException.cs @@ -0,0 +1,90 @@ +/* + 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; + +namespace Titanium.Web.Proxy.ProxySocket +{ + /// + /// The exception that is thrown when a proxy error occurs. + /// + [Serializable] + internal class ProxyException : Exception + { + /// + /// Initializes a new instance of the ProxyException class. + /// + public ProxyException() : this("An error occured while talking to the proxy server.") { } + + /// + /// Initializes a new instance of the ProxyException class. + /// + /// The message that describes the error. + public ProxyException(string message) : base(message) { } + + /// + /// Initializes a new instance of the ProxyException class. + /// + /// The error number returned by a SOCKS5 server. + public ProxyException(int socks5Error) : this(ProxyException.Socks5ToString(socks5Error)) { } + + /// + /// Converts a SOCKS5 error number to a human readable string. + /// + /// The error number returned by a SOCKS5 server. + /// A string representation of the specified SOCKS5 error number. + public static string Socks5ToString(int socks5Error) + { + switch (socks5Error) + { + case 0: + return "Connection succeeded."; + case 1: + return "General SOCKS server failure."; + case 2: + return "Connection not allowed by ruleset."; + case 3: + return "Network unreachable."; + case 4: + return "Host unreachable."; + case 5: + return "Connection refused."; + case 6: + return "TTL expired."; + case 7: + return "Command not supported."; + case 8: + return "Address type not supported."; + default: + return "Unspecified SOCKS error."; + } + } + } +} diff --git a/src/Titanium.Web.Proxy/ProxySocket/ProxySocket.cs b/src/Titanium.Web.Proxy/ProxySocket/ProxySocket.cs new file mode 100644 index 000000000..30b6f0987 --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/ProxySocket.cs @@ -0,0 +1,529 @@ +/* + 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; + +// Implements a number of classes to allow Sockets to connect trough a firewall. +namespace Titanium.Web.Proxy.ProxySocket +{ + /// + /// Specifies the type of proxy servers that an instance of the ProxySocket class can use. + /// + internal enum ProxyTypes + { + /// No proxy server; the ProxySocket object behaves exactly like an ordinary Socket object. + None, + + /// A HTTPS (CONNECT) proxy server. + Https, + + /// A SOCKS4[A] proxy server. + Socks4, + + /// A SOCKS5 proxy server. + Socks5 + } + + /// + /// Implements a Socket class that can connect trough a SOCKS proxy server. + /// + /// This class implements SOCKS4[A] and SOCKS5.
It does not, however, implement the BIND commands, so you cannot .
+ internal class ProxySocket : Socket + { + /// + /// Initializes a new instance of the ProxySocket class. + /// + /// One of the AddressFamily values. + /// One of the SocketType values. + /// One of the ProtocolType values. + /// The combination of addressFamily, socketType, and protocolType results in an invalid socket. + public ProxySocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) : this( + addressFamily, socketType, protocolType, "") + { + } + + /// + /// Initializes a new instance of the ProxySocket class. + /// + /// One of the AddressFamily values. + /// One of the SocketType values. + /// One of the ProtocolType values. + /// The username to use when authenticating with the proxy server. + /// The combination of addressFamily, socketType, and protocolType results in an invalid socket. + /// proxyUsername is null. + public ProxySocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, + string proxyUsername) : this(addressFamily, socketType, protocolType, proxyUsername, "") + { + } + + /// + /// Initializes a new instance of the ProxySocket class. + /// + /// One of the AddressFamily values. + /// One of the SocketType values. + /// One of the ProtocolType values. + /// The username to use when authenticating with the proxy server. + /// The password to use when authenticating with the proxy server. + /// The combination of addressFamily, socketType, and protocolType results in an invalid socket. + /// proxyUsername -or- proxyPassword is null. + public ProxySocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType, + string proxyUsername, string proxyPassword) : base(addressFamily, socketType, protocolType) + { + ProxyUser = proxyUsername; + ProxyPass = proxyPassword; + ToThrow = new InvalidOperationException(); + } + + /// + /// Establishes a connection to a remote device. + /// + /// An EndPoint that represents the remote device. + /// The remoteEP parameter is a null reference (Nothing in Visual Basic). + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + /// An error occurred while talking to the proxy server. + public new void Connect(EndPoint remoteEP) + { + if (remoteEP == null) + throw new ArgumentNullException(" cannot be null."); + if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) + base.Connect(remoteEP); + else + { + base.Connect(ProxyEndPoint); + if (ProxyType == ProxyTypes.Https) + (new HttpsHandler(this, ProxyUser, ProxyPass)).Negotiate((IPEndPoint)remoteEP); + else if (ProxyType == ProxyTypes.Socks4) + (new Socks4Handler(this, ProxyUser)).Negotiate((IPEndPoint)remoteEP); + else if (ProxyType == ProxyTypes.Socks5) + (new Socks5Handler(this, ProxyUser, ProxyPass)).Negotiate((IPEndPoint)remoteEP); + } + } + + /// + /// Establishes a connection to a remote device. + /// + /// The remote host to connect to. + /// The remote port to connect to. + /// The host parameter is a null reference (Nothing in Visual Basic). + /// The port parameter is invalid. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + /// An error occurred while talking to the proxy server. + /// If you use this method with a SOCKS4 server, it will let the server resolve the hostname. Not all SOCKS4 servers support this 'remote DNS' though. + public new void Connect(string host, int port) + { + if (host == null) + throw new ArgumentNullException(" cannot be null."); + if (port <= 0 || port > 65535) + throw new ArgumentException("Invalid port."); + if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) + base.Connect(new IPEndPoint(Dns.GetHostEntry(host).AddressList[0], port)); + else + { + base.Connect(ProxyEndPoint); + if (ProxyType == ProxyTypes.Https) + (new HttpsHandler(this, ProxyUser, ProxyPass)).Negotiate(host, port); + else if (ProxyType == ProxyTypes.Socks4) + (new Socks4Handler(this, ProxyUser)).Negotiate(host, port); + else if (ProxyType == ProxyTypes.Socks5) + (new Socks5Handler(this, ProxyUser, ProxyPass)).Negotiate(host, port); + } + } + + /// + /// Begins an asynchronous request for a connection to a network device. + /// + /// An EndPoint address that represents the remote device. + /// An EndPoint port that represents the remote device. + /// The AsyncCallback delegate. + /// An object that contains state information for this request. + /// An IAsyncResult that references the asynchronous connection. + /// The remoteEP parameter is a null reference (Nothing in Visual Basic). + /// An operating system error occurs while creating the Socket. + /// The Socket has been closed. + public new IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback callback, object state) + { + var remoteEP = new IPEndPoint(address, port); + return BeginConnect(remoteEP, callback, state); + } + + /// + /// Begins an asynchronous request for a connection to a network device. + /// + /// An EndPoint that represents the remote device. + /// The AsyncCallback delegate. + /// An object that contains state information for this request. + /// An IAsyncResult that references the asynchronous connection. + /// The remoteEP parameter is a null reference (Nothing in Visual Basic). + /// An operating system error occurs while creating the Socket. + /// The Socket has been closed. + public new IAsyncResult BeginConnect(EndPoint remoteEP, AsyncCallback callback, object state) + { + if (remoteEP == null) + throw new ArgumentNullException(); + if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) + { + return base.BeginConnect(remoteEP, callback, state); + } + else + { + CallBack = callback; + if (ProxyType == ProxyTypes.Https) + { + AsyncResult = (new HttpsHandler(this, ProxyUser, ProxyPass)).BeginNegotiate((IPEndPoint)remoteEP, + new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } + else if (ProxyType == ProxyTypes.Socks4) + { + AsyncResult = (new Socks4Handler(this, ProxyUser)).BeginNegotiate((IPEndPoint)remoteEP, + new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } + else if (ProxyType == ProxyTypes.Socks5) + { + AsyncResult = (new Socks5Handler(this, ProxyUser, ProxyPass)).BeginNegotiate((IPEndPoint)remoteEP, + new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } + + return null; + } + } + + /// + /// Begins an asynchronous request for a connection to a network device. + /// + /// The host to connect to. + /// The port on the remote host to connect to. + /// The AsyncCallback delegate. + /// An object that contains state information for this request. + /// An IAsyncResult that references the asynchronous connection. + /// The host parameter is a null reference (Nothing in Visual Basic). + /// The port parameter is invalid. + /// An operating system error occurs while creating the Socket. + /// The Socket has been closed. + public new IAsyncResult BeginConnect(string host, int port, AsyncCallback callback, object state) + { + if (host == null) + throw new ArgumentNullException(); + if (port <= 0 || port > 65535) + throw new ArgumentException(); + CallBack = callback; + if (this.ProtocolType != ProtocolType.Tcp || ProxyType == ProxyTypes.None || ProxyEndPoint == null) + { + RemotePort = port; + AsyncResult = BeginDns(host, new HandShakeComplete(this.OnHandShakeComplete)); + return AsyncResult; + } + else + { + if (ProxyType == ProxyTypes.Https) + { + AsyncResult = (new HttpsHandler(this, ProxyUser, ProxyPass)).BeginNegotiate(host, port, + new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } + else if (ProxyType == ProxyTypes.Socks4) + { + AsyncResult = (new Socks4Handler(this, ProxyUser)).BeginNegotiate(host, port, + new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } + else if (ProxyType == ProxyTypes.Socks5) + { + AsyncResult = (new Socks5Handler(this, ProxyUser, ProxyPass)).BeginNegotiate(host, port, + new HandShakeComplete(this.OnHandShakeComplete), ProxyEndPoint); + return AsyncResult; + } + + return null; + } + } + + /// + /// Ends a pending asynchronous connection request. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + /// The asyncResult parameter is a null reference (Nothing in Visual Basic). + /// The asyncResult parameter was not returned by a call to the BeginConnect method. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + /// EndConnect was previously called for the asynchronous connection. + /// The proxy server refused the connection. + public new void EndConnect(IAsyncResult asyncResult) + { + if (asyncResult == null) + throw new ArgumentNullException(); + // In case we called Socket.BeginConnect() directly + if (!(asyncResult is IAsyncProxyResult)) + { + base.EndConnect(asyncResult); + return; + } + + if (!asyncResult.IsCompleted) + asyncResult.AsyncWaitHandle.WaitOne(); + if (ToThrow != null) + throw ToThrow; + return; + } + + /// + /// Begins an asynchronous request to resolve a DNS host name or IP address in dotted-quad notation to an IPAddress instance. + /// + /// The host to resolve. + /// The method to call when the hostname has been resolved. + /// An IAsyncResult instance that references the asynchronous request. + /// There was an error while trying to resolve the host. + internal IAsyncProxyResult BeginDns(string host, HandShakeComplete callback) + { + try + { + Dns.BeginGetHostEntry(host, new AsyncCallback(this.OnResolved), this); + return new IAsyncProxyResult(); + } + catch + { + throw new SocketException(); + } + } + + /// + /// Called when the specified hostname has been resolved. + /// + /// The result of the asynchronous operation. + private void OnResolved(IAsyncResult asyncResult) + { + try + { + IPHostEntry dns = Dns.EndGetHostEntry(asyncResult); + base.BeginConnect(new IPEndPoint(dns.AddressList[0], RemotePort), new AsyncCallback(this.OnConnect), + State); + } + catch (Exception e) + { + OnHandShakeComplete(e); + } + } + + /// + /// Called when the Socket is connected to the remote host. + /// + /// The result of the asynchronous operation. + private void OnConnect(IAsyncResult asyncResult) + { + try + { + base.EndConnect(asyncResult); + OnHandShakeComplete(null); + } + catch (Exception e) + { + OnHandShakeComplete(e); + } + } + + /// + /// Called when the Socket has finished talking to the proxy server and is ready to relay data. + /// + /// The error to throw when the EndConnect method is called. + private void OnHandShakeComplete(Exception error) + { + if (error != null) + this.Close(); + ToThrow = error; + AsyncResult.Reset(); + if (CallBack != null) + CallBack(AsyncResult); + } + + /// + /// Gets or sets the EndPoint of the proxy server. + /// + /// An IPEndPoint object that holds the IP address and the port of the proxy server. + public IPEndPoint ProxyEndPoint + { + get + { + return _proxyEndPoint; + } + set + { + _proxyEndPoint = value; + } + } + + /// + /// Gets or sets the type of proxy server to use. + /// + /// One of the ProxyTypes values. + public ProxyTypes ProxyType + { + get + { + return _proxyType; + } + set + { + _proxyType = value; + } + } + + /// + /// Gets or sets a user-defined object. + /// + /// The user-defined object. + private object State + { + get + { + return _state; + } + set + { + _state = value; + } + } + + /// + /// Gets or sets the username to use when authenticating with the proxy. + /// + /// A string that holds the username that's used when authenticating with the proxy. + /// The specified value is null. + public string? ProxyUser + { + get + { + return _proxyUser; + } + set + { + _proxyUser = value ?? throw new ArgumentNullException(); + } + } + + /// + /// Gets or sets the password to use when authenticating with the proxy. + /// + /// A string that holds the password that's used when authenticating with the proxy. + /// The specified value is null. + public string? ProxyPass + { + get + { + return _proxyPass; + } + set + { + _proxyPass = value ?? throw new ArgumentNullException(); + } + } + + /// + /// Gets or sets the asynchronous result object. + /// + /// An instance of the IAsyncProxyResult class. + private IAsyncProxyResult AsyncResult + { + get + { + return _asyncResult; + } + set + { + _asyncResult = value; + } + } + + /// + /// Gets or sets the exception to throw when the EndConnect method is called. + /// + /// An instance of the Exception class (or subclasses of Exception). + private Exception ToThrow + { + get + { + return _toThrow; + } + set + { + _toThrow = value; + } + } + + /// + /// Gets or sets the remote port the user wants to connect to. + /// + /// An integer that specifies the port the user wants to connect to. + private int RemotePort + { + get + { + return _remotePort; + } + set + { + _remotePort = value; + } + } + + // private variables + /// Holds the value of the State property. + private object _state; + + /// Holds the value of the ProxyEndPoint property. + private IPEndPoint _proxyEndPoint; + + /// Holds the value of the ProxyType property. + private ProxyTypes _proxyType = ProxyTypes.None; + + /// Holds the value of the ProxyUser property. + private string? _proxyUser; + + /// Holds the value of the ProxyPass property. + private string? _proxyPass; + + /// Holds a pointer to the method that should be called when the Socket is connected to the remote device. + private AsyncCallback CallBack; + + /// Holds the value of the AsyncResult property. + private IAsyncProxyResult _asyncResult; + + /// Holds the value of the ToThrow property. + private Exception _toThrow; + + /// Holds the value of the RemotePort property. + private int _remotePort; + } +} diff --git a/src/Titanium.Web.Proxy/ProxySocket/Socks4Handler.cs b/src/Titanium.Web.Proxy/ProxySocket/Socks4Handler.cs new file mode 100644 index 000000000..f7beb1947 --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/Socks4Handler.cs @@ -0,0 +1,273 @@ +/* + 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 Titanium.Web.Proxy.ProxySocket +{ + /// + /// Implements the SOCKS4[A] protocol. + /// + internal sealed class Socks4Handler : SocksHandler + { + /// + /// Initializes a new instance of the SocksHandler class. + /// + /// The socket connection with the proxy server. + /// The username to use when authenticating with the server. + /// server -or- user is null. + public Socks4Handler(Socket server, string user) : base(server, user) { } + + /// + /// Creates an array of bytes that has to be sent when the user wants to connect to a specific host/port combination. + /// + /// The host to connect to. + /// The port to connect to. + /// An array of bytes that has to be sent when the user wants to connect to a specific host/port combination. + /// Resolving the host name will be done at server side. Do note that some SOCKS4 servers do not implement this functionality. + /// host is null. + /// port is invalid. + private byte[] GetHostPortBytes(string host, int port) + { + if (host == null) + throw new ArgumentNullException(); + if (port <= 0 || port > 65535) + throw new ArgumentException(); + byte[] connect = new byte[10 + Username.Length + host.Length]; + connect[0] = 4; + connect[1] = 1; + Array.Copy(PortToBytes(port), 0, connect, 2, 2); + connect[4] = connect[5] = connect[6] = 0; + connect[7] = 1; + Array.Copy(Encoding.ASCII.GetBytes(Username), 0, connect, 8, Username.Length); + connect[8 + Username.Length] = 0; + Array.Copy(Encoding.ASCII.GetBytes(host), 0, connect, 9 + Username.Length, host.Length); + connect[9 + Username.Length + host.Length] = 0; + return connect; + } + + /// + /// Creates an array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. + /// + /// The IPEndPoint to connect to. + /// An array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. + /// remoteEP is null. + private byte[] GetEndPointBytes(IPEndPoint remoteEP) + { + if (remoteEP == null) + throw new ArgumentNullException(); + byte[] connect = new byte[9 + Username.Length]; + connect[0] = 4; + connect[1] = 1; + Array.Copy(PortToBytes(remoteEP.Port), 0, connect, 2, 2); + Array.Copy(remoteEP.Address.GetAddressBytes(), 0, connect, 4, 4); + Array.Copy(Encoding.ASCII.GetBytes(Username), 0, connect, 8, Username.Length); + connect[8 + Username.Length] = 0; + return connect; + } + + /// + /// 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. + public override void Negotiate(string host, int port) + { + Negotiate(GetHostPortBytes(host, port)); + } + + /// + /// 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. + public override void Negotiate(IPEndPoint remoteEP) + { + Negotiate(GetEndPointBytes(remoteEP)); + } + + /// + /// Starts negotiating with the SOCKS server. + /// + /// The bytes to send when trying to authenticate. + /// connect is null. + /// connect is too small. + /// The proxy rejected the request. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + private void Negotiate(byte[] connect) + { + if (connect == null) + throw new ArgumentNullException(); + if (connect.Length < 2) + throw new ArgumentException(); + if (Server.Send(connect) < connect.Length) + throw new SocketException(10054); + byte[] buffer = ReadBytes(8); + if (buffer[1] != 90) + { + Server.Close(); + throw new ProxyException("Negotiation failed."); + } + } + + /// + /// Starts negotiating asynchronously with a SOCKS proxy server. + /// + /// The remote server to connect to. + /// The remote port to connect to. + /// The method to call when the connection has been established. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, + IPEndPoint proxyEndPoint) + { + ProtocolComplete = callback; + Buffer = GetHostPortBytes(host, port); + Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); + AsyncResult = new IAsyncProxyResult(); + return AsyncResult; + } + + /// + /// Starts negotiating asynchronously with a SOCKS proxy server. + /// + /// An IPEndPoint that represents the remote device. + /// The method to call when the connection has been established. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, + IPEndPoint proxyEndPoint) + { + ProtocolComplete = callback; + Buffer = GetEndPointBytes(remoteEP); + Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); + AsyncResult = new IAsyncProxyResult(); + return AsyncResult; + } + + /// + /// Called when the Socket is connected to the remote proxy 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.OnSent), Server); + } + catch (Exception e) + { + ProtocolComplete(e); + } + } + + /// + /// Called when the Socket has sent the handshake data. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnSent(IAsyncResult ar) + { + try + { + HandleEndSend(ar, Buffer.Length); + } + catch (Exception e) + { + ProtocolComplete(e); + return; + } + + try + { + Buffer = new byte[8]; + Received = 0; + Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceive), + Server); + } + catch (Exception e) + { + ProtocolComplete(e); + } + } + + /// + /// Called when the Socket has received a reply from the remote proxy server. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnReceive(IAsyncResult ar) + { + try + { + HandleEndReceive(ar); + if (Received == 8) + { + if (Buffer[1] == 90) + ProtocolComplete(null); + else + { + Server.Close(); + ProtocolComplete(new ProxyException("Negotiation failed.")); + } + } + else + { + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, + new AsyncCallback(this.OnReceive), Server); + } + } + catch (Exception e) + { + ProtocolComplete(e); + } + } + } +} diff --git a/src/Titanium.Web.Proxy/ProxySocket/Socks5Handler.cs b/src/Titanium.Web.Proxy/ProxySocket/Socks5Handler.cs new file mode 100644 index 000000000..2049254ab --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/Socks5Handler.cs @@ -0,0 +1,534 @@ +/* + 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; +using Titanium.Web.Proxy.ProxySocket.Authentication; + +namespace Titanium.Web.Proxy.ProxySocket +{ + /// + /// Implements the SOCKS5 protocol. + /// + internal sealed class Socks5Handler : SocksHandler + { + /// + /// Initializes a new Socks5Handler instance. + /// + /// The socket connection with the proxy server. + /// server is null. + public Socks5Handler(Socket server) : this(server, "") { } + + /// + /// Initializes a new Socks5Handler instance. + /// + /// The socket connection with the proxy server. + /// The username to use. + /// server -or- user is null. + public Socks5Handler(Socket server, string user) : this(server, user, "") { } + + /// + /// Initializes a new Socks5Handler instance. + /// + /// The socket connection with the proxy server. + /// The username to use. + /// The password to use. + /// server -or- user -or- pass is null. + public Socks5Handler(Socket server, string user, string pass) : base(server, user) + { + Password = pass; + } + + /// + /// Starts the synchronous authentication process. + /// + /// Authentication with the proxy server failed. + /// The proxy server uses an invalid protocol. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + private void Authenticate() + { + if (Server.Send(new byte[] { 5, 2, 0, 2 }) < 4) + throw new SocketException(10054); + byte[] buffer = ReadBytes(2); + if (buffer[1] == 255) + throw new ProxyException("No authentication method accepted."); + AuthMethod authenticate; + switch (buffer[1]) + { + case 0: + authenticate = new AuthNone(Server); + break; + case 2: + authenticate = new AuthUserPass(Server, Username, Password); + break; + default: + throw new ProtocolViolationException(); + } + + authenticate.Authenticate(); + } + + /// + /// Creates an array of bytes that has to be sent when the user wants to connect to a specific host/port combination. + /// + /// The host to connect to. + /// The port to connect to. + /// An array of bytes that has to be sent when the user wants to connect to a specific host/port combination. + /// host is null. + /// port or host is invalid. + private byte[] GetHostPortBytes(string host, int port) + { + if (host == null) + throw new ArgumentNullException(); + if (port <= 0 || port > 65535 || host.Length > 255) + throw new ArgumentException(); + byte[] connect = new byte[7 + host.Length]; + connect[0] = 5; + connect[1] = 1; + connect[2] = 0; //reserved + connect[3] = 3; + connect[4] = (byte)host.Length; + Array.Copy(Encoding.ASCII.GetBytes(host), 0, connect, 5, host.Length); + Array.Copy(PortToBytes(port), 0, connect, host.Length + 5, 2); + return connect; + } + + /// + /// Creates an array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. + /// + /// The IPEndPoint to connect to. + /// An array of bytes that has to be sent when the user wants to connect to a specific IPEndPoint. + /// remoteEP is null. + private byte[] GetEndPointBytes(IPEndPoint remoteEP) + { + if (remoteEP == null) + throw new ArgumentNullException(); + byte[] connect = new byte[10]; + connect[0] = 5; + connect[1] = 1; + connect[2] = 0; //reserved + connect[3] = 1; + Array.Copy(remoteEP.Address.GetAddressBytes(), 0, connect, 4, 4); + Array.Copy(PortToBytes(remoteEP.Port), 0, connect, 8, 2); + return connect; + } + + /// + /// 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) + { + Negotiate(GetHostPortBytes(host, port)); + } + + /// + /// 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) + { + Negotiate(GetEndPointBytes(remoteEP)); + } + + /// + /// Starts negotiating with the SOCKS server. + /// + /// The bytes to send when trying to authenticate. + /// connect is null. + /// connect is too small. + /// 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. + private void Negotiate(byte[] connect) + { + Authenticate(); + if (Server.Send(connect) < connect.Length) + throw new SocketException(10054); + byte[] buffer = ReadBytes(4); + if (buffer[1] != 0) + { + Server.Close(); + throw new ProxyException(buffer[1]); + } + + switch (buffer[3]) + { + case 1: + buffer = ReadBytes(6); //IPv4 address with port + break; + case 3: + buffer = ReadBytes(1); + buffer = ReadBytes(buffer[0] + 2); //domain name with port + break; + case 4: + buffer = ReadBytes(18); //IPv6 address with port + break; + default: + Server.Close(); + throw new ProtocolViolationException(); + } + } + + /// + /// Starts negotiating asynchronously with the SOCKS server. + /// + /// The host to connect to. + /// The port to connect to. + /// The method to call when the negotiation is complete. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public override IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, + IPEndPoint proxyEndPoint) + { + ProtocolComplete = callback; + HandShake = GetHostPortBytes(host, port); + Server.BeginConnect(proxyEndPoint, new AsyncCallback(this.OnConnect), Server); + AsyncResult = new IAsyncProxyResult(); + return AsyncResult; + } + + /// + /// Starts negotiating asynchronously with the SOCKS server. + /// + /// An IPEndPoint that represents the remote device. + /// The method to call when the negotiation is complete. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public override IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, + IPEndPoint proxyEndPoint) + { + ProtocolComplete = callback; + HandShake = GetEndPointBytes(remoteEP); + 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(new byte[] { 5, 2, 0, 2 }, 0, 4, SocketFlags.None, new AsyncCallback(this.OnAuthSent), + Server); + } + catch (Exception e) + { + ProtocolComplete(e); + } + } + + /// + /// Called when the authentication bytes have been sent. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnAuthSent(IAsyncResult ar) + { + try + { + HandleEndSend(ar, 4); + } + catch (Exception e) + { + ProtocolComplete(e); + return; + } + + try + { + Buffer = new byte[1024]; + Received = 0; + Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnAuthReceive), + Server); + } + catch (Exception e) + { + ProtocolComplete(e); + } + } + + /// + /// Called when an authentication reply has been received. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnAuthReceive(IAsyncResult ar) + { + try + { + HandleEndReceive(ar); + } + catch (Exception e) + { + ProtocolComplete(e); + return; + } + + try + { + if (Received < 2) + { + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, + new AsyncCallback(this.OnAuthReceive), Server); + } + else + { + AuthMethod authenticate; + switch (Buffer[1]) + { + case 0: + authenticate = new AuthNone(Server); + break; + case 2: + authenticate = new AuthUserPass(Server, Username, Password); + break; + default: + ProtocolComplete(new SocketException()); + return; + } + + authenticate.BeginAuthenticate(new HandShakeComplete(this.OnAuthenticated)); + } + } + catch (Exception e) + { + ProtocolComplete(e); + } + } + + /// + /// Called when the socket has been successfully authenticated with the server. + /// + /// The exception that has occurred while authenticating, or null if no error occurred. + private void OnAuthenticated(Exception e) + { + if (e != null) + { + ProtocolComplete(e); + return; + } + + try + { + Server.BeginSend(HandShake, 0, HandShake.Length, SocketFlags.None, new AsyncCallback(this.OnSent), + Server); + } + catch (Exception ex) + { + ProtocolComplete(ex); + } + } + + /// + /// Called when the connection request has been sent. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnSent(IAsyncResult ar) + { + try + { + HandleEndSend(ar, HandShake.Length); + } + catch (Exception e) + { + ProtocolComplete(e); + return; + } + + try + { + Buffer = new byte[5]; + Received = 0; + Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReceive), + Server); + } + catch (Exception e) + { + ProtocolComplete(e); + } + } + + /// + /// Called when a connection reply has been received. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnReceive(IAsyncResult ar) + { + try + { + HandleEndReceive(ar); + } + catch (Exception e) + { + ProtocolComplete(e); + return; + } + + try + { + if (Received == Buffer.Length) + ProcessReply(Buffer); + else + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, + new AsyncCallback(this.OnReceive), Server); + } + catch (Exception e) + { + ProtocolComplete(e); + } + } + + /// + /// Processes the received reply. + /// + /// The received reply + /// The received reply is invalid. + private void ProcessReply(byte[] buffer) + { + switch (buffer[3]) + { + case 1: + Buffer = new byte[5]; //IPv4 address with port - 1 byte + break; + case 3: + Buffer = new byte[buffer[4] + 2]; //domain name with port + break; + case 4: + buffer = new byte[17]; //IPv6 address with port - 1 byte + break; + default: + throw new ProtocolViolationException(); + } + + Received = 0; + Server.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.OnReadLast), Server); + } + + /// + /// Called when the last bytes are read from the socket. + /// + /// Stores state information for this asynchronous operation as well as any user-defined data. + private void OnReadLast(IAsyncResult ar) + { + try + { + HandleEndReceive(ar); + } + catch (Exception e) + { + ProtocolComplete(e); + return; + } + + try + { + if (Received == Buffer.Length) + ProtocolComplete(null); + else + Server.BeginReceive(Buffer, Received, Buffer.Length - Received, SocketFlags.None, + new AsyncCallback(this.OnReadLast), Server); + } + catch (Exception e) + { + ProtocolComplete(e); + } + } + + /// + /// Gets or sets the password to use when authenticating with the SOCKS5 server. + /// + /// The password to use when authenticating with the SOCKS5 server. + private string Password + { + get + { + return _password; + } + set + { + if (value == null) + throw new ArgumentNullException(); + _password = value; + } + } + + /// + /// Gets or sets the bytes to use when sending a connect request to the proxy server. + /// + /// The array of bytes to use when sending a connect request to the proxy server. + private byte[] HandShake + { + get + { + return _handShake; + } + set + { + _handShake = value; + } + } + + // private variables + /// Holds the value of the Password property. + private string _password; + + /// Holds the value of the HandShake property. + private byte[] _handShake; + } +} diff --git a/src/Titanium.Web.Proxy/ProxySocket/SocksHandler.cs b/src/Titanium.Web.Proxy/ProxySocket/SocksHandler.cs new file mode 100644 index 000000000..144119a2e --- /dev/null +++ b/src/Titanium.Web.Proxy/ProxySocket/SocksHandler.cs @@ -0,0 +1,279 @@ +/* + 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; + +namespace Titanium.Web.Proxy.ProxySocket +{ + /// + /// References the callback method to be called when the protocol negotiation is completed. + /// + internal delegate void HandShakeComplete(Exception error); + + /// + /// Implements a specific version of the SOCKS protocol. This is an abstract class; it must be inherited. + /// + internal abstract class SocksHandler + { + /// + /// Initializes a new instance of the SocksHandler class. + /// + /// The socket connection with the proxy server. + /// The username to use when authenticating with the server. + /// server -or- user is null. + public SocksHandler(Socket server, string user) + { + Server = server; + Username = user; + } + + /// + /// Converts a port number to an array of bytes. + /// + /// The port to convert. + /// An array of two bytes that represents the specified port. + protected byte[] PortToBytes(int port) + { + byte[] ret = new byte[2]; + ret[0] = (byte)(port / 256); + ret[1] = (byte)(port % 256); + return ret; + } + + /// + /// Converts an IP address to an array of bytes. + /// + /// The IP address to convert. + /// An array of four bytes that represents the specified IP address. + protected byte[] AddressToBytes(long address) + { + byte[] ret = new byte[4]; + ret[0] = (byte)(address % 256); + ret[1] = (byte)((address / 256) % 256); + ret[2] = (byte)((address / 65536) % 256); + ret[3] = (byte)(address / 16777216); + return ret; + } + + /// + /// Reads a specified number of bytes from the Server socket. + /// + /// The number of bytes to return. + /// An array of bytes. + /// The number of bytes to read is invalid. + /// An operating system error occurs while accessing the Socket. + /// The Socket has been closed. + protected byte[] ReadBytes(int count) + { + if (count <= 0) + throw new ArgumentException(); + byte[] buffer = new byte[count]; + int received = 0; + while (received != count) + { + int recv = Server.Receive(buffer, received, count - received, SocketFlags.None); + if (recv == 0) + { + throw new SocketException(10054); + } + + received += recv; + } + + return buffer; + } + + /// + /// Reads number of received bytes and ensures that socket was not shut down + /// + /// IAsyncResult for receive operation + /// + protected void HandleEndReceive(IAsyncResult ar) + { + int recv = Server.EndReceive(ar); + if (recv <= 0) + throw new SocketException(10054); + Received += recv; + } + + /// + /// Verifies that whole buffer was sent successfully + /// + /// IAsyncResult for receive operation + /// Length of buffer that was sent + /// + protected void HandleEndSend(IAsyncResult ar, int expectedLength) + { + if (Server.EndSend(ar) < expectedLength) + throw new SocketException(10054); + } + + /// + /// Gets or sets the socket connection with the proxy server. + /// + /// A Socket object that represents the connection with the proxy server. + /// The specified value is null. + protected Socket Server + { + get + { + return _server; + } + set + { + if (value == null) + throw new ArgumentNullException(); + _server = value; + } + } + + /// + /// Gets or sets the username to use when authenticating with the proxy server. + /// + /// A string that holds the username to use when authenticating with the proxy server. + /// The specified value is null. + protected string Username + { + get + { + return _username; + } + set + { + if (value == null) + throw new ArgumentNullException(); + _username = value; + } + } + + /// + /// Gets or sets the return value of the BeginConnect call. + /// + /// An IAsyncProxyResult object that is the return value of the BeginConnect call. + protected IAsyncProxyResult AsyncResult + { + get + { + return _asyncResult; + } + set + { + _asyncResult = value; + } + } + + /// + /// Gets or sets a byte buffer. + /// + /// An array of bytes. + protected byte[] Buffer + { + get + { + return _buffer; + } + set + { + _buffer = value; + } + } + + /// + /// Gets or sets the number of bytes that have been received from the remote proxy server. + /// + /// An integer that holds the number of bytes that have been received from the remote proxy server. + protected int Received + { + get + { + return _received; + } + set + { + _received = value; + } + } + + // private variables + /// Holds the value of the Server property. + private Socket _server; + + /// Holds the value of the Username property. + private string _username; + + /// Holds the value of the AsyncResult property. + private IAsyncProxyResult _asyncResult; + + /// Holds the value of the Buffer property. + private byte[] _buffer; + + /// Holds the value of the Received property. + private int _received; + + /// Holds the address of the method to call when the SOCKS protocol has been completed. + protected HandShakeComplete ProtocolComplete; + + /// + /// Starts negotiating with a SOCKS proxy server. + /// + /// The remote server to connect to. + /// The remote port to connect to. + public abstract void Negotiate(string host, int port); + + /// + /// Starts negotiating with a SOCKS proxy server. + /// + /// The remote endpoint to connect to. + public abstract void Negotiate(IPEndPoint remoteEP); + + /// + /// Starts negotiating asynchronously with a SOCKS proxy server. + /// + /// An IPEndPoint that represents the remote device. + /// The method to call when the connection has been established. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public abstract IAsyncProxyResult BeginNegotiate(IPEndPoint remoteEP, HandShakeComplete callback, + IPEndPoint proxyEndPoint); + + /// + /// Starts negotiating asynchronously with a SOCKS proxy server. + /// + /// The remote server to connect to. + /// The remote port to connect to. + /// The method to call when the connection has been established. + /// The IPEndPoint of the SOCKS proxy server. + /// An IAsyncProxyResult that references the asynchronous connection. + public abstract IAsyncProxyResult BeginNegotiate(string host, int port, HandShakeComplete callback, + IPEndPoint proxyEndPoint); + } +}