Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. using System.Text.RegularExpressions;
  9. using System.Security;
  10. using System.Security.Cryptography;
  11.  
  12. namespace WebSock
  13. {
  14.     class Program
  15.     {
  16.         static void server_thread()
  17.         {
  18.             EndPoint server_end_point = new IPEndPoint(IPAddress.Any, 8080);
  19.             IPAddress ipAd = IPAddress.Any; //use local m/c IP address, and use the same in the client
  20.             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  21.             socket.Bind(new IPEndPoint(ipAd, 8080));
  22.  
  23.             bool connected = false;
  24.  
  25.             while (true)
  26.             {
  27.                 socket.Listen(1);
  28.  
  29.                
  30.                 Socket s = socket.Accept();
  31.                 Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
  32.                 connected = true;
  33.  
  34.                 while (connected)
  35.                 {
  36.                     byte[] b = new byte[512];
  37.                     if (s.Receive(b) > 0)
  38.                     {
  39.                         string input = Encoding.ASCII.GetString(b);
  40.                         Console.WriteLine("Recived: " + input);
  41.  
  42.                         string get_regexp = @"GET\s([^\s]+)\sHTTP([^\r\n]+)\r\n";
  43.                         string host_regexp = @"HOST:\s([^\r\n]+)\r\n";
  44.                         string key1_regexp = @"Sec-WebSocket-Key1:\s(.*?)\r\n";
  45.                         string key2_regexp = @"Sec-WebSocket-Key2:\s(.*?)\r\n";
  46.                         string protocol_regexp = @"Sec-WebSocket-Protocol: (.*?)\r\n";
  47.                         string origin_regexp = @"Origin: (.*?)\r\n";
  48.                         string code_regexp = @"^(.+)\r\n(.+)$";
  49.  
  50.                         string resource, httpver, host, key1, key2, protocol = "", origin, code;
  51.  
  52.                         Match match;
  53.  
  54.                         match = Regex.Match(input, get_regexp, RegexOptions.IgnoreCase | RegexOptions.Multiline);
  55.                         resource = match.Groups[1].Value;
  56.                         httpver = match.Groups[2].Value;
  57.  
  58.                         match = Regex.Match(input, host_regexp, RegexOptions.IgnoreCase | RegexOptions.Multiline);
  59.                         host = match.Groups[1].Value;
  60.  
  61.                         match = Regex.Match(input, key1_regexp, RegexOptions.Multiline | RegexOptions.IgnoreCase);
  62.                         key1 = match.Groups[1].Value;
  63.  
  64.                         match = Regex.Match(input, key2_regexp, RegexOptions.Multiline | RegexOptions.IgnoreCase);
  65.                         key2 = match.Groups[1].Value;
  66.  
  67.                         match = Regex.Match(input, protocol_regexp, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
  68.                         if (match.Success)
  69.                             protocol = match.Groups[1].Value;
  70.  
  71.                         match = Regex.Match(input, code_regexp, RegexOptions.Singleline | RegexOptions.IgnoreCase);
  72.                         code = match.Groups[2].Value;
  73.  
  74.                         match = Regex.Match(input, origin_regexp, RegexOptions.Multiline | RegexOptions.IgnoreCase);
  75.                         origin = match.Groups[1].Value;
  76.  
  77.                         string output = "HTTP" + httpver + " 101 WebSocket Protocol Handshake\r\n" +
  78.                             "Upgrade: WebSocket\r\n" +
  79.                             "Connection: Upgrade\r\n" +
  80.                             "Sec-WebSocket-Origin: " + origin + "\r\n" +
  81.                             "Sec-WebSocket-Location: ws://" + host + resource + "\r\n" +
  82.                             (protocol != "" ? "Sec-WebSocket-Protocol: " + protocol + "\r\n" : "") +
  83.                             "\r\n" +
  84.                             create_handshake_code(key1, key2, code);
  85.                         Console.WriteLine("Key11: {0}, Key2: {1}, Code: {2}\r\n", key1, key2, code);
  86.  
  87.                         Console.WriteLine("Output: " + output);
  88.  
  89.                         if (resource == "/test")
  90.                         {
  91.                             s.Send(Encoding.ASCII.GetBytes(output));
  92.                             s.Receive(Encoding.ASCII.GetBytes(output));
  93.                         }
  94.  
  95.                         // s.Send(Encoding.ASCII.GetBytes("Oh snap!"));
  96.  
  97.                     }
  98.                 }
  99.                 s.Close();
  100.             }
  101.  
  102.             socket.Close();
  103.             Console.WriteLine("Stopping server");
  104.         }
  105.  
  106.  
  107.         static int _doStuffToObtainAnInt32(string key)
  108.         {
  109.             MatchCollection number = Regex.Matches(key, "[0-9]");
  110.             MatchCollection space = Regex.Matches(key, @"\s");
  111.  
  112.             if (number.Count > 0 && space.Count > 0)
  113.             {
  114.                 string num = "";
  115.                 for (int i = 0; i < number.Count; ++i)
  116.                 {
  117.                     num += number[i].Value;
  118.                 }
  119.                 return int.Parse(num) / space.Count;
  120.             }
  121.             else
  122.             {
  123.                 return 0;
  124.             }
  125.         }
  126.  
  127.         static string md5(string str)
  128.         {
  129.             MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
  130.  
  131.             byte[] data = Encoding.ASCII.GetBytes(str);
  132.  
  133.             data = x.ComputeHash(data);
  134.  
  135.             return System.Text.Encoding.ASCII.GetString(data);
  136.         }
  137.  
  138.         static string create_handshake_code(string key1, string key2, string code)
  139.         {
  140.  
  141.             return md5(string.Format("N", _doStuffToObtainAnInt32(key1)) +
  142.                 string.Format("N", _doStuffToObtainAnInt32(key2)) +
  143.                 code);
  144.         }
  145.  
  146.         static void Main(string[] args)
  147.         {
  148.             Thread serverThread = new Thread(new ThreadStart(server_thread));
  149.             serverThread.Start();
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement