mirror of
https://github.com/netchx/netch.git
synced 2026-03-20 18:19:44 +08:00
使用进程模式时网卡DNS将自动替换为1.1.1.1和8.8.8.8,关闭后自动恢复
更新驱动文件1.5.9.3
This commit is contained in:
@@ -15,6 +15,7 @@ namespace Netch.Controllers
|
||||
|
||||
private static readonly string BinDriver = string.Empty;
|
||||
private static readonly string SystemDriver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys";
|
||||
private static string[] _sysDns = { };
|
||||
|
||||
static NFController()
|
||||
{
|
||||
@@ -102,7 +103,15 @@ namespace Netch.Controllers
|
||||
{
|
||||
Thread.Sleep(250);
|
||||
|
||||
if (State == State.Started) return true;
|
||||
if (State == State.Started)
|
||||
{
|
||||
|
||||
//备份并替换系统DNS
|
||||
_sysDns = DNS.getSystemDns();
|
||||
string[] dns = { "1.1.1.1", "8.8.8.8" };
|
||||
DNS.SetDNS(dns);
|
||||
|
||||
return true; }
|
||||
}
|
||||
|
||||
Logging.Error(Name + "启动超时");
|
||||
@@ -257,6 +266,8 @@ namespace Netch.Controllers
|
||||
public override void Stop()
|
||||
{
|
||||
StopInstance();
|
||||
//恢复系统DNS
|
||||
DNS.SetDNS(_sysDns);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -70,6 +70,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Management" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.ServiceProcess" />
|
||||
<Reference Include="System.Web" />
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Management;
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace Netch.Utils
|
||||
{
|
||||
@@ -45,5 +48,51 @@ namespace Netch.Utils
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置DNS
|
||||
/// </summary>
|
||||
/// <param name="dns"></param>
|
||||
public static void SetDNS(string[] dns)
|
||||
{
|
||||
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
||||
ManagementObjectCollection moc = wmi.GetInstances();
|
||||
ManagementBaseObject inPar = null;
|
||||
ManagementBaseObject outPar = null;
|
||||
foreach (ManagementObject mo in moc)
|
||||
{
|
||||
//如果没有启用IP设置的网络设备则跳过,如果是虚拟机网卡也跳过
|
||||
if (!(bool)mo["IPEnabled"] ||
|
||||
mo["Description"].ToString().Contains("Virtual") ||
|
||||
mo["Description"].ToString().Contains("VMware") ||
|
||||
mo["Description"].ToString().Contains("TAP"))
|
||||
continue;
|
||||
|
||||
//设置DNS地址
|
||||
if (dns != null)
|
||||
{
|
||||
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
|
||||
inPar["DNSServerSearchOrder"] = dns;
|
||||
outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 从网卡获取ip设置信息
|
||||
/// </summary>
|
||||
public static string[] getSystemDns()
|
||||
{
|
||||
var systemDns = new[] { "223.5.5.5", "1.1.1.1" };
|
||||
foreach (var network in NetworkInterface.GetAllNetworkInterfaces())
|
||||
if (!network.Description.Contains("Virtual") &&
|
||||
!network.Description.Contains("VMware") &&
|
||||
!network.Description.Contains("TAP") &&
|
||||
network.OperationalStatus == OperationalStatus.Up &&
|
||||
network.GetIPProperties()?.GatewayAddresses.Count != 0)
|
||||
{
|
||||
systemDns = network.GetIPProperties().DnsAddresses.Select(dns => dns.ToString()).ToArray();
|
||||
}
|
||||
|
||||
return systemDns;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
binaries
2
binaries
Submodule binaries updated: 841b75af2f...2adb17a398
Reference in New Issue
Block a user