/* 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 Org.Mentalis.Network.ProxySocket { /// /// Specifies the type of proxy servers that an instance of the ProxySocket class can use. /// public 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 .
public 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 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 m_ProxyEndPoint; } set { m_ProxyEndPoint = value; } } /// /// Gets or sets the type of proxy server to use. /// /// One of the ProxyTypes values. public ProxyTypes ProxyType { get { return m_ProxyType; } set { m_ProxyType = value; } } /// /// Gets or sets a user-defined object. /// /// The user-defined object. private object State { get { return m_State; } set { m_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 m_ProxyUser; } set { if (value == null) throw new ArgumentNullException(); m_ProxyUser = value; } } /// /// 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 m_ProxyPass; } set { if (value == null) throw new ArgumentNullException(); m_ProxyPass = value; } } /// /// Gets or sets the asynchronous result object. /// /// An instance of the IAsyncProxyResult class. private IAsyncProxyResult AsyncResult { get { return m_AsyncResult; } set { m_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 m_ToThrow; } set { m_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 m_RemotePort; } set { m_RemotePort = value; } } // private variables /// Holds the value of the State property. private object m_State; /// Holds the value of the ProxyEndPoint property. private IPEndPoint m_ProxyEndPoint = null; /// Holds the value of the ProxyType property. private ProxyTypes m_ProxyType = ProxyTypes.None; /// Holds the value of the ProxyUser property. private string m_ProxyUser = null; /// Holds the value of the ProxyPass property. private string m_ProxyPass = null; /// Holds a pointer to the method that should be called when the Socket is connected to the remote device. private AsyncCallback CallBack = null; /// Holds the value of the AsyncResult property. private IAsyncProxyResult m_AsyncResult; /// Holds the value of the ToThrow property. private Exception m_ToThrow = null; /// Holds the value of the RemotePort property. private int m_RemotePort; } }