ftpud12

Untitled

Dec 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.34 KB | None | 0 0
  1. using SimpleWebServer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11.  
  12. namespace ServerControl
  13. {
  14.    class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             WebServer ws = new WebServer("http://*:8080/");            
  19.             ws.AddPage("/", rootPage);
  20.             ws.AddPage("/list", list);
  21.             ws.AddPage("/set", set);
  22.             ws.AddPage("/skip", skipCurrent);
  23.             ws.AddPage("/current", getCurrent);
  24.             ws.Run();
  25.             Console.WriteLine("A simple webserver. Press a key to quit.");
  26.             while (true)
  27.             {
  28.                 Thread.Sleep(1000);
  29.             }
  30.             ws.Stop();
  31.         }
  32.  
  33.         public static string rootPage(HttpListenerRequest request) {
  34.             return File.ReadAllText("index.html");
  35.         }
  36.  
  37.         public static string list(HttpListenerRequest request)
  38.         {
  39.             return executeProcess("find ./content/music/* | sort -V | grep mp4");
  40.         }
  41.  
  42.         public static string set(HttpListenerRequest request)
  43.         {
  44.             var p = request.QueryString["number"];
  45.             if (p != null && p.All(Char.IsDigit) && p.Length<5) {
  46.                 File.WriteAllText("counter.cfg", p);
  47.             }
  48.             executeProcess("killall ffmpeg_stream");
  49.             return "done";
  50.         }
  51.  
  52.         private static string skipCurrent(HttpListenerRequest request)
  53.         {
  54.             return executeProcess("killall ffmpeg_stream");
  55.         }
  56.  
  57.         private static string getCurrent(HttpListenerRequest request)
  58.         {
  59.             return File.ReadAllText("counter.cfg");
  60.         }
  61.  
  62.  
  63.         private static string executeProcess(string proc)
  64.         {
  65.             Process process = new Process();
  66.             var startInfo = new ProcessStartInfo
  67.             {
  68.                 FileName = "/bin/bash",
  69.                 Arguments = $"-c \"{proc}\""
  70.             };
  71.             startInfo.UseShellExecute = false;
  72.             startInfo.RedirectStandardOutput = true;
  73.             process.StartInfo = startInfo;
  74.             process.Start();
  75.             string output = process.StandardOutput.ReadToEnd();
  76.             return output;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment