private void UninstallServiceToolStripMenuItem_Click(object sender, EventArgs e) { Enabled = false; StatusText(i18N.Translate("Uninstalling Service")); Task.Run(() => { try { if (NFController.UninstallDriver()) { MessageBoxX.Show(i18N.Translate("Service has been uninstalled"), owner: this); } } catch (Exception e) { MessageBoxX.Show(e.ToString(), info: false); Console.WriteLine(e); throw; } StatusText(i18N.Translate(StateExtension.GetStatusString(State.Waiting))); Enabled = true; }); }
// static IDictionary <Enum, HashSet <Transition> > BuildTransitions(TypeBuilder typeBuilder, Type machineType, Type stateType) { var methods = StateMethods.GetStateMethods(machineType); var transitions = new Dictionary <Enum, HashSet <Transition> >(7); for (int i = 0; i < methods.Length; i++) { Enum state = (Enum)StateExtension.GetStateCore(methods[i], stateType); HashSet <Transition> actions; if (!transitions.TryGetValue(state, out actions)) { actions = new HashSet <Transition>(); transitions.Add(state, actions); } var info = new Transition(methods[i].MetadataToken); if (!actions.Contains(info)) { info.Method = CreateTransition(typeBuilder, methods[i]); actions.Add(info); } } return(transitions); }
public void ControlFun() { SaveConfigs(); if (State == State.Waiting || State == State.Stopped) { // 服务器、模式 需选择 if (ServerComboBox.SelectedIndex == -1) { MessageBoxX.Show(i18N.Translate("Please select a server first")); return; } if (ModeComboBox.SelectedIndex == -1) { MessageBoxX.Show(i18N.Translate("Please select an mode first")); return; } //MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false; UpdateStatus(State.Starting); Firewall.AddNetchFwRules(); 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(() => { UpdateStatus(State.Started); StatusText(i18N.Translate(StateExtension.GetStatusString(State)) + PortText(server.Type, mode.Type)); LastUploadBandwidth = 0; //LastDownloadBandwidth = 0; //UploadSpeedLabel.Text = "↑: 0 KB/s"; DownloadSpeedLabel.Text = "↑↓: 0 KB/s"; UsedBandwidthLabel.Text = $"{i18N.Translate("Used",": ")}0 KB"; UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = true; UploadSpeedLabel.Visible = false; Bandwidth.NetTraffic(server, mode, MainController); }); // 如果勾选启动后最小化 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(); } 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); MainController.Stop(); UpdateStatus(State.Stopped); Task.Run(() => { TestServer(); }); } }
public void ControlFun() { SaveConfigs(); if (State == State.Waiting || State == State.Stopped) { // 服务器、模式 需选择 if (ServerComboBox.SelectedIndex == -1) { MessageBoxX.Show(i18N.Translate("Please select a server first")); return; } if (ModeComboBox.SelectedIndex == -1) { MessageBoxX.Show(i18N.Translate("Please select an mode first")); return; } //MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false; UpdateStatus(State.Starting); Firewall.AddNetchFwRules(); 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",": ")}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(); }); } }
/// <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.Enabled = true; ControlButton.Text = i18N.Translate("Start"); MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true; updateACLWithProxyToolStripMenuItem.Enabled = true; NatTypeStatusText(); break; case State.Starting: ControlButton.Enabled = false; ControlButton.Text = "..."; ServerComboBox.Enabled = false; ModeComboBox.Enabled = false; UninstallServiceToolStripMenuItem.Enabled = false; updateACLWithProxyToolStripMenuItem.Enabled = false; UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false; reinstallTapDriverToolStripMenuItem.Enabled = false; break; case State.Started: ControlButton.Enabled = true; ControlButton.Text = i18N.Translate("Stop"); 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.Enabled = true; ControlButton.Text = i18N.Translate("Start"); 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 Test01_GetStateType() { Assert.AreEqual(typeof(int), StateExtension.GetStateType(typeof(IntMachine))); Assert.AreEqual(typeof(State), StateExtension.GetStateType(typeof(EnumMachine))); }
/// <summary> /// 更新 UI, 状态栏文本, 状态 /// </summary> /// <param name="state"></param> /// <param name="text"></param> private void UpdateStatus(State state, string text = "") { State = state; StatusText(text == "" ? i18N.Translate(StateExtension.GetStatusString(state)) : text); void MenuStripsEnabled(bool enabled) { // 需要禁用的菜单项 UninstallServiceToolStripMenuItem.Enabled = updateACLWithProxyToolStripMenuItem.Enabled = UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = reinstallTapDriverToolStripMenuItem.Enabled = enabled; } // TODO 补充 switch (state) { case State.Waiting: ControlButton.Enabled = true; ControlButton.Text = i18N.Translate("Start"); break; case State.Starting: ControlButton.Enabled = false; ControlButton.Text = "..."; ConfigurationGroupBox.Enabled = false; MenuStripsEnabled(false); break; case State.Started: ControlButton.Enabled = true; ControlButton.Text = i18N.Translate("Stop"); LastUploadBandwidth = 0; //LastDownloadBandwidth = 0; //UploadSpeedLabel.Text = "↑: 0 KB/s"; DownloadSpeedLabel.Text = @"↑↓: 0 KB/s"; UsedBandwidthLabel.Text = $@"{i18N.Translate("Used", ": ")}0 KB"; UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = true; break; case State.Stopping: ControlButton.Enabled = false; ControlButton.Text = "..."; ProfileGroupBox.Enabled = false; UsedBandwidthLabel.Visible /*= UploadSpeedLabel.Visible*/ = DownloadSpeedLabel.Visible = false; NatTypeStatusText(); break; case State.Stopped: ControlButton.Enabled = true; ControlButton.Text = i18N.Translate("Start"); LastUploadBandwidth = 0; LastDownloadBandwidth = 0; ProfileGroupBox.Enabled = true; ConfigurationGroupBox.Enabled = true; MenuStripsEnabled(true); break; case State.Terminating: break; } }
private void ControlFun() { //防止模式选择框变成蓝色:D ModeComboBox.Select(0, 0); if (State == State.Waiting || State == State.Stopped) { // 服务器、模式 需选择 if (ServerComboBox.SelectedIndex == -1) { MessageBoxX.Show(i18N.Translate("Please select a server first")); return; } if (ModeComboBox.SelectedIndex == -1) { MessageBoxX.Show(i18N.Translate("Please select an mode first")); return; } UpdateStatus(State.Starting); Task.Run(() => { Task.Run(Firewall.AddNetchFwRules); var server = ServerComboBox.SelectedItem as Models.Server; var mode = ModeComboBox.SelectedItem as Models.Mode; if (_mainController.Start(server, mode)) { Task.Run(() => { UpdateStatus(State.Started, i18N.Translate(StateExtension.GetStatusString(State.Started)) + PortText(server.Type, mode.Type)); Bandwidth.NetTraffic(server, mode, _mainController); }); // 如果勾选启动后最小化 if (Global.Settings.MinimizeWhenStarted) { WindowState = FormWindowState.Minimized; NotifyIcon.Visible = true; if (_isFirstCloseWindow) { // 显示提示语 NotifyIcon.ShowBalloonTip(5, UpdateChecker.Name, i18N.Translate( "Netch is now minimized to the notification bar, double click this icon to restore."), ToolTipIcon.Info); _isFirstCloseWindow = false; } Hide(); } 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, i18N.Translate("Start failed")); } }); } else { // 停止 UpdateStatus(State.Stopping); Task.Run(() => { _mainController.Stop(); UpdateStatus(State.Stopped); TestServer(); }); } }
public void GetStateByNameTest() { State alaska = StateExtension.GetStateByName("ALASKA"); Assert.AreEqual(alaska, State.AK); }
public void GetStateFullNameTest() { string alaska = StateExtension.GetStateFullName(State.AK); Assert.AreEqual("ALASKA", alaska); }