shatafacka

SocksProxy

Sep 18th, 2011
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.20 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5.  
  6. namespace COMail
  7. {
  8.     public class ConnectionException : ApplicationException
  9.     {
  10.         public ConnectionException(string message)
  11.             : base(message)
  12.         {
  13.         }
  14.     }
  15.  
  16.     /// <summary>
  17.     /// Provides sock5 functionality to clients (Connect only).
  18.     /// </summary>
  19.     public class SocksProxy
  20.     {
  21.  
  22.         private SocksProxy() { }
  23.  
  24.         #region ErrorMessages
  25.         private static string[] errorMsgs = {
  26.                                         "Operation completed successfully.",
  27.                                         "General SOCKS server failure.",
  28.                                         "Connection not allowed by ruleset.",
  29.                                         "Network unreachable.",
  30.                                         "Host unreachable.",
  31.                                         "Connection refused.",
  32.                                         "TTL expired.",
  33.                                         "Command not supported.",
  34.                                         "Address type not supported.",
  35.                                         "Unknown error."
  36.                                     };
  37.         #endregion
  38.  
  39.  
  40.         public static Socket ConnectToSocks(string proxyAdress, ushort proxyPort)
  41.         {
  42.             IPAddress proxyIP = null;
  43.             byte[] request = new byte[257];
  44.             byte[] response = new byte[257];
  45.             ushort nIndex;
  46.  
  47.             try
  48.             {
  49.                 proxyIP = IPAddress.Parse(proxyAdress);
  50.             }
  51.             catch (FormatException)
  52.             {   // get the IP address
  53.                 proxyIP = Dns.GetHostByAddress(proxyAdress).AddressList[0];
  54.             }
  55.  
  56.             IPEndPoint proxyEndPoint = new IPEndPoint(proxyIP, proxyPort);
  57.  
  58.             // open a TCP connection to SOCKS server...
  59.             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  60.             s.Connect(proxyEndPoint);
  61.  
  62.             nIndex = 0;
  63.             request[nIndex++] = 0x05; // Version 5.
  64.             request[nIndex++] = 0x02; // 2 Authentication methods are in packet...
  65.             request[nIndex++] = 0x00; // NO AUTHENTICATION REQUIRED
  66.             request[nIndex++] = 0x00; // USERNAME/PASSWORD
  67.             // Send the authentication negotiation request...
  68.             s.Send(request, nIndex, SocketFlags.None);
  69.  
  70.             // Receive 2 byte response...
  71.             int nGot = s.Receive(response, 2, SocketFlags.None);
  72.             if (nGot != 2)
  73.                 throw new ConnectionException("Bad response received from proxy server.");
  74.  
  75.             if (response[1] == 0xFF)
  76.             {   // No authentication method was accepted close the socket.
  77.                 s.Close();
  78.                 throw new ConnectionException("None of the authentication method was accepted by proxy server.");
  79.             }
  80.  
  81.             // Send connect request now...
  82.             nIndex = 0;
  83.             request[nIndex++] = 0x05;   // version 5.
  84.             request[nIndex++] = 0x01;   // command = connect.
  85.             request[nIndex++] = 0x00;   // Reserve = must be 0x00
  86.  
  87.             // send connect request.
  88.             s.Send(request, nIndex, SocketFlags.None);
  89.             s.Receive(response);    // Get variable length response...
  90.             if (response[1] != 0x00)
  91.                 throw new ConnectionException(errorMsgs[response[1]]);
  92.             // Success Connected...
  93.             return s;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment