ethernet-switcher/EthernetSwitcher/NetworkManager.cs

196 lines
6.9 KiB
C#

using System;
using System.Diagnostics;
using System.Linq;
namespace EthernetSwitcher
{
public static class NetworkManager
{
public static bool InterfaceExists(string interfaceName)
{
try
{
var escapedName = EscapePowerShellString(interfaceName);
var output = ExecutePowerShell($"Get-NetAdapter -Name '{escapedName}' -ErrorAction SilentlyContinue");
return !string.IsNullOrEmpty(output) && !output.Contains("Get-NetAdapter") && output.Contains("Name");
}
catch (Exception ex)
{
return false;
}
}
public static void EnableInterface(string interfaceName)
{
try
{
var status = GetInterfaceStatus(interfaceName);
if (!status)
{
var escapedName = EscapePowerShellString(interfaceName);
ExecutePowerShell($"Enable-NetAdapter -Name '{escapedName}' -Confirm:$false");
Console.WriteLine($"Interface enabled: {interfaceName}");
}
else
{
Console.WriteLine($"Interface already enabled: {interfaceName}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error enabling interface {interfaceName}: {ex.Message}");
}
}
public static void SetIPv4Status(string interfaceName, bool status)
{
try
{
var isCurrentlyEnabled = IsIPv4Enabled(interfaceName);
if (isCurrentlyEnabled == status)
{
return;
}
var escapedName = EscapePowerShellString(interfaceName);
var command = status
? $"Enable-NetAdapterBinding -Name '{escapedName}' -ComponentID ms_tcpip -Confirm:$false"
: $"Disable-NetAdapterBinding -Name '{escapedName}' -ComponentID ms_tcpip -Confirm:$false";
ExecutePowerShell(command);
Console.WriteLine($"IPv4 {(status ? "enabled" : "disabled")} for interface: {interfaceName}");
}
catch (Exception ex)
{
// Error handling without logging
}
}
public static void SetIPv6Status(string interfaceName, bool status)
{
try
{
var isCurrentlyEnabled = IsIPv6Enabled(interfaceName);
if (isCurrentlyEnabled == status)
{
return;
}
var escapedName = EscapePowerShellString(interfaceName);
var command = status
? $"Enable-NetAdapterBinding -Name '{escapedName}' -ComponentID ms_tcpip6 -Confirm:$false"
: $"Disable-NetAdapterBinding -Name '{escapedName}' -ComponentID ms_tcpip6 -Confirm:$false";
ExecutePowerShell(command);
Console.WriteLine($"IPv6 {(status ? "enabled" : "disabled")} for interface: {interfaceName}");
}
catch (Exception ex)
{
// Error handling without logging
}
}
public static bool IsIPv4Enabled(string interfaceName)
{
try
{
var escapedName = EscapePowerShellString(interfaceName);
var command = $"Get-NetAdapterBinding -Name '{escapedName}' -ComponentID ms_tcpip -ErrorAction SilentlyContinue";
var output = ExecutePowerShell(command);
var lines = output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (line.Contains("ms_tcpip"))
{
return line.Contains("True");
}
}
return false;
}
catch (Exception ex)
{
return false;
}
}
public static bool IsIPv6Enabled(string interfaceName)
{
try
{
var escapedName = EscapePowerShellString(interfaceName);
var command = $"Get-NetAdapterBinding -Name '{escapedName}' -ComponentID ms_tcpip6 -ErrorAction SilentlyContinue";
var output = ExecutePowerShell(command);
var lines = output.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (line.Contains("ms_tcpip6"))
{
return line.Contains("True");
}
}
return false;
}
catch (Exception ex)
{
return false;
}
}
public static bool GetInterfaceStatus(string interfaceName)
{
try
{
var escapedName = EscapePowerShellString(interfaceName);
var output = ExecutePowerShell($"Get-NetAdapter -Name '{escapedName}' | Select-Object -ExpandProperty Status -ErrorAction SilentlyContinue");
return output.Trim() == "Up";
}
catch (Exception ex)
{
return false;
}
}
public static string EscapePowerShellString(string input)
{
return input.Replace("'", "''");
}
private static string ExecutePowerShell(string command)
{
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"& {{{command}}}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
Verb = "runas"
}
};
process.Start();
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0 && !string.IsNullOrEmpty(error))
{
throw new Exception($"PowerShell command failed: {error}");
}
return output;
}
catch (Exception ex)
{
throw new Exception($"Error executing PowerShell command '{command}': {ex.Message}");
}
}
}
}