Esempio n. 1
0
 private void onCustomRulesEditButtonClick(object sender, RoutedEventArgs e)
 {
     Rules.OpenEditor();
 }
Esempio n. 2
0
        private void onWindowLoaded(object sender, RoutedEventArgs e)
        {
            versionTextBlock.Text += resources["Version"] as string + " " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
            websiteUrlText.Text    = settings.WebsiteUrl;
            feedbackEmailText.Text = settings.FeedbackEmail;

            plink.Started += () => {
                Dispatcher.BeginInvoke(new Action(() => {
                    sshInformationGrid.IsEnabled = false;
                    sshConnectButton.IsEnabled   = true;
                    sshConnectButton.Content     = resources["Stop"] as string;
                    connectStatus.SetStatus(ConnectStatus.Status.Processing, resources["Connecting"] as string);
                }));
            };

            plink.Connected += () => {
                Dispatcher.BeginInvoke(new Action(() => {
                    sshConnectButton.IsEnabled = true;
                    sshConnectButton.Content   = resources["Disconnect"] as string;
                    connectStatus.SetStatus(ConnectStatus.Status.OK, resources["Connected"] as string, String.Format(resources["SuccessConnectDescription"] as string, settings.SshServer));
                }));
            };

            plink.ReconnectCountingDown += (seconds) => {
                Dispatcher.BeginInvoke(new Action(() => {
                    sshConnectButton.IsEnabled = true;
                    sshConnectButton.Content   = resources["Stop"] as string;
                    connectStatus.SetStatus(ConnectStatus.Status.Stopped, String.Format(resources["ReconnectDescription"] as string, seconds));
                }));
            };

            plink.Disconnected += (isLastSuccess, isReconnect) => {
                Dispatcher.BeginInvoke(new Action(() => {
                    sshInformationGrid.IsEnabled = true;
                    sshConnectButton.IsEnabled   = true;
                    sshConnectButton.Content     = resources["Connect"] as string;

                    if (plink.Error != null)
                    {
                        connectStatus.SetStatus(ConnectStatus.Status.Error, resources["ErrorConnect"] as string, plink.Error, System.Windows.Forms.ToolTipIcon.Error);
                    }
                    else if (isLastSuccess)
                    {
                        connectStatus.SetStatus(ConnectStatus.Status.Stopped, resources["Disconnected"] as string, resources["DisconnectedDescription"] as string, System.Windows.Forms.ToolTipIcon.Warning);
                    }
                    else if (plink.IsNormallyStopped)
                    {
                        connectStatus.SetStatus(ConnectStatus.Status.Stopped, resources["ConnectStopped"] as string);
                    }
                    else if (isReconnect)
                    {
                        connectStatus.SetStatus(ConnectStatus.Status.Stopped, resources["ConnectFailed"] as string);
                    }
                    else
                    {
                        connectStatus.SetStatus(ConnectStatus.Status.Stopped, resources["ConnectFailed"] as string, String.Format(resources["ConnectFailedDescription"] as string, settings.SshServer), System.Windows.Forms.ToolTipIcon.Warning);
                    }
                }));
            };

            //RULES
            //online rules
            if (settings.OnlineRulesLastUpdateTime.Ticks > 0)
            {
                lastUpdateTimeTextBlock.Text = settings.OnlineRulesLastUpdateTime.ToShortDateString();
            }

            Rules.OnlineRules.UpdateStarted += () => {
                Dispatcher.BeginInvoke(new Action(() => {
                    onlineRulesUpdateButton.Content   = resources["Updating"] as string;
                    onlineRulesUpdateButton.IsEnabled = false;
                }));
            };

            Rules.OnlineRules.Updated += (success) => {
                Dispatcher.BeginInvoke(new Action(() => {
                    onlineRulesUpdateButton.Content   = resources["Update"] as string;
                    onlineRulesUpdateButton.IsEnabled = true;
                    if (success)
                    {
                        lastUpdateTimeTextBlock.Text = settings.OnlineRulesLastUpdateTime.ToShortDateString();
                    }
                    else
                    {
                        connectStatus.Tray.ShowBalloonTip(0, resources["UpdateOnlineRulesFailed"] as string, resources["UpdateOnlineRulesFailedDescription"] as string, System.Windows.Forms.ToolTipIcon.Warning);
                    }
                }));
            };

            //custom rules
            updateCustomRulesStatus();

            Rules.CustomRules.Rules.ListChanged += (o, a) => {
                Dispatcher.BeginInvoke(new Action(updateCustomRulesStatus));
            };

            Rules.Initialize();

            privoxy.Start();

            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "ProxyPort": break;

                case "ListenToLocalOnly": break;

                default: return;
                }

                privoxy.Start();
            };

            if (settings.ProxyType == "SSH" && settings.AutoStart)
            {
                plink.Start();
            }

            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "ProxyType": break;

                default: return;
                }

                if (settings.ProxyType == "SSH")
                {
                    if (settings.AutoStart)
                    {
                        plink.Start();
                    }
                }
                else
                {
                    plink.Stop();
                }
            };

            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "SshSocksPort": break;

                default: return;
                }

                if (settings.ProxyType == "SSH")
                {
                    if (plink.IsConnected || plink.IsConnecting)
                    {
                        plink.Start();
                    }
                }
            };

            checkVersion();

            if (WindowState != WindowState.Minimized)
            {
                Activate();
            }
        }
Esempio n. 3
0
        private void onWindowLoaded(object sender, RoutedEventArgs e)
        {
            InitializeBinding();

            var gaAppIdsToolTip = new ToolTip();

            gaAppIdsToolTip.Content         = resources["GaAppIdsTooltip"] as string;
            gaAppIdsToolTip.StaysOpen       = true;
            gaAppIdsToolTip.PlacementTarget = gaAppIdsTextBox;
            gaAppIdsToolTip.Placement       = System.Windows.Controls.Primitives.PlacementMode.Top;
            gaAppIdsTextBox.ToolTip         = gaAppIdsToolTip;


            notificationController = new NotificationController(this);
            plink   = new Plink();
            privoxy = new Privoxy();
            goagent = new GoAgent();


            versionTextBlock.Text += resources["Version"] as string + " " + Assembly.GetExecutingAssembly().GetName().Version.ToString();
            websiteUrlText.Text    = settings.WebsiteUrl;
            feedbackEmailText.Text = settings.FeedbackEmail;

            if (!(
                    (settings.ProxyType == "SSH" && !Plink.CheckSettings()) ||
                    (settings.ProxyType == "GA" && settings.GaAppIds == "")
                    ))
            {
                WindowState = WindowState.Minimized;
            }

            goagent.Started += () => {
                notificationController.SetStatus("GA", NotificationController.Status.OK);
            };

            goagent.Stopped += () => {
                notificationController.SetStatus("GA", NotificationController.Status.Stopped);
            };

            //goagent.RequireRunas += () => {
            //    MessageBox.Show(resources["GoAgentRequiresRunasMessage"] as string);

            //    //var si = new ProcessStartInfo();
            //    //si.FileName = System.Windows.Forms.Application.ExecutablePath;
            //    //si.Verb = "runas";
            //    //si.Arguments = "restart";
            //    //Process.Start(si);
            //    Process.Start(System.Windows.Forms.Application.ExecutablePath, "restart");


            //    //App.Current.Shutdown();
            //};


            plink.Started += () => {
                Dispatcher.BeginInvoke(new Action(() => {
                    sshInformationGrid.IsEnabled = false;
                    sshConnectButton.IsEnabled   = true;
                    sshConnectButton.Content     = resources["Stop"] as string;
                    notificationController.SetStatus("SSH", NotificationController.Status.Processing, resources["Connecting"] as string);
                }));
            };

            plink.Connected += () => {
                Dispatcher.BeginInvoke(new Action(() => {
                    sshConnectButton.IsEnabled = true;
                    sshConnectButton.Content   = resources["Disconnect"] as string;
                    notificationController.SetStatus("SSH", NotificationController.Status.OK, resources["Connected"] as string, settings.SshNotification ? String.Format(resources["SuccessConnectDescription"] as string, settings.SshServer) : null);
                }));
            };

            plink.ReconnectCountingDown += (seconds) => {
                Dispatcher.BeginInvoke(new Action(() => {
                    sshConnectButton.IsEnabled = true;
                    sshConnectButton.Content   = resources["Stop"] as string;
                    notificationController.SetStatus("SSH", NotificationController.Status.Stopped, String.Format(resources["ReconnectDescription"] as string, seconds));
                }));
            };

            plink.Disconnected += (isLastSuccess, isReconnect) => {
                Dispatcher.BeginInvoke(new Action(() => {
                    sshInformationGrid.IsEnabled = true;
                    sshConnectButton.IsEnabled   = true;
                    sshConnectButton.Content     = resources["Connect"] as string;

                    if (plink.Error != null)
                    {
                        notificationController.SetStatus("SSH", NotificationController.Status.Error, resources["ErrorConnect"] as string, plink.Error != lastPlinkError ? plink.Error : null, System.Windows.Forms.ToolTipIcon.Error);
                    }
                    else if (isLastSuccess)
                    {
                        notificationController.SetStatus("SSH", NotificationController.Status.Stopped, resources["Disconnected"] as string, settings.SshNotification ? resources["DisconnectedDescription"] as string : null, System.Windows.Forms.ToolTipIcon.Warning);
                    }
                    else if (plink.IsNormallyStopped)
                    {
                        notificationController.SetStatus("SSH", NotificationController.Status.Stopped, resources["ConnectStopped"] as string);
                    }
                    else if (isReconnect)
                    {
                        notificationController.SetStatus("SSH", NotificationController.Status.Stopped, resources["ConnectFailed"] as string);
                    }
                    else
                    {
                        notificationController.SetStatus("SSH", NotificationController.Status.Stopped, resources["ConnectFailed"] as string, String.Format(resources["ConnectFailedDescription"] as string, settings.SshServer), System.Windows.Forms.ToolTipIcon.Warning);
                    }

                    lastPlinkError = plink.Error;
                }));
            };

            //RULES
            //online rules
            if (settings.OnlineRulesLastUpdateTime.Ticks > 0)
            {
                lastUpdateTimeTextBlock.Text = settings.OnlineRulesLastUpdateTime.ToString(@"M\/d\/yyyy");
            }

            Rules.OnlineRules.UpdateStarted += () => {
                Dispatcher.BeginInvoke(new Action(() => {
                    onlineRulesUpdateButton.Content   = resources["Updating"] as string;
                    onlineRulesUpdateButton.IsEnabled = false;
                }));
            };

            Rules.OnlineRules.Updated += (success) => {
                Dispatcher.BeginInvoke(new Action(() => {
                    onlineRulesUpdateButton.Content   = resources["Update"] as string;
                    onlineRulesUpdateButton.IsEnabled = true;
                    if (success)
                    {
                        lastUpdateTimeTextBlock.Text = settings.OnlineRulesLastUpdateTime.ToString(@"M\/d\/yyyy");
                    }
                    //else
                    //    notificationController.Tray.ShowBalloonTip(0, resources["UpdateOnlineRulesFailed"] as string, resources["UpdateOnlineRulesFailedDescription"] as string, System.Windows.Forms.ToolTipIcon.Warning);
                }));
            };

            //custom rules
            updateCustomRulesStatus();

            Rules.CustomRules.Rules.ListChanged += (o, a) => {
                Dispatcher.BeginInvoke(new Action(updateCustomRulesStatus));
            };

            Rules.Initialize();

            //* DEBUG CODE
            privoxy.Start();

            var server = new Microsoft.VisualStudio.WebHost.Server(settings.LocalServerPort, "/", App.AppDataDirectory + settings.LocalServerFolderName);

            try {
                server.Start();
            }
            catch {
                MessageBox.Show(string.Format(resources["FailedStartLocalServer"] as string, settings.LocalServerPort));
            }

            App.Current.Exit += (o, a) => {
                try {
                    server.Stop();
                }
                catch { }
            };

            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "ProxyPort": break;

                case "ListenToLocalOnly": break;

                case "UseIntranetProxy": break;

                case "IntranetProxyServer": break;

                case "IntranetProxyPort": break;

                default: return;
                }

                privoxy.Start();
            };
            //*/

            //ga

            if (settings.ProxyType == "GA")
            {
                goagent.Start();
            }

            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "ProxyType": break;

                default: return;
                }

                if (settings.ProxyType == "GA")
                {
                    goagent.Start();
                }
                else
                {
                    goagent.Stop();
                }
            };

            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "GaPort": break;

                case "GaProfile": break;

                case "GaAppIds": break;

                default: return;
                }

                if (settings.ProxyType == "GA")
                {
                    goagent.Start();
                }
            };


            //ssh

            if (settings.ProxyType == "SSH" && settings.AutoStart)
            {
                plink.Start();
            }

            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "ProxyType": break;

                default: return;
                }

                if (settings.ProxyType == "SSH")
                {
                    if (settings.AutoStart)
                    {
                        plink.Start();
                    }
                }
                else
                {
                    plink.Stop();
                }
            };

            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "SshSocksPort": break;

                case "SshCompression": break;

                case "SshPlonkKeyword": break;

                default: return;
                }

                if (settings.ProxyType == "SSH")
                {
                    if (plink.IsConnected || plink.IsConnecting)
                    {
                        plink.Start();
                    }
                }
            };

            var usePlonkChangeBack = false;

            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "SshUsePlonk":
                    if (usePlonkChangeBack)
                    {
                        usePlonkChangeBack = false;
                        return;
                    }
                    else
                    {
                        break;
                    }

                default: return;
                }

                if (settings.SshUsePlonk && !File.Exists(settings.PlonkFileName))
                {
                    usePlonkChangeBack            = true;
                    sshUsePlonkCheckBox.IsChecked = false;
                    MessageBox.Show(resources["PlonkMissingMessage"] as string);
                }
                else if (settings.ProxyType == "SSH")
                {
                    if (plink.IsConnected || plink.IsConnecting)
                    {
                        plink.Start();
                    }
                }
            };

            //http&socks
            settings.PropertyChanged += (o, a) => {
                switch (a.PropertyName)
                {
                case "ProxyType": break;

                default: return;
                }

                initIconStatus();
            };

            initIconStatus();

            var ruleCommandWatcher = new FileSystemWatcher(App.AppDataDirectory + settings.ConfigsFolderName, "*-cmd");

            ruleCommandWatcher.Created            += ruleCommandHandler;
            ruleCommandWatcher.Changed            += ruleCommandHandler;
            ruleCommandWatcher.EnableRaisingEvents = true;

            Timer updateCheckTimer = null;

            updateCheckTimer = new Timer((st) => {
                Dispatcher.BeginInvoke(new Action(() => {
                    if (onlineVersionStr == null)
                    {
                        checkVersion();
                    }
                    else
                    {
                        updateCheckTimer.Dispose();
                    }
                }));
            }, null, 0, settings.UpdateCheckDelay * 1000);

            if (App.Updated)
            {
                new Action(() => {
                    Thread.Sleep(1000);
                    try {
                        File.Delete(App.AppDataDirectory + settings.ResourcesFolderName + settings.UpdateInstallerName);
                    }
                    catch { }
                    notificationController.SendMessage(resources["UpdateSuccessTitle"] as string, resources["UpdateSuccessDetails"] as string);
                }).BeginInvoke(null, null);
            }

            if (App.Updated || App.FirstRun)
            {
                new WebClient().DownloadStringAsync(new Uri(settings.UpdateReportUrl + "?v=" + Assembly.GetExecutingAssembly().GetName().Version.ToString()));
            }

            if (WindowState != WindowState.Minimized)
            {
                Activate();
            }

            //if (!settings.SubmitNewRuleAsked) {
            //    settings.SubmitNewRuleAsked = true;
            //    var message = (resources["ShareRuleMessage"] as string).Replace("%n ", Environment.NewLine + Environment.NewLine);

            //    var result = MessageBox.Show(message, resources["ShareRuleTitle"] as string, MessageBoxButton.YesNo);
            //    settings.SubmitNewRule = result == MessageBoxResult.Yes;
            //}
        }