diff --git a/Netch/Controllers/DNSController.cs b/Netch/Controllers/DNSController.cs index 38de1625..0c4ab3f0 100644 --- a/Netch/Controllers/DNSController.cs +++ b/Netch/Controllers/DNSController.cs @@ -19,7 +19,7 @@ namespace Netch.Controllers /// 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")) diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index fb023f63..fcfb6303 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -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); diff --git a/Netch/Controllers/NFController.cs b/Netch/Controllers/NFController.cs index 6b2e42d2..ad9071b6 100644 --- a/Netch/Controllers/NFController.cs +++ b/Netch/Controllers/NFController.cs @@ -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) diff --git a/Netch/Controllers/NTTController.cs b/Netch/Controllers/NTTController.cs index 637c4c42..eb9b19c7 100644 --- a/Netch/Controllers/NTTController.cs +++ b/Netch/Controllers/NTTController.cs @@ -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")) diff --git a/Netch/Controllers/SSController.cs b/Netch/Controllers/SSController.cs index 674bfbae..b3bbb4c0 100644 --- a/Netch/Controllers/SSController.cs +++ b/Netch/Controllers/SSController.cs @@ -29,7 +29,7 @@ namespace Netch.Controllers /// 是否启动成功 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 diff --git a/Netch/Controllers/SSRController.cs b/Netch/Controllers/SSRController.cs index d2475b46..ba243802 100644 --- a/Netch/Controllers/SSRController.cs +++ b/Netch/Controllers/SSRController.cs @@ -28,7 +28,7 @@ namespace Netch.Controllers /// 是否启动成功 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"); diff --git a/Netch/Controllers/TUNTAPController.cs b/Netch/Controllers/TUNTAPController.cs index f9cead05..ce54400d 100644 --- a/Netch/Controllers/TUNTAPController.cs +++ b/Netch/Controllers/TUNTAPController.cs @@ -69,7 +69,7 @@ namespace Netch.Controllers /// 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 /// 是否成功 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 diff --git a/Netch/Controllers/TrojanController.cs b/Netch/Controllers/TrojanController.cs index 96a191fb..c49420d0 100644 --- a/Netch/Controllers/TrojanController.cs +++ b/Netch/Controllers/TrojanController.cs @@ -30,7 +30,7 @@ namespace Netch.Controllers /// 是否启动成功 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")) diff --git a/Netch/Controllers/VMessController.cs b/Netch/Controllers/VMessController.cs index 68e555b6..3689aebf 100644 --- a/Netch/Controllers/VMessController.cs +++ b/Netch/Controllers/VMessController.cs @@ -31,7 +31,7 @@ namespace Netch.Controllers /// 是否启动成功 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; diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index bd8fe2ea..5bb6c379 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Net; using System.ServiceProcess; +using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; @@ -26,7 +27,7 @@ namespace Netch.Forms /// /// 当前状态 /// - public State State = State.Waiting; + public State State { get; private set; } = State.Waiting; /// /// 主控制器 @@ -108,10 +109,8 @@ namespace Netch.Forms { try { - Parallel.ForEach(Global.Settings.Server, new ParallelOptions { MaxDegreeOfParallelism = 16 }, server => - { - server.Test(); - }); + Parallel.ForEach(Global.Settings.Server, new ParallelOptions {MaxDegreeOfParallelism = 16}, + server => { server.Test(); }); } catch (Exception) { @@ -171,14 +170,6 @@ namespace Netch.Forms UsedBandwidthLabel.Text = i18N.Translate("Used: 0 KB"); DownloadSpeedLabel.Text = i18N.Translate("↓: 0 KB/s"); UploadSpeedLabel.Text = i18N.Translate("↑: 0 KB/s"); - - if (!isStarted) - { - UsedBandwidthLabel.Text = $@"{i18N.Translate("Used")}{i18N.Translate(": ")}0 KB"; - StatusLabel.Text = $@"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Waiting for command")}"; - ControlButton.Text = i18N.Translate("Start"); - } - NotifyIcon.Text = i18N.Translate("Netch"); ShowMainFormToolStripButton.Text = i18N.Translate("Show"); ExitToolStripButton.Text = i18N.Translate("Exit"); @@ -186,6 +177,9 @@ namespace Netch.Forms ProfileGroupBox.Text = i18N.Translate("Profiles"); // 加载翻译 + UsedBandwidthLabel.Text = $@"{i18N.Translate("Used")}{i18N.Translate(": ")}0 KB"; + UpdateStatus(); + VersionLabel.Text = UpdateChecker.Version; } @@ -303,6 +297,7 @@ namespace Netch.Forms SelectLastMode(); } + public void UpdateMode(Models.Mode NewMode, Models.Mode OldMode) { ModeComboBox.Items.Clear(); @@ -320,7 +315,6 @@ namespace Netch.Forms Global.Settings.ServerComboBoxSelectedIndex = ServerComboBox.SelectedIndex; if (ModeComboBox.Items.Count != 0 && ModeComboBox.SelectedItem != null) { - if (ModeComboBox.Tag is object[] list) { Global.Settings.ModeComboBoxSelectedIndex = list.ToList().IndexOf(ModeComboBox.SelectedItem); @@ -337,7 +331,6 @@ namespace Netch.Forms { try { - var cbx = sender as ComboBox; var eWidth = ServerComboBox.Width / 10; @@ -566,13 +559,14 @@ namespace Netch.Forms 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) { - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting update subscription")}"; + StatusText(i18N.Translate("Starting update subscription")); DeletePictureBox.Enabled = false; UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false; @@ -588,6 +582,7 @@ namespace Netch.Forms MainController = new MainController(); MainController.Start(ServerComboBox.SelectedItem as Models.Server, mode); } + foreach (var item in Global.Settings.SubscribeLink) { using var client = new WebClient(); @@ -627,6 +622,7 @@ namespace Netch.Forms { x.Group = item.Remark; } + Global.Settings.Server.AddRange(result); NotifyIcon.ShowBalloonTip(5, UpdateChecker.Name, @@ -656,7 +652,7 @@ namespace Netch.Forms NatTypeStatusLabel.Text = ""; } Configuration.Save(); - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Subscription updated")}"; + StatusText(i18N.Translate("Subscription updated")); }).ContinueWith(task => { BeginInvoke(new Action(() => @@ -679,7 +675,7 @@ namespace Netch.Forms private void RestartServiceToolStripMenuItem_Click(object sender, EventArgs e) { Enabled = false; - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Restarting service")}"; + StatusText(i18N.Translate("Restarting service")); Task.Run(() => { @@ -712,7 +708,7 @@ namespace Netch.Forms private void UninstallServiceToolStripMenuItem_Click(object sender, EventArgs e) { Enabled = false; - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Uninstalling Service")}"; + StatusText(i18N.Translate("Uninstalling Service")); Task.Run(() => { @@ -776,7 +772,7 @@ namespace Netch.Forms DNS.Cache.Clear(); MessageBox.Show(this, i18N.Translate("DNS cache cleanup succeeded"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("DNS cache cleanup succeeded")}"; + StatusText(i18N.Translate("DNS cache cleanup succeeded")); Enabled = true; }); } @@ -846,14 +842,14 @@ namespace Netch.Forms private void SpeedPictureBox_Click(object sender, EventArgs e) { Enabled = false; - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Testing")}"; + StatusText(i18N.Translate("Testing")); Task.Run(() => { TestServer(); Enabled = true; - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Test done")}"; + StatusText(i18N.Translate("Test done")); Refresh(); Configuration.Save(); }); @@ -865,19 +861,19 @@ namespace Netch.Forms ModeComboBox.Select(0, 0); ControlFun(); } + public void ControlFun() { SaveConfigs(); if (State == State.Waiting || State == State.Stopped) { - // 当前 ServerComboBox 中至少有一项 + // 服务器、模式 需选择 if (ServerComboBox.SelectedIndex == -1) { MessageBox.Show(i18N.Translate("Please select a server first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); return; } - // 当前 ModeComboBox 中至少有一项 if (ModeComboBox.SelectedIndex == -1) { MessageBox.Show(i18N.Translate("Please select an mode first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); @@ -886,21 +882,7 @@ namespace Netch.Forms //MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false; - //关闭启动按钮 - ControlButton.Enabled = false; - - //关闭部分选项功能 - RestartServiceToolStripMenuItem.Enabled = false; - UninstallServiceToolStripMenuItem.Enabled = false; - updateACLWithProxyToolStripMenuItem.Enabled = false; - UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false; - reinstallTapDriverToolStripMenuItem.Enabled = false; - ServerComboBox.Enabled = false; - ModeComboBox.Enabled = false; - - ControlButton.Text = "..."; - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting")}"; - State = State.Starting; + UpdateStatus(State.Starting); Task.Run(() => { @@ -948,51 +930,41 @@ namespace Netch.Forms Hide(); } - ControlButton.Enabled = true; - ControlButton.Text = i18N.Translate("Stop"); - - if (mode.Type != 3 && mode.Type != 5) + var text = new StringBuilder(" ("); + text.Append(Global.Settings.LocalAddress == "0.0.0.0" ? i18N.Translate("Allow other Devices to connect") + " " : ""); + if (server.Type == "Socks5") { - if (server.Type != "Socks5") + // 不可控Socks5 + if (mode.Type == 3 && mode.Type == 5) { - if (Global.Settings.LocalAddress == "0.0.0.0") - { - - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Started")} ({i18N.Translate("Allow other Devices to connect")} Socks5 {i18N.Translate("Local Port")}{i18N.Translate(": ")}{Global.Settings.Socks5LocalPort})"; - } - else - { - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Started")} (Socks5 {i18N.Translate("Local Port")}{i18N.Translate(": ")}{Global.Settings.Socks5LocalPort}{")"}"; - } + // 可控HTTP + text.Append($"HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}"); } else { - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Started")}"; + // 不可控HTTP + text.Clear(); } } else { - if (server.Type != "Socks5") + // 可控Socks5 + text.Append($"Socks5 {i18N.Translate("Local Port", ": ")}{Global.Settings.Socks5LocalPort}"); + if (mode.Type == 3 || mode.Type == 5) { - if (Global.Settings.LocalAddress == "0.0.0.0") - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Started")} ({i18N.Translate("Allow other Devices to connect")} Socks5 {i18N.Translate("Local Port")}{i18N.Translate(": ")}{Global.Settings.Socks5LocalPort} | HTTP {i18N.Translate("Local Port")}{i18N.Translate(": ")}{Global.Settings.HTTPLocalPort}{")"}"; - else - { - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Started")} (Socks5 {i18N.Translate("Local Port")}{i18N.Translate(": ")}{Global.Settings.Socks5LocalPort} | HTTP {i18N.Translate("Local Port")}{i18N.Translate(": ")}{Global.Settings.HTTPLocalPort})"; - } - } - else - { - if (Global.Settings.LocalAddress == "0.0.0.0") - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Started")} ({i18N.Translate("Allow other Devices to connect")} HTTP {i18N.Translate("Local Port")}{i18N.Translate(": ")}{Global.Settings.HTTPLocalPort}{")"}"; - else - { - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Started")} (HTTP {i18N.Translate("Local Port")}{i18N.Translate(": ")}{Global.Settings.HTTPLocalPort})"; - } + //有HTTP + text.Append($" | HTTP {i18N.Translate("Local Port", ": ")}{Global.Settings.HTTPLocalPort}"); } } - State = State.Started; + if (text.Length > 0) + { + text.Append(")"); + } + + UpdateStatus(State.Started); + StatusText(i18N.Translate(StateExtension.GetStatusString(State)) + text); + if (Global.Settings.StartedTcping) { // 自动检测延迟 @@ -1018,31 +990,15 @@ namespace Netch.Forms } else { - MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true; - - RestartServiceToolStripMenuItem.Enabled = true; - UninstallServiceToolStripMenuItem.Enabled = true; - updateACLWithProxyToolStripMenuItem.Enabled = true; - UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; - reinstallTapDriverToolStripMenuItem.Enabled = true; - ServerComboBox.Enabled = true; - ModeComboBox.Enabled = true; - //隐藏NTT测试 - NatTypeStatusLabel.Visible = false; - - ControlButton.Text = i18N.Translate("Start"); - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Start failed")}"; - State = State.Stopped; + UpdateStatus(State.Stopped); + StatusText(i18N.Translate("Start Failed")); } }); } else { - - ControlButton.Enabled = false; - ControlButton.Text = "..."; - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Stopping")}"; - State = State.Stopping; + // 停止 + UpdateStatus(State.Stopping); MenuStrip.Enabled = ConfigurationGroupBox.Enabled = SettingsButton.Enabled = true; @@ -1056,29 +1012,7 @@ namespace Netch.Forms MainController.Stop(); NatTypeStatusLabel.Text = ""; - 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 = false; - - ControlButton.Enabled = true; - ProfileGroupBox.Enabled = true; - - RestartServiceToolStripMenuItem.Enabled = true; - UninstallServiceToolStripMenuItem.Enabled = true; - updateACLWithProxyToolStripMenuItem.Enabled = true; - UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; - reinstallTapDriverToolStripMenuItem.Enabled = true; - ServerComboBox.Enabled = true; - ModeComboBox.Enabled = true; - //隐藏NTT测试 - NatTypeStatusLabel.Visible = false; - - ControlButton.Text = i18N.Translate("Start"); - StatusLabel.Text = $"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Stopped")}"; - State = State.Stopped; + UpdateStatus(State.Stopped); TestServer(); }); @@ -1259,10 +1193,7 @@ namespace Netch.Forms ProfileButtons[index].Text = i18N.Translate("None"); }); } - } - - } // init at MainFrom_Load() @@ -1451,10 +1382,8 @@ namespace Netch.Forms MessageBox.Show(i18N.Translate("Please select a server first"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); } } - public void StatusText(string text) - { - StatusLabel.Text = text; - } + + public void NatTypeStatusText(string text) { NatTypeStatusLabel.Visible = true; @@ -1471,23 +1400,23 @@ namespace Netch.Forms private void exitToolStripMenuItem_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; // 托盘图标隐藏 + ShowInTaskbar = true; + WindowState = FormWindowState.Normal; + NotifyIcon.Visible = true; return; } - // 否则直接调用停止按钮的方法 + // 自动停止 ControlButton_Click(sender, e); } @@ -1502,7 +1431,7 @@ namespace Netch.Forms private void updateACLToolStripMenuItem_Click(object sender, EventArgs e) { - StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Starting update ACL")}"); + StatusText(i18N.Translate("Starting update ACL")); using var client = new WebClient(); client.DownloadFileTaskAsync(Global.Settings.ACL, "bin\\default.acl"); @@ -1525,7 +1454,7 @@ namespace Netch.Forms } finally { - StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Waiting for command")}"); + UpdateStatus(State.Waiting); } }); } @@ -1559,7 +1488,7 @@ namespace Netch.Forms client.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}"); - StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Updating in the background")}"); + StatusText(i18N.Translate("Updating in the background")); try { client.DownloadFile(Global.Settings.ACL, "bin\\default.acl"); @@ -1577,8 +1506,7 @@ namespace Netch.Forms updateACLWithProxyToolStripMenuItem.Enabled = true; MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true; - ControlButton.Text = i18N.Translate("Start"); - StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Waiting for command")}"); + UpdateStatus(State.Waiting); MainController.Stop(); } }); @@ -1588,7 +1516,7 @@ namespace Netch.Forms { Task.Run(() => { - StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Reinstalling TUN/TAP driver")}"); + StatusText(i18N.Translate("Reinstalling TUN/TAP driver")); Enabled = false; try { @@ -1606,8 +1534,7 @@ namespace Netch.Forms } finally { - ControlButton.Text = i18N.Translate("Start"); - StatusText($"{i18N.Translate("Status")}{i18N.Translate(": ")}{i18N.Translate("Waiting for command")}"); + UpdateStatus(State.Waiting); Enabled = true; } }); @@ -1623,14 +1550,84 @@ namespace Netch.Forms if (!Visible) return; - if (i18N.LangCode!=Global.Settings.Language) + if (i18N.LangCode != Global.Settings.Language) { i18N.Load(Global.Settings.Language); InitText(State == State.Started); + InitProfile(); } if (ProfileButtons.Count != Global.Settings.ProfileCount) InitProfile(); } + + public void StatusText(string text) + { + StatusLabel.Text = i18N.Translate("Status", ": ") + text; + } + + public void UpdateStatus(State state) + { + switch (state) + { + 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.Waiting: + ControlButton.Text = i18N.Translate("Start"); + ControlButton.Enabled = true; + break; + case State.Started: + ControlButton.Text = i18N.Translate("Stop"); + ControlButton.Enabled = true; + break; + case State.Stopping: + NatTypeStatusLabel.Visible = false; + UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = false; + + ControlButton.Enabled = false; + ControlButton.Text = "..."; + break; + case State.Stopped: + LastUploadBandwidth = 0; + LastDownloadBandwidth = 0; + + UploadSpeedLabel.Text = "↑: 0 KB/s"; + DownloadSpeedLabel.Text = "↓: 0 KB/s"; + UsedBandwidthLabel.Text = $"{i18N.Translate("Used")}{i18N.Translate(": ")}0 KB"; + + ServerComboBox.Enabled = true; + ModeComboBox.Enabled = true; + ControlButton.Text = i18N.Translate("Start"); + ControlButton.Enabled = true; + ProfileGroupBox.Enabled = true; + + UninstallServiceToolStripMenuItem.Enabled = true; + updateACLWithProxyToolStripMenuItem.Enabled = true; + UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; + reinstallTapDriverToolStripMenuItem.Enabled = true; + break; + case State.Terminating: + + break; + } + + State = state; + StatusText(i18N.Translate(StateExtension.GetStatusString(state))); + } + + public void UpdateStatus() + { + UpdateStatus(State); + } } -} +} \ No newline at end of file diff --git a/Netch/Models/State.cs b/Netch/Models/State.cs index b313c69c..a284008c 100644 --- a/Netch/Models/State.cs +++ b/Netch/Models/State.cs @@ -36,4 +36,21 @@ /// 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", + _ => "" + }; + } + } +} \ No newline at end of file