Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Dc Mass - Socks5Proxy.cs

Socks5Proxy.cs

Caricato da:
Scarica il programma completo

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5.    
  6. /*
  7. * zahmed
  8. * Date 23 Jan 2004
  9. * Socks 5 RFC is available at http://www.faqs.org/rfcs/rfc1928.html.
  10. */
  11. namespace SaTaNaDcMaSS
  12. {
  13.         public class ConnectionException:ApplicationException
  14.         {
  15.                 public ConnectionException(string message)
  16.                         :base(message)
  17.                 {
  18.                 }
  19.         }
  20.  
  21.         /// <summary>
  22.         /// Provides sock5 functionality to clients (Connect only).
  23.         /// </summary>
  24.         public class SocksProxy
  25.         {
  26.  
  27.                 private SocksProxy(){}
  28.  
  29.                 #region ErrorMessages
  30.                 private static string[] errorMsgs=      {
  31.                                                                                                 "Operation completed successfully.",
  32.                                                                                                 "General SOCKS server failure.",
  33.                                                                                                 "Connection not allowed by ruleset.",
  34.                                                                                                 "Network unreachable.",
  35.                                                                                                 "Host unreachable.",
  36.                                                                                                 "Connection refused.",
  37.                                                                                                 "TTL expired.",
  38.                                                                                                 "Command not supported.",
  39.                                                                                                 "Address type not supported.",
  40.                                                                                                 "Unknown error."
  41.                                                                                         };
  42.                 #endregion
  43.  
  44.  
  45.                 public static Socket ConnectToSocks5Proxy(string proxyAdress, ushort proxyPort, string destAddress, ushort destPort,
  46.                         string userName, string password)
  47.                 {
  48.                         IPAddress destIP = null;
  49.                         IPAddress proxyIP = null;
  50.                         byte[] request = new byte[257];
  51.                         byte[] response = new byte[257];
  52.                         ushort nIndex;
  53.  
  54.                         try
  55.                         {
  56.                                 proxyIP =  IPAddress.Parse(proxyAdress);
  57.                         }
  58.                         catch(FormatException)
  59.                         {       // get the IP address
  60.                                 proxyIP = Dns.GetHostByAddress(proxyAdress).AddressList[0];
  61.                         }
  62.  
  63.                         // Parse destAddress (assume it in string dotted format "212.116.65.112" )
  64.                         try
  65.                         {
  66.                                 destIP = IPAddress.Parse(destAddress);
  67.                         }
  68.                         catch(FormatException)
  69.                         {
  70.                                 // wrong assumption its in domain name format "www.microsoft.com"
  71.                         }
  72.  
  73.                         IPEndPoint proxyEndPoint = new IPEndPoint(proxyIP,proxyPort);
  74.  
  75.                         // open a TCP connection to SOCKS server...
  76.                         Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
  77.                         s.Connect(proxyEndPoint);      
  78.  
  79.                         nIndex = 0;
  80.                         request[nIndex++]=0x05; // Version 5.
  81.                         request[nIndex++]=0x02; // 2 Authentication methods are in packet...
  82.                         request[nIndex++]=0x00; // NO AUTHENTICATION REQUIRED
  83.                         request[nIndex++]=0x02; // USERNAME/PASSWORD
  84.                         // Send the authentication negotiation request...
  85.                         s.Send(request,nIndex,SocketFlags.None);
  86.  
  87.                         // Receive 2 byte response...
  88.                         int nGot = s.Receive(response,2,SocketFlags.None);     
  89.                         if (nGot!=2)
  90.                                 throw new ConnectionException("Bad response received from proxy server.");
  91.  
  92.                         if (response[1]==0xFF)
  93.                         {       // No authentication method was accepted close the socket.
  94.                                 s.Close();
  95.                                 throw new ConnectionException("None of the authentication method was accepted by proxy server.");
  96.                         }
  97.    
  98.                         byte[] rawBytes;
  99.  
  100.                         if (/*response[1]==0x02*/true)
  101.                         {//Username/Password Authentication protocol
  102.                                 nIndex = 0;
  103.                                 request[nIndex++]=0x05; // Version 5.
  104.        
  105.                                 // add user name
  106.                                 request[nIndex++]=(byte)userName.Length;
  107.                                 rawBytes = Encoding.Default.GetBytes(userName);
  108.                                 rawBytes.CopyTo(request,nIndex);
  109.                                 nIndex+=(ushort)rawBytes.Length;
  110.  
  111.                                 // add password
  112.                                 request[nIndex++]=(byte)password.Length;
  113.                                 rawBytes = Encoding.Default.GetBytes(password);
  114.                                 rawBytes.CopyTo(request,nIndex);
  115.                                 nIndex+=(ushort)rawBytes.Length;
  116.  
  117.                                 // Send the Username/Password request
  118.                                 s.Send(request,nIndex,SocketFlags.None);
  119.                                 // Receive 2 byte response...
  120.                                 nGot = s.Receive(response,2,SocketFlags.None);
  121.                                 System.Windows.Forms.MessageBox.Show(Encoding.Default.GetString(response));
  122.                                 if (nGot!=2)
  123.                                 throw new ConnectionException("Bad response received from proxy server.");
  124.                                 //if (response[1] != 0x00)
  125.                                 //throw new ConnectionException("Bad Usernaem/Password.");
  126.                         }
  127.                         // This version only supports connect command.
  128.                         // UDP and Bind are not supported.
  129.  
  130.                         // Send connect request now...
  131.                         nIndex = 0;
  132.                         request[nIndex++]=0x05; // version 5.
  133.                         request[nIndex++]=0x01; // command = connect.
  134.                         request[nIndex++]=0x00; // Reserve = must be 0x00
  135.  
  136.                         if (destIP != null)
  137.                         {// Destination adress in an IP.
  138.                                 switch(destIP.AddressFamily)
  139.                                 {
  140.                                         case AddressFamily.InterNetwork:
  141.                                                 // Address is IPV4 format
  142.                                                 request[nIndex++]=0x01;
  143.                                                 rawBytes = destIP.GetAddressBytes();
  144.                                                 rawBytes.CopyTo(request,nIndex);
  145.                                                 nIndex+=(ushort)rawBytes.Length;
  146.                                                 break;
  147.                                         case AddressFamily.InterNetworkV6:
  148.                                                 // Address is IPV6 format
  149.                                                 request[nIndex++]=0x04;
  150.                                                 rawBytes = destIP.GetAddressBytes();
  151.                                                 rawBytes.CopyTo(request,nIndex);
  152.                                                 nIndex+=(ushort)rawBytes.Length;
  153.                                                 break;
  154.                                 }
  155.                         }
  156.                         else
  157.                         {// Dest. address is domain name.
  158.                                 request[nIndex++]=0x03; // Address is full-qualified domain name.
  159.                                 request[nIndex++]=Convert.ToByte(destAddress.Length); // length of address.
  160.                                 rawBytes = Encoding.Default.GetBytes(destAddress);
  161.                                 rawBytes.CopyTo(request,nIndex);
  162.                                 nIndex+=(ushort)rawBytes.Length;
  163.                         }
  164.  
  165.                         // using big-edian byte order
  166.                         byte[] portBytes = BitConverter.GetBytes(destPort);
  167.                         for (int i=portBytes.Length-1;i>=0;i--)
  168.                                 request[nIndex++]=portBytes[i];
  169.    
  170.                         // send connect request.
  171.                         s.Send(request,nIndex,SocketFlags.None);
  172.                         s.Receive(response);    // Get variable length response...
  173.                         //if (response[1]!=0x00)
  174.                         //      throw new ConnectionException(errorMsgs[response[1]]);
  175.                         // Success Connected...
  176.                         return s;
  177.                 }
  178.         }
  179. }