Пример #1
0
        private void menuExit_Click(object Sender, EventArgs e)
        {
            //notifyIcon1.Visible = false;

            // Close the form, which closes the application.
            //Application.Exit();

            if (Priv10Service.IsInstalled() && AdminFunc.IsAdministrator())
            {
                MessageBoxResult res = MessageBox.Show(Translate.fmt("msg_stop_svc"), App.Title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
                switch (res)
                {
                case MessageBoxResult.Yes:
                    if (!Priv10Service.Terminate())
                    {
                        MessageBox.Show(Translate.fmt("msg_stop_svc_err"), App.Title, MessageBoxButton.OK, MessageBoxImage.Stop);
                    }
                    break;

                case MessageBoxResult.Cancel:
                    return;
                }
            }
            Application.Current.Shutdown();
        }
Пример #2
0
        static void TrayAction(object sender, TrayIcon.TrayEventArgs args)
        {
            if (MainWnd == null || !MainWnd.FullyLoaded)
            {
                return;
            }

            switch (args.Action)
            {
            case TrayIcon.Actions.ToggleWindow:
            {
                if (MainWnd.IsVisible)
                {
                    MainWnd.Hide();
                }
                else
                {
                    MainWnd.Show();
                }
                break;
            }

            case TrayIcon.Actions.ToggleNotify:
            {
                if (MainWnd.notificationWnd.IsVisible)
                {
                    MainWnd.notificationWnd.HideWnd();
                }
                else if (!MainWnd.notificationWnd.IsEmpty())
                {
                    MainWnd.notificationWnd.ShowWnd();
                }
                break;
            }

            case TrayIcon.Actions.CloseApplication:
            {
                if (Priv10Service.IsInstalled() && AdminFunc.IsAdministrator())
                {
                    MessageBoxResult res = MessageBox.Show(Translate.fmt("msg_stop_svc"), App.Title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
                    switch (res)
                    {
                    case MessageBoxResult.Yes:
                        if (!Priv10Service.Terminate())
                        {
                            MessageBox.Show(Translate.fmt("msg_stop_svc_err"), App.Title, MessageBoxButton.OK, MessageBoxImage.Stop);
                        }
                        break;

                    case MessageBoxResult.Cancel:
                        return;
                    }
                }
                Application.Current.Shutdown();
                break;
            }
            }
        }
Пример #3
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateSysMenu(); // Install system menu

            if (AdminFunc.IsAdministrator() && App.GetConfigInt("Startup", "ShowSetup", 1) == 1 && !Priv10Service.IsInstalled())
            {
                ShowSetup();
            }
        }
Пример #4
0
        public static void Main(string[] args)
        {
            App.args = args;

            HasConsole = WinConsole.Initialize(TestArg("-console"));

            if (TestArg("-dbg_wait"))
            {
                MessageBox.Show("Waiting for debugger. (press ok when attached)");
            }

            if (TestArg("-dbg_log"))
            {
                AppDomain.CurrentDomain.FirstChanceException += FirstChanceExceptionHandler;
            }

            StartModes startMode = StartModes.Normal; // Normal GUI Mode

            if (TestArg("-svc"))
            {
                startMode = StartModes.Service;
            }
            else if (TestArg("-engine"))
            {
                startMode = StartModes.Engine;
            }

            Log = new AppLog(Key);
            AppLog.ExceptionLogID    = (long)EventIDs.Exception;
            AppLog.ExceptionCategory = (short)EventFlags.DebugEvents;

            if (startMode == StartModes.Normal)
            {
                Log.EnableLogging();
                Log.LoadLog();
            }
            // When running as worker we need the windows event log
            else if (!Log.UsingEventLog())
            {
                Log.SetupEventLog(Key);
            }

            // load current version
            exePath = Process.GetCurrentProcess().MainModule.FileName; //System.Reflection.Assembly.GetExecutingAssembly().Location;
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(exePath);

            Version = fvi.FileMajorPart + "." + fvi.FileMinorPart;
            if (fvi.FileBuildPart != 0)
            {
                Version += "." + fvi.FileBuildPart;
            }
            if (fvi.FilePrivatePart != 0)
            {
                Version += (char)('a' + (fvi.FilePrivatePart - 1));
            }
            appPath = Path.GetDirectoryName(exePath);

            Translate.Load();

            dataPath = appPath + @"\Data";
            if (File.Exists(GetINIPath())) // if an ini exists in the app path, its considdered to be a portable run
            {
                isPortable = true;

                AppLog.Debug("Portable Mode");
            }
            else
            {
                string progData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                if (progData == null)
                {
                    progData = @"C:\ProgramData";
                }

                dataPath = progData + "\\" + Key;
            }

            AppLog.Debug("Config Directory: {0}", dataPath);

            // execute commandline commands
            if (ExecuteCommands())
            {
                return;
            }

            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }
            if (AdminFunc.IsAdministrator())
            {
                FileOps.SetAnyDirSec(dataPath);
            }

            App.LogInfo("PrivateWin10 Process Started, Mode {0}.", startMode.ToString());

            Session = Process.GetCurrentProcess().SessionId;

            // setup custom assembly resolution for x86/x64 synamic compatybility
            AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolveHandler;

            // is the process starting as a service/worker?
            if (startMode != StartModes.Normal)
            {
                engine = new Priv10Engine();
                if (startMode == StartModes.Service)
                {
                    using (Priv10Service svc = new Priv10Service())
                        ServiceBase.Run(svc);
                }
                else
                {
                    engine.Run();
                }
                return;
            }

            Thread.CurrentThread.Name = "Gui";

            client = new Priv10Client();

            // Encure wie have the required privilegs
            //if (!AdminFunc.IsDebugging())
            {
                AppLog.Debug("Trying to connect to Engine...");
                int conRes = client.Connect(1000);
                if (conRes == 0)
                {
                    if (!AdminFunc.IsAdministrator())
                    {
                        AppLog.Debug("Trying to obtain Administrative proivilegs...");
                        if (AdminFunc.SkipUacRun(App.Key, App.args))
                        {
                            return;
                        }

                        AppLog.Debug("Trying to start with 'runas'...");
                        // Restart program and run as admin
                        string           arguments = "\"" + string.Join("\" \"", args) + "\"";
                        ProcessStartInfo startInfo = new ProcessStartInfo(exePath, arguments);
                        startInfo.UseShellExecute = true;
                        startInfo.Verb            = "runas";
                        try
                        {
                            Process.Start(startInfo);
                            return; // we restarted as admin
                        }
                        catch
                        {
                            //MessageBox.Show(Translate.fmt("msg_admin_rights", mName), mName);
                            //return; // no point in cintinuing without admin rights or an already running engine
                        }
                    }
                    else if (Priv10Service.IsInstalled())
                    {
                        AppLog.Debug("Trying to start service...");
                        if (Priv10Service.Startup())
                        {
                            AppLog.Debug("Trying to connect to service...");

                            if (client.Connect() != 0)
                            {
                                AppLog.Debug("Connected to service...");
                            }
                            else
                            {
                                AppLog.Debug("Failed to connect to service...");
                            }
                        }
                        else
                        {
                            AppLog.Debug("Failed to start service...");
                        }
                    }
                }
                else if (conRes == -1)
                {
                    MessageBox.Show(Translate.fmt("msg_dupliate_session", Title), Title);
                    return; // no point in cintinuing without admin rights or an already running engine
                }
            }

            //

            tweaks = new TweakManager();

            // if we couldn't connect to the engine start it and connect
            if (!client.IsConnected() && AdminFunc.IsAdministrator())
            {
                AppLog.Debug("Starting Engine Thread...");

                engine = new Priv10Engine();

                engine.Start();

                AppLog.Debug("... engine started.");

                client.Connect();
            }

            var app = new App();

            app.InitializeComponent();

            InitLicense();

            MainWnd = new MainWindow();

            TrayIcon         = new TrayIcon();
            TrayIcon.Action += TrayAction;
            TrayIcon.Visible = (GetConfigInt("Startup", "Tray", 0) != 0) || App.TestArg("-autorun");

            if (!App.TestArg("-autorun") || !TrayIcon.Visible)
            {
                MainWnd.Show();
            }

            app.Run();

            TrayIcon.DestroyNotifyicon();

            client.Close();

            tweaks.Store();

            if (engine != null)
            {
                engine.Stop();
            }
        }
Пример #5
0
        static bool ExecuteCommands()
        {
            if (TestArg("-help") || TestArg("/?"))
            {
                string   Message = "Available command line options\r\n";
                string[] Help    =
                {
                    "Available Console Commands:",
                    "========================================",
                    "",
                    "-state\t\t\tShow instalation state",
                    "-uninstall\t\tUninstall Private Win10",
                    "-shutdown\t\tClose Private Win10 instances",
                    "-restart\t\tRestart Win10 and reload settings",
                    "",
                    "-svc_install\t\tInstall priv10 service (invokes -log_install)",
                    "-svc_remove\t\tRemove priv10 service",
                    "",
                    "-log_install\t\tInstall PrivateWin10 Custom Event Log",
                    "-log_remove\t\tRemove PrivateWin10 Custom Event Log",
                    "",
                    "-restore_dns\t\tRestore original DNS Configuration",
                    "",
                    "-console\t\tShow console with debug output",
                    "-help\t\t\tShow this help message"
                };
                if (!HasConsole)
                {
                    MessageBox.Show(Message + string.Join("\r\n", Help));
                }
                else
                {
                    Console.WriteLine(Message);
                    for (int j = 0; j < Help.Length; j++)
                    {
                        Console.WriteLine(" " + Help[j]);
                    }
                }
                return(true);
            }


            bool bDone = false;

            if (TestArg("-uninstall"))
            {
                AppLog.Debug("Uninstalling Private Win10");
                bDone = true;
            }

            if (TestArg("-svc_remove") || (Priv10Service.IsInstalled() && TestArg("-uninstall")))
            {
                AppLog.Debug("Removing Service...");
                Priv10Service.Uninstall();
                bDone = true;
            }

            if (TestArg("-shutdown") || TestArg("-restart") || TestArg("-restore") || TestArg("-uninstall"))
            {
                AppLog.Debug("Closing instances...");
                if (Priv10Service.IsInstalled())
                {
                    Priv10Service.Terminate();
                }

                Thread.Sleep(500);

                foreach (var proc in Process.GetProcessesByName(App.Key))
                {
                    if (proc.Id == ProcFunc.CurID)
                    {
                        continue;
                    }
                    proc.Kill();
                }

                bDone = true;
            }

            if (TestArg("-restore"))
            {
                string zipPath = GetArg("-restore");

                try
                {
                    if (zipPath == null || !File.Exists(zipPath))
                    {
                        throw new Exception("Data backup zip not specifyed or invalid path");
                    }

                    Console.WriteLine("Restoring settings from {0}", zipPath);

                    string extractPath = App.dataPath;

                    // Normalizes the path.
                    extractPath = Path.GetFullPath(extractPath);

                    // Ensures that the last character on the extraction path
                    // is the directory separator char.
                    // Without this, a malicious zip file could try to traverse outside of the expected
                    // extraction path.
                    if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
                    {
                        extractPath += Path.DirectorySeparatorChar;
                    }

                    // create data directory
                    if (!Directory.Exists(dataPath))
                    {
                        Directory.CreateDirectory(dataPath);
                    }

                    // ensure its writable by non administrators
                    FileOps.SetAnyDirSec(dataPath);

                    // Extract the backuped files
                    using (ZipArchive archive = ZipFile.OpenRead(zipPath))
                    {
                        foreach (ZipArchiveEntry entry in archive.Entries)
                        {
                            // Gets the full path to ensure that relative segments are removed.
                            string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName));

                            // Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                            // are case-insensitive.
                            if (!destinationPath.StartsWith(extractPath, StringComparison.Ordinal))
                            {
                                continue;
                            }

                            Console.WriteLine("Restored file {0}", entry.FullName);
                            if (File.Exists(destinationPath))
                            {
                                FileOps.DeleteFile(destinationPath);
                            }
                            else if (!Directory.Exists(Path.GetDirectoryName(destinationPath)))
                            {
                                Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
                            }

                            entry.ExtractToFile(destinationPath);
                        }
                    }
                }
                catch (Exception err)
                {
                    Console.WriteLine(err.Message);
                    MessageBox.Show(Translate.fmt("msg_restore_error", err.Message), App.Title, MessageBoxButton.OK, MessageBoxImage.Stop);
                }

                bDone = true;
            }

            if (TestArg("-restart") || TestArg("-restore"))
            {
                Thread.Sleep(500);

                AppLog.Debug("Starting instances...");
                if (Priv10Service.IsInstalled())
                {
                    Priv10Service.Startup();
                }

                Thread.Sleep(500);

                ProcessStartInfo startInfo = new ProcessStartInfo(App.exePath);
                startInfo.UseShellExecute = true;
                startInfo.Verb            = "runas";
                Process.Start(startInfo);

                bDone = true;
            }

            if (TestArg("-log_remove") || (Log.UsingEventLog() && TestArg("-uninstall")))
            {
                AppLog.Debug("Removing Event Log...");
                Log.RemoveEventLog(Key);
                bDone = true;
            }

            if (TestArg("-svc_install"))
            {
                AppLog.Debug("Installing Service...");
                Priv10Service.Install(TestArg("-svc_start"));
                bDone = true;
            }

            if (TestArg("-log_install") || TestArg("-svc_install")) // service needs the event log
            {
                AppLog.Debug("Setting up Event Log...");
                Log.SetupEventLog(Key);
                bDone = true;
            }

            if (TestArg("-restore_dns") || (DnsConfigurator.IsAnyLocalDNS() && TestArg("-uninstall")))
            {
                AppLog.Debug("Restoring DNS Config...");
                DnsConfigurator.RestoreDNS();
                bDone = true;
            }

            if (TestArg("-uninstall") && AdminFunc.IsSkipUac(App.Key))
            {
                AppLog.Debug("Removing UAC Bypass...");
                AdminFunc.SkipUacEnable(App.Key, false);
                bDone = true;
            }

            if (TestArg("-uninstall") && App.IsAutoStart())
            {
                AppLog.Debug("Removing Autostart...");
                App.AutoStart(false);
                bDone = true;
            }

            if (bDone)
            {
                AppLog.Debug("done");
            }


            if (TestArg("-state"))
            {
                Console.WriteLine();
                Console.WriteLine("Instalation State:");
                Console.WriteLine("========================="); // 25
                Console.Write("Auto Start:\t");
                Console.WriteLine(App.IsAutoStart());
                Console.Write("UAC Bypass:\t");
                Console.WriteLine(AdminFunc.IsSkipUac(App.Key));
                Console.Write("Service:\t");
                Console.WriteLine(Priv10Service.IsInstalled());
                Console.Write("Event Log:\t");
                Console.WriteLine(Log.UsingEventLog());
                Console.Write("Local DNS:\t");
                Console.WriteLine(DnsConfigurator.IsAnyLocalDNS());
                Console.WriteLine();
                bDone = true;
            }

            if (TestArg("-wait"))
            {
                Console.WriteLine();
                for (int i = 10; i >= 0; i--)
                {
                    Console.Write("\r{0}%   ", i);
                    Thread.Sleep(1000);
                }
            }

            return(bDone);
        }
Пример #6
0
        public static void Main(string[] args)
        {
            App.args = args;

            HasConsole = WinConsole.Initialize(TestArg("-console"));

            if (TestArg("-dbg_wait"))
            {
                MessageBox.Show("Waiting for debugger. (press ok when attached)");
            }

            if (TestArg("-dbg_log"))
            {
                AppDomain.CurrentDomain.FirstChanceException += FirstChanceExceptionHandler;
            }

            Log = new AppLog(Key);
            AppLog.ExceptionLogID    = (long)Priv10Logger.EventIDs.Exception;
            AppLog.ExceptionCategory = (short)Priv10Logger.EventFlags.DebugEvents;

            Log.EnableLogging();
            Log.LoadLog();

            // load current version
            exePath = Process.GetCurrentProcess().MainModule.FileName; //System.Reflection.Assembly.GetExecutingAssembly().Location;
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(exePath);

            Version = fvi.FileMajorPart + "." + fvi.FileMinorPart;
            if (fvi.FileBuildPart != 0)
            {
                Version += "." + fvi.FileBuildPart;
            }
            if (fvi.FilePrivatePart != 0)
            {
                Version += (char)('a' + (fvi.FilePrivatePart - 1));
            }
            appPath = Path.GetDirectoryName(exePath);

            Translate.Load();

            dataPath = appPath + @"\Data";
            if (File.Exists(GetINIPath())) // if an ini exists in the app path, its considdered to be a portable run
            {
                isPortable = true;

                AppLog.Debug("Portable Mode");
            }
            else
            {
                string progData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                if (progData == null)
                {
                    progData = @"C:\ProgramData";
                }

                dataPath = progData + "\\" + Key;
            }

            AppLog.Debug("Config Directory: {0}", dataPath);

            // execute commandline commands
            if (ExecuteCommands())
            {
                return;
            }

            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }
            if (AdminFunc.IsAdministrator())
            {
                FileOps.SetAnyDirSec(dataPath);
            }

            // setup custom assembly resolution for x86/x64 synamic compatybility
            //AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolveHandler;

            Thread.CurrentThread.Name = "Gui";

            client = new Priv10Client();

            if (!AdminFunc.IsAdministrator())
            {
                if (AdminFunc.SkipUacRun(App.Key, App.args))
                {
                    return;
                }

                if (App.GetConfigInt("Startup", "ShowSetup", 1) == 1)
                {
                    AppLog.Debug("Trying to restart as admin...");
                    if (Restart(true))
                    {
                        return;
                    }
                }
            }

            AppLog.Debug("Trying to connect to engine...");
            int conRes = client.Connect(1000);

            if (conRes == 0)
            {
                if (Priv10Service.IsProperlyInstalled())
                {
                    if (!AdminFunc.IsAdministrator())
                    {
                        MessageBox.Show(Translate.fmt("msg_admin_rights_svc", Title, SvcName), Title);
                        Restart(true);
                        return;
                    }

                    AppLog.Debug("Trying to start service...");
                    if (!Priv10Service.Startup())
                    {
                        AppLog.Debug("Failed to start service...");
                    }
                }
                else if (App.GetConfigInt("Firewall", "Enabled", 0) != 0)
                {
                    AppLog.Debug("Trying to start engine process...");
                    StartEngine();
                }


                AppLog.Debug("Trying to connect to engine...");
                if (client.Connect() != 0)
                {
                    AppLog.Debug("Connected to engine...");
                }
                else
                {
                    AppLog.Debug("Failed to connect to engine...");
                }
            }

            tweaks  = new TweakManager();
            presets = new PresetManager();

            var app = new App();

            app.InitializeComponent();

            InitLicense();

            MainWnd = new MainWindow();

            TrayIcon         = new TrayIcon();
            TrayIcon.Visible = (GetConfigInt("Startup", "Tray", 1) != 0) || App.TestArg("-autorun");

            if (!App.TestArg("-autorun") || !TrayIcon.Visible)
            {
                MainWnd.Show();
            }

            app.Run();

            TrayIcon.DestroyNotifyicon();

            presets.Store();
            tweaks.Store();

            if (EngineProc != null)
            {
                StopEngine();
            }
            else
            {
                client.Close();
            }
        }