Ejemplo n.º 1
0
        private void HandleKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                SettingsButton.PerformClick();
            }

            if (e.KeyCode == Keys.Left)
            {
                foreach (Control control in this.Controls)
                {
                    control.Location = new Point(control.Location.X - 1, control.Location.Y - 1);
                }
            }
        }
Ejemplo n.º 2
0
        private void ControlFun()
        {
            //防止模式选择框变成蓝色:D
            ModeComboBox.Select(0, 0);

            if (State == State.Waiting || State == State.Stopped)
            {
                #region  务器、模式 需选择
                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;
                }
                #endregion

                #region 检查端口是否被占用
                if (PortHelper.PortInUse(Global.Settings.Socks5LocalPort))
                {
                    MessageBoxX.Show("The Socks5 port is in use. Click OK to modify it.");
                    SettingsButton.PerformClick();
                    return;
                }

                if (PortHelper.PortInUse(Global.Settings.HTTPLocalPort))
                {
                    MessageBoxX.Show("The HTTP port is in use. Click OK to modify it.");
                    SettingsButton.PerformClick();
                    return;
                }

                if (PortHelper.PortInUse(Global.Settings.RedirectorTCPPort, PortType.TCP))
                {
                    MessageBoxX.Show("The RedirectorTCP port is in use. Click OK to modify it.");
                    SettingsButton.PerformClick();
                    return;
                }
                #endregion

                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();
                });
            }
        }