rename namespace and move some classes

This commit is contained in:
ChsBuffer
2020-10-02 16:18:20 +08:00
parent 36dc4a07f1
commit 4f3b0e5afb
69 changed files with 5472 additions and 260 deletions

View File

@@ -4,13 +4,10 @@ using System.Text;
namespace Netch.Controllers
{
public class DNSController : Controller
public class DNSController : IController
{
public DNSController()
{
Name = "DNS Service";
RedirectStd = false;
}
public string Name { get; } = "DNS Service";
/// <summary>
/// 启动DNS服务
@@ -27,7 +24,7 @@ namespace Netch.Controllers
aiodns_init();
}
public override void Stop()
public void Stop()
{
aiodns_free();
}

View File

@@ -12,12 +12,18 @@ using Timer = System.Timers.Timer;
namespace Netch.Controllers
{
abstract partial class Controller
public abstract class Guard
{
public abstract string Name { get; protected set; }
/// <summary>
/// 主程序名
/// </summary>
public string MainFile { get; protected set; }
public abstract string MainFile { get; protected set; }
protected State State { get; set; } = State.Waiting;
public abstract void Stop();
/// <summary>
/// 成功启动关键词

View File

@@ -4,29 +4,23 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using Netch.Models;
using Netch.ServerEx.Socks5;
using Netch.Servers.Socks5;
using Netch.Utils;
namespace Netch.Controllers
{
public class HTTPController : ModeController
public class HTTPController : IModeController
{
public override bool TestNatRequired { get; } = false;
public bool TestNatRequired { get; } = false;
public const string IEProxyExceptions = "localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;192.168.*";
/// <summary>
/// 实例
/// </summary>
public PrivoxyController pPrivoxyController = new PrivoxyController();
private string prevBypass, prevHTTP, prevPAC;
private bool prevEnabled;
public HTTPController()
{
Name = "HTTP";
}
public string Name { get; } = "HTTP";
/// <summary>
/// 启动
@@ -34,7 +28,7 @@ namespace Netch.Controllers
/// <param name="s">服务器</param>
/// <param name="mode">模式</param>
/// <returns>是否启动成功</returns>
public override bool Start(Server s, Mode mode)
public bool Start(Server s, Mode mode)
{
RecordPrevious();
@@ -97,7 +91,7 @@ namespace Netch.Controllers
/// <summary>
/// 停止
/// </summary>
public override void Stop()
public void Stop()
{
var tasks = new[]
{

View File

@@ -1,184 +0,0 @@
using System;
using System.Management;
using Netch.Utils;
using NETCONLib;
using WinFW;
namespace Netch.Controllers
{
public class ICSController
{
public static bool Enabled
{
get
{
TUNTAPController.SearchTapAdapter();
foreach (NetworkConnection connection in new NetworkConnectionCollection())
{
if (connection.DeviceName == Global.TUNTAP.Adapter.Description)
{
return connection.SharingEnabled;
}
}
return false;
}
}
public static bool Enable()
{
Utils.Utils.SearchOutboundAdapter();
TUNTAPController.SearchTapAdapter();
if (Global.TUNTAP.Adapter == null || Global.Outbound.Adapter == null)
{
return false;
}
try
{
CleanupWMISharingEntries();
#region Save Outbound IP Config
var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
var moc = wmi.GetInstances();
var dhcpEnabled = true;
string[] ipAddress = null;
string[] subnetMask = null;
string[] gateway = null;
ushort[] gatewayMetric = null;
string[] dns = null;
var outboundWmi = GetManagementObjectByDeviceNameOrDefault(Global.Outbound.Adapter.Description);
if (outboundWmi == null)
{
return false;
}
if (!(dhcpEnabled = (bool) outboundWmi["DHCPEnabled"]))
{
ipAddress = (string[]) outboundWmi["IPAddress"];
subnetMask = (string[]) outboundWmi["IPSubnet"];
gateway = (string[]) outboundWmi["DefaultIPGateway"];
gatewayMetric = (ushort[]) outboundWmi["GatewayCostMetric"];
dns = (string[]) outboundWmi["DNSServerSearchOrder"];
ipAddress = new[] {ipAddress[0]};
subnetMask = new[] {subnetMask[0]};
}
#endregion
#region Setting ICS
foreach (NetworkConnection connection in new NetworkConnectionCollection())
{
if (connection.DeviceName == Global.TUNTAP.Adapter.Description)
{
if (connection.SharingEnabled)
connection.DisableSharing();
connection.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PUBLIC);
}
else if (connection.DeviceName == Global.Outbound.Adapter.Description)
{
if (connection.SharingEnabled)
connection.DisableSharing();
connection.EnableSharing(tagSHARINGCONNECTIONTYPE.ICSSHARINGTYPE_PRIVATE);
}
}
#endregion
#region Reset Outbound IP Config
if (dhcpEnabled)
{
outboundWmi.InvokeMethod("EnableDHCP", null, null);
}
else
{
//Set static IP and subnet mask
var newIP = outboundWmi.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = ipAddress;
newIP["SubnetMask"] = subnetMask;
outboundWmi.InvokeMethod("EnableStatic", newIP, null);
//Set default gateway
var newGateway = outboundWmi.GetMethodParameters("SetGateways");
newGateway["DefaultIPGateway"] = gateway;
newGateway["GatewayCostMetric"] = gatewayMetric;
outboundWmi.InvokeMethod("SetGateways", newGateway, null);
//Set dns servers
var newDNS = outboundWmi.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = dns;
outboundWmi.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
}
#endregion
return true;
}
catch (Exception e)
{
try
{
Disable();
}
catch
{
// ignored
}
Logging.Error($"网络连接共享设置失败: {e}");
return false;
}
}
public static void Disable()
{
foreach (NetworkConnection connection in new NetworkConnectionCollection())
{
if (connection.SharingEnabled)
connection.DisableSharing();
}
CleanupWMISharingEntries();
}
private static void CleanupWMISharingEntries()
{
var scope = new ManagementScope("root\\Microsoft\\HomeNet");
scope.Connect();
var options = new PutOptions();
options.Type = PutType.UpdateOnly;
var query = new ObjectQuery("SELECT * FROM HNet_ConnectionProperties");
var srchr = new ManagementObjectSearcher(scope, query);
foreach (ManagementObject entry in srchr.Get())
{
if ((bool) entry["IsIcsPrivate"])
entry["IsIcsPrivate"] = false;
if ((bool) entry["IsIcsPublic"])
entry["IsIcsPublic"] = false;
entry.Put(options);
}
}
public static ManagementObject GetManagementObjectByDeviceNameOrDefault(string deviceName)
{
foreach (ManagementObject mo in new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances())
{
if (((string) mo["Caption"]).EndsWith(deviceName))
{
return mo;
}
}
return null;
}
}
}

View File

@@ -0,0 +1,15 @@
namespace Netch.Controllers
{
public interface IController
{
/// <summary>
/// 控制器名
/// </summary>
public string Name { get; }
/// <summary>
/// 停止
/// </summary>
public abstract void Stop();
}
}

View File

@@ -2,7 +2,7 @@ using Netch.Models;
namespace Netch.Controllers
{
public abstract class ModeController : Controller
public interface IModeController : IController
{
/// <summary>
/// 启动

View File

@@ -0,0 +1,19 @@
using Netch.Models;
namespace Netch.Controllers
{
public interface IServerController : IController
{
public int? Socks5LocalPort { get; set; }
public string LocalAddress { get; set; }
/// <summary>
/// 启动
/// </summary>
/// <param name="server">服务器</param>
/// <param name="mode">模式</param>
/// <returns>是否启动成功</returns>
public abstract bool Start(Server server, Mode mode);
}
}

View File

@@ -1,22 +0,0 @@
using Netch.Models;
namespace Netch.Controllers
{
public abstract partial class Controller
{
/// <summary>
/// 控制器名
/// </summary>
public string Name { get; protected set; }
/// <summary>
/// 当前状态
/// </summary>
public State State { get; protected set; } = State.Waiting;
/// <summary>
/// 停止
/// </summary>
public abstract void Stop();
}
}

View File

@@ -10,8 +10,8 @@ namespace Netch.Controllers
{
public static class MainController
{
public static ServerController ServerController { get; private set; }
public static ModeController ModeController { get; private set; }
public static IServerController ServerController { get; private set; }
public static IModeController ModeController { get; private set; }
public static bool NttTested;
@@ -95,9 +95,13 @@ namespace Netch.Controllers
return true;
}
ServerController = Servers.GetUtilByTypeName(server.Type).GetController();
ServerController = ServerHelper.GetUtilByTypeName(server.Type).GetController();
if (ServerController is Guard instanceController)
{
Utils.Utils.KillProcessByName(instanceController.MainFile);
}
Utils.Utils.KillProcessByName(ServerController.MainFile);
PortCheckAndShowMessageBox(Global.Settings.Socks5LocalPort, "Socks5");
Global.MainForm.StatusText(i18N.Translate("Starting ", ServerController.Name));

View File

@@ -9,9 +9,9 @@ using nfapinet;
namespace Netch.Controllers
{
public class NFController : ModeController
public class NFController : IModeController
{
public override bool TestNatRequired { get; } = true;
public bool TestNatRequired { get; } = true;
private static readonly ServiceController NFService = new ServiceController("netfilter2");
@@ -19,42 +19,40 @@ namespace Netch.Controllers
private static readonly string SystemDriver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys";
private static string _sysDns;
public string Name { get; } = "Redirector";
static NFController()
{
string fileName;
switch ($"{Environment.OSVersion.Version.Major}.{Environment.OSVersion.Version.Minor}")
{
case "10.0":
BinDriver = "Win-10.sys";
fileName = "Win-10.sys";
break;
case "6.3":
case "6.2":
BinDriver = "Win-8.sys";
fileName = "Win-8.sys";
break;
case "6.1":
case "6.0":
BinDriver = "Win-7.sys";
fileName = "Win-7.sys";
break;
default:
Logging.Error($"不支持的系统版本:{Environment.OSVersion.Version}");
return;
}
BinDriver = "bin\\" + BinDriver;
BinDriver = "bin\\" + fileName;
}
public NFController()
public bool Start(Server s, Mode mode)
{
Name = "Redirector";
}
public override bool Start(Server s, Mode mode)
{
Logging.Info("内置驱动版本: " + Utils.Utils.FileVersion(BinDriver));
if (Utils.Utils.FileVersion(SystemDriver) != Utils.Utils.FileVersion(BinDriver))
Logging.Info("内置驱动版本: " + Utils.Utils.GetFileVersion(BinDriver));
if (Utils.Utils.GetFileVersion(SystemDriver) != Utils.Utils.GetFileVersion(BinDriver))
{
if (File.Exists(SystemDriver))
{
Logging.Info("系统驱动版本: " + Utils.Utils.FileVersion(SystemDriver));
Logging.Info("系统驱动版本: " + Utils.Utils.GetFileVersion(SystemDriver));
Logging.Info("更新驱动");
UninstallDriver();
}
@@ -99,7 +97,7 @@ namespace Netch.Controllers
return aio_init();
}
public override void Stop()
public void Stop()
{
Task.Run(() =>
{

View File

@@ -5,18 +5,14 @@ using Netch.Utils;
namespace Netch.Controllers
{
public class NTTController : Controller
public class NTTController : Guard, IController
{
private string _localEnd;
private string _publicEnd;
private string _result;
private string _bindingTest;
public NTTController()
{
Name = "NTT";
MainFile = "NTT.exe";
}
public override string Name { get; protected set; } = "NTT";
public override string MainFile { get; protected set; } = "NTT.exe";
/// <summary>
/// 启动 NatTypeTester
@@ -55,6 +51,7 @@ namespace Netch.Controllers
}
}
private new void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrEmpty(e.Data)) return;

View File

@@ -3,15 +3,17 @@ using Netch.Models;
namespace Netch.Controllers
{
public class PrivoxyController : Controller
public class PrivoxyController : Guard, IController
{
public PrivoxyController()
{
Name = "Privoxy";
MainFile = "Privoxy.exe";
RedirectStd = false;
}
public override string Name { get; protected set; } = "Privoxy";
public override string MainFile { get; protected set; } = "Privoxy.exe";
public bool Start(Server server, Mode mode)
{
var text = File.ReadAllText("bin\\default.conf")

View File

@@ -14,9 +14,9 @@ using Netch.Utils;
namespace Netch.Controllers
{
public class TUNTAPController : ModeController
public class TUNTAPController : Guard, IModeController
{
public override bool TestNatRequired { get; } = true;
public bool TestNatRequired { get; } = true;
// ByPassLan IP
private readonly List<string> _bypassLanIPs = new List<string>
@@ -37,12 +37,13 @@ namespace Netch.Controllers
public TUNTAPController()
{
Name = "tun2socks";
MainFile = "tun2socks.exe";
StartedKeywords.Add("Running");
StoppedKeywords.AddRange(new[] {"failed", "invalid vconfig file"});
}
public override string Name { get; protected set; } = "tun2socks";
public override string MainFile { get; protected set; } = "tun2socks.exe";
/// <summary>
/// 配置 TUNTAP 适配器
/// </summary>
@@ -64,6 +65,7 @@ namespace Netch.Controllers
private readonly List<IPNetwork> _directIPs = new List<IPNetwork>();
private readonly List<IPNetwork> _proxyIPs = new List<IPNetwork>();
/// <summary>
/// 设置绕行规则
/// </summary>
@@ -200,7 +202,8 @@ namespace Netch.Controllers
return true;
}
public override bool Start(Server s, Mode mode)
public bool Start(Server s, Mode mode)
{
_savedMode = mode;
_savedServer = s;
@@ -265,15 +268,17 @@ namespace Netch.Controllers
var helpStr = new StringBuilder();
try
{
void OnOutputDataReceived(object sender,DataReceivedEventArgs e)
void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null)
{
exited = true;
return;
}
helpStr.Append(e.Data);
}
InitInstance("-h");
// Instance.OutputDataReceived += OnOutputDataReceived;
Instance.ErrorDataReceived += OnOutputDataReceived;