using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using FakeeDeck.ButtonType;
using System.Web;
using static System.Net.WebRequestMethods;
namespace FakeeDeck
{
internal class HttpServer
{
public static HttpListener listener;
public static string url = "http://*:8000/";
public static int pageViews = 0;
public static int requestCount = 0;
public static string pageHeader =
"" +
"" +
"
" +
" " +
"";
public static string pageData = "";
public static async Task HandleIncomingConnections()
{
bool runServer = true;
// While a user hasn't visited the `shutdown` url, keep on handling requests
while (runServer)
{
// Will wait here until we hear from a connection
HttpListenerContext ctx = await listener.GetContextAsync();
// Peel out the requests and response objects
HttpListenerRequest req = ctx.Request;
HttpListenerResponse resp = ctx.Response;
// Print out some info about the request
Console.WriteLine("Request #: {0}", ++requestCount);
Console.WriteLine(req.Url.ToString());
Console.WriteLine(req.HttpMethod);
Console.WriteLine(req.UserHostName);
Console.WriteLine(req.UserAgent);
Console.WriteLine();
Dictionary postParams = new Dictionary();
if(req.HttpMethod == "POST"){
using (var reader = new StreamReader(req.InputStream, req.ContentEncoding))
{
string postData = reader.ReadToEnd();
var parsedData = HttpUtility.ParseQueryString(postData);
foreach (string key in parsedData)
{
postParams[key] = parsedData[key];
}
}
}
// If `shutdown` url requested w/ POST, then shutdown the server after serving the page
if ((req.HttpMethod == "POST") && (req.Url.AbsolutePath == "/shutdown"))
{
Console.WriteLine("Shutdown requested");
runServer = false;
}
// If `shutdown` url requested w/ POST, then shutdown the server after serving the page
if ((req.HttpMethod == "POST") && (req.Url.AbsolutePath == "/keyboard/stratogem"))
{
//new uint[] { 0x65, 0x68, 0x62, 0x66, 0x64, 0x68, 0x0D, 0x0D, }
foreach (var key in HelldiversTwoMacro.stratogems[postParams["stratogem"]])
{
KeyboardMacro.SendKey(key);
Console.WriteLine(key);
}
resp.Redirect("/");
}
// Make sure we don't increment the page views counter if `favicon.ico` is requested
if (req.Url.AbsolutePath != "/favicon.ico")
{
pageViews += 1;
}
// Write the response info
string disableSubmit = !runServer ? "disabled" : "";
byte[] data = Encoding.UTF8.GetBytes(String.Format((pageHeader + pageData + pageFooter), pageViews, disableSubmit));
resp.ContentType = "text/html";
resp.ContentEncoding = Encoding.UTF8;
resp.ContentLength64 = data.LongLength;
// Write out to the response stream (asynchronously), then close it
await resp.OutputStream.WriteAsync(data, 0, data.Length);
resp.Close();
}
}
public static void serv()
{
foreach (var stratogem in HelldiversTwoMacro.stratogems)
{
Console.WriteLine(HelldiversTwoMacro.stratogemsIcons[stratogem.Key]);
pageData +=
"
" +
" " +
"
";
}
// Create a Http server and start listening for incoming connections
listener = new HttpListener();
listener.Prefixes.Add(url);
listener.Start();
Console.WriteLine("Listening for connections on {0}", url);
// Handle requests
Task listenTask = HandleIncomingConnections();
listenTask.GetAwaiter().GetResult();
// Close the listener
listener.Close();
}
public static string FirstLetterToUpper(string str)
{
if (str == null)
return null;
if (str.Length > 1)
return char.ToUpper(str[0]) + str.Substring(1);
return str.ToUpper();
}
}
}