Advertisement
isendrak

SocksForSharp.cs

Jan 22nd, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.11 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace SocksForSharp{
  8.     public enum ProxyType{
  9.         Socks4, Socks5
  10.     }
  11.  
  12.     public class Proxy{
  13.         public ProxyType Type{ get; set; }
  14.         public IPEndPoint EndPoint{ get; set; }
  15.         public IPAddress Address{ get{return EndPoint.Address;} set{EndPoint.Address = value;} }
  16.         public int Port{ get{return EndPoint.Port;} set{EndPoint.Port = value;} }
  17.         public string Username{ get; set; }
  18.         public string Password{ get; set; }
  19.         public Proxy(){
  20.             EndPoint = new IPEndPoint(0, 0);
  21.             Username = Password = "";
  22.         }
  23.         public override string ToString(){
  24.             if(!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password)){
  25.                 return string.Format("{0}://{1}:{2}@{3}:{4}", Type, Username, new string('*', Password.Length), Address, Port);
  26.             }
  27.             else if(!string.IsNullOrEmpty(Username)){
  28.                 return string.Format("{0}://{1}@{2}:{3}", Type, Username, Address, Port);
  29.             }
  30.             else{
  31.                 return string.Format("{0}://{1}:{2}", Type, Address, Port);
  32.             }
  33.         }
  34.     }
  35.  
  36.     public class SocksError:Exception{
  37.         public override string Message{ get{return _Message;} }
  38.         private string _Message;
  39.         public SocksError(string Message){
  40.             _Message = Message;
  41.         }
  42.         public override string ToString(){
  43.             return string.Format("SocksError: {0}", Message);
  44.         }
  45.     }
  46.  
  47.     public static class TcpClientEx{
  48.         public static void ConnectProxy(this TcpClient tcpClient, IPEndPoint target, Proxy proxy){
  49.             ConnectProxy(tcpClient, target.Address.ToString(), target.Port, proxy);
  50.         }
  51.  
  52.         public static void ConnectProxy(this TcpClient tcpClient, IPAddress address, int port, Proxy proxy){
  53.             ConnectProxy(tcpClient, address.ToString(), port, proxy);
  54.         }
  55.  
  56.         public static void ConnectProxy(this TcpClient tcpClient, string target, int port, Proxy proxy){
  57.             tcpClient.Connect(proxy.EndPoint);
  58.             if(proxy.Type == ProxyType.Socks4){
  59.                 IPAddress address;
  60.                 MemoryStream ms = new MemoryStream();
  61.                 if(!IPAddress.TryParse(target, out address)){
  62.                     byte[] target_bytes = Encoding.UTF8.GetBytes(target);
  63.                     ms.Write(new byte[] { 4, 1, (byte)((port >> 8) & 0xff), (byte)(port & 0xff), 0, 0, 0, 1, 0 }, 0, 9);
  64.                     ms.Write(target_bytes, 0, target_bytes.Length);
  65.                     byte[] username_bytes = Encoding.UTF8.GetBytes(proxy.Username);
  66.                     ms.Write(username_bytes, 0, username_bytes.Length);
  67.                     ms.WriteByte(0);
  68.                 }
  69.                 else{
  70.                     ms.Write(new byte[] { 4, 1, (byte)((port>>8)&0xff), (byte)(port&0xff) }, 0, 4);
  71.                     byte[] username_bytes = Encoding.UTF8.GetBytes(proxy.Username);
  72.                     ms.Write(username_bytes, 0, username_bytes.Length);
  73.                     ms.WriteByte(0);
  74.                     byte[] address_bytes = address.GetAddressBytes();
  75.                     ms.Write(address_bytes, 0, address_bytes.Length);
  76.                     ms.WriteByte(0);
  77.                 }
  78.                 tcpClient.Client.Send(ms.ToArray(), 0, (int)ms.Length, SocketFlags.None);
  79.                 ms.Close();
  80.                 ms.Dispose();
  81.                 byte[] buffer = new byte[8];
  82.                 if(tcpClient.Client.Receive(buffer, 0, 8, SocketFlags.None) != 8){
  83.                     tcpClient.Close();
  84.                     throw new SocksError("Wrong number of bytes received.");
  85.                 }
  86.                 if(buffer[0] != 0){
  87.                     tcpClient.Close();
  88.                     throw new SocksError(string.Format("Wrong protocol version ({0}).", buffer[1]));
  89.                 }
  90.                 if(buffer[1] != 0x5a){
  91.                     tcpClient.Close();
  92.                     throw new SocksError(string.Format("Socks connection failed ({0:x}).", buffer[1]));
  93.                 }
  94.             }
  95.             else if(proxy.Type == ProxyType.Socks5){
  96.                 if(string.IsNullOrEmpty(proxy.Username) || string.IsNullOrEmpty(proxy.Password))
  97.                     tcpClient.Client.Send(new byte[]{5, 1, 0});
  98.                 else
  99.                     tcpClient.Client.Send(new byte[]{5, 2, 0, 2});
  100.                 byte[] buffer = new byte[0xfff];
  101.                 if(tcpClient.Client.Receive(buffer, 0, buffer.Length, SocketFlags.None) != 2){
  102.                     tcpClient.Close();
  103.                     throw new SocksError("Wrong number of bytes received.");
  104.                 }
  105.                 if(buffer[0] != 5){
  106.                     tcpClient.Close();
  107.                     throw new SocksError(string.Format("Wrong protocol version ({0}).", buffer[0]));
  108.                 }
  109.                 if(buffer[1] == 0xff){
  110.                     tcpClient.Close();
  111.                     throw new SocksError("No acceptable authentication method.");
  112.                 }
  113.                 MemoryStream ms;
  114.                 if(buffer[1] == 2){
  115.                     ms = new MemoryStream();
  116.                     ms.WriteByte(1);
  117.                     byte[] username_bytes = Encoding.UTF8.GetBytes(proxy.Username);
  118.                     ms.WriteByte((byte)username_bytes.Length);
  119.                     ms.Write(username_bytes, 0, username_bytes.Length);
  120.                     byte[] password_bytes = Encoding.UTF8.GetBytes(proxy.Password);
  121.                     ms.WriteByte((byte)password_bytes.Length);
  122.                     ms.Write(password_bytes, 0, password_bytes.Length);
  123.                     tcpClient.Client.Send(ms.ToArray(), 0, (int)ms.Length, SocketFlags.None);
  124.                     ms.Close();
  125.                     ms.Dispose();
  126.                     if(tcpClient.Client.Receive(buffer, 0, 2, SocketFlags.None) != 2){
  127.                         tcpClient.Close();
  128.                         throw new SocksError("Wrong number of bytes received.");
  129.                     }
  130.                     if(buffer[1] != 0){
  131.                         tcpClient.Close();
  132.                         throw new SocksError("Authentication failed.");
  133.                     }
  134.                 }
  135.                 ms = new MemoryStream();
  136.                 IPAddress address;
  137.                 byte[] target_bytes;
  138.                 byte address_type = 0xff;
  139.                 if(IPAddress.TryParse(target, out address)){
  140.                     target_bytes = address.GetAddressBytes();
  141.                     address_type = (byte)(target_bytes.Length == 4 ? 1 : 4);
  142.                 }
  143.                 else{
  144.                     target_bytes = Encoding.UTF8.GetBytes(target);
  145.                     address_type = 3;
  146.                 }
  147.                 ms.Write(new byte[]{5,1,0,address_type},0,4);
  148.                 if(address_type == 3)
  149.                     ms.WriteByte((byte)target_bytes.Length);
  150.                 ms.Write(target_bytes, 0, target_bytes.Length);
  151.                 ms.Write(new byte[] { (byte)((port >> 8) & 0xff), (byte)(port & 0xff) }, 0, 2);
  152.                 tcpClient.Client.Send(ms.ToArray(), 0, (byte)ms.Length, SocketFlags.None);
  153.                 int c = tcpClient.Client.Receive(buffer, 0, (byte)ms.Length, SocketFlags.None);
  154.                 if(c < 4){
  155.                     tcpClient.Close();
  156.                     throw new SocksError("Wrong number of bytes received.");
  157.                 }
  158.                 if(buffer[0] != 5){
  159.                     tcpClient.Close();
  160.                     throw new SocksError(string.Format("Wrong protocol version ({0}).", buffer[0]));
  161.                 }
  162.                 if(buffer[1] != 0){
  163.                     tcpClient.Close();
  164.                     throw new SocksError(string.Format("Socks connection failed ({0}).", buffer[1]));
  165.                 }
  166.             }
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement