Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.Text.RegularExpressions;
- using System.Security;
- using System.Security.Cryptography;
- namespace WebSock
- {
- class Program
- {
- static void server_thread()
- {
- EndPoint server_end_point = new IPEndPoint(IPAddress.Any, 8080);
- IPAddress ipAd = IPAddress.Any; //use local m/c IP address, and use the same in the client
- Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- socket.Bind(new IPEndPoint(ipAd, 8080));
- bool connected = false;
- while (true)
- {
- socket.Listen(1);
- Socket s = socket.Accept();
- Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
- connected = true;
- while (connected)
- {
- byte[] b = new byte[512];
- if (s.Receive(b) > 0)
- {
- string input = Encoding.ASCII.GetString(b);
- Console.WriteLine("Recived: " + input);
- string get_regexp = @"GET\s([^\s]+)\sHTTP([^\r\n]+)\r\n";
- string host_regexp = @"HOST:\s([^\r\n]+)\r\n";
- string key1_regexp = @"Sec-WebSocket-Key1:\s(.*?)\r\n";
- string key2_regexp = @"Sec-WebSocket-Key2:\s(.*?)\r\n";
- string protocol_regexp = @"Sec-WebSocket-Protocol: (.*?)\r\n";
- string origin_regexp = @"Origin: (.*?)\r\n";
- string code_regexp = @"^(.+)\r\n(.+)$";
- string resource, httpver, host, key1, key2, protocol = "", origin, code;
- Match match;
- match = Regex.Match(input, get_regexp, RegexOptions.IgnoreCase | RegexOptions.Multiline);
- resource = match.Groups[1].Value;
- httpver = match.Groups[2].Value;
- match = Regex.Match(input, host_regexp, RegexOptions.IgnoreCase | RegexOptions.Multiline);
- host = match.Groups[1].Value;
- match = Regex.Match(input, key1_regexp, RegexOptions.Multiline | RegexOptions.IgnoreCase);
- key1 = match.Groups[1].Value;
- match = Regex.Match(input, key2_regexp, RegexOptions.Multiline | RegexOptions.IgnoreCase);
- key2 = match.Groups[1].Value;
- match = Regex.Match(input, protocol_regexp, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
- if (match.Success)
- protocol = match.Groups[1].Value;
- match = Regex.Match(input, code_regexp, RegexOptions.Singleline | RegexOptions.IgnoreCase);
- code = match.Groups[2].Value;
- match = Regex.Match(input, origin_regexp, RegexOptions.Multiline | RegexOptions.IgnoreCase);
- origin = match.Groups[1].Value;
- string output = "HTTP" + httpver + " 101 WebSocket Protocol Handshake\r\n" +
- "Upgrade: WebSocket\r\n" +
- "Connection: Upgrade\r\n" +
- "Sec-WebSocket-Origin: " + origin + "\r\n" +
- "Sec-WebSocket-Location: ws://" + host + resource + "\r\n" +
- (protocol != "" ? "Sec-WebSocket-Protocol: " + protocol + "\r\n" : "") +
- "\r\n" +
- create_handshake_code(key1, key2, code);
- Console.WriteLine("Key11: {0}, Key2: {1}, Code: {2}\r\n", key1, key2, code);
- Console.WriteLine("Output: " + output);
- if (resource == "/test")
- {
- s.Send(Encoding.ASCII.GetBytes(output));
- s.Receive(Encoding.ASCII.GetBytes(output));
- }
- // s.Send(Encoding.ASCII.GetBytes("Oh snap!"));
- }
- }
- s.Close();
- }
- socket.Close();
- Console.WriteLine("Stopping server");
- }
- static int _doStuffToObtainAnInt32(string key)
- {
- MatchCollection number = Regex.Matches(key, "[0-9]");
- MatchCollection space = Regex.Matches(key, @"\s");
- if (number.Count > 0 && space.Count > 0)
- {
- string num = "";
- for (int i = 0; i < number.Count; ++i)
- {
- num += number[i].Value;
- }
- return int.Parse(num) / space.Count;
- }
- else
- {
- return 0;
- }
- }
- static string md5(string str)
- {
- MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
- byte[] data = Encoding.ASCII.GetBytes(str);
- data = x.ComputeHash(data);
- return System.Text.Encoding.ASCII.GetString(data);
- }
- static string create_handshake_code(string key1, string key2, string code)
- {
- return md5(string.Format("N", _doStuffToObtainAnInt32(key1)) +
- string.Format("N", _doStuffToObtainAnInt32(key2)) +
- code);
- }
- static void Main(string[] args)
- {
- Thread serverThread = new Thread(new ThreadStart(server_thread));
- serverThread.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement