Guest User

C# SOCKS5 Proxy Implementation doesn't always work on local

a guest
Jun 24th, 2018
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. /// <summary>
  2. /// SOCK Reference is at https://www.ietf.org/rfc/rfc1928.txt
  3. /// </summary>
  4. public class SocksServer
  5. {
  6.     public void Start()
  7.     {
  8.         new Thread(StartInternal).Start();
  9.     }
  10.  
  11.     private void StartInternal()
  12.     {
  13.         IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 32128);
  14.         TcpListener listener = new TcpListener(localEndPoint);
  15.  
  16.         listener.Start();
  17.         Console.Out.WriteLine("Waiting for clients...");
  18.  
  19.         while (true)
  20.         {
  21.             var client = listener.AcceptTcpClient();
  22.             Console.Out.WriteLine("Connected");
  23.  
  24.             new Thread(() =>
  25.             {
  26.                 var stream = client.GetStream();
  27.                 var reader = new BinaryReader(stream);
  28.                 var writer = new BinaryWriter(stream);
  29.  
  30.                 // ======== CLIENT -> SERVER: Methods Offer (Page 3; I) ========
  31.                 byte socksVersion = reader.ReadByte(); // VER
  32.                 if (socksVersion != 5)
  33.                 {
  34.                     client.Close();
  35.                     return;
  36.                 }
  37.  
  38.                 bool methodNoAuth = false;
  39.                 byte methods = reader.ReadByte(); // NMETHODS
  40.                 for (int i = 0; i < methods; i++)
  41.                     methodNoAuth |= reader.ReadByte() == 0; // METHODS
  42.  
  43.                 if (!methodNoAuth)
  44.                 {
  45.                     client.Close();
  46.                     return;
  47.                 }
  48.  
  49.                 // ======== SERVER -> CLIENT: Method Select (Page 3; II) ========
  50.                 writer.Write((byte)5); // VER
  51.                 writer.Write((byte)0); // METHOD
  52.                 writer.Flush();
  53.  
  54.                 // ======== CLIENT -> SERVER: Request (Page 4) ========
  55.                 reader.ReadByte(); // VER
  56.                 reader.ReadByte(); // CMD
  57.                 reader.ReadByte(); // RSV
  58.                 byte type = reader.ReadByte(); // ATYP
  59.  
  60.                 if (type == 1)
  61.                 {
  62.                     byte[] ipAddrBytes = reader.ReadBytes(4); // DST.ADDR
  63.                     short port = BitConverter.ToInt16(reader.ReadBytes(2).Reverse().ToArray(), 0); // DST.PORT
  64.  
  65.                     TcpClient forwardClient = new TcpClient();
  66.                     forwardClient.Connect(new IPAddress(ipAddrBytes), port);
  67.                     NetworkStream forwardStream = forwardClient.GetStream();
  68.  
  69.                     // ======== SERVER -> CLIENT: Reply (Page 5-6) ========
  70.                     writer.Write((byte)5); // VER
  71.                     writer.Write((byte)0); // REP
  72.                     writer.Write((byte)0); // RSV
  73.                     writer.Write((byte)1); // ATYP
  74.                     writer.Write(new byte[4]); // BND.ADDR
  75.                     writer.Write(new byte[2]); // BND.PORT
  76.                     writer.Flush();
  77.  
  78.                     // Relay stream to my own servers
  79.                     try
  80.                     {
  81.                         while (true)
  82.                         {
  83.                             byte[] buffer = new byte[4096];
  84.                             int clientByteCount = stream.Read(buffer, 0, buffer.Length);
  85.                             forwardStream.Write(buffer, 0, clientByteCount);
  86.                             forwardStream.Flush();
  87.  
  88.                             int forwardReadBytes = forwardStream.Read(buffer, 0, buffer.Length);
  89.                             stream.Write(buffer, 0, forwardReadBytes);
  90.                             stream.Flush();
  91.                         }
  92.                     }
  93.                     catch { }
  94.                 }
  95.             }).Start();
  96.         }
  97.     }
  98. }
Add Comment
Please, Sign In to add comment