mirror of
https://github.com/netchx/netch.git
synced 2026-05-11 23:45:06 +08:00
基本完成重构 Controller
This commit is contained in:
@@ -36,7 +36,7 @@ namespace Netch.Forms
|
||||
//MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false;
|
||||
|
||||
UpdateStatus(State.Starting);
|
||||
|
||||
|
||||
Firewall.AddNetchFwRules();
|
||||
|
||||
Task.Run(() =>
|
||||
@@ -44,7 +44,7 @@ namespace Netch.Forms
|
||||
var server = ServerComboBox.SelectedItem as Models.Server;
|
||||
var mode = ModeComboBox.SelectedItem as Models.Mode;
|
||||
|
||||
MainController = new MainController();
|
||||
MainController ??= new MainController();
|
||||
|
||||
var startResult = MainController.Start(server, mode);
|
||||
|
||||
@@ -94,7 +94,7 @@ namespace Netch.Forms
|
||||
if (server.Type == "Socks5")
|
||||
{
|
||||
// 不可控Socks5
|
||||
if (mode.Type == 3 && mode.Type == 5)
|
||||
if (mode.Type == 3 || mode.Type == 5)
|
||||
{
|
||||
// 可控HTTP
|
||||
text.Append(
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.ServiceProcess;
|
||||
@@ -18,7 +17,9 @@ using WebClient = Netch.Override.WebClient;
|
||||
namespace Netch.Forms
|
||||
{
|
||||
partial class Dummy
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
partial class MainForm
|
||||
{
|
||||
#region MenuStrip
|
||||
@@ -160,7 +161,7 @@ namespace Netch.Forms
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// 跳过
|
||||
// ignored
|
||||
}
|
||||
|
||||
Global.Settings.Server = Global.Settings.Server.Where(server => server.Group != item.Remark).ToList();
|
||||
@@ -261,39 +262,18 @@ namespace Netch.Forms
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
var driver = $"{Environment.SystemDirectory}\\drivers\\netfilter2.sys";
|
||||
if (File.Exists(driver))
|
||||
try
|
||||
{
|
||||
try
|
||||
if (new NFController().UninstallDriver())
|
||||
{
|
||||
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);
|
||||
|
||||
MessageBoxX.Show(i18N.Translate("Service has been uninstalled"), owner: this);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBoxX.Show(i18N.Translate("Error") + i18N.Translate(": ") + ex, info: false, owner: this);
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception e)
|
||||
{
|
||||
MessageBoxX.Show(i18N.Translate("Service has been uninstalled"), owner: this);
|
||||
MessageBox.Show(i18N.Translate("Error", e.Message));
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
|
||||
Enabled = true;
|
||||
@@ -398,8 +378,8 @@ namespace Netch.Forms
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Info("使用代理更新 ACL 失败!" + e.Message);
|
||||
MessageBoxX.Show(i18N.Translate("ACL update failed") + "\n" + e.Message);
|
||||
Logging.Error("使用代理更新 ACL 失败!" + e);
|
||||
MessageBoxX.Show(i18N.Translate("ACL update failed") + "\n" + e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -414,17 +394,7 @@ namespace Netch.Forms
|
||||
|
||||
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (State != State.Waiting && State != State.Stopped)
|
||||
{
|
||||
ControlButton_Click(sender, e);
|
||||
}
|
||||
SaveConfigs();
|
||||
|
||||
UpdateStatus(State.Terminating);
|
||||
NotifyIcon.Visible = false;
|
||||
Close();
|
||||
Dispose();
|
||||
Environment.Exit(Environment.ExitCode);
|
||||
Exit(true);
|
||||
}
|
||||
|
||||
private void RelyToolStripMenuItem_Click(object sender, EventArgs e)
|
||||
@@ -461,7 +431,7 @@ namespace Netch.Forms
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Info("ACL 更新失败!" + args.Error);
|
||||
Logging.Error("ACL 更新失败!" + args.Error);
|
||||
MessageBoxX.Show(i18N.Translate("ACL update failed") + "\n" + args.Error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Netch.Forms
|
||||
partial class MainForm
|
||||
{
|
||||
|
||||
// init at MainFrom_Load()
|
||||
/// init at <see cref="MainForm_Load"/>
|
||||
private int _sizeHeight;
|
||||
private int _controlHeight;
|
||||
private int _profileBoxHeight;
|
||||
|
||||
@@ -148,7 +148,7 @@ namespace Netch.Forms
|
||||
// 如果勾选了关闭时退出,自动点击退出按钮
|
||||
else
|
||||
{
|
||||
ExitToolStripButton.PerformClick();
|
||||
Exit(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -414,35 +414,52 @@ namespace Netch.Forms
|
||||
Activate();
|
||||
}
|
||||
|
||||
private void ExitToolStripButton_Click(object sender, EventArgs e)
|
||||
private void Exit(bool forceExit = false)
|
||||
{
|
||||
// 已启动
|
||||
if (State != State.Waiting && State != State.Stopped)
|
||||
{
|
||||
// 未开启自动停止
|
||||
if (!Global.Settings.StopWhenExited)
|
||||
if (forceExit)
|
||||
ControlFun();
|
||||
else
|
||||
{
|
||||
MessageBoxX.Show(i18N.Translate("Please press Stop button first"));
|
||||
if (!Global.Settings.StopWhenExited)
|
||||
{
|
||||
// 未开启自动停止
|
||||
MessageBoxX.Show(i18N.Translate("Please press Stop button first"));
|
||||
|
||||
Visible = true;
|
||||
ShowInTaskbar = true; // 显示在系统任务栏
|
||||
WindowState = FormWindowState.Normal; // 还原窗体
|
||||
NotifyIcon.Visible = true; // 托盘图标隐藏
|
||||
Visible = true;
|
||||
ShowInTaskbar = true; // 显示在系统任务栏
|
||||
WindowState = FormWindowState.Normal; // 还原窗体
|
||||
NotifyIcon.Visible = true; // 托盘图标隐藏
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 自动停止
|
||||
|
||||
ControlButton_Click(sender, e);
|
||||
}
|
||||
|
||||
SaveConfigs();
|
||||
|
||||
UpdateStatus(State.Terminating);
|
||||
NotifyIcon.Visible = false;
|
||||
Close();
|
||||
Dispose();
|
||||
Environment.Exit(Environment.ExitCode);
|
||||
Hide();
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
for (var i = 0; i < 16; i++)
|
||||
{
|
||||
if (State == State.Waiting || State == State.Stopped)
|
||||
break;
|
||||
Thread.Sleep(250);
|
||||
}
|
||||
|
||||
SaveConfigs();
|
||||
UpdateStatus(State.Terminating);
|
||||
Dispose();
|
||||
Environment.Exit(Environment.ExitCode);
|
||||
});
|
||||
}
|
||||
|
||||
private void ExitToolStripButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Exit();
|
||||
}
|
||||
|
||||
private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
|
||||
|
||||
Reference in New Issue
Block a user