private void ImportServersFromClipboardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var texts = Clipboard.GetText();

            if (!string.IsNullOrWhiteSpace(texts))
            {
                var result = ShareLink.Parse(texts);

                if (result != null)
                {
                    Global.Settings.Server.AddRange(result);
                }
                else
                {
                    MessageBoxX.Show(i18N.Translate("Import servers error!"), info: false);
                }

                InitServer();
                Configuration.Save();
            }
        }
Beispiel #2
0
        private void ImportServersFromClipboardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var texts = Clipboard.GetText();

            if (!string.IsNullOrWhiteSpace(texts))
            {
                var result = ShareLink.Parse(texts);

                if (result != null)
                {
                    foreach (var server in result)
                    {
                        Global.Settings.Server.Add(server);
                    }
                }
                else
                {
                    MessageBoxX.Show(i18N.Translate("Import servers error!"), LogLevel.ERROR);
                }

                Configuration.Save();
            }
        }
        private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var bak_State     = State;
            var bak_StateText = StatusLabel.Text;

            if (Global.Settings.UseProxyToUpdateSubscription && ServerComboBox.SelectedIndex == -1)
            {
                Global.Settings.UseProxyToUpdateSubscription = false;
            }

            if (Global.Settings.UseProxyToUpdateSubscription)
            {
                // 当前 ServerComboBox 中至少有一项
                if (ServerComboBox.SelectedIndex == -1)
                {
                    MessageBoxX.Show(i18N.Translate("Please select a server first"));
                    return;
                }

                MenuStrip.Enabled  = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = false;
                ControlButton.Text = "...";
            }

            if (Global.Settings.SubscribeLink.Count > 0)
            {
                StatusText(i18N.Translate("Starting update subscription"));
                DeleteServerPictureBox.Enabled = false;

                UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false;
                Task.Run(() =>
                {
                    if (Global.Settings.UseProxyToUpdateSubscription)
                    {
                        var mode = new Models.Mode
                        {
                            Remark = "ProxyUpdate",
                            Type   = 5
                        };
                        _mainController = new MainController();
                        _mainController.Start(ServerComboBox.SelectedItem as Models.Server, mode);
                    }

                    foreach (var item in Global.Settings.SubscribeLink)
                    {
                        using var client = new 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 WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}");
                            }

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

                            try
                            {
                                response = ShareLink.URLSafeBase64Decode(response);
                            }
                            catch (Exception)
                            {
                                // ignored
                            }

                            Global.Settings.Server = Global.Settings.Server.Where(server => server.Group != item.Remark).ToList();
                            var result             = ShareLink.Parse(response);

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

                                Global.Settings.Server.AddRange(result);
                                NotifyIcon.ShowBalloonTip(5,
                                                          UpdateChecker.Name,
                                                          string.Format(i18N.Translate("Update {1} server(s) from {0}"), item.Remark, result.Count),
                                                          ToolTipIcon.Info);
                            }
                            else
                            {
                                NotifyIcon.ShowBalloonTip(5,
                                                          UpdateChecker.Name,
                                                          string.Format(i18N.Translate("Update servers error from {0}"), item.Remark),
                                                          ToolTipIcon.Error);
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }

                    InitServer();
                    DeleteServerPictureBox.Enabled = true;
                    if (Global.Settings.UseProxyToUpdateSubscription)
                    {
                        MenuStrip.Enabled  = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
                        ControlButton.Text = i18N.Translate("Start");
                        _mainController.Stop();
                        NatTypeStatusLabel.Text = "";
                    }

                    Configuration.Save();
                    StatusText(i18N.Translate("Subscription updated"));

                    MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
                    State             = bak_State;
                    StatusLabel.Text  = bak_StateText;
                }).ContinueWith(task => { BeginInvoke(new Action(() => { UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true; })); });

                NotifyIcon.ShowBalloonTip(5,
                                          UpdateChecker.Name,
                                          i18N.Translate("Updating in the background"),
                                          ToolTipIcon.Info);
            }
            else
            {
                MessageBoxX.Show(i18N.Translate("No subscription link"));
            }
        }
Beispiel #4
0
        private async void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            void DisableItems(bool v)
            {
                MenuStrip.Enabled = ConfigurationGroupBox.Enabled = ProfileGroupBox.Enabled = ControlButton.Enabled = v;
            }

            if (Global.Settings.UseProxyToUpdateSubscription && ServerComboBox.SelectedIndex == -1)
            {
                Global.Settings.UseProxyToUpdateSubscription = false;
            }

            if (Global.Settings.UseProxyToUpdateSubscription && ServerComboBox.SelectedIndex == -1)
            {
                MessageBoxX.Show(i18N.Translate("Please select a server first"));
                return;
            }

            if (Global.Settings.SubscribeLink.Count <= 0)
            {
                MessageBoxX.Show(i18N.Translate("No subscription link"));
                return;
            }

            StatusText(i18N.Translate("Starting update subscription"));
            DisableItems(false);
            try
            {
                if (Global.Settings.UseProxyToUpdateSubscription)
                {
                    var mode = new Models.Mode
                    {
                        Remark = "ProxyUpdate",
                        Type   = 5
                    };
                    await MainController.Start(ServerComboBox.SelectedItem as Models.Server, mode);
                }

                var serverLock = new object();

                await Task.WhenAll(Global.Settings.SubscribeLink.Select(async item => await Task.Run(async() =>
                {
                    try
                    {
                        var request = WebUtil.CreateRequest(item.Link);

                        if (!string.IsNullOrEmpty(item.UserAgent))
                        {
                            request.UserAgent = item.UserAgent;
                        }
                        if (Global.Settings.UseProxyToUpdateSubscription)
                        {
                            request.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}");
                        }

                        var str = await WebUtil.DownloadStringAsync(request);

                        try
                        {
                            str = ShareLink.URLSafeBase64Decode(str);
                        }
                        catch
                        {
                            // ignored
                        }

                        lock (serverLock)
                        {
                            Global.Settings.Server.RemoveAll(server => server.Group == item.Remark);

                            var result = ShareLink.Parse(str);
                            if (result != null)
                            {
                                foreach (var server in result)
                                {
                                    server.Group = item.Remark;
                                    Global.Settings.Server.Add(server);
                                }
                            }

                            NotifyTip(i18N.TranslateFormat("Update {1} server(s) from {0}", item.Remark, result?.Count ?? 0));
                        }
                    }
                    catch (WebException e)
                    {
                        NotifyTip($"{i18N.TranslateFormat("Update servers error from {0}", item.Remark)}\n{e.Message}", info: false);
                    }
                    catch (Exception e)
                    {
                        Logging.Error(e.ToString());
                    }
                })).ToArray());

                InitServer();
                Configuration.Save();
                StatusText(i18N.Translate("Subscription updated"));
            }
            catch (Exception)
            {
                // ignored
            }
            finally
            {
                if (Global.Settings.UseProxyToUpdateSubscription)
                {
                    await MainController.Stop();
                }

                DisableItems(true);
            }
        }
Beispiel #5
0
        private void UpdateServersFromSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Global.Settings.UseProxyToUpdateSubscription && ServerComboBox.SelectedIndex == -1)
            {
                Global.Settings.UseProxyToUpdateSubscription = false;
            }

            if (Global.Settings.UseProxyToUpdateSubscription && ServerComboBox.SelectedIndex == -1)
            {
                MessageBoxX.Show(i18N.Translate("Please select a server first"));
                return;
            }

            if (Global.Settings.SubscribeLink.Count > 0)
            {
                DeleteServerPictureBox.Enabled = false;
                UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = false;

                Task.Run(() =>
                {
                    if (Global.Settings.UseProxyToUpdateSubscription)
                    {
                        var mode = new Models.Mode
                        {
                            Remark = "ProxyUpdate",
                            Type   = 5
                        };
                        State = State.Starting;
                        if (_mainController.Start(ServerComboBox.SelectedItem as Models.Server, mode))
                        {
                            StatusText(i18N.Translate("Starting update subscription"));
                        }
                    }
                    else
                    {
                        StatusText(i18N.Translate("Starting update subscription"));
                    }

                    Task.WaitAll(Global.Settings.SubscribeLink.Select(item => Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            var request = WebUtil.CreateRequest(item.Link);

                            if (!string.IsNullOrEmpty(item.UserAgent))
                            {
                                request.UserAgent = item.UserAgent;
                            }
                            if (Global.Settings.UseProxyToUpdateSubscription)
                            {
                                request.Proxy = new WebProxy($"http://127.0.0.1:{Global.Settings.HTTPLocalPort}");
                            }

                            var str = WebUtil.DownloadString(request);

                            try
                            {
                                str = ShareLink.URLSafeBase64Decode(str);
                            }
                            catch
                            {
                                // ignored
                            }

                            Global.Settings.Server = Global.Settings.Server.Where(server => server.Group != item.Remark).ToList();
                            var result             = ShareLink.Parse(str);

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

                                Global.Settings.Server.AddRange(result);
                                NotifyTip(i18N.TranslateFormat("Update {1} server(s) from {0}", item.Remark, result.Count));
                            }
                        }
                        catch (WebException e)
                        {
                            NotifyTip($"{i18N.TranslateFormat("Update servers error from {0}", item.Remark)}\n{e.Message}", info: false);
                        }
                    })).ToArray());

                    InitServer();

                    Configuration.Save();
                    NotifyTip(i18N.Translate("Subscription updated"));

                    if (Global.Settings.UseProxyToUpdateSubscription)
                    {
                        _mainController.Stop();
                        State = State.Stopped;
                    }

                    State = State.Waiting;
                    DeleteModePictureBox.Enabled = true;
                    MenuStrip.Enabled            = ConfigurationGroupBox.Enabled = ControlButton.Enabled = SettingsButton.Enabled = true;
                    UpdateServersFromSubscribeLinksToolStripMenuItem.Enabled = true;
                });

                NotifyTip(i18N.Translate("Updating in the background"));
            }
            else
            {
                MessageBoxX.Show(i18N.Translate("No subscription link"));
            }
        }