Merge pull request #292 from chsbuffer/master

分割MainForm类,捉虫
This commit is contained in:
AmazingDM
2020-07-06 11:57:34 +08:00
committed by GitHub
23 changed files with 1640 additions and 1576 deletions

View File

@@ -19,7 +19,7 @@ namespace Netch.Controllers
/// <returns></returns>
public bool Start()
{
MainForm.Instance.StatusText($"{i18N.Translate("Starting dns Service")}");
MainForm.Instance.StatusText(i18N.Translate("Starting dns Service"));
try
{
if (!File.Exists("bin\\unbound.exe") && !File.Exists("bin\\unbound-service.conf") && !File.Exists("bin\\forward-zone.conf"))

View File

@@ -25,6 +25,7 @@ namespace Netch.Controllers
/// <returns>是否启动成功</returns>
public bool Start(Server server, Mode mode)
{
RecordPrevious();
try
{
if (server.Type == "Socks5")
@@ -43,21 +44,7 @@ namespace Netch.Controllers
if (mode.Type != 5)
{
RecordPrevious();
NativeMethods.SetGlobal($"127.0.0.1:{Global.Settings.HTTPLocalPort}", "<local>");
// HTTP 系统代理模式,启动系统代理
/*
using (var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true))
{
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", $"127.0.0.1:{Global.Settings.HTTPLocalPort}");
Win32Native.InternetSetOption(IntPtr.Zero, 39, IntPtr.Zero, 0);
Win32Native.InternetSetOption(IntPtr.Zero, 37, IntPtr.Zero, 0);
}
*/
}
}
catch (Exception e)
@@ -114,18 +101,6 @@ namespace Netch.Controllers
if (!prevEnabled)
NativeMethods.SetDIRECT();
prevEnabled = false;
/*
using (var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true))
{
registry.SetValue("ProxyEnable", 0);
registry.DeleteValue("ProxyServer", false);
Win32Native.InternetSetOption(IntPtr.Zero, 39, IntPtr.Zero, 0);
Win32Native.InternetSetOption(IntPtr.Zero, 37, IntPtr.Zero, 0);
}
*/
}
catch (Exception e)
{

View File

@@ -141,7 +141,7 @@ namespace Netch.Controllers
result = pNFController.Start(server, mode, false);
if (!result)
{
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Restarting Redirector")}");
MainForm.Instance.StatusText(i18N.Translate("Restarting Redirector"));
Logging.Info("正常启动失败后尝试停止驱动服务再重新启动");
//正常启动失败后尝试停止驱动服务再重新启动
result = pNFController.Start(server, mode, true);

View File

@@ -48,7 +48,7 @@ namespace Netch.Controllers
public bool Start(Server server, Mode mode, bool StopServiceAndRestart)
{
if (!StopServiceAndRestart)
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting Redirector")}");
MainForm.Instance.StatusText(i18N.Translate("Starting Redirector"));
if (!File.Exists("bin\\Redirector.exe"))
{
@@ -130,12 +130,12 @@ namespace Netch.Controllers
// 防止其他程序占用 重置 NF 百万连接数限制
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting netfilter2 Service")}");
MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service"));
service.Start();
}
else if (service.Status == ServiceControllerStatus.Stopped)
{
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting netfilter2 Service")}");
MainForm.Instance.StatusText(i18N.Translate("Starting netfilter2 Service"));
service.Start();
}
}
@@ -267,7 +267,7 @@ namespace Netch.Controllers
Logging.Info(e.ToString());
return false;
}
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Register driver")}");
MainForm.Instance.StatusText(i18N.Translate("Register driver"));
// 注册驱动文件
var result = NFAPI.nf_registerDriver("netfilter2");
if (result != NF_STATUS.NF_STATUS_SUCCESS)

View File

@@ -27,7 +27,7 @@ namespace Netch.Controllers
public (bool, string, string, string) Start()
{
Thread.Sleep(1000);
MainForm.Instance.NatTypeStatusText($"{i18N.Translate("Starting NatTester")}");
MainForm.Instance.NatTypeStatusText(i18N.Translate("Starting NatTester"));
try
{
if (!File.Exists("bin\\NTT.exe"))

View File

@@ -29,7 +29,7 @@ namespace Netch.Controllers
/// <returns>是否启动成功</returns>
public bool Start(Server server, Mode mode)
{
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting Shadowsocks")}");
MainForm.Instance.StatusText(i18N.Translate("Starting Shadowsocks"));
File.Delete("logging\\shadowsocks.log");
//从DLL启动Shaowsocks

View File

@@ -28,7 +28,7 @@ namespace Netch.Controllers
/// <returns>是否启动成功</returns>
public bool Start(Server server, Mode mode)
{
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting ShadowsocksR")}");
MainForm.Instance.StatusText(i18N.Translate("Starting ShadowsocksR"));
File.Delete("logging\\shadowsocksr.log");

View File

@@ -69,7 +69,7 @@ namespace Netch.Controllers
/// </summary>
public bool SetupBypass()
{
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("SetupBypass")}");
MainForm.Instance.StatusText(i18N.Translate("SetupBypass"));
Logging.Info("设置绕行规则 → 设置让服务器 IP 走直连");
// 让服务器 IP 走直连
foreach (var address in ServerAddresses)
@@ -375,7 +375,7 @@ namespace Netch.Controllers
/// <returns>是否成功</returns>
public bool Start(Server server, Mode mode)
{
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting Tap")}");
MainForm.Instance.StatusText(i18N.Translate("Starting Tap"));
foreach (var proc in Process.GetProcessesByName("tun2socks"))
{
try

View File

@@ -30,7 +30,7 @@ namespace Netch.Controllers
/// <returns>是否启动成功</returns>
public bool Start(Server server, Mode mode)
{
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting Trojan")}");
MainForm.Instance.StatusText(i18N.Translate("Starting Trojan"));
File.Delete("logging\\trojan.log");
if (!File.Exists("bin\\Trojan.exe"))

View File

@@ -31,7 +31,7 @@ namespace Netch.Controllers
/// <returns>是否启动成功</returns>
public bool Start(Server server, Mode mode)
{
MainForm.Instance.StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting V2ray")}");
MainForm.Instance.StatusText(i18N.Translate("Starting V2ray"));
if (!File.Exists("bin\\v2ray.exe") || !File.Exists("bin\\v2ctl.exe"))
{
return false;

View File

@@ -0,0 +1,227 @@
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Netch.Controllers;
using Netch.Models;
using Netch.Utils;
namespace Netch.Forms
{
public partial class Dummy
{
}
partial class MainForm
{
public void ControlFun()
{
SaveConfigs();
if (State == State.Waiting || State == State.Stopped)
{
// 服务器、模式 需选择
if (ServerComboBox.SelectedIndex == -1)
{
MessageBox.Show(i18N.Translate("Please select a server first"), i18N.Translate("Information"),
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (ModeComboBox.SelectedIndex == -1)
{
MessageBox.Show(i18N.Translate("Please select an mode first"), i18N.Translate("Information"),
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false;
UpdateStatus(State.Starting);
Task.Run(() =>
{
var server = ServerComboBox.SelectedItem as Models.Server;
var mode = ModeComboBox.SelectedItem as Models.Mode;
MainController = new MainController();
var startResult = MainController.Start(server, mode);
if (startResult)
{
Task.Run(() =>
{
LastUploadBandwidth = 0;
//LastDownloadBandwidth = 0;
//UploadSpeedLabel.Text = "↑: 0 KB/s";
DownloadSpeedLabel.Text = "↑↓: 0 KB/s";
UsedBandwidthLabel.Text = $"{i18N.Translate("Used")}{i18N.Translate(": ")}0 KB";
UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = true;
UploadSpeedLabel.Visible = false;
Bandwidth.NetTraffic(server, mode, MainController);
});
//MainController.pNFController.OnBandwidthUpdated += OnBandwidthUpdated;
// 如果勾选启动后最小化
if (Global.Settings.MinimizeWhenStarted)
{
WindowState = FormWindowState.Minimized;
NotifyIcon.Visible = true;
if (IsFirstOpened)
{
// 显示提示语
NotifyIcon.ShowBalloonTip(5,
UpdateChecker.Name,
i18N.Translate(
"Netch is now minimized to the notification bar, double click this icon to restore."),
ToolTipIcon.Info);
IsFirstOpened = false;
}
Hide();
}
// TODO 是否需要移到一个函数中
var text = new StringBuilder(" (");
text.Append(Global.Settings.LocalAddress == "0.0.0.0"
? i18N.Translate("Allow other Devices to connect") + " "
: "");
if (server.Type == "Socks5")
{
// 不可控Socks5
if (mode.Type == 3 && mode.Type == 5)
{
// 可控HTTP
text.Append(
$"HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
else
{
// 不可控HTTP
text.Clear();
}
}
else
{
// 可控Socks5
text.Append(
$"Socks5 {i18N.Translate("Local Port", ": ")}{Global.Settings.Socks5LocalPort}");
if (mode.Type == 3 || mode.Type == 5)
{
//有HTTP
text.Append(
$" | HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}");
}
}
if (text.Length > 0)
{
text.Append(")");
}
UpdateStatus(State.Started);
StatusText(i18N.Translate(StateExtension.GetStatusString(State)) + text);
if (Global.Settings.StartedTcping)
{
// 自动检测延迟
Task.Run(() =>
{
while (true)
{
if (State == State.Started)
{
server.Test();
// 重载服务器列表
InitServer();
Thread.Sleep(Global.Settings.StartedTcping_Interval * 1000);
}
else
{
break;
}
}
});
}
}
else
{
UpdateStatus(State.Stopped);
StatusText(i18N.Translate("Start Failed"));
}
});
}
else
{
// 停止
UpdateStatus(State.Stopping);
Task.Run(() =>
{
var server = ServerComboBox.SelectedItem as Models.Server;
var mode = ModeComboBox.SelectedItem as Models.Mode;
MainController.Stop();
UpdateStatus(State.Stopped);
TestServer();
});
}
}
public void OnBandwidthUpdated(long download)
{
try
{
UsedBandwidthLabel.Text = $"{i18N.Translate("Used", ": ")}{Bandwidth.Compute(download)}";
//UploadSpeedLabel.Text = $"↑: {Utils.Bandwidth.Compute(upload - LastUploadBandwidth)}/s";
DownloadSpeedLabel.Text = $"↑↓: {Bandwidth.Compute(download - LastDownloadBandwidth)}/s";
//LastUploadBandwidth = upload;
LastDownloadBandwidth = download;
Refresh();
}
catch (Exception)
{
}
}
public void OnBandwidthUpdated(long upload, long download)
{
try
{
if (upload < 1 || download < 1)
{
return;
}
UsedBandwidthLabel.Text =
$"{i18N.Translate("Used")}{i18N.Translate(": ")}{Bandwidth.Compute(upload + download)}";
UploadSpeedLabel.Text = $"↑: {Bandwidth.Compute(upload - LastUploadBandwidth)}/s";
DownloadSpeedLabel.Text = $"↓: {Bandwidth.Compute(download - LastDownloadBandwidth)}/s";
LastUploadBandwidth = upload;
LastDownloadBandwidth = download;
Refresh();
}
catch (Exception)
{
}
}
/// <summary>
/// 上一次上传的流量
/// </summary>
public long LastUploadBandwidth;
/// <summary>
/// 上一次下载的流量
/// </summary>
public long LastDownloadBandwidth;
}
}

View File

@@ -42,18 +42,17 @@ namespace Netch.Forms
this.AddTrojanServerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ModeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.CreateProcessModeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ReloadModesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.SubscribeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ManageSubscribeLinksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.UpdateServersFromSubscribeLinksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.OptionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ReloadModesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.RestartServiceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.UninstallServiceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.OpenDirectoryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.CleanDNSCacheToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.UpdateACLToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.updateACLWithProxyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.UninstallServiceToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.reinstallTapDriverToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.OpenDirectoryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.AboutToolStripButton = new System.Windows.Forms.ToolStripButton();
this.VersionLabel = new System.Windows.Forms.ToolStripLabel();
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@@ -67,9 +66,9 @@ namespace Netch.Forms
this.ModeComboBox = new System.Windows.Forms.SearchComboBox();
this.ServerComboBox = new System.Windows.Forms.ComboBox();
this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
this.EditPictureBox = new System.Windows.Forms.PictureBox();
this.EditServerPictureBox = new System.Windows.Forms.PictureBox();
this.CopyLinkPictureBox = new System.Windows.Forms.PictureBox();
this.DeletePictureBox = new System.Windows.Forms.PictureBox();
this.DeleteServerPictureBox = new System.Windows.Forms.PictureBox();
this.SpeedPictureBox = new System.Windows.Forms.PictureBox();
this.tableLayoutPanel3 = new System.Windows.Forms.TableLayoutPanel();
this.EditModePictureBox = new System.Windows.Forms.PictureBox();
@@ -92,9 +91,9 @@ namespace Netch.Forms
this.ConfigurationGroupBox.SuspendLayout();
this.configLayoutPanel.SuspendLayout();
this.tableLayoutPanel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.EditPictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.EditServerPictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.CopyLinkPictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.DeletePictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.DeleteServerPictureBox)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.SpeedPictureBox)).BeginInit();
this.tableLayoutPanel3.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.EditModePictureBox)).BeginInit();
@@ -182,7 +181,8 @@ namespace Netch.Forms
// ModeToolStripMenuItem
//
this.ModeToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.CreateProcessModeToolStripMenuItem});
this.CreateProcessModeToolStripMenuItem,
this.ReloadModesToolStripMenuItem});
this.ModeToolStripMenuItem.Margin = new System.Windows.Forms.Padding(0, 0, 0, 1);
this.ModeToolStripMenuItem.Name = "ModeToolStripMenuItem";
this.ModeToolStripMenuItem.Size = new System.Drawing.Size(55, 21);
@@ -195,6 +195,13 @@ namespace Netch.Forms
this.CreateProcessModeToolStripMenuItem.Text = "Create Process Mode";
this.CreateProcessModeToolStripMenuItem.Click += new System.EventHandler(this.CreateProcessModeToolStripButton_Click);
//
// ReloadModesToolStripMenuItem
//
this.ReloadModesToolStripMenuItem.Name = "ReloadModesToolStripMenuItem";
this.ReloadModesToolStripMenuItem.Size = new System.Drawing.Size(202, 22);
this.ReloadModesToolStripMenuItem.Text = "Reload Modes";
this.ReloadModesToolStripMenuItem.Click += new System.EventHandler(this.ReloadModesToolStripMenuItem_Click);
//
// SubscribeToolStripMenuItem
//
this.SubscribeToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
@@ -222,39 +229,23 @@ namespace Netch.Forms
// OptionsToolStripMenuItem
//
this.OptionsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ReloadModesToolStripMenuItem,
this.RestartServiceToolStripMenuItem,
this.UninstallServiceToolStripMenuItem,
this.OpenDirectoryToolStripMenuItem,
this.CleanDNSCacheToolStripMenuItem,
this.UpdateACLToolStripMenuItem,
this.updateACLWithProxyToolStripMenuItem,
this.reinstallTapDriverToolStripMenuItem,
this.OpenDirectoryToolStripMenuItem});
this.UninstallServiceToolStripMenuItem,
this.reinstallTapDriverToolStripMenuItem});
this.OptionsToolStripMenuItem.Margin = new System.Windows.Forms.Padding(0, 0, 0, 1);
this.OptionsToolStripMenuItem.Name = "OptionsToolStripMenuItem";
this.OptionsToolStripMenuItem.Size = new System.Drawing.Size(66, 21);
this.OptionsToolStripMenuItem.Text = "Options";
//
// ReloadModesToolStripMenuItem
// OpenDirectoryToolStripMenuItem
//
this.ReloadModesToolStripMenuItem.Name = "ReloadModesToolStripMenuItem";
this.ReloadModesToolStripMenuItem.Size = new System.Drawing.Size(219, 22);
this.ReloadModesToolStripMenuItem.Text = "Reload Modes";
this.ReloadModesToolStripMenuItem.Click += new System.EventHandler(this.ReloadModesToolStripMenuItem_Click);
//
// RestartServiceToolStripMenuItem
//
this.RestartServiceToolStripMenuItem.Name = "RestartServiceToolStripMenuItem";
this.RestartServiceToolStripMenuItem.Size = new System.Drawing.Size(219, 22);
this.RestartServiceToolStripMenuItem.Text = "Restart Service";
this.RestartServiceToolStripMenuItem.Click += new System.EventHandler(this.RestartServiceToolStripMenuItem_Click);
//
// UninstallServiceToolStripMenuItem
//
this.UninstallServiceToolStripMenuItem.Name = "UninstallServiceToolStripMenuItem";
this.UninstallServiceToolStripMenuItem.Size = new System.Drawing.Size(219, 22);
this.UninstallServiceToolStripMenuItem.Text = "Uninstall Service";
this.UninstallServiceToolStripMenuItem.Click += new System.EventHandler(this.UninstallServiceToolStripMenuItem_Click);
this.OpenDirectoryToolStripMenuItem.Name = "OpenDirectoryToolStripMenuItem";
this.OpenDirectoryToolStripMenuItem.Size = new System.Drawing.Size(219, 22);
this.OpenDirectoryToolStripMenuItem.Text = "Open Directory";
this.OpenDirectoryToolStripMenuItem.Click += new System.EventHandler(this.OpenDirectoryToolStripMenuItem_Click);
//
// CleanDNSCacheToolStripMenuItem
//
@@ -277,6 +268,13 @@ namespace Netch.Forms
this.updateACLWithProxyToolStripMenuItem.Text = "Update ACL with proxy";
this.updateACLWithProxyToolStripMenuItem.Click += new System.EventHandler(this.updateACLWithProxyToolStripMenuItem_Click);
//
// UninstallServiceToolStripMenuItem
//
this.UninstallServiceToolStripMenuItem.Name = "UninstallServiceToolStripMenuItem";
this.UninstallServiceToolStripMenuItem.Size = new System.Drawing.Size(219, 22);
this.UninstallServiceToolStripMenuItem.Text = "Uninstall Service";
this.UninstallServiceToolStripMenuItem.Click += new System.EventHandler(this.UninstallServiceToolStripMenuItem_Click);
//
// reinstallTapDriverToolStripMenuItem
//
this.reinstallTapDriverToolStripMenuItem.Name = "reinstallTapDriverToolStripMenuItem";
@@ -284,13 +282,6 @@ namespace Netch.Forms
this.reinstallTapDriverToolStripMenuItem.Text = "Reinstall TUN/TAP driver";
this.reinstallTapDriverToolStripMenuItem.Click += new System.EventHandler(this.reinstallTapDriverToolStripMenuItem_Click);
//
// OpenDirectoryToolStripMenuItem
//
this.OpenDirectoryToolStripMenuItem.Name = "OpenDirectoryToolStripMenuItem";
this.OpenDirectoryToolStripMenuItem.Size = new System.Drawing.Size(219, 22);
this.OpenDirectoryToolStripMenuItem.Text = "Open Directory";
this.OpenDirectoryToolStripMenuItem.Click += new System.EventHandler(this.OpenDirectoryToolStripMenuItem_Click);
//
// AboutToolStripButton
//
this.AboutToolStripButton.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
@@ -318,7 +309,7 @@ namespace Netch.Forms
this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
this.exitToolStripMenuItem.Size = new System.Drawing.Size(40, 22);
this.exitToolStripMenuItem.Text = "Exit";
this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
this.exitToolStripMenuItem.Click += new System.EventHandler(this.ExitToolStripButton_Click);
//
// RelyToolStripMenuItem
//
@@ -435,9 +426,9 @@ namespace Netch.Forms
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel2.Controls.Add(this.EditPictureBox, 0, 0);
this.tableLayoutPanel2.Controls.Add(this.EditServerPictureBox, 0, 0);
this.tableLayoutPanel2.Controls.Add(this.CopyLinkPictureBox, 3, 0);
this.tableLayoutPanel2.Controls.Add(this.DeletePictureBox, 1, 0);
this.tableLayoutPanel2.Controls.Add(this.DeleteServerPictureBox, 1, 0);
this.tableLayoutPanel2.Controls.Add(this.SpeedPictureBox, 2, 0);
this.tableLayoutPanel2.Location = new System.Drawing.Point(606, 3);
this.tableLayoutPanel2.Name = "tableLayoutPanel2";
@@ -446,16 +437,17 @@ namespace Netch.Forms
this.tableLayoutPanel2.Size = new System.Drawing.Size(94, 24);
this.tableLayoutPanel2.TabIndex = 12;
//
// EditPictureBox
// EditServerPictureBox
//
this.EditPictureBox.Cursor = System.Windows.Forms.Cursors.Hand;
this.EditPictureBox.Image = ((System.Drawing.Image)(resources.GetObject("EditPictureBox.Image")));
this.EditPictureBox.Location = new System.Drawing.Point(3, 3);
this.EditPictureBox.Name = "EditPictureBox";
this.EditPictureBox.Size = new System.Drawing.Size(16, 16);
this.EditPictureBox.TabIndex = 7;
this.EditPictureBox.TabStop = false;
this.EditPictureBox.Click += new System.EventHandler(this.EditPictureBox_Click);
this.EditServerPictureBox.BackColor = System.Drawing.SystemColors.Control;
this.EditServerPictureBox.Cursor = System.Windows.Forms.Cursors.Hand;
this.EditServerPictureBox.Image = global::Netch.Properties.Resources.edit;
this.EditServerPictureBox.Location = new System.Drawing.Point(3, 3);
this.EditServerPictureBox.Name = "EditServerPictureBox";
this.EditServerPictureBox.Size = new System.Drawing.Size(16, 16);
this.EditServerPictureBox.TabIndex = 7;
this.EditServerPictureBox.TabStop = false;
this.EditServerPictureBox.Click += new System.EventHandler(this.EditServerPictureBox_Click);
//
// CopyLinkPictureBox
//
@@ -468,21 +460,21 @@ namespace Netch.Forms
this.CopyLinkPictureBox.TabStop = false;
this.CopyLinkPictureBox.Click += new System.EventHandler(this.CopyLinkPictureBox_Click);
//
// DeletePictureBox
// DeleteServerPictureBox
//
this.DeletePictureBox.Cursor = System.Windows.Forms.Cursors.Hand;
this.DeletePictureBox.Image = ((System.Drawing.Image)(resources.GetObject("DeletePictureBox.Image")));
this.DeletePictureBox.Location = new System.Drawing.Point(26, 3);
this.DeletePictureBox.Name = "DeletePictureBox";
this.DeletePictureBox.Size = new System.Drawing.Size(16, 16);
this.DeletePictureBox.TabIndex = 8;
this.DeletePictureBox.TabStop = false;
this.DeletePictureBox.Click += new System.EventHandler(this.DeletePictureBox_Click);
this.DeleteServerPictureBox.Cursor = System.Windows.Forms.Cursors.Hand;
this.DeleteServerPictureBox.Image = global::Netch.Properties.Resources.delete;
this.DeleteServerPictureBox.Location = new System.Drawing.Point(26, 3);
this.DeleteServerPictureBox.Name = "DeleteServerPictureBox";
this.DeleteServerPictureBox.Size = new System.Drawing.Size(16, 16);
this.DeleteServerPictureBox.TabIndex = 8;
this.DeleteServerPictureBox.TabStop = false;
this.DeleteServerPictureBox.Click += new System.EventHandler(this.DeleteServerPictureBox_Click);
//
// SpeedPictureBox
//
this.SpeedPictureBox.Cursor = System.Windows.Forms.Cursors.Hand;
this.SpeedPictureBox.Image = ((System.Drawing.Image)(resources.GetObject("SpeedPictureBox.Image")));
this.SpeedPictureBox.Image = global::Netch.Properties.Resources.speed;
this.SpeedPictureBox.Location = new System.Drawing.Point(49, 3);
this.SpeedPictureBox.Name = "SpeedPictureBox";
this.SpeedPictureBox.Size = new System.Drawing.Size(16, 16);
@@ -509,9 +501,7 @@ namespace Netch.Forms
// EditModePictureBox
//
this.EditModePictureBox.Cursor = System.Windows.Forms.Cursors.Hand;
this.EditModePictureBox.ErrorImage = global::Netch.Properties.Resources.edit;
this.EditModePictureBox.Image = global::Netch.Properties.Resources.edit;
this.EditModePictureBox.InitialImage = global::Netch.Properties.Resources.edit;
this.EditModePictureBox.Location = new System.Drawing.Point(3, 3);
this.EditModePictureBox.Name = "EditModePictureBox";
this.EditModePictureBox.Size = new System.Drawing.Size(16, 16);
@@ -576,7 +566,8 @@ namespace Netch.Forms
// NatTypeStatusLabel
//
this.NatTypeStatusLabel.Name = "NatTypeStatusLabel";
this.NatTypeStatusLabel.Size = new System.Drawing.Size(0, 17);
this.NatTypeStatusLabel.Size = new System.Drawing.Size(36, 17);
this.NatTypeStatusLabel.Text = "NAT:";
//
// ControlButton
//
@@ -686,9 +677,9 @@ namespace Netch.Forms
this.configLayoutPanel.ResumeLayout(false);
this.configLayoutPanel.PerformLayout();
this.tableLayoutPanel2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.EditPictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.EditServerPictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.CopyLinkPictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.DeletePictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.DeleteServerPictureBox)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.SpeedPictureBox)).EndInit();
this.tableLayoutPanel3.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.EditModePictureBox)).EndInit();
@@ -703,63 +694,62 @@ namespace Netch.Forms
}
#endregion
private System.Windows.Forms.MenuStrip MenuStrip;
private System.Windows.Forms.ToolStripMenuItem ServerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem SubscribeToolStripMenuItem;
private System.Windows.Forms.ToolStripLabel VersionLabel;
private System.Windows.Forms.GroupBox ConfigurationGroupBox;
private System.Windows.Forms.Label ServerLabel;
private System.Windows.Forms.Label ModeLabel;
private System.Windows.Forms.SearchComboBox ModeComboBox;
private System.Windows.Forms.ComboBox ServerComboBox;
private System.Windows.Forms.StatusStrip StatusStrip;
private System.Windows.Forms.ToolStripStatusLabel StatusLabel;
private System.Windows.Forms.Button ControlButton;
private System.Windows.Forms.ToolStripMenuItem AddSocks5ServerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem AddShadowsocksServerToolStripMenuItem;
private System.Windows.Forms.ToolStripButton AboutToolStripButton;
private System.Windows.Forms.ToolStripMenuItem AddShadowsocksRServerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem AddShadowsocksServerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem AddSocks5ServerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem AddTrojanServerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem AddVMessServerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem CleanDNSCacheToolStripMenuItem;
private System.Windows.Forms.TableLayoutPanel configLayoutPanel;
private System.Windows.Forms.GroupBox ConfigurationGroupBox;
private System.Windows.Forms.Button ControlButton;
private System.Windows.Forms.PictureBox CopyLinkPictureBox;
private System.Windows.Forms.ToolStripMenuItem CreateProcessModeToolStripMenuItem;
private System.Windows.Forms.PictureBox DeleteModePictureBox;
private System.Windows.Forms.PictureBox DeleteServerPictureBox;
private System.Windows.Forms.ToolStripStatusLabel DownloadSpeedLabel;
private System.Windows.Forms.PictureBox EditModePictureBox;
private System.Windows.Forms.PictureBox EditServerPictureBox;
private System.Windows.Forms.ToolStripMenuItem ExitToolStripButton;
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem ImportServersFromClipboardToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem ManageSubscribeLinksToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem UpdateServersFromSubscribeLinksToolStripMenuItem;
private System.Windows.Forms.PictureBox SpeedPictureBox;
private System.Windows.Forms.PictureBox DeletePictureBox;
private System.Windows.Forms.PictureBox EditPictureBox;
private System.Windows.Forms.ToolStripMenuItem RestartServiceToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem UninstallServiceToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem OptionsToolStripMenuItem;
private System.Windows.Forms.MenuStrip MenuStrip;
private System.Windows.Forms.SearchComboBox ModeComboBox;
private System.Windows.Forms.Label ModeLabel;
private System.Windows.Forms.ToolStripMenuItem ModeToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem CreateProcessModeToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem ReloadModesToolStripMenuItem;
private System.Windows.Forms.ToolStripStatusLabel NatTypeStatusLabel;
private System.Windows.Forms.NotifyIcon NotifyIcon;
private System.Windows.Forms.ContextMenuStrip NotifyMenu;
private System.Windows.Forms.ToolStripMenuItem ShowMainFormToolStripButton;
private System.Windows.Forms.ToolStripMenuItem ExitToolStripButton;
private System.Windows.Forms.Button SettingsButton;
private System.Windows.Forms.ToolStripButton AboutToolStripButton;
private System.Windows.Forms.ToolStripStatusLabel UsedBandwidthLabel;
private System.Windows.Forms.ToolStripStatusLabel UploadSpeedLabel;
private System.Windows.Forms.ToolStripStatusLabel DownloadSpeedLabel;
private System.Windows.Forms.ToolStripMenuItem CleanDNSCacheToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem OpenDirectoryToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem OptionsToolStripMenuItem;
private System.Windows.Forms.GroupBox ProfileGroupBox;
private System.Windows.Forms.Label ProfileLabel;
private System.Windows.Forms.TextBox ProfileNameText;
private System.Windows.Forms.GroupBox ProfileGroupBox;
private System.Windows.Forms.TableLayoutPanel ProfileTable;
private System.Windows.Forms.PictureBox EditModePictureBox;
private System.Windows.Forms.PictureBox DeleteModePictureBox;
private System.Windows.Forms.PictureBox CopyLinkPictureBox;
private System.Windows.Forms.ToolStripStatusLabel NatTypeStatusLabel;
private System.Windows.Forms.TableLayoutPanel configLayoutPanel;
private System.Windows.Forms.ToolStripMenuItem reinstallTapDriverToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem ReloadModesToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem RelyToolStripMenuItem;
private System.Windows.Forms.ComboBox ServerComboBox;
private System.Windows.Forms.Label ServerLabel;
private System.Windows.Forms.ToolStripMenuItem ServerToolStripMenuItem;
private System.Windows.Forms.Button SettingsButton;
private System.Windows.Forms.ToolStripMenuItem ShowMainFormToolStripButton;
private System.Windows.Forms.PictureBox SpeedPictureBox;
private System.Windows.Forms.ToolStripStatusLabel StatusLabel;
private System.Windows.Forms.StatusStrip StatusStrip;
private System.Windows.Forms.ToolStripMenuItem SubscribeToolStripMenuItem;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel3;
private System.Windows.Forms.ToolStripMenuItem UninstallServiceToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem UpdateACLToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem updateACLWithProxyToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem reinstallTapDriverToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem AddTrojanServerToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem RelyToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem OpenDirectoryToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem UpdateServersFromSubscribeLinksToolStripMenuItem;
private System.Windows.Forms.ToolStripStatusLabel UploadSpeedLabel;
private System.Windows.Forms.ToolStripStatusLabel UsedBandwidthLabel;
private System.Windows.Forms.ToolStripLabel VersionLabel;
#endregion
}
}

View File

@@ -0,0 +1,495 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceProcess;
using System.Threading.Tasks;
using System.Windows.Forms;
using Netch.Controllers;
using Netch.Forms.Mode;
using Netch.Forms.Server;
using Netch.Models;
using Netch.Utils;
using nfapinet;
using Trojan = Netch.Forms.Server.Trojan;
using VMess = Netch.Forms.Server.VMess;
using WebClient = Netch.Override.WebClient;
namespace Netch.Forms
{
partial class Dummy
{}
partial class MainForm
{
#region MenuStrip
#region
private void ImportServersFromClipboardToolStripMenuItem_Click(object sender, EventArgs e)
{
var texts = Clipboard.GetText();
if (!string.IsNullOrWhiteSpace(texts))
{
var result = ShareLink.Parse(texts);
if (result != null)
{
Global.Settings.Server.AddRange(result);
}
else
{
MessageBox.Show(i18N.Translate("Import servers error!"), i18N.Translate("Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
InitServer();
Configuration.Save();
}
}
private void AddSocks5ServerToolStripMenuItem_Click(object sender, EventArgs e)
{
new Socks5().Show();
Hide();
}
private void AddShadowsocksServerToolStripMenuItem_Click(object sender, EventArgs e)
{
new Shadowsocks().Show();
Hide();
}
private void AddShadowsocksRServerToolStripMenuItem_Click(object sender, EventArgs e)
{
new ShadowsocksR().Show();
Hide();
}
private void AddVMessServerToolStripMenuItem_Click(object sender, EventArgs e)
{
new VMess().Show();
Hide();
}
private void AddTrojanServerToolStripMenuItem_Click(object sender, EventArgs e)
{
new Trojan().Show();
Hide();
}
#endregion
#region
private void CreateProcessModeToolStripButton_Click(object sender, EventArgs e)
{
new Process().Show();
Hide();
}
#endregion
#region
private void ManageSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e)
{
new SubscribeForm().Show();
Hide();
}
private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Global.Settings.UseProxyToUpdateSubscription && ServerComboBox.SelectedIndex == -1)
Global.Settings.UseProxyToUpdateSubscription = false;
if (Global.Settings.UseProxyToUpdateSubscription)
{
// 当前 ServerComboBox 中至少有一项
if (ServerComboBox.SelectedIndex == -1)
{
MessageBox.Show(i18N.Translate("Please select a server first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false;
ControlButton.Text = "...";
}
if (Global.Settings.SubscribeLink.Count > 0)
{
StatusText(i18N.Translate("Starting update subscription"));
DeleteServerPictureBox.Enabled = false;
UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false;
Task.Run(() =>
{
if (Global.Settings.UseProxyToUpdateSubscription)
{
var mode = new Models.Mode
{
Remark = "ProxyUpdate",
Type = 5
};
MainController = new MainController();
MainController.Start(ServerComboBox.SelectedItem as Models.Server, mode);
}
foreach (var item in Global.Settings.SubscribeLink)
{
using var client = new WebClient();
try
{
if (!string.IsNullOrEmpty(item.UserAgent))
{
client.Headers.Add("User-Agent", item.UserAgent);
}
else
{
client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36");
}
if (Global.Settings.UseProxyToUpdateSubscription)
{
client.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}");
}
var response = client.DownloadString(item.Link);
try
{
response = ShareLink.URLSafeBase64Decode(response);
}
catch (Exception)
{
// 跳过
}
Global.Settings.Server = Global.Settings.Server.Where(server => server.Group != item.Remark).ToList();
var result = ShareLink.Parse(response);
if (result != null)
{
foreach (var x in result)
{
x.Group = item.Remark;
}
Global.Settings.Server.AddRange(result);
NotifyIcon.ShowBalloonTip(5,
UpdateChecker.Name,
string.Format(i18N.Translate("Update {1} server(s) from {0}"), item.Remark, result.Count),
ToolTipIcon.Info);
}
else
{
NotifyIcon.ShowBalloonTip(5,
UpdateChecker.Name,
string.Format(i18N.Translate("Update servers error from {0}"), item.Remark),
ToolTipIcon.Error);
}
}
catch (Exception)
{
}
}
InitServer();
DeleteServerPictureBox.Enabled = true;
if (Global.Settings.UseProxyToUpdateSubscription)
{
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
ControlButton.Text = i18N.Translate("Start");
MainController.Stop();
NatTypeStatusLabel.Text = "";
}
Configuration.Save();
StatusText(i18N.Translate("Subscription updated"));
}).ContinueWith(task => { BeginInvoke(new Action(() => { UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; })); });
NotifyIcon.ShowBalloonTip(5,
UpdateChecker.Name,
i18N.Translate("Updating in the background"),
ToolTipIcon.Info);
}
else
{
MessageBox.Show(i18N.Translate("No subscription link"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
#endregion
#region
private void RestartServiceToolStripMenuItem_Click(object sender, EventArgs e)
{
Enabled = false;
StatusText(i18N.Translate("Restarting service"));
Task.Run(() =>
{
try
{
var service = new ServiceController("netfilter2");
if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
else if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
}
catch (Exception)
{
NFAPI.nf_registerDriver("netfilter2");
}
MessageBox.Show(this, i18N.Translate("Service has been restarted"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
Enabled = true;
});
}
private void UninstallServiceToolStripMenuItem_Click(object sender, EventArgs e)
{
Enabled = false;
StatusText(i18N.Translate("Uninstalling Service"));
Task.Run(() =>
{
var driver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys";
if (File.Exists(driver))
{
try
{
var service = new ServiceController("netfilter2");
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
}
}
catch (Exception)
{
// 跳过
}
try
{
NFAPI.nf_unRegisterDriver("netfilter2");
File.Delete(driver);
MessageBox.Show(this, i18N.Translate("Service has been uninstalled"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(this, i18N.Translate("Error") + i18N.Translate(": ") + ex, i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show(this, i18N.Translate("Service has been uninstalled"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Enabled = true;
});
}
private void ReloadModesToolStripMenuItem_Click(object sender, EventArgs e)
{
Enabled = false;
SaveConfigs();
Task.Run(() =>
{
InitMode();
MessageBox.Show(this, i18N.Translate("Modes have been reload"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
Enabled = true;
});
}
private void CleanDNSCacheToolStripMenuItem_Click(object sender, EventArgs e)
{
Enabled = false;
Task.Run(() =>
{
DNS.Cache.Clear();
MessageBox.Show(this, i18N.Translate("DNS cache cleanup succeeded"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
StatusText(i18N.Translate("DNS cache cleanup succeeded"));
Enabled = true;
});
}
private void OpenDirectoryToolStripMenuItem_Click(object sender, EventArgs e)
{
Utils.Utils.OpenDir(@".\");
}
private void reinstallTapDriverToolStripMenuItem_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
StatusText(i18N.Translate("Reinstalling TUN/TAP driver"));
Enabled = false;
try
{
Configuration.deltapall();
Configuration.addtap();
NotifyIcon.ShowBalloonTip(5,
UpdateChecker.Name, i18N.Translate("Reinstall TUN/TAP driver successfully"),
ToolTipIcon.Info);
}
catch
{
NotifyIcon.ShowBalloonTip(5,
UpdateChecker.Name, i18N.Translate("Reinstall TUN/TAP driver failed"),
ToolTipIcon.Error);
}
finally
{
UpdateStatus(State.Waiting);
Enabled = true;
}
});
}
private void updateACLWithProxyToolStripMenuItem_Click(object sender, EventArgs e)
{
updateACLWithProxyToolStripMenuItem.Enabled = false;
// 当前 ServerComboBox 中至少有一项
if (ServerComboBox.SelectedIndex == -1)
{
MessageBox.Show(i18N.Translate("Please select a server first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false;
ControlButton.Text = "...";
Task.Run(() =>
{
var mode = new Models.Mode
{
Remark = "ProxyUpdate",
Type = 5
};
MainController = new MainController();
MainController.Start(ServerComboBox.SelectedItem as Models.Server, mode);
using var client = new WebClient();
client.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}");
StatusText(i18N.Translate("Updating in the background"));
try
{
client.DownloadFile(Global.Settings.ACL, "bin\\default.acl");
NotifyIcon.ShowBalloonTip(5,
UpdateChecker.Name, i18N.Translate("ACL updated successfully"),
ToolTipIcon.Info);
}
catch (Exception e)
{
Logging.Info("使用代理更新 ACL 失败!" + e.Message);
MessageBox.Show(i18N.Translate("ACL update failed") + "\n" + e.Message);
}
finally
{
UpdateStatus(State.Waiting);
MainController.Stop();
}
});
}
#endregion
private void ExitToolStripButton_Click(object sender, EventArgs e)
{
// 已启动
if (State != State.Waiting && State != State.Stopped)
{
// 未开启自动停止
if (!Global.Settings.StopWhenExited)
{
MessageBox.Show(i18N.Translate("Please press Stop button first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
Visible = true;
ShowInTaskbar = true; // 显示在系统任务栏
WindowState = FormWindowState.Normal; // 还原窗体
NotifyIcon.Visible = true; // 托盘图标隐藏
return;
}
// 自动停止
ControlButton_Click(sender, e);
}
SaveConfigs();
UpdateStatus(State.Terminating);
NotifyIcon.Visible = false;
Close();
Dispose();
Environment.Exit(Environment.ExitCode);
}
private void RelyToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://mega.nz/file/9OQ1EazJ#0pjJ3xt57AVLr29vYEEv15GSACtXVQOGlEOPpi_2Ico");
}
private void VersionLabel_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start($"https://github.com/{UpdateChecker.Owner}/{UpdateChecker.Repo}/releases");
}
private void AboutToolStripButton_Click(object sender, EventArgs e)
{
new AboutForm().Show();
Hide();
}
private void updateACLToolStripMenuItem_Click(object sender, EventArgs e)
{
StatusText(i18N.Translate("Starting update ACL"));
using var client = new WebClient();
client.DownloadFileTaskAsync(Global.Settings.ACL, "bin\\default.acl");
client.DownloadFileCompleted += ((sender, args) =>
{
try
{
if (args.Error == null)
{
NotifyIcon.ShowBalloonTip(5,
UpdateChecker.Name, i18N.Translate("ACL updated successfully"),
ToolTipIcon.Info);
}
else
{
Logging.Info("ACL 更新失败!" + args.Error);
MessageBox.Show(i18N.Translate("ACL update failed") + "\n" + args.Error);
}
}
finally
{
UpdateStatus(State.Waiting);
}
});
}
#endregion
}
}

View File

@@ -0,0 +1,32 @@
using System.ComponentModel;
using System.Windows.Forms;
using Netch.Controllers;
using Netch.Utils;
namespace Netch.Forms
{
/// <summary lang="en">
/// this class is used to disable Designer <para />
/// </summary>
/// <summary lang="zh">
/// 此类用于禁用设计器
/// </summary>
[DesignerCategory("")] public partial class Dummy { }
partial class MainForm
{
private void CheckUpdate()
{
var updater = new UpdateChecker();
updater.NewVersionFound += (o, args) =>
{
NotifyIcon.ShowBalloonTip(5,
UpdateChecker.Name,
$"{i18N.Translate(@"New version available")}{i18N.Translate(@": ")}{updater.LatestVersionNumber}",
ToolTipIcon.Info);
};
updater.Check(false, false);
}
}
}

View File

@@ -0,0 +1,206 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Netch.Models;
using Netch.Utils;
namespace Netch.Forms
{
public partial class Dummy { }
partial class MainForm
{
// init at MainFrom_Load()
private int _sizeHeight;
private int _controlHeight;
private int _profileBoxHeight;
private int _configurationGroupBoxHeight;
private void InitProfile()
{
foreach (var button in ProfileButtons)
{
button.Dispose();
}
ProfileButtons.Clear();
ProfileTable.ColumnStyles.Clear();
ProfileTable.RowStyles.Clear();
var numProfile = Global.Settings.ProfileCount;
if (numProfile == 0)
{
configLayoutPanel.RowStyles[2].SizeType = SizeType.Percent;
configLayoutPanel.RowStyles[2].Height = 0;
ProfileGroupBox.Visible = false;
ConfigurationGroupBox.Size = new Size(ConfigurationGroupBox.Size.Width, _configurationGroupBoxHeight - _controlHeight);
Size = new Size(Size.Width, _sizeHeight - (_controlHeight + _profileBoxHeight));
return;
}
configLayoutPanel.RowStyles[2].SizeType = SizeType.AutoSize;
ProfileGroupBox.Visible = true;
ConfigurationGroupBox.Size = new Size(ConfigurationGroupBox.Size.Width, _configurationGroupBoxHeight);
Size = new Size(Size.Width, _sizeHeight);
ProfileTable.ColumnCount = numProfile;
while (Global.Settings.Profiles.Count < numProfile)
{
Global.Settings.Profiles.Add(new Profile());
}
// buttons
for (var i = 0; i < numProfile; ++i)
{
var b = new Button();
ProfileTable.Controls.Add(b, i, 0);
b.Location = new Point(i * 100, 0);
b.Click += ProfileButton_Click;
b.Dock = DockStyle.Fill;
b.Text = !Global.Settings.Profiles[i].IsDummy ? Global.Settings.Profiles[i].ProfileName : i18N.Translate("None");
ProfileButtons.Add(b);
}
// equal column
for (var i = 1; i <= ProfileTable.RowCount; i++)
{
ProfileTable.RowStyles.Add(new RowStyle(SizeType.Percent, 1));
}
for (var i = 1; i <= ProfileTable.ColumnCount; i++)
{
ProfileTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));
}
}
private string LoadProfile(int index)
{
var p = Global.Settings.Profiles[index];
if (p.IsDummy)
throw new Exception("Profile not found.");
var result = false;
foreach (Models.Server server in ServerComboBox.Items)
{
if (server.Remark.Equals(p.ServerRemark))
{
ServerComboBox.SelectedItem = server;
result = true;
break;
}
}
if (!result)
throw new Exception("Server not found.");
result = false;
foreach (Models.Mode mode in ModeComboBox.Items)
{
if (mode.Remark.Equals(p.ModeRemark))
{
ModeComboBox.SelectedItem = mode;
result = true;
break;
}
}
if (!result)
throw new Exception("Mode not found.");
return p.ProfileName;
}
private void SaveProfile(int index)
{
var selectedServer = (Models.Server) ServerComboBox.SelectedItem;
var selectedMode = (Models.Mode) ModeComboBox.SelectedItem;
var name = ProfileNameText.Text;
Global.Settings.Profiles[index] = new Profile(selectedServer, selectedMode, name);
}
public List<Button> ProfileButtons = new List<Button>();
private void ProfileButton_Click(object sender, EventArgs e)
{
var index = ProfileButtons.IndexOf((Button) sender);
//Utils.Logging.Info(String.Format("Button no.{0} clicked", index));
if (ModifierKeys == Keys.Control)
{
if (ServerComboBox.SelectedIndex == -1)
{
MessageBox.Show(i18N.Translate("Please select a server first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (ModeComboBox.SelectedIndex == -1)
{
MessageBox.Show(i18N.Translate("Please select an mode first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (ProfileNameText.Text == "")
{
MessageBox.Show(i18N.Translate("Please enter a profile name first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SaveProfile(index);
ProfileButtons[index].Text = ProfileNameText.Text;
}
}
else
{
if (ProfileButtons[index].Text == i18N.Translate("Error") || ProfileButtons[index].Text == i18N.Translate("None"))
{
MessageBox.Show(i18N.Translate("No saved profile here. Save a profile first by Ctrl+Click on the button"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
}
try
{
ProfileNameText.Text = LoadProfile(index);
// start the profile
var need2ndStart = true;
if (State == State.Waiting || State == State.Stopped)
{
need2ndStart = false;
}
ControlButton.PerformClick();
if (need2ndStart)
{
Task.Run(() =>
{
while (State != State.Stopped)
{
Thread.Sleep(200);
}
ControlButton.PerformClick();
});
}
}
catch (Exception ee)
{
Task.Run(() =>
{
Logging.Info(ee.Message);
ProfileButtons[index].Text = i18N.Translate("Error");
Thread.Sleep(1200);
ProfileButtons[index].Text = i18N.Translate("None");
});
}
}
}
}
}

View File

@@ -0,0 +1,263 @@
using System;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Netch.Utils;
namespace Netch.Forms
{
public partial class Dummy
{
}
partial class MainForm
{
#region Server
public void TestServer()
{
try
{
Parallel.ForEach(Global.Settings.Server, new ParallelOptions {MaxDegreeOfParallelism = 16},
server => { server.Test(); });
}
catch (Exception)
{
}
}
public void InitServer()
{
ServerComboBox.Items.Clear();
ServerComboBox.Items.AddRange(Global.Settings.Server.ToArray());
// 如果值合法,选中该位置
if (Global.Settings.ServerComboBoxSelectedIndex > 0 &&
Global.Settings.ServerComboBoxSelectedIndex < ServerComboBox.Items.Count)
{
ServerComboBox.SelectedIndex = Global.Settings.ServerComboBoxSelectedIndex;
}
// 如果值非法,且当前 ServerComboBox 中有元素,选择第一个位置
else if (ServerComboBox.Items.Count > 0)
{
ServerComboBox.SelectedIndex = 0;
}
// 如果当前 ServerComboBox 中没元素,不做处理
}
#endregion
#region Mode
public void InitMode()
{
ModeComboBox.Items.Clear();
Global.ModeFiles.Clear();
if (Directory.Exists("mode"))
{
foreach (var name in Directory.GetFiles("mode", "*.txt"))
{
var ok = true;
var mode = new Models.Mode();
using (var sr = new StringReader(File.ReadAllText(name)))
{
var i = 0;
string text;
while ((text = sr.ReadLine()) != null)
{
if (i == 0)
{
var splited = text.Trim().Substring(1).Split(',');
if (splited.Length == 0)
{
ok = false;
break;
}
if (splited.Length >= 1)
{
mode.Remark = i18N.Translate(splited[0].Trim());
}
if (splited.Length >= 2)
{
if (int.TryParse(splited[1], out var result))
{
mode.Type = result;
}
else
{
ok = false;
break;
}
}
if (splited.Length >= 3)
{
if (int.TryParse(splited[2], out var result))
{
mode.BypassChina = result == 1;
}
else
{
ok = false;
break;
}
}
}
else
{
if (!text.StartsWith("#") && !string.IsNullOrWhiteSpace(text))
{
mode.Rule.Add(text.Trim());
}
}
i++;
}
}
if (ok)
{
mode.FileName = Path.GetFileNameWithoutExtension(name);
Global.ModeFiles.Add(mode);
}
}
var array = Global.ModeFiles.ToArray();
Array.Sort(array, (a, b) => string.Compare(a.Remark, b.Remark, StringComparison.Ordinal));
ModeComboBox.Items.AddRange(array);
SelectLastMode();
}
}
public void SelectLastMode()
{
// 如果值合法,选中该位置
if (Global.Settings.ModeComboBoxSelectedIndex > 0 &&
Global.Settings.ModeComboBoxSelectedIndex < ModeComboBox.Items.Count)
{
ModeComboBox.SelectedIndex = Global.Settings.ModeComboBoxSelectedIndex;
}
// 如果值非法,且当前 ModeComboBox 中有元素,选择第一个位置
else if (ModeComboBox.Items.Count > 0)
{
ModeComboBox.SelectedIndex = 0;
}
// 如果当前 ModeComboBox 中没元素,不做处理
}
public void AddMode(Models.Mode mode)
{
ModeComboBox.Items.Clear();
Global.ModeFiles.Add(mode);
var array = Global.ModeFiles.ToArray();
Array.Sort(array, (a, b) => string.Compare(a.Remark, b.Remark, StringComparison.Ordinal));
ModeComboBox.Items.AddRange(array);
SelectLastMode();
}
public void UpdateMode(Models.Mode NewMode, Models.Mode OldMode)
{
ModeComboBox.Items.Clear();
Global.ModeFiles.Remove(OldMode);
Global.ModeFiles.Add(NewMode);
var array = Global.ModeFiles.ToArray();
Array.Sort(array, (a, b) => string.Compare(a.Remark, b.Remark, StringComparison.Ordinal));
ModeComboBox.Items.AddRange(array);
SelectLastMode();
}
#endregion
/// <summary>
/// Init at MainForm_Load()
/// </summary>
private int _eWidth;
private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
if (!(sender is ComboBox cbx))
{
return;
}
// 绘制背景颜色
e.Graphics.FillRectangle(new SolidBrush(Color.White), e.Bounds);
if (e.Index < 0) return;
// 绘制 备注/名称 字符串
e.Graphics.DrawString(cbx.Items[e.Index].ToString(), cbx.Font, new SolidBrush(Color.Black), e.Bounds);
switch (cbx.Items[e.Index])
{
case Models.Server _:
{
var item = cbx.Items[e.Index] as Models.Server;
// 计算延迟底色
SolidBrush brush;
if (item.Delay > 200)
{
// 红色
brush = new SolidBrush(Color.Red);
}
else if (item.Delay > 80)
{
// 黄色
brush = new SolidBrush(Color.Yellow);
}
else if (item.Delay >= 0)
{
// 绿色
brush = new SolidBrush(Color.FromArgb(50, 255, 56));
}
else
{
// 灰色
brush = new SolidBrush(Color.Gray);
}
// 绘制延迟底色
e.Graphics.FillRectangle(brush, _eWidth * 9, e.Bounds.Y, _eWidth, e.Bounds.Height);
// 绘制延迟字符串
e.Graphics.DrawString(item.Delay.ToString(), cbx.Font, new SolidBrush(Color.Black),
_eWidth * 9 + _eWidth / 30, e.Bounds.Y);
break;
}
case Models.Mode _:
{
var item = cbx.Items[e.Index] as Models.Mode;
// 绘制 模式Box 底色
e.Graphics.FillRectangle(new SolidBrush(Color.Gray), _eWidth * 9, e.Bounds.Y, _eWidth,
e.Bounds.Height);
// 绘制 模式行数 字符串
e.Graphics.DrawString(item.Rule.Count.ToString(), cbx.Font, new SolidBrush(Color.Black),
_eWidth * 9 + _eWidth / 30, e.Bounds.Y);
break;
}
}
}
catch (Exception)
{
}
}
}
}

View File

@@ -0,0 +1,108 @@
using Netch.Models;
using Netch.Utils;
namespace Netch.Forms
{
public partial class Dummy
{
}
partial class MainForm
{
/// <summary>
/// 当前状态
/// </summary>
public State State { get; private set; } = State.Waiting;
public void NatTypeStatusText(string text = "")
{
if (State != State.Started)
{
NatTypeStatusLabel.Text = "";
NatTypeStatusLabel.Visible = true;
return;
}
NatTypeStatusLabel.Text = "NAT" + i18N.Translate(": ") +
(!string.IsNullOrEmpty(text) ? text.Trim() : i18N.Translate("Test failed"));
NatTypeStatusLabel.Visible = true;
}
public void StatusText(string text)
{
StatusLabel.Text = i18N.Translate("Status", ": ") + text;
}
/// <summary>
/// Update UI, Status, Status Label
/// </summary>
/// <param name="state"></param>
public void UpdateStatus(State state)
{
State = state;
StatusText(i18N.Translate(StateExtension.GetStatusString(state)));
// TODO 补充
switch (state)
{
case State.Waiting:
ControlButton.Text = i18N.Translate("Start");
ControlButton.Enabled = true;
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
updateACLWithProxyToolStripMenuItem.Enabled = true;
NatTypeStatusText();
break;
case State.Starting:
ControlButton.Text = "...";
ControlButton.Enabled = false;
ServerComboBox.Enabled = false;
ModeComboBox.Enabled = false;
UninstallServiceToolStripMenuItem.Enabled = false;
updateACLWithProxyToolStripMenuItem.Enabled = false;
UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false;
reinstallTapDriverToolStripMenuItem.Enabled = false;
break;
case State.Started:
ControlButton.Text = i18N.Translate("Stop");
ControlButton.Enabled = true;
break;
case State.Stopping:
ControlButton.Enabled = false;
ControlButton.Text = "...";
ProfileGroupBox.Enabled = false;
MenuStrip.Enabled = ConfigurationGroupBox.Enabled = SettingsButton.Enabled = true;
UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = false;
NatTypeStatusText();
break;
case State.Stopped:
ControlButton.Text = i18N.Translate("Start");
ControlButton.Enabled = true;
LastUploadBandwidth = 0;
LastDownloadBandwidth = 0;
ServerComboBox.Enabled = true;
ModeComboBox.Enabled = true;
ProfileGroupBox.Enabled = true;
UninstallServiceToolStripMenuItem.Enabled = true;
updateACLWithProxyToolStripMenuItem.Enabled = true;
UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true;
reinstallTapDriverToolStripMenuItem.Enabled = true;
break;
case State.Terminating:
break;
}
}
public void UpdateStatus()
{
UpdateStatus(State);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -120,51 +120,6 @@
<metadata name="MenuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="EditPictureBox.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAg9JREFUOE+F
U81uElEYnTdw58qFunBlYnwAE7e+QhMT38CNcefWxKWufADjiqqtmsZIW2gRulBTY4fGGGmLMFOGn5aB
GYa5c+f4nTsINKF6kkPIxz3nO/f7Llas1IXXGyW8Wi9h+T/kmVx+C7WGA8F14S2LxR8HdfjDAMMwFI7O
YQg/CI14pVDGKIoGYnDZomMgB4gk0VAqgUoWkxjHMd5t7eCkTz2uWIxGd601nHYPR66H+nH7LKXW9LrG
JBrHeL+9g1N/gDRNl4wBoxHhKDJxmWierPG3lGeiaGrQ9DpPjMFADJjA7fSko4ff0nWerDntWQJegQbl
XRsLEiweZMAEEoFn1opl9GXopa97sFaLlWmCY0nAjo2WdCYn3ednIGvHodPCSIw+2z9hrRQqZoWzBLOu
jBvJ1MlYyARK+LGl8LCWYq+vsyvMttA17oduiwOaru4vEhGvdzSuVRUu2THu7KvJDCYJxrHKugr5XdZk
6kSsgWJXo+pr7J5q3K7GeFoXA75EDuRfYOyCdLZFTDhhipyboDsIv2QGg6H5YREorvSyzgTFH7wEnh8g
0frxdAbn4Vs/xYtmNguK10TcCXjF+JmUrlpvNj/lDxqu2cDZFxialb1sKly0FZYl8oZc4yRK+CYeiPie
8IYlH3dz+W3zuub5Vv5x33/V8VwG9aimsCniMEl92dZN0SxRB+D+Hw2grtB8iuYFAAAAAElFTkSuQmCC
</value>
</data>
<data name="DeletePictureBox.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAglJREFUOE+V
k81PE1EUxUcXunEFS02MJibsZGfc6L6iazGaGI07QXcaE+PClf+ECxP/CKWwUFJqpVNKodAPGgtiW2g7
03am89WZ47mvTdwUhJv8mua9e+7cd995GoAL5Cq5SB42jQ7S+RIWf2TwNZFWxJM6fm4U0GgZN5kzQc6T
KXJNCpwjb/1gMJMt7GBhJY217TJqhy2YPUtRb7aRK1WkULC6WTRdz/9IzQ1yWQqcpXhqJZvHdz2nBEeF
1Xcged/S62CROJeuSIGYfFnEfhCoxONiEIZIskgqt40oimY1nvnzQmIVHcsepfw/+p6HOI+6f9D8oMnA
9HJ1tHXyyFf3kVjbhCbT/tM0hqtOH1G9huigPh7uwR7OqNXpceA6tC/86crgAh/u+zew79+FPXtvPNxz
X88j6nXhOC4WU9lhgV6P5+e53FdzsO7chj0zHit2C87cE0Qd81+BeDKDettUbcFoI9wpIayUx1MusvdD
ldruWspkmjgsW9lTi6eJwl4Ny/oGNNpTXYnNlk4anh9gicPfrTU+aVGEGO2pHDYYhKOUo4Pm4Vspqiuk
qd6JE5/SlsqeyfWtYzuRPBEvpTJi6xdcUlaeJPOO5/liT3l5W5VdtMyu8r5g8M6Lv34roXx5JH5MrkuB
R+QMmWB7l2jP55IkExaBIP+X+VaqtcbLMAynmftgqMOzv61mlBRRiHFjAAAAAElFTkSuQmCC
</value>
</data>
<data name="SpeedPictureBox.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAk9JREFUOE9N
k19ojWEcx8+xsRgzbSlaJouGRCNuXIyMXJ1LpVyYFAttqylEsdKQC1yZpF24oJQrN0q5UJKUlP3DWTPz
pzmm2Tk753nf9+fzfd/3nJ2nPr3v8z7P7/d7nu/v+ybMbBUsA703wwk/sKcPJz07POzszOg8HXAKBr55
bLMmWKOgxZCEBfH7zg9/fev55Gw8G9jUXGC/IFMI7HXGt7ZBZ9fGHNtsJVQogYIrQMFVOc8GLn5x4eby
UWB6k8AXU75N5gJ9aoBkMYGqL4JLbHB9bFRA+Xj207Pr0fcbTNtgC1QWE1TC6t/5INvF0UdmwgqlkZ4N
rJvvP3JBmul6OArSrloJ6mAz3H383bMHE6FApZFlqitpbcbZhTnfWrzA9rNUDfVKsAFSY1Q5+9mFopWP
IU6TGnJ2nrXetCslY6hwnRLo/q1XWVTr/iHwdCECQS2PFpl81IUnBLZ+LNit8bALRyA8gTTY0f816vVl
Klwh2UneH5GweJ53076d5pv04SrSQiLWKkEVbFUl9VtItPYRZ6/iVr7549sx5sORuP2wBw5BgxIsgY2w
DVZAlwLVc21/GwdTWRfvhXp4Cbv1rgRrQRbeDge466xaplO8x5FxMEvWCctBRTpgH4RGWgdyoRZuSyiJ
qcrSIQ5+DgtBCXQCuXAphFaWjfVsnMD752iX2tQ5b6g7sAv0w20CiV50b3gCuTDB1ub7mGjvYCF0XRzc
B/rrGkGqK7BYVI5MaFILSdzVdI9WHuRvG42Cj4Mc2g4toEJKUAMZSJlZzX8eY2N9ZDKIEQAAAABJRU5E
rkJggg==
</value>
</data>
<metadata name="StatusStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>122, 17</value>
</metadata>
@@ -174,6 +129,7 @@
<metadata name="NotifyMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>393, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="NotifyIcon.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAMAAAAAAAEAIAAoIAQANgAAAEBAAAABACAAKEIAAF4gBAAgIAAAAQAgAKgQAACGYgQAKAAAAAAB

View File

@@ -104,7 +104,7 @@ namespace Netch.Forms
AclAddr.Text = Global.Settings.ACL;
LanguageLabel.Text = i18N.Translate(LanguageLabel.Text);
LanguageComboBox.Items.AddRange(i18N.TranslatesList.ToArray());
LanguageComboBox.Items.AddRange(i18N.GetTranslateList().ToArray());
LanguageComboBox.SelectedItem = Global.Settings.Language;
}

View File

@@ -36,4 +36,21 @@
/// </summary>
Terminating
}
}
public static class StateExtension
{
public static string GetStatusString(State state)
{
return state switch
{
State.Started => state.ToString(),
State.Stopping => state.ToString(),
State.Stopped => state.ToString(),
State.Terminating => state.ToString(),
State.Starting => state.ToString(),
State.Waiting => "Waiting for command",
_ => ""
};
}
}
}

View File

@@ -24,7 +24,7 @@
"Starting NatTester": "正在启动 NAT 测试",
"Starting LocalDns service": "正在启动本地 DNS 服务",
"Starting Redirector": "正在启动 Redirector",
"ReStarting Redirector": "正常启动失败,正在尝试重新启动 Redirector",
"Restarting Redirector": "正常启动失败,正在尝试重新启动 Redirector",
"Starting netfilter2 Service": "正在启动 netfilter2 服务",
"Starting dns Service": "正在启动 DNS 服务",
"SetupBypass": "设置绕行规则",

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Netch.Properties;
using Newtonsoft.Json;
@@ -10,21 +11,7 @@ namespace Netch.Utils
{
public static class i18N
{
static i18N()
{
TranslatesList = new List<string> {"System", "zh-CN", "en-US"};
if (!Directory.Exists("i18n")) return;
foreach (var fileName in Directory.GetFiles("i18n", "*"))
{
TranslatesList.Add(fileName.Substring(5));
}
}
/// <summary>
/// 可用语言列表
/// </summary>
public static List<string> TranslatesList { get; }
/// <summary>
/// 数据
/// </summary>
@@ -38,14 +25,15 @@ namespace Netch.Utils
/// <param name="langCode">语言代码</param>
public static void Load(string langCode)
{
LangCode = langCode;
var text = "";
if (langCode.Equals("System"))
{
// 加载系统语言
langCode = CultureInfo.CurrentCulture.Name;
}
LangCode = langCode;
if (langCode == "zh-CN")
@@ -83,12 +71,23 @@ namespace Netch.Utils
/// <returns>翻译完毕的文本</returns>
public static string Translate(string text)
{
if (Data.Contains(text))
{
return Data[text].ToString();
}
return Data.Contains(text) ? Data[text].ToString() : text;
}
public static string Translate(params string[] text)
{
var a = new StringBuilder();
foreach (var t in text)
a.Append(Data.Contains(t) ? Data[t].ToString() : t);
return a.ToString();
}
public static List<string> GetTranslateList()
{
var translateFile = new List<string> {"System", "zh-CN", "en-US"};
return text;
if (!Directory.Exists("i18n")) return translateFile;
translateFile.AddRange(Directory.GetFiles("i18n", "*").Select(fileName => fileName.Substring(5)));
return translateFile;
}
}
}
}