コード例 #1
0
ファイル: App.xaml.cs プロジェクト: coolinc/XWall
        protected override void OnStartup(StartupEventArgs eventArgs)
        {
            base.OnStartup(eventArgs);
            LoadLanguage();
            var resources = App.Current.Resources;

            var executablePath = System.Windows.Forms.Application.ExecutablePath;

            Environment.CurrentDirectory = Path.GetDirectoryName(executablePath);
            Microsoft.Win32.SystemEvents.SessionEnding += (sender, e) => {
                try {
                    IsShutDown = true;
                    App.Current.Shutdown();
                }
                catch { }
            };

            var settings = Settings.Default;

            Directory.CreateDirectory(AppDataDirectory);
            Directory.CreateDirectory(AppDataDirectory + settings.ConfigsFolderName);
            Directory.CreateDirectory(AppDataDirectory + settings.ResourcesFolderName);

            var ignoreRunningInstance = false;

            if (eventArgs.Args.Length > 0)
            {
                var commandStr = eventArgs.Args[0];
                var match      = new Regex(@"^(.*?)(?:/(.*))?$").Match(commandStr);
                var command    = match.Groups[1].Value;
                var commandArg = match.Groups[2].Value;

                switch (command)
                {
                case "uninstall":
                    Operation.KillProcess(executablePath);
                    Operation.KillProcess(settings.PrivoxyFileName);
                    Operation.KillProcess(settings.PlinkFileName);
                    if (settings.SetProxyAutomatically)
                    {
                        Operation.Proxies.RestoreProxy();
                    }
                    Operation.SetAutoStart(false);
                    //Operation.RegisterXWallProtocol(false);
                    IsShutDown = true;
                    App.Current.Shutdown();
                    return;

                case "restart":
                    ignoreRunningInstance = true;
                    break;

                //case "xwall:new-rule":
                //    File.WriteAllText(settings.ConfigsFolderName + settings.NewRuleFileName, commandArg);
                //    IsShutDown = true;
                //    App.Current.Shutdown();
                //    return;
                //case "xwall:del-rule":
                //    File.WriteAllText(settings.ConfigsFolderName + settings.DeleteRuleFileName, commandArg);
                //    IsShutDown = true;
                //    App.Current.Shutdown();
                //    return;
                default:
                    break;
                }
            }

            if (!ignoreRunningInstance)
            {
                Process          current = Process.GetCurrentProcess();
                MessageBoxResult?result  = null;
                foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                {
                    if (process.Id != current.Id)
                    {
                        if (result == null)
                        {
                            result = MessageBox.Show(resources["XWallAlreadyStartedDescription"] as string, resources["XWallTitle"] as string, MessageBoxButton.OKCancel);
                        }
                        if (result == MessageBoxResult.OK)
                        {
                            try {
                                process.Kill();
                            }
                            catch { }
                        }
                        else
                        {
                            IsShutDown = true;
                            App.Current.Shutdown();
                            return;
                        }
                    }
                }
            }

            if (settings.UpgradeRequired)
            {
                settings.Upgrade();
                settings.UpgradeRequired = false;

                if (!settings.FirstRun)
                {
                    Updated = true;
                }
            }

            if (settings.FirstRun || settings.ToUseGoAgent)
            {
                if (settings.ToUseGoAgent)
                {
                    settings.ToUseGoAgent = false;
                }

                if (Directory.Exists(AppDataDirectory + settings.GaFolderName))
                {
                    settings.ProxyType = "GA";
                }
            }

            if (settings.ProxyType == "GA" && !Directory.Exists(AppDataDirectory + settings.GaFolderName))
            {
                settings.ProxyType = "SSH";
            }

            if (settings.FirstRun)
            {
                settings.FirstRun = false;
                FirstRun          = true;

                //first time stuffs.
                Operation.SetAvailablePorts();
            }

            //* DEBUG CODE
            var autoStart = Operation.SetAutoStart(settings.AutoStart);

            if (autoStart != settings.AutoStart)
            {
                settings.AutoStart = autoStart;
            }

            if (settings.SetProxyAutomatically)
            {
                Operation.Proxies.SetXWallProxy();
            }
            //Operation.RegisterXWallProtocol(true);

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

                default: return;
                }

                if (settings.SetProxyAutomatically)
                {
                    Operation.Proxies.SetXWallProxy();
                }
            };

            settings.PropertyChanged += (sender, e) => {
                switch (e.PropertyName)
                {
                case "AutoStart": break;

                default: return;
                }

                var rst = Operation.SetAutoStart(settings.AutoStart);
                if (rst != settings.AutoStart)
                {
                    settings.AutoStart = rst;
                }
            };

            //Operation.SetAvailablePorts();

            App.Current.Exit += (sender, e) => {
                if (settings.SetProxyAutomatically)
                {
                    Operation.Proxies.RestoreProxy();
                }
            };

            SystemEvents.SessionEnded += (sender, e) => {
                try {
                    App.Current.Shutdown();
                }
                catch { }
            };

            Dispatcher.UnhandledException += (sender, e) => {
                var query = HttpUtility.ParseQueryString("");
                query["data"] = Assembly.GetExecutingAssembly().GetName().Version.ToString() + "\r\n" + e.Exception.ToString();

                var client = new WebClient();
                client.UploadValuesAsync(new Uri(settings.ErrorReportUrl), query);

                var msg = (resources["UnhandledExceptionMessage"] as string).Replace("%n", Environment.NewLine);
                MessageBox.Show(msg, resources["XWall"] as string, MessageBoxButton.OK, MessageBoxImage.Error);

                e.Handled = true;
            };
            //*/
        }
コード例 #2
0
        protected override void OnStartup(StartupEventArgs eventArgs)
        {
            base.OnStartup(eventArgs);
            LoadLanguage();

            var executablePath = System.Windows.Forms.Application.ExecutablePath;

            Environment.CurrentDirectory = Path.GetDirectoryName(executablePath);

            var settings = Settings.Default;

            if (eventArgs.Args.Contains("uninstall"))
            {
                Operation.KillProcess(executablePath);
                Operation.KillProcess(settings.PrivoxyFileName);
                Operation.KillProcess(settings.PlinkFileName);
                Operation.Proxies.RestoreProxy();
                Operation.SetAutoStart(false);
                IsShutDown = true;
                App.Current.Shutdown();
                return;
            }

            Process          current   = Process.GetCurrentProcess();
            var              resources = App.Current.Resources;
            MessageBoxResult?result    = null;

            foreach (Process process in Process.GetProcessesByName(current.ProcessName))
            {
                if (process.Id != current.Id)
                {
                    if (result == null)
                    {
                        result = MessageBox.Show(resources["XWallAlreadyStartedDescription"] as string, resources["XWallTitle"] as string, MessageBoxButton.OKCancel);
                    }
                    if (result == MessageBoxResult.OK)
                    {
                        process.Kill();
                    }
                    else
                    {
                        IsShutDown = true;
                        App.Current.Shutdown();
                        return;
                    }
                }
            }

            if (settings.UpgradeRequired)
            {
                settings.Upgrade();
                settings.UpgradeRequired = false;
            }

            if (settings.FirstRun)
            {
                settings.FirstRun = false;

                //first time stuffs.
                Operation.SetAvailablePorts();
            }

            Operation.SetAutoStart(settings.AutoStart);
            Operation.Proxies.SetProxy("127.0.0.1:" + settings.ProxyPort);

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

                default: return;
                }

                Operation.Proxies.SetProxy("127.0.0.1:" + settings.ProxyPort);
            };

            settings.PropertyChanged += (sender, e) => {
                switch (e.PropertyName)
                {
                case "AutoStart": break;

                default: return;
                }

                Operation.SetAutoStart(settings.AutoStart);
            };
            //Operation.SetAvailablePorts();

            App.Current.Exit += (sender, e) => {
                Operation.Proxies.RestoreProxy();
            };
        }