Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. //
  2. // csc wsserver.cs
  3. // wsserver.exe
  4.  
  5. using System;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10.  
  11. class Server {
  12.     public static void Main() {
  13.         string ip = "127.0.0.1";
  14.         int port = 80;
  15.         var server = new TcpListener(IPAddress.Parse(ip), port);
  16.  
  17.         server.Start();
  18.         Console.WriteLine("Server has started on {0}:{1}, Waiting for a connection...", ip, port);
  19.  
  20.         TcpClient client = server.AcceptTcpClient();
  21.         Console.WriteLine("A client connected.");
  22.  
  23.         NetworkStream stream = client.GetStream();
  24.  
  25.         // enter to an infinite cycle to be able to handle every change in stream
  26.         while (true) {
  27.             while (!stream.DataAvailable);
  28.             while (client.Available < 3); // match against "get"
  29.  
  30.             byte[] bytes = new byte[client.Available];
  31.             stream.Read(bytes, 0, client.Available);
  32.             string s = Encoding.UTF8.GetString(bytes);
  33.  
  34.             if (Regex.IsMatch(s, "^GET", RegexOptions.IgnoreCase)) {
  35.                 Console.WriteLine("=====Handshaking from client=====\n{0}", s);
  36.  
  37.                 // 1. Obtain the value of the "Sec-WebSocket-Key" request header without any leading or trailing whitespace
  38.                 // 2. Concatenate it with "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" (a special GUID specified by RFC 6455)
  39.                 // 3. Compute SHA-1 and Base64 hash of the new value
  40.                 // 4. Write the hash back as the value of "Sec-WebSocket-Accept" response header in an HTTP response
  41.                 string swk = Regex.Match(s, "Sec-WebSocket-Key: (.*)").Groups[1].Value.Trim();
  42.                 string swka = swk + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
  43.                 byte[] swkaSha1 = System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(swka));
  44.                 string swkaSha1Base64 = Convert.ToBase64String(swkaSha1);
  45.  
  46.                 // HTTP/1.1 defines the sequence CR LF as the end-of-line marker
  47.                 byte[] response = Encoding.UTF8.GetBytes(
  48.                     "HTTP/1.1 101 Switching Protocols\r\n" +
  49.                     "Connection: Upgrade\r\n" +
  50.                     "Upgrade: websocket\r\n" +
  51.                     "Sec-WebSocket-Accept: " + swkaSha1Base64 + "\r\n\r\n");
  52.  
  53.                 stream.Write(response, 0, response.Length);
  54.             } else {
  55.                 bool fin = (bytes[0] & 0b10000000) != 0,
  56.                     mask = (bytes[1] & 0b10000000) != 0; // must be true, "All messages from the client to the server have this bit set"
  57.  
  58.                 int opcode = bytes[0] & 0b00001111, // expecting 1 - text message
  59.                     msglen = bytes[1] - 128, // & 0111 1111
  60.                     offset = 2;
  61.  
  62.                 if (msglen == 126) {
  63.                     // was ToUInt16(bytes, offset) but the result is incorrect
  64.                     msglen = BitConverter.ToUInt16(new byte[] { bytes[3], bytes[2] }, 0);
  65.                     offset = 4;
  66.                 } else if (msglen == 127) {
  67.                     Console.WriteLine("TODO: msglen == 127, needs qword to store msglen");
  68.                     // i don't really know the byte order, please edit this
  69.                     // msglen = BitConverter.ToUInt64(new byte[] { bytes[5], bytes[4], bytes[3], bytes[2], bytes[9], bytes[8], bytes[7], bytes[6] }, 0);
  70.                     // offset = 10;
  71.                 }
  72.  
  73.                 if (msglen == 0)
  74.                     Console.WriteLine("msglen == 0");
  75.                 else if (mask) {
  76.                     byte[] decoded = new byte[msglen];
  77.                     byte[] masks = new byte[4] { bytes[offset], bytes[offset + 1], bytes[offset + 2], bytes[offset + 3] };
  78.                     offset += 4;
  79.                    
  80.                     for (int i = 0; i < msglen; ++i)
  81.                         decoded[i] = (byte)(bytes[offset + i] ^ masks[i % 4]);
  82.  
  83.                     string text = Encoding.UTF8.GetString(decoded);
  84.                     Console.WriteLine("{0}", text);
  85.                 } else
  86.                     Console.WriteLine("mask bit not set");
  87.  
  88.                 Console.WriteLine();
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement