/*
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 Org.Mentalis.Network.ProxySocket.Authentication;
namespace Org.Mentalis.Network.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 m_Password;
}
set {
if (value == null)
throw new ArgumentNullException();
m_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 m_HandShake;
}
set {
m_HandShake = value;
}
}
// private variables
/// Holds the value of the Password property.
private string m_Password;
/// Holds the value of the HandShake property.
private byte[] m_HandShake;
}
}