From bfcb895cb3282b80c6257160d840417f5786029a Mon Sep 17 00:00:00 2001 From: Azaaxin Date: Mon, 26 Dec 2022 04:56:30 +0100 Subject: [PATCH] Adding first version of the api rewrite. Removing many dependencies. --- .../GotobedServer/GotobedServer.csproj | 13 + Minimal-server/GotobedServer/Program.cs | 20 ++ .../Properties/launchSettings.json | 28 +++ .../appsettings.Development.json | 8 + Minimal-server/GotobedServer/appsettings.json | 9 + Minimal-server/GotobedServer/handler.cs | 235 ++++++++++++++++++ Minimal-server/GotobedServer/mdnsService.cs | 51 ++++ 7 files changed, 364 insertions(+) create mode 100644 Minimal-server/GotobedServer/GotobedServer.csproj create mode 100644 Minimal-server/GotobedServer/Program.cs create mode 100644 Minimal-server/GotobedServer/Properties/launchSettings.json create mode 100644 Minimal-server/GotobedServer/appsettings.Development.json create mode 100644 Minimal-server/GotobedServer/appsettings.json create mode 100644 Minimal-server/GotobedServer/handler.cs create mode 100644 Minimal-server/GotobedServer/mdnsService.cs diff --git a/Minimal-server/GotobedServer/GotobedServer.csproj b/Minimal-server/GotobedServer/GotobedServer.csproj new file mode 100644 index 0000000..c8ce61a --- /dev/null +++ b/Minimal-server/GotobedServer/GotobedServer.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/Minimal-server/GotobedServer/Program.cs b/Minimal-server/GotobedServer/Program.cs new file mode 100644 index 0000000..91762c4 --- /dev/null +++ b/Minimal-server/GotobedServer/Program.cs @@ -0,0 +1,20 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); +var welcomeMessage = "Go to bed server."; +var status = handler.FirstRun.status("https://github.com/Myxelium/go-to-bed/"); +handler.MdsService.Start(); + +app.MapGet("/", () => Results.Text(welcomeMessage + "Status: " + status, "text/html")); + +app.MapPost("/go-to-bed/sleep", () => handler.ShutdownCommand.Hibernate()); + +app.MapPost("/go-to-bed/shutdown", () => handler.ShutdownCommand.Shutdown()); + +app.MapPost("/go-to-bed/reboot", () => handler.ShutdownCommand.Reboot()); + +app.MapPost("/go-to-bed/logout", () => handler.ShutdownCommand.Logout()); + +app.MapPost("/go-to-bed/lock", () => handler.ShutdownCommand.Lock()); + + +app.Run(); \ No newline at end of file diff --git a/Minimal-server/GotobedServer/Properties/launchSettings.json b/Minimal-server/GotobedServer/Properties/launchSettings.json new file mode 100644 index 0000000..8b6a63a --- /dev/null +++ b/Minimal-server/GotobedServer/Properties/launchSettings.json @@ -0,0 +1,28 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:3916", + "sslPort": 44344 + } + }, + "profiles": { + "TodoApi": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7001;http://localhost:5230", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Minimal-server/GotobedServer/appsettings.Development.json b/Minimal-server/GotobedServer/appsettings.Development.json new file mode 100644 index 0000000..ff66ba6 --- /dev/null +++ b/Minimal-server/GotobedServer/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Minimal-server/GotobedServer/appsettings.json b/Minimal-server/GotobedServer/appsettings.json new file mode 100644 index 0000000..4d56694 --- /dev/null +++ b/Minimal-server/GotobedServer/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Minimal-server/GotobedServer/handler.cs b/Minimal-server/GotobedServer/handler.cs new file mode 100644 index 0000000..5c6524b --- /dev/null +++ b/Minimal-server/GotobedServer/handler.cs @@ -0,0 +1,235 @@ +using System.Diagnostics; +using System.Net.NetworkInformation; +using System.Net.Sockets; +using System.Runtime.InteropServices; +using Makaretu.Dns; +using Microsoft.Win32; + +namespace handler +{ + public class MdsService + { + public static void Start() + { + var macAddress = ( + from networkInterface in NetworkInterface.GetAllNetworkInterfaces() + where networkInterface.OperationalStatus == OperationalStatus.Up + select networkInterface.GetPhysicalAddress().ToString()).FirstOrDefault(); + + var addresses = MulticastService.GetIPAddresses() + .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).ToArray(); + + //TODO find macadress and send it + var serviceDiscovery = new ServiceDiscovery(); + var service = new ServiceProfile("GoToBed-Server", "_gotobed._tcp", 13378); + service.AddProperty("mac", "macAddr"); + service.AddProperty("protocol", "http"); + service.AddProperty("ip", addresses[0].MapToIPv4().ToString()); + serviceDiscovery.Advertise(service); + } + } + + public class FirstRun + { + public static readonly string configPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/go-to-bed-server-first-run"; + + public static void openGithubDocumentation(string uri) + { + Uri url = new Uri(uri); + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Process.Start(new ProcessStartInfo(url.AbsoluteUri) { UseShellExecute = true }); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("xdg-open", url.AbsoluteUri); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + Process.Start("open", url.AbsolutePath); + } + else + { + throw new Exception("Unsupported OS!"); + } + } + public static bool status(string documentationPath) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\GoToBed", true); + if (key == null) + { + key = Registry.CurrentUser.CreateSubKey(@"Software\GoToBed"); + key.SetValue("FirstRun", true); + openGithubDocumentation(documentationPath); + return true; + } + + return false; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + if (!File.Exists(configPath)) + { + File.Create(configPath); + openGithubDocumentation(documentationPath); + return true; + } + + return false; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + if (!File.Exists(configPath)) + { + File.Create(configPath); + openGithubDocumentation(documentationPath); + return true; + } + else + { + return false; + } + } + else + { + throw new Exception("Unsupported OS!"); + } + } + } + + public class ShutdownCommand + { + [DllImport("user32.dll")] + private static extern bool ExitWindowsEx(uint uFlags, uint dwReason); + + // TODO: will clean only few are needed. Checked out what things does from documentation. + private const uint EWX_LOGOFF = 0x00000000; + private const uint EWX_SHUTDOWN = 0x00000001; + private const uint EWX_REBOOT = 0x00000002; + private const uint EWX_FORCE = 0x00000004; + private const uint EWX_POWEROFF = 0x00000008; + private const uint EWX_FORCEIFHUNG = 0x00000010; + private const uint EWX_QUICKRESOLVE = 0x00000020; + private const uint EWX_RESTARTAPPS = 0x00000040; + private const uint EWX_HYBRID_SHUTDOWN = 0x00400000; + private const uint EWX_BOOTOPTIONS = 0x01000000; + + private const uint SHTDN_REASON_MAJOR_OTHER = 0x00000000; + private const uint SHTDN_REASON_MINOR_OTHER = 0x00000000; + private const uint SHTDN_REASON_FLAG_PLANNED = 0x80000000; + + public static void Shutdown() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("shutdown", "-h now"); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + Process.Start("shutdown", "-h now"); + } + else + { + throw new Exception("Unsupported OS!"); + } + } + + public static void Hibernate() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + ExitWindowsEx(EWX_HYBRID_SHUTDOWN | EWX_FORCE, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("systemctl", "suspend"); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + Process.Start("pmset", "sleepnow"); + } + else + { + throw new Exception("Unsupported OS!"); + } + } + + public static void Abort() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + ExitWindowsEx(EWX_FORCEIFHUNG | EWX_FORCE, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED); + } + else + { + throw new Exception("Unsupported OS!"); + } + } + + public static void Reboot() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + ExitWindowsEx(EWX_REBOOT | EWX_FORCE, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("shutdown", "-r now"); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + Process.Start("shutdown", "-r now"); + } + else + { + throw new Exception("Unsupported OS!"); + } + } + public static void Logout() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + ExitWindowsEx(EWX_LOGOFF | EWX_FORCE, SHTDN_REASON_MAJOR_OTHER | SHTDN_REASON_MINOR_OTHER | SHTDN_REASON_FLAG_PLANNED); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("shutdown", "-l now"); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + Process.Start("shutdown", "-l now"); + } + else + { + throw new Exception("Unsupported OS!"); + } + } + + public static void Lock() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Process.Start("rundll32.exe", "user32.dll,LockWorkStation"); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("gnome-screensaver-command", "-l"); + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + Process.Start("pmset", "displaysleepnow"); + } + else + { + throw new Exception("Unsupported OS!"); + } + } + } +} \ No newline at end of file diff --git a/Minimal-server/GotobedServer/mdnsService.cs b/Minimal-server/GotobedServer/mdnsService.cs new file mode 100644 index 0000000..1050f32 --- /dev/null +++ b/Minimal-server/GotobedServer/mdnsService.cs @@ -0,0 +1,51 @@ +using System.Net.NetworkInformation; +using Makaretu.Dns; + +public class MdsService +{ + + public void Start() + { + var macAddr = ( + from nic in NetworkInterface.GetAllNetworkInterfaces() + where nic.OperationalStatus == OperationalStatus.Up + select nic.GetPhysicalAddress().ToString()).FirstOrDefault(); + + var sd = new ServiceDiscovery(); + var service = new ServiceProfile("x", "_foo._tcp", 1024); + service.AddProperty("mac", macAddr); + sd.Advertise(service); + } +} +/* +var sd = new ServiceDiscovery(); +var service = new ServiceProfile("x", "_foo._tcp", 1024); +service.AddProperty("mac", "bar"); +sd.Advertise(service); + + +/* +var service = "..."; +var mdns = new MulticastService(); +mdns.QueryReceived += (s, e) => +{ + var msg = e.Message; + if (msg.Questions.Any(q => q.Name == service)) + { + var res = msg.CreateResponse(); + var addresses = MulticastService.GetIPAddresses() + .Where(ip => ip.AddressFamily == AddressFamily.InterNetwork); + foreach (var address in addresses) + { + res.Answers.Add(new ARecord + { + Name = service, + Address = address + }); + } + mdns.SendAnswer(res); + } +}; +mdns.Start(); + +*/ \ No newline at end of file