feat: initial development
This commit is contained in:
50
NaiveHttpServer/NetAclChecker.cs
Normal file
50
NaiveHttpServer/NetAclChecker.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace NaiveHttpServer
|
||||
{
|
||||
public static class NetAclChecker
|
||||
{
|
||||
public static ILogger? Logger { get; set; }
|
||||
|
||||
public static void AddAddress(string address)
|
||||
{
|
||||
AddAddress(address, Environment.UserDomainName, Environment.UserName);
|
||||
}
|
||||
|
||||
public static void AddAddress(string address, string domain, string user)
|
||||
{
|
||||
string args = $@"http add urlacl url={address}, user={domain}\{user}";
|
||||
|
||||
try
|
||||
{
|
||||
ProcessStartInfo processStartInfo = new("netsh", args)
|
||||
{
|
||||
Verb = "runas",
|
||||
CreateNoWindow = true,
|
||||
WindowStyle = ProcessWindowStyle.Hidden,
|
||||
UseShellExecute = true,
|
||||
};
|
||||
|
||||
var process = Process.Start(processStartInfo);
|
||||
process?.WaitForExit();
|
||||
}
|
||||
catch (Win32Exception e)
|
||||
{
|
||||
if (e.NativeErrorCode == 1223)
|
||||
{
|
||||
Logger?.Info("User canceled the operation by rejected the UAC.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger?.Warning($"Failed to 'netsh http add urlacl {address}' with an {nameof(Win32Exception)}.", e);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger?.Warning($"Failed to 'netsh http add urlacl {address}'.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user