Example #1
0
        private void Main_Load(object sender, EventArgs e)
        {
            Log.Clear();
            Settings.Load();

            //Global keyboard hook logic by https://github.com/gmamaladze/globalmousekeyhook/blob/vNext/Demo/Main.cs
            Subscribe();

            //Add Firewall exception
            AddException();

            //Load languages to combobox:
            LoadLanguages();

            //Check plugins:
            CheckPlugins();

            //Load the keys:
            nextKeyTextBox.Text          = Settings.NextKey;
            previousKeyTextBox.Text      = Settings.PreviousKey;
            stopKeyTextBox.Text          = Settings.StopKey;
            volumeUpKeyTextBox.Text      = Settings.VolumeUpKey;
            volumeDownKeyTextBox.Text    = Settings.VolumeDownKey;
            makeFavouriteKeyTextbox.Text = Settings.MakeFavouriteKey;
            goToFavouriteKeyTextbox.Text = Settings.GoToFavouriteKey;

            nextButtonTextBox.Text          = Settings.NextButton;
            previousButtonTextBox.Text      = Settings.PreviousButton;
            stopButtonTextBox.Text          = Settings.StopButton;
            volumeUpButtonTextBox.Text      = Settings.VolumeUpButton;
            volumeDownButtonTextBox.Text    = Settings.VolumeDownButton;
            makeFavouriteButtonTextbox.Text = Settings.MakeFavouriteButton;
            goToFavouriteButtonTextbox.Text = Settings.GoToFavouriteButton;

            comboController.SelectedText = Settings.Controller;

            //Load favourites
            Favourites.Load();

            //Start telemetry grabbing:
            Telemetry       = new Ets2SdkTelemetry(250);
            Telemetry.Data += Telemetry_Data;

            if (Telemetry.Error != null)
            {
                MessageBox.Show(
                    "General info:\r\nFailed to open memory map " + Telemetry.Map +
                    " - on some systems you need to run the client (this app) with elevated permissions, because e.g. you're running Steam/ETS2 with elevated permissions as well. .NET reported the following Exception:\r\n" +
                    Telemetry.Error.Message + "\r\n\r\nStacktrace:\r\n" + Telemetry.Error.StackTrace);
            }

            //Open server:
            myServer = new SimpleHTTPServer(Directory.GetCurrentDirectory() + "\\web", Settings.Port);
            writeFile("none", "0", "0");

            //Load IP addresses:
            LoadAddresses();

            if (AttachJoystick())
            {
                foreach (var item in joystick.AvailableDevices)
                {
                    comboController.Items.Add(item.InstanceName);
                }
            }
            AttachJoystick();

            currentGameTimer.Start();

            //Add handlers:
            nextKeyTextBox.KeyDown          += keyInput;
            previousKeyTextBox.KeyDown      += keyInput;
            stopKeyTextBox.KeyDown          += keyInput;
            volumeUpKeyTextBox.KeyDown      += keyInput;
            volumeDownKeyTextBox.KeyDown    += keyInput;
            makeFavouriteKeyTextbox.KeyDown += keyInput;
            goToFavouriteKeyTextbox.KeyDown += keyInput;

            //Remove key binding:
            nextKeyTextBox.KeyDown          += removeBinding;
            previousKeyTextBox.KeyDown      += removeBinding;
            stopKeyTextBox.KeyDown          += removeBinding;
            volumeUpKeyTextBox.KeyDown      += removeBinding;
            volumeDownKeyTextBox.KeyDown    += removeBinding;
            makeFavouriteKeyTextbox.KeyDown += removeBinding;
            goToFavouriteKeyTextbox.KeyDown += removeBinding;

            nextButtonTextBox.KeyDown          += removeBinding;
            previousButtonTextBox.KeyDown      += removeBinding;
            stopButtonTextBox.KeyDown          += removeBinding;
            volumeUpButtonTextBox.KeyDown      += removeBinding;
            volumeDownButtonTextBox.KeyDown    += removeBinding;
            makeFavouriteButtonTextbox.KeyDown += removeBinding;
            goToFavouriteButtonTextbox.KeyDown += removeBinding;
        }
Example #2
0
        private void Process(HttpListenerContext context)
        {
            context.Response.AddHeader("Cache-Control", "no-store, must-revalidate");

            string filename = context.Request.Url.AbsolutePath;

            //Console.WriteLine(filename);
            if (filename.Contains("?"))
            {
                filename = filename.Split("?".ToCharArray())[0];
            }
            //filename = filename.Replace("%20", " ");
            filename = WebUtility.UrlDecode(filename);
            filename = filename.Substring(1);
            if (filename.Contains("/"))
            {
                filename = filename.Replace("/", @"\");
            }
            if (string.IsNullOrEmpty(filename))
            {
                foreach (string indexFile in _indexFiles)
                {
                    if (File.Exists(Path.Combine(_rootDirectory, indexFile)))
                    {
                        filename = indexFile;
                        break;
                    }
                }
            }

            filename = Path.Combine(_rootDirectory, filename);

            if (context.Request.Url.AbsolutePath.StartsWith("/favourite/"))
            {
                string favourite = context.Request.Url.AbsoluteUri;
                favourite = WebUtility.UrlDecode(favourite);
                favourite = favourite.Split(new string[] { "/favourite/" }, StringSplitOptions.None)[1];
                Console.WriteLine(favourite);
                try
                {
                    string[] favouriteArray = favourite.Split(new string[] { "/" }, StringSplitOptions.None);
                    string   text           = "";
                    if (favouriteArray.Length == 1 && favourite == "")
                    {
                        text = Favourites.Get("");
                    }
                    else if (favouriteArray.Length == 1)
                    {
                        text = "{\"Name\": \"" + Favourites.Get(favourite) + "\"}";
                    }
                    else
                    {
                        Favourites.Set(favouriteArray[0], favouriteArray[1]);
                        Favourites.Save();
                        text = "{\"Success\": true}";
                    }
                    context.Response.ContentType     = "application/json";
                    context.Response.ContentLength64 = Encoding.UTF8.GetBytes(text).Length;
                    context.Response.StatusCode      = (int)HttpStatusCode.OK;
                    context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(text), 0,
                                                        Encoding.UTF8.GetBytes(text).Length);
                    context.Response.OutputStream.Flush();
                }
                catch (Exception exception)
                {
                    Log.Write(exception.ToString());
                }
            }
            else if (context.Request.Url.AbsolutePath.StartsWith("/station/"))
            {
                string station = context.Request.Url.AbsoluteUri;
                station = WebUtility.UrlDecode(station);
                station = station.Split(new string[] { "/station/" }, StringSplitOptions.None)[1];
                Console.WriteLine(station);
                try
                {
                    Station.SetStation(station.Split("/".ToCharArray())[0], station.Split("/".ToCharArray())[1], station.Split("?".ToCharArray())[1]);
                }
                catch (Exception)
                {
                    Station.SetStation(station.Split("/".ToCharArray())[0], "0");
                }

                string text = "{\"Success\": true}";

                context.Response.ContentType     = "application/json";
                context.Response.ContentLength64 = Encoding.UTF8.GetBytes(text).Length;
                context.Response.StatusCode      = (int)HttpStatusCode.OK;
                context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(text), 0, Encoding.UTF8.GetBytes(text).Length);
                context.Response.OutputStream.Flush();
            }
            else if (context.Request.Url.AbsolutePath == "/api/")
            {
                string text = Newtonsoft.Json.JsonConvert.SerializeObject(Main.ets2data);

                context.Response.ContentType     = "application/json";
                context.Response.ContentLength64 = Encoding.UTF8.GetBytes(text).Length;
                context.Response.StatusCode      = (int)HttpStatusCode.OK;
                context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(text), 0, Encoding.UTF8.GetBytes(text).Length);
                context.Response.OutputStream.Flush();
            }
            else if (context.Request.Url.AbsolutePath == "/commands/")
            {
                string text = Newtonsoft.Json.JsonConvert.SerializeObject(Main.commandsData);

                context.Response.ContentType     = "application/json";
                context.Response.ContentLength64 = Encoding.UTF8.GetBytes(text).Length;
                context.Response.StatusCode      = (int)HttpStatusCode.OK;
                context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(text), 0, Encoding.UTF8.GetBytes(text).Length);
                context.Response.OutputStream.Flush();
            }
            else if (context.Request.Url.AbsolutePath.StartsWith("/eskago/"))
            {
                string station = context.Request.Url.AbsoluteUri;
                station = WebUtility.UrlDecode(station);
                station = station.Split(new string[] { "/eskago/" }, StringSplitOptions.None)[1];

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.eskago.pl/radio/" + station);
                request.AutomaticDecompression = DecompressionMethods.GZip;

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    using (Stream stream = response.GetResponseStream())
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            string data = reader.ReadToEnd();

                            Regex  streamRegex = new Regex(@".*streamUrl\s=\s'(.+)';.*");
                            string baseUrl     = streamRegex.Match(data).Groups[1].Value;

                            Regex  icsuRegex = new Regex(".*icsu\"\\svalue=\"(.+)\"\\s.*");
                            string append    = icsuRegex.Match(data).Groups[1].Value;

                            context.Response.RedirectLocation = baseUrl + append;
                            context.Response.StatusCode       = 302;
                            context.Response.OutputStream.Flush();
                        }
            }
            else if (File.Exists(filename))
            {
                try
                {
                    Stream input = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                    //Adding permanent http response headers
                    string mime;
                    context.Response.ContentType = _mimeTypeMappings.TryGetValue(Path.GetExtension(filename), out mime)
                        ? mime
                        : "application/octet-stream";
                    context.Response.ContentLength64 = input.Length;
                    //context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
                    //context.Response.AddHeader("Last-Modified", System.IO.File.GetLastWriteTime(filename).ToString("r"));
                    byte[] buffer = new byte[1024 * 16];
                    int    nbytes;
                    while ((nbytes = input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        context.Response.OutputStream.Write(buffer, 0, nbytes);
                    }
                    input.Close();

                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                    context.Response.OutputStream.Flush();
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    Log.Write(ex.ToString());
                }
            }
            else
            {
                //context.Response.AddHeader("X-Requested-File", filename);
                context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                Log.Write("Not found: " + filename);
            }

            context.Response.OutputStream.Close();
        }