Adding first version of the api rewrite.
Removing many dependencies.
This commit is contained in:
13
Minimal-server/GotobedServer/GotobedServer.csproj
Normal file
13
Minimal-server/GotobedServer/GotobedServer.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Makaretu.Dns.Multicast" Version="0.27.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
20
Minimal-server/GotobedServer/Program.cs
Normal file
20
Minimal-server/GotobedServer/Program.cs
Normal file
@@ -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();
|
||||
28
Minimal-server/GotobedServer/Properties/launchSettings.json
Normal file
28
Minimal-server/GotobedServer/Properties/launchSettings.json
Normal file
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Minimal-server/GotobedServer/appsettings.json
Normal file
9
Minimal-server/GotobedServer/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
235
Minimal-server/GotobedServer/handler.cs
Normal file
235
Minimal-server/GotobedServer/handler.cs
Normal file
@@ -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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
51
Minimal-server/GotobedServer/mdnsService.cs
Normal file
51
Minimal-server/GotobedServer/mdnsService.cs
Normal file
@@ -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();
|
||||
|
||||
*/
|
||||
Reference in New Issue
Block a user