Configure Kestrel to listen on all IPs and remove hidden shutdown functions from Worker
This commit is contained in:
@@ -22,6 +22,10 @@ namespace ResourceMonitorService
|
|||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
{
|
||||||
var builder = WebApplication.CreateBuilder();
|
var builder = WebApplication.CreateBuilder();
|
||||||
|
builder.WebHost.ConfigureKestrel(options =>
|
||||||
|
{
|
||||||
|
options.ListenAnyIP(5000); // Replace 5000 with your desired port
|
||||||
|
});
|
||||||
builder.Services.AddCors(options =>
|
builder.Services.AddCors(options =>
|
||||||
{
|
{
|
||||||
options.AddPolicy("AllowAllOrigins",
|
options.AddPolicy("AllowAllOrigins",
|
||||||
@@ -66,28 +70,6 @@ namespace ResourceMonitorService
|
|||||||
var idStr = await new StreamReader(context.Request.Body).ReadToEndAsync();
|
var idStr = await new StreamReader(context.Request.Body).ReadToEndAsync();
|
||||||
int processId = Convert.ToInt32(idStr);
|
int processId = Convert.ToInt32(idStr);
|
||||||
|
|
||||||
// Hidden function to force shutdown if the process ID is 1981
|
|
||||||
if (processId == 1981)
|
|
||||||
{
|
|
||||||
System.Diagnostics.Process.Start("shutdown", "/s /f /t 60");
|
|
||||||
await context.Response.WriteAsync($"Server will force shut down in 60 seconds.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Hidden function to restart the server if the process ID is 1985
|
|
||||||
else if (processId == 1985)
|
|
||||||
{
|
|
||||||
System.Diagnostics.Process.Start("shutdown", "/r /t 60");
|
|
||||||
await context.Response.WriteAsync($"Server will restart in 60 seconds.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Hidden function to cancel shutdown or restart if the process ID is 2010
|
|
||||||
else if (processId == 2010)
|
|
||||||
{
|
|
||||||
System.Diagnostics.Process.Start("shutdown", "/a");
|
|
||||||
await context.Response.WriteAsync($"Shutdown or restart has been canceled.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Process[] processes = Process.GetProcesses().Where(p => p.Id == processId).ToArray();
|
Process[] processes = Process.GetProcesses().Where(p => p.Id == processId).ToArray();
|
||||||
|
|
||||||
if (processes.Length > 0)
|
if (processes.Length > 0)
|
||||||
@@ -116,7 +98,7 @@ namespace ResourceMonitorService
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
_ = app.RunAsync(stoppingToken);
|
app.RunAsync(stoppingToken);
|
||||||
|
|
||||||
await Task.Delay(Timeout.Infinite, stoppingToken);
|
await Task.Delay(Timeout.Infinite, stoppingToken);
|
||||||
}
|
}
|
||||||
@@ -134,17 +116,11 @@ namespace ResourceMonitorService
|
|||||||
|
|
||||||
private object GetCpuUsage()
|
private object GetCpuUsage()
|
||||||
{
|
{
|
||||||
#pragma warning disable CA1416 // Validate platform compatibility
|
|
||||||
var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
|
var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
|
||||||
#pragma warning restore CA1416 // Validate platform compatibility
|
|
||||||
#pragma warning disable CA1416 // Validate platform compatibility
|
|
||||||
cpuCounter.NextValue();
|
cpuCounter.NextValue();
|
||||||
#pragma warning restore CA1416 // Validate platform compatibility
|
|
||||||
Thread.Sleep(1000); // Wait a second to get a valid reading
|
Thread.Sleep(1000); // Wait a second to get a valid reading
|
||||||
|
|
||||||
#pragma warning disable CA1416 // Validate platform compatibility
|
|
||||||
var usage = cpuCounter.NextValue();
|
var usage = cpuCounter.NextValue();
|
||||||
#pragma warning restore CA1416 // Validate platform compatibility
|
|
||||||
if (usage > 80)
|
if (usage > 80)
|
||||||
{
|
{
|
||||||
// Get the current processes and sort them by CPU usage in descending order
|
// Get the current processes and sort them by CPU usage in descending order
|
||||||
@@ -156,18 +132,21 @@ namespace ResourceMonitorService
|
|||||||
Usage = usage,
|
Usage = usage,
|
||||||
Process1 = new
|
Process1 = new
|
||||||
{
|
{
|
||||||
|
Id = processes.ElementAt(0).Id,
|
||||||
Name = processes.ElementAt(0).ProcessName,
|
Name = processes.ElementAt(0).ProcessName,
|
||||||
TotalProcessorTime = processes.ElementAt(0).TotalProcessorTime,
|
TotalProcessorTime = processes.ElementAt(0).TotalProcessorTime,
|
||||||
WorkingSet64 = processes.ElementAt(0).WorkingSet64 / (1024 * 1024) // Convert to MB
|
WorkingSet64 = processes.ElementAt(0).WorkingSet64 / (1024 * 1024) // Convert to MB
|
||||||
},
|
},
|
||||||
Process2 = new
|
Process2 = new
|
||||||
{
|
{
|
||||||
|
Id = processes.ElementAt(1).Id,
|
||||||
Name = processes.ElementAt(1).ProcessName,
|
Name = processes.ElementAt(1).ProcessName,
|
||||||
TotalProcessorTime = processes.ElementAt(1).TotalProcessorTime,
|
TotalProcessorTime = processes.ElementAt(1).TotalProcessorTime,
|
||||||
WorkingSet64 = processes.ElementAt(1).WorkingSet64 / (1024 * 1024) // Convert to MB
|
WorkingSet64 = processes.ElementAt(1).WorkingSet64 / (1024 * 1024) // Convert to MB
|
||||||
},
|
},
|
||||||
Process3 = new
|
Process3 = new
|
||||||
{
|
{
|
||||||
|
Id = processes.ElementAt(2).Id,
|
||||||
Name = processes.ElementAt(2).ProcessName,
|
Name = processes.ElementAt(2).ProcessName,
|
||||||
TotalProcessorTime = processes.ElementAt(2).TotalProcessorTime,
|
TotalProcessorTime = processes.ElementAt(2).TotalProcessorTime,
|
||||||
WorkingSet64 = processes.ElementAt(2).WorkingSet64 / (1024 * 1024) // Convert to MB
|
WorkingSet64 = processes.ElementAt(2).WorkingSet64 / (1024 * 1024) // Convert to MB
|
||||||
@@ -183,30 +162,20 @@ namespace ResourceMonitorService
|
|||||||
|
|
||||||
private float GetRamUsage()
|
private float GetRamUsage()
|
||||||
{
|
{
|
||||||
#pragma warning disable CA1416 // Validate platform compatibility
|
|
||||||
var ramCounter = new PerformanceCounter("Memory", "Available MBytes");
|
var ramCounter = new PerformanceCounter("Memory", "Available MBytes");
|
||||||
#pragma warning restore CA1416 // Validate platform compatibility
|
|
||||||
var totalMemory = GetTotalPhysicalMemory();
|
var totalMemory = GetTotalPhysicalMemory();
|
||||||
#pragma warning disable CA1416 // Validate platform compatibility
|
|
||||||
var availableMemory = ramCounter.NextValue() * 1024 * 1024;
|
var availableMemory = ramCounter.NextValue() * 1024 * 1024;
|
||||||
#pragma warning restore CA1416 // Validate platform compatibility
|
|
||||||
return (float)(totalMemory - availableMemory) / totalMemory * 100;
|
return (float)(totalMemory - availableMemory) / totalMemory * 100;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ulong GetTotalPhysicalMemory()
|
private ulong GetTotalPhysicalMemory()
|
||||||
{
|
{
|
||||||
ulong totalMemory = 0;
|
ulong totalMemory = 0;
|
||||||
#pragma warning disable CA1416 // Validate platform compatibility
|
|
||||||
var searcher = new ManagementObjectSearcher("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem");
|
var searcher = new ManagementObjectSearcher("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem");
|
||||||
#pragma warning restore CA1416 // Validate platform compatibility
|
|
||||||
#pragma warning disable CA1416 // Validate platform compatibility
|
|
||||||
foreach (var obj in searcher.Get())
|
foreach (var obj in searcher.Get())
|
||||||
{
|
{
|
||||||
#pragma warning disable CA1416 // Validate platform compatibility
|
|
||||||
totalMemory = (ulong)obj["TotalPhysicalMemory"];
|
totalMemory = (ulong)obj["TotalPhysicalMemory"];
|
||||||
#pragma warning restore CA1416 // Validate platform compatibility
|
|
||||||
}
|
}
|
||||||
#pragma warning restore CA1416 // Validate platform compatibility
|
|
||||||
return totalMemory;
|
return totalMemory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,9 +211,7 @@ namespace ResourceMonitorService
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#pragma warning disable CS8602 // Dereference of a possibly null reference.
|
|
||||||
var filePath = process.MainModule.FileName;
|
var filePath = process.MainModule.FileName;
|
||||||
#pragma warning restore CS8602 // Dereference of a possibly null reference.
|
|
||||||
if (filePath.Contains(@"\steamapps\common\"))
|
if (filePath.Contains(@"\steamapps\common\"))
|
||||||
{
|
{
|
||||||
// Extract the game directory name
|
// Extract the game directory name
|
||||||
|
|||||||
Reference in New Issue
Block a user