Exemplo n.º 1
0
        private async Task ControlFun()
        {
            Configuration.Save();
            if (State == State.Waiting || State == State.Stopped)
            {
                // 服务器、模式 需选择
                if (!(ServerComboBox.SelectedItem is Server server))
                {
                    MessageBoxX.Show(i18N.Translate("Please select a server first"));
                    return;
                }

                if (!(ModeComboBox.SelectedItem is Models.Mode mode))
                {
                    MessageBoxX.Show(i18N.Translate("Please select a mode first"));
                    return;
                }

                // 清除模式搜索框文本选择
                ModeComboBox.Select(0, 0);

                State = State.Starting;

                if (await MainController.Start(server, mode))
                {
                    State = State.Started;
                    _     = Task.Run(() => { Bandwidth.NetTraffic(); });
                    // 如果勾选启动后最小化
                    if (Global.Settings.MinimizeWhenStarted)
                    {
                        WindowState = FormWindowState.Minimized;

                        if (_isFirstCloseWindow)
                        {
                            // 显示提示语
                            NotifyTip(i18N.Translate("Netch is now minimized to the notification bar, double click this icon to restore."));
                            _isFirstCloseWindow = false;
                        }

                        Hide();
                    }
                }
                else
                {
                    State = State.Stopped;
                    StatusText(i18N.Translate("Start failed"));
                }
            }
            else
            {
                // 停止
                State = State.Stopping;
                await MainController.Stop();

                State = State.Stopped;
            }
        }
Exemplo n.º 2
0
        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();
                });
            }
        }
Exemplo n.º 3
0
        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();
                });
            }
        }
Exemplo n.º 4
0
        private async void ControlFun()
        {
            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 a mode first"));
                    return;
                }

                // 清除模式搜索框文本选择
                ModeComboBox.Select(0, 0);

                State = State.Starting;

                var server = ServerComboBox.SelectedItem as Models.Server;
                var mode   = ModeComboBox.SelectedItem as Models.Mode;

                if (await _mainController.Start(server, mode))
                {
                    State = State.Started;
                    _     = Task.Run(() => { Bandwidth.NetTraffic(server, mode, ref _mainController); });
                    // 如果勾选启动后最小化
                    if (Global.Settings.MinimizeWhenStarted)
                    {
                        WindowState = FormWindowState.Minimized;

                        if (_isFirstCloseWindow)
                        {
                            // 显示提示语
                            NotifyTip(i18N.Translate("Netch is now minimized to the notification bar, double click this icon to restore."));
                            _isFirstCloseWindow = false;
                        }

                        Hide();
                    }

                    if (Global.Settings.StartedTcping)
                    {
                        // 自动检测延迟
                        _ = Task.Run(() =>
                        {
                            while (State == State.Started)
                            {
                                server.Test();
                                // 重载服务器列表
                                InitServer();

                                Thread.Sleep(Global.Settings.StartedTcping_Interval * 1000);
                            }
                        });
                    }
                }
                else
                {
                    State = State.Stopped;
                    StatusText(i18N.Translate("Start failed"));
                }
            }
            else
            {
                // 停止
                State = State.Stopping;
                await _mainController.Stop();

                State = State.Stopped;
                _     = Task.Run(TestServer);
            }
        }
Exemplo n.º 5
0
        private void ControlFun()
        {
            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 a mode first"));
                    return;
                }

                State = State.Starting;

                // 清除模式搜索框文本选择
                ModeComboBox.Select(0, 0);

                Task.Run(() =>
                {
                    Task.Run(Firewall.AddNetchFwRules);

                    var server = ServerComboBox.SelectedItem as Models.Server;
                    var mode   = ModeComboBox.SelectedItem as Models.Mode;
                    var result = false;

                    try
                    {
                        // TODO 完善控制器异常处理
                        result = _mainController.Start(server, mode);
                    }
                    catch (Exception e)
                    {
                        if (e is DllNotFoundException || e is FileNotFoundException)
                        {
                            MessageBoxX.Show(e.Message + "\n\n" + i18N.Translate("Missing File or runtime components"), owner: this);
                        }

                        Netch.Application_OnException(this, new ThreadExceptionEventArgs(e));
                    }

                    if (result)
                    {
                        Task.Run(() =>
                        {
                            State = State.Started;
                            StatusTextAppend(LocalPortText(server.Type, mode.Type));
                            Bandwidth.NetTraffic(server, mode, _mainController);
                        });
                        // 如果勾选启动后最小化
                        if (Global.Settings.MinimizeWhenStarted)
                        {
                            WindowState = FormWindowState.Minimized;

                            if (_isFirstCloseWindow)
                            {
                                // 显示提示语
                                NotifyTip(i18N.Translate("Netch is now minimized to the notification bar, double click this icon to restore."));
                                _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
                    {
                        State = State.Stopped;
                        StatusText(i18N.Translate("Start failed"));
                    }
                });
            }
            else
            {
                State = State.Stopping;
                Task.Run(() =>
                {
                    // 停止
                    _mainController.Stop();
                    State = State.Stopped;
                    Task.Run(TestServer);
                });
            }
        }
Exemplo n.º 6
0
        private void ControlFun()
        {
            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 a mode first"));
                    return;
                }

                State = State.Starting;

                // 清除模式搜索框文本选择
                ModeComboBox.Select(0, 0);

                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(() =>
                        {
                            State = State.Started;
                            StatusTextAppend(LocalPortText(server.Type, mode.Type));
                            Bandwidth.NetTraffic(server, mode, _mainController);
                        });
                        // 如果勾选启动后最小化
                        if (Global.Settings.MinimizeWhenStarted)
                        {
                            WindowState = FormWindowState.Minimized;

                            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
                    {
                        State = State.Stopped;
                        StatusText(i18N.Translate("Start failed"));
                    }
                });
            }
            else
            {
                Task.Run(() =>
                {
                    // 停止
                    State = State.Stopping;
                    _mainController.Stop();
                    State = State.Stopped;
                });
                Task.Run(TestServer);
            }
        }
Exemplo n.º 7
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();
                });
            }
        }