예제 #1
0
        private void ControlButton_Click(object sender, EventArgs e)
        {
            if (State == Objects.State.Waiting || State == Objects.State.Stopped)
            {
                // 当前ServerComboBox中至少有一项
                if (ServerComboBox.SelectedIndex == -1)
                {
                    MessageBox.Show(Utils.i18N.Translate("Please select a server first"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                // 当前ModeComboBox中至少有一项
                if (ModeComboBox.SelectedIndex == -1)
                {
                    MessageBox.Show(Utils.i18N.Translate("Please select an mode first"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                MenuStrip.Enabled  = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false;
                ControlButton.Text = "...";
                StatusLabel.Text   = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting")}";
                State = Objects.State.Starting;

                Task.Run(() =>
                {
                    var server = ServerComboBox.SelectedItem as Objects.Server;
                    var mode   = ModeComboBox.SelectedItem as Objects.Mode;

                    MainController = new Controllers.MainController();
                    if (MainController.Start(server, mode))
                    {
                        if (mode.Type == 0)
                        {
                            UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = true;
                            MainController.pNFController.OnBandwidthUpdated += OnBandwidthUpdated;
                        }

                        ControlButton.Enabled = true;
                        ControlButton.Text    = Utils.i18N.Translate("Stop");

                        if (mode.Type != 3 && mode.Type != 5)
                        {
                            if (server.Type != "Socks5")
                            {
                                if (Global.Settings.LocalAddress == "0.0.0.0")
                                {
                                    StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Started")} ({Utils.i18N.Translate("Allow other Devices to connect")} Socks5 {Utils.i18N.Translate("Local Port")}{Utils.i18N.Translate(": ")}{Global.Settings.Socks5LocalPort})";
                                }
                                else
                                {
                                    StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Started")} (Socks5 {Utils.i18N.Translate("Local Port")}{Utils.i18N.Translate(": ")}{Global.Settings.Socks5LocalPort}{")"}";
                                }
                            }
                            else
                            {
                                StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Started")}";
                            }
                        }
                        else
                        {
                            if (server.Type != "Socks5")
                            {
                                if (Global.Settings.LocalAddress == "0.0.0.0")
                                {
                                    StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Started")} ({Utils.i18N.Translate("Allow other Devices to connect")} Socks5 {Utils.i18N.Translate("Local Port")}{Utils.i18N.Translate(": ")}{Global.Settings.Socks5LocalPort} | HTTP {Utils.i18N.Translate("Local Port")}{Utils.i18N.Translate(": ")}{Global.Settings.HTTPLocalPort}{")"}";
                                }
                                else
                                {
                                    StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Started")} (Socks5 {Utils.i18N.Translate("Local Port")}{Utils.i18N.Translate(": ")}{Global.Settings.Socks5LocalPort} | HTTP {Utils.i18N.Translate("Local Port")}{Utils.i18N.Translate(": ")}{Global.Settings.HTTPLocalPort})";
                                }
                            }
                            else
                            {
                                if (Global.Settings.LocalAddress == "0.0.0.0")
                                {
                                    StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Started")} ({Utils.i18N.Translate("Allow other Devices to connect")} HTTP {Utils.i18N.Translate("Local Port")}{Utils.i18N.Translate(": ")}{Global.Settings.HTTPLocalPort}{")"}";
                                }
                                else
                                {
                                    StatusLabel.Text = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Started")} (HTTP {Utils.i18N.Translate("Local Port")}{Utils.i18N.Translate(": ")}{Global.Settings.HTTPLocalPort})";
                                }
                            }
                        }

                        State = Objects.State.Started;
                    }
                    else
                    {
                        MenuStrip.Enabled  = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
                        ControlButton.Text = Utils.i18N.Translate("Start");
                        StatusLabel.Text   = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Start failed")}";
                        State = Objects.State.Stopped;
                    }
                });
            }
            else
            {
                ControlButton.Enabled = false;
                ControlButton.Text    = "...";
                StatusLabel.Text      = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Stopping")}";
                State = Objects.State.Stopping;

                Task.Run(() =>
                {
                    var server = ServerComboBox.SelectedItem as Objects.Server;
                    var mode   = ModeComboBox.SelectedItem as Objects.Mode;

                    MainController.Stop();

                    if (mode.Type == 0)
                    {
                        LastUploadBandwidth        = 0;
                        LastDownlaodBandwidth      = 0;
                        UploadSpeedLabel.Text      = "↑: 0 KB/s";
                        DownloadSpeedLabel.Text    = "↓: 0 KB/s";
                        UsedBandwidthLabel.Text    = $"{Utils.i18N.Translate("Used")}{Utils.i18N.Translate(": ")}0 KB";
                        UsedBandwidthLabel.Visible = UploadSpeedLabel.Visible = DownloadSpeedLabel.Visible = false;
                    }

                    MenuStrip.Enabled  = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
                    ControlButton.Text = Utils.i18N.Translate("Start");
                    StatusLabel.Text   = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Stopped")}";
                    State = Objects.State.Stopped;

                    TestServer();
                });
            }
        }
예제 #2
0
파일: MainForm.cs 프로젝트: yz41/Netch
        private void ControlButton_Click(object sender, EventArgs e)
        {
            if (State == Objects.State.Waiting || State == Objects.State.Stopped)
            {
                // 当前ServerComboBox中至少有一项
                if (ServerComboBox.SelectedIndex == -1)
                {
                    MessageBox.Show(Utils.i18N.Translate("Please select an server first"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                // 当前ModeComboBox中至少有一项
                if (ModeComboBox.SelectedIndex == -1)
                {
                    MessageBox.Show(Utils.i18N.Translate("Please select an mode first"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                ToolStrip.Enabled  = ConfigurationGroupBox.Enabled = ControlButton.Enabled = false;
                ControlButton.Text = "...";
                StatusLabel.Text   = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Starting")}";
                State = Objects.State.Starting;

                Task.Run(() =>
                {
                    MainController = new Controllers.MainController();
                    if (MainController.Start(ServerComboBox.SelectedItem as Objects.Server, ModeComboBox.SelectedItem as Objects.Mode))
                    {
                        ControlButton.Enabled = true;
                        ControlButton.Text    = Utils.i18N.Translate("Stop");
                        StatusLabel.Text      = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Started")}";
                        State = Objects.State.Started;
                    }
                    else
                    {
                        ToolStrip.Enabled  = ConfigurationGroupBox.Enabled = ControlButton.Enabled = true;
                        ControlButton.Text = Utils.i18N.Translate("Start");
                        StatusLabel.Text   = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Start failed")}";
                        State = Objects.State.Stopped;
                    }
                });
            }
            else
            {
                ControlButton.Enabled = false;
                ControlButton.Text    = "...";
                StatusLabel.Text      = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Stopping")}";
                State = Objects.State.Stopping;

                Task.Run(() =>
                {
                    MainController.Stop();

                    ToolStrip.Enabled  = ConfigurationGroupBox.Enabled = ControlButton.Enabled = true;
                    ControlButton.Text = Utils.i18N.Translate("Start");
                    StatusLabel.Text   = $"{Utils.i18N.Translate("Status")}{Utils.i18N.Translate(": ")}{Utils.i18N.Translate("Stopped")}";
                    State = Objects.State.Stopped;

                    TestServer();
                });
            }
        }
예제 #3
0
        private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Global.Settings.UseProxyToUpdateSubscription)
            {
                // 当前ServerComboBox中至少有一项
                if (ServerComboBox.SelectedIndex == -1)
                {
                    MessageBox.Show(Utils.i18N.Translate("Please select a server first"), Utils.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)
            {
                DeletePictureBox.Enabled = false;

                Task.Run(() =>
                {
                    if (Global.Settings.UseProxyToUpdateSubscription)
                    {
                        var mode = new Models.Mode()
                        {
                            Remark = "ProxyUpdate",
                            Type   = 5
                        };
                        MainController = new Controllers.MainController();
                        MainController.Start(ServerComboBox.SelectedItem as Models.Server, mode);
                    }
                    foreach (var item in Global.Settings.SubscribeLink)
                    {
                        using (var client = new Override.WebClient())
                        {
                            try
                            {
                                if (!String.IsNullOrEmpty(item.UserAgent))
                                {
                                    client.Headers.Add("User-Agent", item.UserAgent);
                                }
                                else
                                {
                                    client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36");
                                }

                                if (Global.Settings.UseProxyToUpdateSubscription)
                                {
                                    client.Proxy = new System.Net.WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}");
                                }

                                var response = client.DownloadString(item.Link);

                                try
                                {
                                    response = Utils.ShareLink.URLSafeBase64Decode(response);
                                }
                                catch (Exception)
                                {
                                    // 跳过
                                }

                                var list = new List <Models.Server>();
                                foreach (var server in Global.Settings.Server)
                                {
                                    if (server.Group != item.Remark)
                                    {
                                        list.Add(server);
                                    }
                                }
                                Global.Settings.Server = list;

                                using (var sr = new StringReader(response))
                                {
                                    string text;

                                    while ((text = sr.ReadLine()) != null)
                                    {
                                        var result = Utils.ShareLink.Parse(text);

                                        if (result != null)
                                        {
                                            foreach (var x in result)
                                            {
                                                x.Group = item.Remark;
                                            }

                                            Global.Settings.Server.AddRange(result);
                                        }
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                continue;
                            }
                        }
                    }

                    InitServer();
                    DeletePictureBox.Enabled = true;
                    if (Global.Settings.UseProxyToUpdateSubscription)
                    {
                        MenuStrip.Enabled  = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
                        ControlButton.Text = Utils.i18N.Translate("Start");
                        MainController.Stop();
                    }
                    MessageBox.Show(this, Utils.i18N.Translate("Update completed"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Utils.Configuration.Save();
                });

                MessageBox.Show(Utils.i18N.Translate("Updating in the background"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(Utils.i18N.Translate("No subscription link"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }