mirror of
https://github.com/snltty/linker.git
synced 2025-10-08 18:40:07 +08:00
重写网卡
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.Diagnostics;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
|
||||||
namespace linker.libs
|
namespace linker.libs
|
||||||
@@ -11,6 +12,7 @@ namespace linker.libs
|
|||||||
}
|
}
|
||||||
public static string PowerShell(string arg, string[] commands, bool readResult = true)
|
public static string PowerShell(string arg, string[] commands, bool readResult = true)
|
||||||
{
|
{
|
||||||
|
if (IsPowerShellInstalled() == false) return string.Empty;
|
||||||
return Execute("powershell.exe", arg, commands, readResult);
|
return Execute("powershell.exe", arg, commands, readResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,5 +81,24 @@ namespace linker.libs
|
|||||||
proc.Dispose();
|
proc.Dispose();
|
||||||
return string.Empty;
|
return string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsPowerShellInstalled()
|
||||||
|
{
|
||||||
|
string[] powerShellPaths = {
|
||||||
|
Environment.ExpandEnvironmentVariables(@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"),
|
||||||
|
Environment.ExpandEnvironmentVariables(@"%SystemRoot%\SysWOW64\WindowsPowerShell\v1.0\powershell.exe")
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (string path in powerShellPaths)
|
||||||
|
{
|
||||||
|
if (File.Exists(path))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -120,9 +120,15 @@ namespace linker.tun
|
|||||||
error = $"shutdown are operating";
|
error = $"shutdown are operating";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
cancellationTokenSource?.Cancel();
|
cancellationTokenSource?.Cancel();
|
||||||
linkerTunDevice?.Shutdown();
|
linkerTunDevice?.Shutdown();
|
||||||
linkerTunDevice?.RemoveNat(out error);
|
linkerTunDevice?.RemoveNat(out error);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
error = string.Empty;
|
error = string.Empty;
|
||||||
Interlocked.Exchange(ref operating, 0);
|
Interlocked.Exchange(ref operating, 0);
|
||||||
|
@@ -126,8 +126,15 @@ namespace linker.tun
|
|||||||
public void RemoveNat(out string error)
|
public void RemoveNat(out string error)
|
||||||
{
|
{
|
||||||
error = string.Empty;
|
error = string.Empty;
|
||||||
|
try
|
||||||
|
{
|
||||||
CommandHelper.PowerShell(string.Empty, new string[] { $"Remove-NetNat -Name {Name}" });
|
CommandHelper.PowerShell(string.Empty, new string[] { $"Remove-NetNat -Name {Name}" });
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
error = ex.Message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void AddRoute(LinkerTunDeviceRouteItem[] ips, IPAddress ip, bool gateway)
|
public void AddRoute(LinkerTunDeviceRouteItem[] ips, IPAddress ip, bool gateway)
|
||||||
{
|
{
|
||||||
|
@@ -44,13 +44,11 @@ namespace linker.plugins.tuntap
|
|||||||
this.config = config;
|
this.config = config;
|
||||||
this.tuntapProxy = tuntapProxy;
|
this.tuntapProxy = tuntapProxy;
|
||||||
this.runningConfig = runningConfig;
|
this.runningConfig = runningConfig;
|
||||||
|
|
||||||
linkerTunDeviceAdapter.Initialize(deviceName, tuntapProxy);
|
linkerTunDeviceAdapter.Initialize(deviceName, tuntapProxy);
|
||||||
linkerTunDeviceAdapter.Shutdown();
|
linkerTunDeviceAdapter.Shutdown();
|
||||||
linkerTunDeviceAdapter.Clear();
|
linkerTunDeviceAdapter.Clear();
|
||||||
AppDomain.CurrentDomain.ProcessExit += (s, e) => linkerTunDeviceAdapter.Shutdown();
|
AppDomain.CurrentDomain.ProcessExit += (s, e) => linkerTunDeviceAdapter.Shutdown();
|
||||||
Console.CancelKeyPress += (s, e) => linkerTunDeviceAdapter.Shutdown();
|
Console.CancelKeyPress += (s, e) => linkerTunDeviceAdapter.Shutdown();
|
||||||
|
|
||||||
clientSignInState.NetworkFirstEnabledHandle += Initialize;
|
clientSignInState.NetworkFirstEnabledHandle += Initialize;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -89,11 +89,17 @@ namespace linker.startup
|
|||||||
foreach (var startup in startups)
|
foreach (var startup in startups)
|
||||||
{
|
{
|
||||||
if (config.Data.Common.Modes.Contains("client"))
|
if (config.Data.Common.Modes.Contains("client"))
|
||||||
|
{
|
||||||
|
LoggerHelper.Instance.Warning($"add startup {startup.Name} client");
|
||||||
startup.AddClient(serviceCollection, config, assemblies);
|
startup.AddClient(serviceCollection, config, assemblies);
|
||||||
|
}
|
||||||
if (config.Data.Common.Modes.Contains("server"))
|
if (config.Data.Common.Modes.Contains("server"))
|
||||||
|
{
|
||||||
|
LoggerHelper.Instance.Warning($"add startup {startup.Name} server");
|
||||||
startup.AddServer(serviceCollection, config, assemblies);
|
startup.AddServer(serviceCollection, config, assemblies);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动
|
/// 启动
|
||||||
@@ -106,10 +112,16 @@ namespace linker.startup
|
|||||||
foreach (var startup in startups)
|
foreach (var startup in startups)
|
||||||
{
|
{
|
||||||
if (config.Data.Common.Modes.Contains("client"))
|
if (config.Data.Common.Modes.Contains("client"))
|
||||||
|
{
|
||||||
|
LoggerHelper.Instance.Warning($"use startup {startup.Name} client");
|
||||||
startup.UseClient(serviceProvider, config, assemblies);
|
startup.UseClient(serviceProvider, config, assemblies);
|
||||||
|
}
|
||||||
if (config.Data.Common.Modes.Contains("server"))
|
if (config.Data.Common.Modes.Contains("server"))
|
||||||
|
{
|
||||||
|
LoggerHelper.Instance.Warning($"use startup {startup.Name} server");
|
||||||
startup.UseServer(serviceProvider, config, assemblies);
|
startup.UseServer(serviceProvider, config, assemblies);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user