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

@@ -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;

View File

@@ -54,7 +54,7 @@ namespace Netch.Forms
var result = s.Substring(start, end - start);
Hide();
Servers.GetUtilByFullName(result).Create();
ServerHelper.GetUtilByFullName(result).Create();
InitServer();
Configuration.Save();
@@ -77,7 +77,7 @@ namespace Netch.Forms
Enabled = false;
try
{
Modes.Load();
ModeHelper.Load();
InitMode();
NotifyTip(i18N.Translate("Modes have been reload"));
}

View File

@@ -157,7 +157,7 @@ namespace Netch.Forms
private void AddAddServerToolStripMenuItems()
{
foreach (var serversUtil in Servers.ServerUtils.Where(i => !string.IsNullOrEmpty(i.FullName)))
foreach (var serversUtil in ServerHelper.ServerUtils.Where(i => !string.IsNullOrEmpty(i.FullName)))
{
var fullName = serversUtil.FullName;
var control = new ToolStripMenuItem

View File

@@ -45,7 +45,7 @@ namespace Netch.Forms
// 计算 ComboBox绘制 目标宽度
_eWidth = ServerComboBox.Width / 10;
Modes.Load();
ModeHelper.Load();
InitMode();
InitServer();
_comboBoxInitialized = true;
@@ -314,7 +314,7 @@ namespace Netch.Forms
Hide();
var server = Global.Settings.Server[ServerComboBox.SelectedIndex];
Servers.GetUtilByTypeName(server.Type).Edit(server);
ServerHelper.GetUtilByTypeName(server.Type).Edit(server);
InitServer();
Configuration.Save();
Show();
@@ -372,7 +372,7 @@ namespace Netch.Forms
return;
}
Modes.Delete((Models.Mode) ModeComboBox.SelectedItem);
ModeHelper.Delete((Models.Mode) ModeComboBox.SelectedItem);
SelectLastMode();
}

View File

@@ -207,14 +207,14 @@ namespace Netch.Forms.Mode
_mode.Rule.Clear();
_mode.Rule.AddRange(RuleListBox.Items.Cast<string>());
Modes.WriteFile(_mode);
ModeHelper.WriteFile(_mode);
Global.MainForm.InitMode();
Edited = false;
MessageBoxX.Show(i18N.Translate("Mode updated successfully"));
}
else
{
var fullName = Modes.GetFullPath(FilenameTextBox.Text + ".txt");
var fullName = ModeHelper.GetFullPath(FilenameTextBox.Text + ".txt");
if (File.Exists(fullName))
{
MessageBoxX.Show(i18N.Translate("File already exists.\n Please Change the filename"));
@@ -230,8 +230,8 @@ namespace Netch.Forms.Mode
};
mode.Rule.AddRange(RuleListBox.Items.Cast<string>());
Modes.WriteFile(mode);
Modes.Add(mode);
ModeHelper.WriteFile(mode);
ModeHelper.Add(mode);
MessageBoxX.Show(i18N.Translate("Mode added successfully"));
}

158
Netch/Forms/ServerForm.Designer.cs generated Normal file
View File

@@ -0,0 +1,158 @@
namespace Netch.Forms
{
partial class ServerForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServerForm));
this.ConfigurationGroupBox = new System.Windows.Forms.GroupBox();
this.AddressLabel = new System.Windows.Forms.Label();
this.PortTextBox = new System.Windows.Forms.TextBox();
this.AddressTextBox = new System.Windows.Forms.TextBox();
this.RemarkTextBox = new System.Windows.Forms.TextBox();
this.RemarkLabel = new System.Windows.Forms.Label();
this.PortLabel = new System.Windows.Forms.Label();
this.ControlButton = new System.Windows.Forms.Button();
this.ConfigurationGroupBox.SuspendLayout();
this.SuspendLayout();
//
// ConfigurationGroupBox
//
this.ConfigurationGroupBox.AutoSize = true;
this.ConfigurationGroupBox.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ConfigurationGroupBox.Controls.Add(this.AddressLabel);
this.ConfigurationGroupBox.Controls.Add(this.PortTextBox);
this.ConfigurationGroupBox.Controls.Add(this.AddressTextBox);
this.ConfigurationGroupBox.Controls.Add(this.RemarkTextBox);
this.ConfigurationGroupBox.Controls.Add(this.RemarkLabel);
this.ConfigurationGroupBox.Controls.Add(this.PortLabel);
this.ConfigurationGroupBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.ConfigurationGroupBox.Location = new System.Drawing.Point(5, 5);
this.ConfigurationGroupBox.Name = "ConfigurationGroupBox";
this.ConfigurationGroupBox.Size = new System.Drawing.Size(434, 147);
this.ConfigurationGroupBox.TabIndex = 0;
this.ConfigurationGroupBox.TabStop = false;
this.ConfigurationGroupBox.Text = "Configuration";
//
// AddressLabel
//
this.AddressLabel.AutoSize = true;
this.AddressLabel.Location = new System.Drawing.Point(10, 51);
this.AddressLabel.Name = "AddressLabel";
this.AddressLabel.Size = new System.Drawing.Size(56, 17);
this.AddressLabel.TabIndex = 2;
this.AddressLabel.Text = "Address";
//
// PortTextBox
//
this.PortTextBox.Location = new System.Drawing.Point(358, 48);
this.PortTextBox.Name = "PortTextBox";
this.PortTextBox.Size = new System.Drawing.Size(56, 23);
this.PortTextBox.TabIndex = 5;
this.PortTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// AddressTextBox
//
this.AddressTextBox.Location = new System.Drawing.Point(120, 48);
this.AddressTextBox.Name = "AddressTextBox";
this.AddressTextBox.Size = new System.Drawing.Size(232, 23);
this.AddressTextBox.TabIndex = 3;
this.AddressTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// RemarkTextBox
//
this.RemarkTextBox.Location = new System.Drawing.Point(120, 19);
this.RemarkTextBox.Name = "RemarkTextBox";
this.RemarkTextBox.Size = new System.Drawing.Size(294, 23);
this.RemarkTextBox.TabIndex = 1;
this.RemarkTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
//
// RemarkLabel
//
this.RemarkLabel.AutoSize = true;
this.RemarkLabel.Location = new System.Drawing.Point(10, 22);
this.RemarkLabel.Name = "RemarkLabel";
this.RemarkLabel.Size = new System.Drawing.Size(53, 17);
this.RemarkLabel.TabIndex = 0;
this.RemarkLabel.Text = "Remark";
//
// PortLabel
//
this.PortLabel.AutoSize = true;
this.PortLabel.Location = new System.Drawing.Point(351, 51);
this.PortLabel.Name = "PortLabel";
this.PortLabel.Size = new System.Drawing.Size(11, 17);
this.PortLabel.TabIndex = 4;
this.PortLabel.Text = ":";
//
// ControlButton
//
this.ControlButton.Location = new System.Drawing.Point(357, 157);
this.ControlButton.Name = "ControlButton";
this.ControlButton.Size = new System.Drawing.Size(75, 23);
this.ControlButton.TabIndex = 1;
this.ControlButton.Text = "Save";
this.ControlButton.UseVisualStyleBackColor = true;
this.ControlButton.Click += new System.EventHandler(this.ControlButton_Click);
//
// ServerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.ClientSize = new System.Drawing.Size(444, 192);
this.Controls.Add(this.ControlButton);
this.Controls.Add(this.ConfigurationGroupBox);
this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon) (resources.GetObject("$this.Icon")));
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.MaximizeBox = false;
this.Name = "ServerForm";
this.Padding = new System.Windows.Forms.Padding(5, 5, 5, 40);
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Socks5";
this.Load += new System.EventHandler(this.Socks5_Load);
this.ConfigurationGroupBox.ResumeLayout(false);
this.ConfigurationGroupBox.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox ConfigurationGroupBox;
private System.Windows.Forms.Button ControlButton;
private System.Windows.Forms.Label RemarkLabel;
private System.Windows.Forms.TextBox RemarkTextBox;
private System.Windows.Forms.Label PortLabel;
private System.Windows.Forms.TextBox AddressTextBox;
private System.Windows.Forms.TextBox PortTextBox;
private System.Windows.Forms.Label AddressLabel;
}
}

69
Netch/Forms/ServerForm.cs Normal file
View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Netch.Models;
using Netch.Servers.Shadowsocks;
using Netch.Utils;
namespace Netch.Forms
{
public abstract partial class ServerForm : Form
{
public Shadowsocks Server;
protected ServerForm(Server server = default)
{
InitializeComponent();
CreateTextBox(name: "EncryptMethod", remark: "Encrypt Method", save: str => { Server.EncryptMethod = str; }, parse: str => true);
}
private void CreateTextBox(string name, string remark, Action<string> save, Func<string, bool> parse = default)
{
var textBox = new TextBox
{
Location = new System.Drawing.Point(358, 48),
Name = $"{name}TextBox",
Size = new System.Drawing.Size(56, 23),
TextAlign = HorizontalAlignment.Center,
};
ParseActions.Add(textBox, parse);
SaveActions.Add(textBox,save);
ConfigurationGroupBox.Controls.AddRange(
new Control[]
{
textBox,
new Label
{
AutoSize = true,
Location = AddressLabel.Location = new System.Drawing.Point(10, 51),
Name = $"{name}Label",
Size = AddressLabel.Size = new System.Drawing.Size(56, 17),
Text = remark,
}
}
);
}
private Dictionary<Control, Action<string>> SaveActions = new Dictionary<Control, Action<string>>();
private Dictionary<Control, Func<string, bool>> ParseActions = new Dictionary<Control, Func<string, bool>>();
private void Socks5_Load(object sender, EventArgs e)
{
i18N.TranslateForm(this);
}
private void ControlButton_Click(object sender, EventArgs e)
{
if (!ushort.TryParse(PortTextBox.Text, out var port)) return;
if (Save())
MessageBoxX.Show(i18N.Translate("Saved"));
else
return;
Close();
}
protected abstract bool Save();
}
}

View File

@@ -38,7 +38,7 @@ namespace Netch.Forms
TUNTAPUseCustomDNSCheckBox_CheckedChanged(null, null);
ProxyDNSCheckBox.Checked = Global.Settings.TUNTAP.ProxyDNS;
UseFakeDNSCheckBox.Checked = Global.Settings.TUNTAP.UseFakeDNS;
ICSCheckBox.Checked = ICSController.Enabled;
ICSCheckBox.Checked = ICSHelper.Enabled;
// Behavior
ExitWhenClosedCheckBox.Checked = Global.Settings.ExitWhenClosed;
@@ -387,12 +387,12 @@ namespace Netch.Forms
{
if (ICSCheckBox.Checked)
{
if (!ICSController.Enabled)
ICSCheckBox.Checked = ICSController.Enable();
if (!ICSHelper.Enabled)
ICSCheckBox.Checked = ICSHelper.Enable();
}
else
{
ICSController.Disable();
ICSHelper.Disable();
}
});
ICSCheckBox.Enabled = true;

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Netch.Controllers;
using Newtonsoft.Json.Linq;
namespace Netch.Models
@@ -33,7 +34,7 @@ namespace Netch.Models
string GetShareLink(Server server);
public abstract ServerController GetController();
public abstract IServerController GetController();
public abstract IEnumerable<Server> ParseUri(string text);

View File

@@ -1,32 +0,0 @@
using Netch.Controllers;
namespace Netch.Models
{
public abstract class ServerController : Controller
{
private int? _socks5Port;
public int Socks5LocalPort
{
get => _socks5Port ?? Global.Settings.Socks5LocalPort;
set => _socks5Port = value;
}
private string _localAddress;
public string LocalAddress
{
get => _localAddress ?? Global.Settings.LocalAddress;
set => _localAddress = value;
}
/// <summary>
/// 启动
/// </summary>
/// <param name="server">服务器</param>
/// <param name="mode">模式</param>
/// <returns>是否启动成功</returns>
public abstract bool Start(Server server, Mode mode);
}
}

View File

@@ -1,7 +1,7 @@
using System;
using NETCONLib;
namespace WinFW
namespace Netch.Models.WinFW
{
public class NetworkConnection : INetConnection, INetConnectionProps, INetSharingConfiguration
{

View File

@@ -1,8 +1,8 @@
using System.Collections;
using System.Collections;
using System.Linq;
using NETCONLib;
using NETCONLib;
namespace WinFW
namespace Netch.Models.WinFW
{
/// <summary>
/// A collection that stores 'NetworkConnection' objects.

View File

@@ -1,4 +1,4 @@
namespace Netch.ServerEx.Shadowsocks.Form
namespace Netch.Servers.Shadowsocks.Form
{
partial class ShadowsocksForm
{

View File

@@ -2,7 +2,7 @@
using System.Windows.Forms;
using Netch.Utils;
namespace Netch.ServerEx.Shadowsocks.Form
namespace Netch.Servers.Shadowsocks.Form
{
public partial class ShadowsocksForm : System.Windows.Forms.Form
{

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace Netch.ServerEx.Shadowsocks.Models.SSD
namespace Netch.Servers.Shadowsocks.Models.SSD
{
public class Main
{

View File

@@ -1,4 +1,4 @@
namespace Netch.ServerEx.Shadowsocks.Models.SSD
namespace Netch.Servers.Shadowsocks.Models.SSD
{
public class SSDServer
{

View File

@@ -1,4 +1,4 @@
namespace Netch.ServerEx.Shadowsocks.Models
namespace Netch.Servers.Shadowsocks.Models
{
public class ShadowsocksConfig
{

View File

@@ -1,19 +1,20 @@
using System.Runtime.InteropServices;
using System.Text;
using Netch.Controllers;
using Netch.Models;
using Netch.Utils;
namespace Netch.ServerEx.Shadowsocks
namespace Netch.Servers.Shadowsocks
{
public class SSController : ServerController
public class SSController : Guard, IServerController
{
public SSController()
{
Name = "Shadowsocks";
MainFile = "Shadowsocks.exe";
}
public override string Name { get; protected set; } = "Shadowsocks";
public override string MainFile { get; protected set; } = "Shadowsocks.exe";
public override bool Start(Server s, Mode mode)
public int? Socks5LocalPort { get; set; }
public string LocalAddress { get; set; }
public bool Start(Server s, Mode mode)
{
bool DllFlag()
{
@@ -56,8 +57,8 @@ namespace Netch.ServerEx.Shadowsocks
argument.Append(
$"-s {server.Hostname} " +
$"-p {server.Port} " +
$"-b {LocalAddress} " +
$"-l {Socks5LocalPort} " +
$"-b {LocalAddress ?? Global.Settings.LocalAddress} " +
$"-l {Socks5LocalPort ?? Global.Settings.Socks5LocalPort} " +
$"-m {server.EncryptMethod} " +
$"-k \"{server.Password}\" " +
"-u ");

View File

@@ -3,14 +3,15 @@ using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Netch.Controllers;
using Netch.Models;
using Netch.ServerEx.Shadowsocks.Form;
using Netch.ServerEx.Shadowsocks.Models.SSD;
using Netch.Servers.Shadowsocks.Form;
using Netch.Servers.Shadowsocks.Models.SSD;
using Netch.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Netch.ServerEx.Shadowsocks
namespace Netch.Servers.Shadowsocks
{
public class SSUtil : IServerUtil
{
@@ -42,7 +43,7 @@ namespace Netch.ServerEx.Shadowsocks
return "ss://" + ShareLink.URLSafeBase64Encode($"{server.EncryptMethod}:{server.Password}@{server.Hostname}:{server.Port}") + "#" + HttpUtility.UrlEncode(server.Remark);
}
public ServerController GetController()
public IServerController GetController()
{
return new SSController();
}

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Netch.Models;
namespace Netch.ServerEx.Shadowsocks
namespace Netch.Servers.Shadowsocks
{
public class Shadowsocks : Server
{

View File

@@ -1,4 +1,4 @@
namespace Netch.ServerEx.ShadowsocksR.Form
namespace Netch.Servers.ShadowsocksR.Form
{
partial class ShadowsocksRForm
{

View File

@@ -2,7 +2,7 @@
using System.Windows.Forms;
using Netch.Utils;
namespace Netch.ServerEx.ShadowsocksR.Form
namespace Netch.Servers.ShadowsocksR.Form
{
public partial class ShadowsocksRForm : System.Windows.Forms.Form
{

View File

@@ -1,18 +1,19 @@
using System.Text;
using Netch.Controllers;
using Netch.Models;
namespace Netch.ServerEx.ShadowsocksR
namespace Netch.Servers.ShadowsocksR
{
public class SSRController : ServerController
public class SSRController : Guard, IServerController
{
public SSRController()
{
Name = "ShadowsocksR";
MainFile = "ShadowsocksR.exe";
}
public override string MainFile { get; protected set; } = "ShadowsocksR.exe";
public override string Name { get; protected set; } = "ShadowsocksR";
public override bool Start(Server s, Mode mode)
public int? Socks5LocalPort { get; set; }
public string LocalAddress { get; set; }
public bool Start(Server s, Mode mode)
{
var server = (ShadowsocksR) s;
@@ -32,7 +33,7 @@ namespace Netch.ServerEx.ShadowsocksR
if (!string.IsNullOrEmpty(server.OBFSParam)) argument.Append($" -g \"{server.OBFSParam}\"");
}
argument.Append($" -b {LocalAddress} -l {Socks5LocalPort} -u");
argument.Append($" -b {LocalAddress ?? Global.Settings.LocalAddress} -l {Socks5LocalPort ?? Global.Settings.Socks5LocalPort} -u");
if (mode.BypassChina) argument.Append(" --acl default.acl");
#endregion

View File

@@ -1,12 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Netch.Controllers;
using Netch.Models;
using Netch.ServerEx.ShadowsocksR.Form;
using Netch.Servers.ShadowsocksR.Form;
using Netch.Utils;
using Newtonsoft.Json.Linq;
namespace Netch.ServerEx.ShadowsocksR
namespace Netch.Servers.ShadowsocksR
{
public class SSRUtil : IServerUtil
{
@@ -42,7 +43,7 @@ namespace Netch.ServerEx.ShadowsocksR
return "ssr://" + ShareLink.URLSafeBase64Encode($"{server.Hostname}:{server.Port}:{server.Protocol}:{server.EncryptMethod}:{server.OBFS}:{ShareLink.URLSafeBase64Encode(server.Password)}{paraStr}");
}
public ServerController GetController()
public IServerController GetController()
{
return new SSRController();
}

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Netch.Models;
namespace Netch.ServerEx.ShadowsocksR
namespace Netch.Servers.ShadowsocksR
{
public class ShadowsocksR : Server
{

View File

@@ -1,4 +1,4 @@
namespace Netch.ServerEx.Socks5.Form
namespace Netch.Servers.Socks5.Form
{
partial class Socks5Form
{

View File

@@ -1,7 +1,7 @@
using System;
using Netch.Utils;
namespace Netch.ServerEx.Socks5.Form
namespace Netch.Servers.Socks5.Form
{
public partial class Socks5Form : System.Windows.Forms.Form
{

View File

@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System.Linq;
using Netch.Controllers;
using Netch.Models;
using Netch.ServerEx.Socks5.Form;
using Netch.Servers.Socks5.Form;
using Newtonsoft.Json.Linq;
namespace Netch.ServerEx.Socks5
namespace Netch.Servers.Socks5
{
public class S5Util : IServerUtil
{
@@ -35,7 +36,7 @@ namespace Netch.ServerEx.Socks5
return $"https://t.me/socks?server={server.Hostname}&port={server.Port}";
}
public ServerController GetController()
public IServerController GetController()
{
return null;
}

View File

@@ -1,6 +1,6 @@
using Netch.Models;
namespace Netch.ServerEx.Socks5
namespace Netch.Servers.Socks5
{
public class Socks5 : Server
{

View File

@@ -1,4 +1,4 @@
namespace Netch.ServerEx.Trojan.Form
namespace Netch.Servers.Trojan.Form
{
partial class TrojanForm
{

View File

@@ -3,7 +3,7 @@ using System.Windows.Forms;
using Netch.Models;
using Netch.Utils;
namespace Netch.ServerEx.Trojan.Form
namespace Netch.Servers.Trojan.Form
{
public partial class TrojanForm : System.Windows.Forms.Form
{

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace Netch.ServerEx.Trojan.Models
namespace Netch.Servers.Trojan.Models
{
public class TrojanConfig
{

View File

@@ -1,6 +1,6 @@
using Netch.Models;
namespace Netch.ServerEx.Trojan
namespace Netch.Servers.Trojan
{
public class Trojan : Server
{

View File

@@ -1,28 +1,33 @@
using System.Collections.Generic;
using System.IO;
using Netch.Controllers;
using Netch.Models;
using Netch.ServerEx.Trojan.Models;
using Netch.Servers.Trojan.Models;
using Newtonsoft.Json;
namespace Netch.ServerEx.Trojan
namespace Netch.Servers.Trojan
{
public class TrojanController : ServerController
public class TrojanController : Guard, IServerController
{
public TrojanController()
{
Name = "Trojan";
MainFile = "Trojan.exe";
StartedKeywords.Add("started");
StoppedKeywords.Add("exiting");
}
public override bool Start(Server s, Mode mode)
public override string MainFile { get; protected set; } = "Trojan.exe";
public override string Name { get; protected set; } = "Trojan";
public int? Socks5LocalPort { get; set; }
public string LocalAddress { get; set; }
public bool Start(Server s, Mode mode)
{
var server = (Trojan) s;
File.WriteAllText("data\\last.json", JsonConvert.SerializeObject(new TrojanConfig
{
local_addr = LocalAddress,
local_port = Socks5LocalPort,
local_addr = LocalAddress ?? Global.Settings.LocalAddress,
local_port = Socks5LocalPort ?? Global.Settings.Socks5LocalPort,
remote_addr = server.Hostname,
remote_port = server.Port,
password = new List<string>

View File

@@ -2,11 +2,12 @@ using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using Netch.Controllers;
using Netch.Models;
using Netch.ServerEx.Trojan.Form;
using Netch.Servers.Trojan.Form;
using Newtonsoft.Json.Linq;
namespace Netch.ServerEx.Trojan
namespace Netch.Servers.Trojan
{
public class TrojanUtil : IServerUtil
{
@@ -37,7 +38,7 @@ namespace Netch.ServerEx.Trojan
return "";
}
public ServerController GetController()
public IServerController GetController()
{
return new TrojanController();
}

View File

@@ -1,4 +1,4 @@
namespace Netch.ServerEx.VMess.Form
namespace Netch.Servers.VMess.Form
{
partial class VMessForm
{

View File

@@ -3,7 +3,7 @@ using System.Windows.Forms;
using Netch.Models;
using Netch.Utils;
namespace Netch.ServerEx.VMess.Form
namespace Netch.Servers.VMess.Form
{
public partial class VMessForm : System.Windows.Forms.Form
{

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
using System.Collections.Generic;
namespace Netch.ServerEx.VMess.Models
namespace Netch.Servers.VMess.Models
{
public class VMessConfig
{

View File

@@ -1,4 +1,4 @@
namespace Netch.ServerEx.VMess.Models
namespace Netch.Servers.VMess.Models
{
/// <summary>
/// 使用 v2rayN 定义的 VMess 链接格式

View File

@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Netch.Models;
namespace Netch.ServerEx.VMess
namespace Netch.Servers.VMess
{
public class VMess : Server
{

View File

@@ -1,22 +1,28 @@
using System.Collections.Generic;
using System.IO;
using Netch.Controllers;
using Netch.Models;
using Netch.ServerEx.VMess.Models;
using Netch.Servers.VMess.Models;
using Newtonsoft.Json;
namespace Netch.ServerEx.VMess
namespace Netch.Servers.VMess
{
public class VMessController : ServerController
public class VMessController : Guard, IServerController
{
public VMessController()
{
Name = "V2Ray";
MainFile = "v2ray.exe";
StartedKeywords.Add("started");
StoppedKeywords.AddRange(new[] {"config file not readable", "failed to"});
}
public override bool Start(Server s, Mode mode)
public override string Name { get; protected set; } = "V2Ray";
public override string MainFile { get; protected set; } = "v2ray.exe";
public int? Socks5LocalPort { get; set; }
public string LocalAddress { get; set; }
public bool Start(Server s, Mode mode)
{
var server = (VMess) s;
File.WriteAllText("data\\last.json", JsonConvert.SerializeObject(new VMessConfig.Config()
@@ -26,8 +32,8 @@ namespace Netch.ServerEx.VMess
new VMessConfig.Inbounds
{
settings = new VMessConfig.InboundSettings(),
port = Socks5LocalPort,
listen = LocalAddress
port = Socks5LocalPort ?? Global.Settings.Socks5LocalPort,
listen = LocalAddress ?? Global.Settings.LocalAddress
}
},
outbounds = new List<VMessConfig.Outbounds>

View File

@@ -1,12 +1,13 @@
using System.Collections.Generic;
using Netch.Controllers;
using Netch.Models;
using Netch.ServerEx.VMess.Form;
using Netch.ServerEx.VMess.Models;
using Netch.Servers.VMess.Form;
using Netch.Servers.VMess.Models;
using Netch.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Netch.ServerEx.VMess
namespace Netch.Servers.VMess
{
public class VMessUtil : IServerUtil
{
@@ -51,7 +52,7 @@ namespace Netch.ServerEx.VMess
return "vmess://" + ShareLink.URLSafeBase64Encode(vmessJson);
}
public ServerController GetController()
public IServerController GetController()
{
return new VMessController();
}

View File

@@ -73,11 +73,13 @@ namespace Netch.Utils
}
else if (MainController.ServerController != null)
{
instances.Add(MainController.ServerController.Instance);
if (MainController.ServerController is Guard instanceController)
instances.Add(instanceController.Instance);
}
else if (MainController.ModeController != null)
{
instances.Add(MainController.ModeController.Instance);
if (MainController.ModeController is Guard instanceController)
instances.Add(instanceController.Instance);
}
var processList = instances.Select(instance => instance.Id).ToList();

View File

@@ -32,7 +32,7 @@ namespace Netch.Utils
foreach (JObject server in settingJObject["Server"])
{
var serverResult = Servers.ParseJObject(server);
var serverResult = ServerHelper.ParseJObject(server);
if (serverResult != null)
Global.Settings.Server.Add(serverResult);
}

View File

@@ -5,7 +5,7 @@ using NetFwTypeLib;
namespace Netch.Utils
{
public class Firewall
public static class Firewall
{
private static readonly string[] ProgramPath =
{

View File

@@ -1,33 +1,30 @@
using System;
using System.Linq;
using System.Management;
using Netch.Utils;
using Netch.Controllers;
using Netch.Models.WinFW;
using NETCONLib;
using WinFW;
namespace Netch.Controllers
namespace Netch.Utils
{
public class ICSController
public static class ICSHelper
{
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;
return (
from NetworkConnection connection in new NetworkConnectionCollection()
where connection.DeviceName == Global.TUNTAP.Adapter.Description
select connection.SharingEnabled
).FirstOrDefault();
}
}
public static bool Enable()
{
Utils.Utils.SearchOutboundAdapter();
Utils.SearchOutboundAdapter();
TUNTAPController.SearchTapAdapter();
if (Global.TUNTAP.Adapter == null || Global.Outbound.Adapter == null)
@@ -41,10 +38,7 @@ namespace Netch.Controllers
#region Save Outbound IP Config
var wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
var moc = wmi.GetInstances();
var dhcpEnabled = true;
bool dhcpEnabled;
string[] ipAddress = null;
string[] subnetMask = null;
string[] gateway = null;
@@ -101,19 +95,19 @@ namespace Netch.Controllers
else
{
//Set static IP and subnet mask
var newIP = outboundWmi.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = ipAddress;
newIP["SubnetMask"] = subnetMask;
outboundWmi.InvokeMethod("EnableStatic", newIP, null);
var ip = outboundWmi.GetMethodParameters("EnableStatic");
ip["IPAddress"] = ipAddress;
ip["SubnetMask"] = subnetMask;
outboundWmi.InvokeMethod("EnableStatic", ip, 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);
var newDns = outboundWmi.GetMethodParameters("SetDNSServerSearchOrder");
newDns["DNSServerSearchOrder"] = dns;
outboundWmi.InvokeMethod("SetDNSServerSearchOrder", newDns, null);
}
#endregion
@@ -139,10 +133,9 @@ namespace Netch.Controllers
public static void Disable()
{
foreach (NetworkConnection connection in new NetworkConnectionCollection())
foreach (var connection in new NetworkConnectionCollection().Cast<NetworkConnection>().Where(connection => connection.SharingEnabled))
{
if (connection.SharingEnabled)
connection.DisableSharing();
connection.DisableSharing();
}
CleanupWMISharingEntries();
@@ -153,25 +146,27 @@ namespace Netch.Controllers
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())
var searchResults = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM HNet_ConnectionProperties"));
foreach (var o in searchResults.Get())
{
var entry = (ManagementObject) o;
if ((bool) entry["IsIcsPrivate"])
entry["IsIcsPrivate"] = false;
if ((bool) entry["IsIcsPublic"])
entry["IsIcsPublic"] = false;
entry.Put(options);
entry.Put(new PutOptions
{
Type = PutType.UpdateOnly
});
}
}
public static ManagementObject GetManagementObjectByDeviceNameOrDefault(string deviceName)
private static ManagementObject GetManagementObjectByDeviceNameOrDefault(string deviceName)
{
foreach (ManagementObject mo in new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances())
foreach (var o in new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances())
{
var mo = (ManagementObject) o;
if (((string) mo["Caption"]).EndsWith(deviceName))
{
return mo;

View File

@@ -6,7 +6,7 @@ using Netch.Models;
namespace Netch.Utils
{
public static class Modes
public static class ModeHelper
{
private const string MODE_DIR = "mode";

View File

@@ -5,7 +5,7 @@ using System.Text;
namespace Netch.Utils
{
public class OnlyInstance
public static class OnlyInstance
{
public enum Commands
{

View File

@@ -7,11 +7,11 @@ using Newtonsoft.Json.Linq;
namespace Netch.Utils
{
public static class Servers
public static class ServerHelper
{
public static readonly IEnumerable<IServerUtil> ServerUtils;
static Servers()
static ServerHelper()
{
var serversUtilsTypes = Assembly.GetExecutingAssembly().GetExportedTypes().Where(type => type.GetInterfaces().Any(t => t == typeof(IServerUtil)));
ServerUtils = serversUtilsTypes.Select(t => (IServerUtil) Activator.CreateInstance(t)).OrderBy(util => util.Priority);

View File

@@ -3,8 +3,8 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Netch.ServerEx.Shadowsocks;
using Netch.ServerEx.Shadowsocks.Models;
using Netch.Servers.Shadowsocks;
using Netch.Servers.Shadowsocks.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Server = Netch.Models.Server;
@@ -105,7 +105,7 @@ namespace Netch.Utils
public static string GetShareLink(Server server)
{
return Servers.GetUtilByTypeName(server.Type).GetShareLink(server);
return ServerHelper.GetUtilByTypeName(server.Type).GetShareLink(server);
}
public static List<Server> ParseText(string text)
@@ -169,7 +169,7 @@ namespace Netch.Utils
{
if (text.StartsWith("tg://socks?") || text.StartsWith("https://t.me/socks?"))
{
list.AddRange(Servers.GetUtilByTypeName("Socks5").ParseUri(text));
list.AddRange(ServerHelper.GetUtilByTypeName("Socks5").ParseUri(text));
}
else if (text.StartsWith("Netch://"))
{
@@ -178,7 +178,7 @@ namespace Netch.Utils
else
{
var scheme = GetUriScheme(text);
var util = Servers.GetUtilByUriScheme(scheme);
var util = ServerHelper.GetUtilByUriScheme(scheme);
if (util == null)
{
Logging.Warning($"无法处理 {scheme} 协议订阅链接");
@@ -230,8 +230,8 @@ namespace Netch.Utils
}
var type = (string) NetchLink["Type"];
var s = Servers.GetUtilByTypeName(type).ParseJObject(NetchLink);
return Servers.GetUtilByTypeName(s.Type).CheckServer(s) ? s : null;
var s = ServerHelper.GetUtilByTypeName(type).ParseJObject(NetchLink);
return ServerHelper.GetUtilByTypeName(s.Type).CheckServer(s) ? s : null;
}
public static string GetNetchLink(Server s)

View File

@@ -96,9 +96,9 @@ namespace Netch.Utils
{
try
{
var SHA256 = System.Security.Cryptography.SHA256.Create();
var sha256 = System.Security.Cryptography.SHA256.Create();
var fileStream = File.OpenRead(filePath);
return SHA256.ComputeHash(fileStream).Aggregate(string.Empty, (current, b) => current + b.ToString("x2"));
return sha256.ComputeHash(fileStream).Aggregate(string.Empty, (current, b) => current + b.ToString("x2"));
}
catch
{
@@ -124,7 +124,7 @@ namespace Netch.Utils
}
}
public static string FileVersion(string file) => File.Exists(file) ? FileVersionInfo.GetVersionInfo(file).FileVersion : string.Empty;
public static string GetFileVersion(string file) => File.Exists(file) ? FileVersionInfo.GetVersionInfo(file).FileVersion : string.Empty;
public static bool SearchOutboundAdapter(bool log = true)
{

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Netch.Utils
{
public class WebUtil
public static class WebUtil
{
public const string DefaultUserAgent =
@"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36";