From 0dc75f3ec8a6724a5c0315003948d1957dffe0f2 Mon Sep 17 00:00:00 2001 From: sr55 Date: Fri, 19 Jun 2020 20:04:26 +0100 Subject: WinGui: Prevent Process isolation feature from being enabled on Windows 7 or Windows 8. We are only going to support this on 10 or later. Fixes #2940 --- win/CS/HandBrake.Worker/HttpServer.cs | 27 +++- win/CS/HandBrake.Worker/Program.cs | 18 +-- win/CS/HandBrakeWPF/Instance/RemoteInstance.cs | 154 ++++++++++++--------- .../HandBrakeWPF/Properties/Resources.Designer.cs | 9 ++ win/CS/HandBrakeWPF/Properties/Resources.resx | 3 + win/CS/HandBrakeWPF/Services/UserSettingService.cs | 7 +- win/CS/HandBrakeWPF/Views/OptionsView.xaml | 8 +- 7 files changed, 142 insertions(+), 84 deletions(-) (limited to 'win/CS') diff --git a/win/CS/HandBrake.Worker/HttpServer.cs b/win/CS/HandBrake.Worker/HttpServer.cs index ef8811ab6..156265f36 100644 --- a/win/CS/HandBrake.Worker/HttpServer.cs +++ b/win/CS/HandBrake.Worker/HttpServer.cs @@ -20,9 +20,10 @@ namespace HandBrake.Worker public class HttpServer { private readonly string uiToken; - private readonly HttpListener httpListener = new HttpListener(); private readonly Dictionary> apiHandlers; + + private bool failedStart = false; public HttpServer(Dictionary> apiCalls, int port, string token) { @@ -45,13 +46,27 @@ namespace HandBrake.Worker } Debug.WriteLine(Environment.NewLine); - - this.httpListener.Start(); + + try + { + this.httpListener.Start(); + } + catch (Exception e) + { + failedStart = true; + Console.WriteLine(string.Format("Worker: Unable to start HTTP Server. Mabye the port {0} is in use?", port)); + Console.WriteLine("Worker Exception: " + e); + } } - public void Run() + public bool Run() { - ThreadPool.QueueUserWorkItem((o) => + if (this.failedStart) + { + return false; + } + + ThreadPool.QueueUserWorkItem(o => { try { @@ -119,6 +134,8 @@ namespace HandBrake.Worker Console.WriteLine("Worker: " + exc); } }); + + return true; } public void Stop() diff --git a/win/CS/HandBrake.Worker/Program.cs b/win/CS/HandBrake.Worker/Program.cs index a71192a41..355ac6d46 100644 --- a/win/CS/HandBrake.Worker/Program.cs +++ b/win/CS/HandBrake.Worker/Program.cs @@ -12,7 +12,6 @@ namespace HandBrake.Worker using System; using System.Collections.Generic; using System.Net; - using System.Runtime.CompilerServices; using System.Threading; using HandBrake.Worker.Routing; @@ -54,13 +53,16 @@ namespace HandBrake.Worker Console.WriteLine("Worker: Starting Web Server on port {0} ...", port); Dictionary> apiHandlers = RegisterApiHandlers(); HttpServer webServer = new HttpServer(apiHandlers, port, token); - webServer.Run(); - - Console.WriteLine("Worker: Server Started"); - - manualResetEvent.WaitOne(); - - webServer.Stop(); + if (webServer.Run()) + { + Console.WriteLine("Worker: Server Started"); + manualResetEvent.WaitOne();webServer.Stop(); + webServer.Stop(); + } + else + { + Console.WriteLine("Worker: Failed to start. Exiting ..."); + } } private static Dictionary> RegisterApiHandlers() diff --git a/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs b/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs index ce9df2fc9..01a91c948 100644 --- a/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs +++ b/win/CS/HandBrakeWPF/Instance/RemoteInstance.cs @@ -76,29 +76,6 @@ namespace HandBrakeWPF.Instance thread1.Start(); } - private void RunEncodeInitProcess(JsonEncodeObject jobToStart) - { - InitCommand initCommand = new InitCommand - { - EnableDiskLogging = false, - AllowDisconnectedWorker = false, - DisableLibDvdNav = !this.userSettingService.GetUserSetting(UserSettingConstants.DisableLibDvdNav), - EnableHardwareAcceleration = true, - LogDirectory = DirectoryUtilities.GetLogDirectory(), - LogVerbosity = this.userSettingService.GetUserSetting(UserSettingConstants.Verbosity) - }; - - initCommand.LogFile = Path.Combine(initCommand.LogDirectory, string.Format("activity_log.worker.{0}.txt", GeneralUtilities.ProcessId)); - - JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; - - string job = JsonConvert.SerializeObject(new EncodeCommand { InitialiseCommand = initCommand, EncodeJob = jobToStart }, Formatting.None, settings); - - var task = Task.Run(async () => await this.MakeHttpJsonPostRequest("StartEncode", job)); - task.Wait(); - this.MonitorEncodeProgress(); - } - public async void StopEncode() { await this.MakeHttpGetRequest("StopEncode"); @@ -122,54 +99,64 @@ namespace HandBrakeWPF.Instance public void Initialize(int verbosityLvl, bool noHardwareMode) { - if (this.workerProcess == null || this.workerProcess.HasExited) + try { - var plainTextBytes = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()); - this.base64Token = Convert.ToBase64String(plainTextBytes); - this.port = this.portService.GetOpenPort(userSettingService.GetUserSetting(UserSettingConstants.ProcessIsolationPort)); - this.serverUrl = string.Format("http://127.0.0.1:{0}/", this.port); - - workerProcess = new Process + if (this.workerProcess == null || this.workerProcess.HasExited) { - StartInfo = - { - FileName = "HandBrake.Worker.exe", - Arguments = string.Format(" --port={0} --token={1}", port, this.base64Token), - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true, - CreateNoWindow = true - } - }; - workerProcess.Exited += this.WorkerProcess_Exited; - workerProcess.OutputDataReceived += this.WorkerProcess_OutputDataReceived; - workerProcess.ErrorDataReceived += this.WorkerProcess_OutputDataReceived; - - workerProcess.Start(); - workerProcess.BeginOutputReadLine(); - workerProcess.BeginErrorReadLine(); + var plainTextBytes = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()); + this.base64Token = Convert.ToBase64String(plainTextBytes); + this.port = this.portService.GetOpenPort(userSettingService.GetUserSetting(UserSettingConstants.ProcessIsolationPort)); + this.serverUrl = string.Format("http://127.0.0.1:{0}/", this.port); - // Set Process Priority - switch ((ProcessPriority)this.userSettingService.GetUserSetting(UserSettingConstants.ProcessPriorityInt)) - { - case ProcessPriority.High: - workerProcess.PriorityClass = ProcessPriorityClass.High; - break; - case ProcessPriority.AboveNormal: - workerProcess.PriorityClass = ProcessPriorityClass.AboveNormal; - break; - case ProcessPriority.Normal: - workerProcess.PriorityClass = ProcessPriorityClass.Normal; - break; - case ProcessPriority.Low: - workerProcess.PriorityClass = ProcessPriorityClass.Idle; - break; - default: - workerProcess.PriorityClass = ProcessPriorityClass.BelowNormal; - break; + workerProcess = new Process + { + StartInfo = + { + FileName = "HandBrake.Worker.exe", + Arguments = + string.Format(" --port={0} --token={1}", port, this.base64Token), + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + CreateNoWindow = true + } + }; + workerProcess.Exited += this.WorkerProcess_Exited; + workerProcess.OutputDataReceived += this.WorkerProcess_OutputDataReceived; + workerProcess.ErrorDataReceived += this.WorkerProcess_OutputDataReceived; + + workerProcess.Start(); + workerProcess.BeginOutputReadLine(); + workerProcess.BeginErrorReadLine(); + + // Set Process Priority + switch ((ProcessPriority)this.userSettingService.GetUserSetting( + UserSettingConstants.ProcessPriorityInt)) + { + case ProcessPriority.High: + workerProcess.PriorityClass = ProcessPriorityClass.High; + break; + case ProcessPriority.AboveNormal: + workerProcess.PriorityClass = ProcessPriorityClass.AboveNormal; + break; + case ProcessPriority.Normal: + workerProcess.PriorityClass = ProcessPriorityClass.Normal; + break; + case ProcessPriority.Low: + workerProcess.PriorityClass = ProcessPriorityClass.Idle; + break; + default: + workerProcess.PriorityClass = ProcessPriorityClass.BelowNormal; + break; + } + + this.logService.LogMessage(string.Format("Remote Process started with Process ID: {0} and port: {1}", this.workerProcess.Id, port)); } - - this.logService.LogMessage(string.Format("Worker Process started with Process ID: {0} and port: {1}", this.workerProcess.Id, port)); + } + catch (Exception e) + { + this.logService.LogMessage("Unable to start worker process."); + this.logService.LogMessage(e.ToString()); } } @@ -204,7 +191,15 @@ namespace HandBrakeWPF.Instance private void StopPollingProgress() { - this.PollEncodeProgress(); // Get the last progress state. + try + { + this.PollEncodeProgress(); // Get the last progress state. + } + catch (Exception exc) + { + Debug.WriteLine(exc); + } + this.encodePollTimer?.Stop(); } @@ -290,5 +285,28 @@ namespace HandBrakeWPF.Instance this.portService.FreePort(this.port); } } + + private void RunEncodeInitProcess(JsonEncodeObject jobToStart) + { + InitCommand initCommand = new InitCommand + { + EnableDiskLogging = false, + AllowDisconnectedWorker = false, + DisableLibDvdNav = !this.userSettingService.GetUserSetting(UserSettingConstants.DisableLibDvdNav), + EnableHardwareAcceleration = true, + LogDirectory = DirectoryUtilities.GetLogDirectory(), + LogVerbosity = this.userSettingService.GetUserSetting(UserSettingConstants.Verbosity) + }; + + initCommand.LogFile = Path.Combine(initCommand.LogDirectory, string.Format("activity_log.worker.{0}.txt", GeneralUtilities.ProcessId)); + + JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; + + string job = JsonConvert.SerializeObject(new EncodeCommand { InitialiseCommand = initCommand, EncodeJob = jobToStart }, Formatting.None, settings); + + var task = Task.Run(async () => await this.MakeHttpJsonPostRequest("StartEncode", job)); + task.Wait(); + this.MonitorEncodeProgress(); + } } } diff --git a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs index 249f7d5f5..f56f22cd0 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs +++ b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs @@ -3663,6 +3663,15 @@ namespace HandBrakeWPF.Properties { } } + /// + /// Looks up a localized string similar to ( Windows 10 Only! ). + /// + public static string OptionsView_Win10Only { + get { + return ResourceManager.GetString("OptionsView_Win10Only", resourceCulture); + } + } + /// /// Looks up a localized string similar to Default network port for worker:. /// diff --git a/win/CS/HandBrakeWPF/Properties/Resources.resx b/win/CS/HandBrakeWPF/Properties/Resources.resx index eaf8f446b..e9561f778 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.resx +++ b/win/CS/HandBrakeWPF/Properties/Resources.resx @@ -2268,4 +2268,7 @@ Please choose a different preset. Rotate and Crop + + ( Windows 10 Only! ) + \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Services/UserSettingService.cs b/win/CS/HandBrakeWPF/Services/UserSettingService.cs index 2e29811ba..a28ed12cb 100644 --- a/win/CS/HandBrakeWPF/Services/UserSettingService.cs +++ b/win/CS/HandBrakeWPF/Services/UserSettingService.cs @@ -28,6 +28,7 @@ namespace HandBrakeWPF.Services using GeneralApplicationException = Exceptions.GeneralApplicationException; using SettingChangedEventArgs = EventArgs.SettingChangedEventArgs; + using SystemInfo = HandBrakeWPF.Utilities.SystemInfo; /// /// The User Setting Service @@ -219,6 +220,10 @@ namespace HandBrakeWPF.Services // Legacy Settings forced Reset. this.userSettings[UserSettingConstants.ScalingMode] = VideoScaler.Lanczos; + if (!SystemInfo.IsWindows10()) + { + this.userSettings[UserSettingConstants.ProcessIsolationEnabled] = false; + } } catch (Exception exc) { @@ -317,7 +322,7 @@ namespace HandBrakeWPF.Services defaults.Add(UserSettingConstants.DefaultPlayer, false); // Experimental - defaults.Add(UserSettingConstants.ProcessIsolationEnabled, true); + defaults.Add(UserSettingConstants.ProcessIsolationEnabled, SystemInfo.IsWindows10() ? true : false); defaults.Add(UserSettingConstants.ProcessIsolationPort, 8037); defaults.Add(UserSettingConstants.SimultaneousEncodes, 1); diff --git a/win/CS/HandBrakeWPF/Views/OptionsView.xaml b/win/CS/HandBrakeWPF/Views/OptionsView.xaml index 1135c6311..d5ad61f31 100644 --- a/win/CS/HandBrakeWPF/Views/OptionsView.xaml +++ b/win/CS/HandBrakeWPF/Views/OptionsView.xaml @@ -420,9 +420,13 @@ - + - + + + + + -- cgit v1.2.3