diff options
author | sr55 <[email protected]> | 2020-06-19 20:04:26 +0100 |
---|---|---|
committer | sr55 <[email protected]> | 2020-06-19 20:04:26 +0100 |
commit | 0dc75f3ec8a6724a5c0315003948d1957dffe0f2 (patch) | |
tree | 3b4693f5441ae51ed6b6a444f0328e233caa72fc /win/CS | |
parent | d8d6db8a08e6e3086bf6ba1c2a5894df940965bf (diff) |
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
Diffstat (limited to 'win/CS')
-rw-r--r-- | win/CS/HandBrake.Worker/HttpServer.cs | 27 | ||||
-rw-r--r-- | win/CS/HandBrake.Worker/Program.cs | 18 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Instance/RemoteInstance.cs | 154 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Properties/Resources.Designer.cs | 9 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Properties/Resources.resx | 3 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Services/UserSettingService.cs | 7 | ||||
-rw-r--r-- | win/CS/HandBrakeWPF/Views/OptionsView.xaml | 8 |
7 files changed, 142 insertions, 84 deletions
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<string, Func<HttpListenerRequest, string>> apiHandlers; + + private bool failedStart = false; public HttpServer(Dictionary<string, Func<HttpListenerRequest, string>> 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<string, Func<HttpListenerRequest, string>> 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<string, Func<HttpListenerRequest, string>> 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<bool>(UserSettingConstants.DisableLibDvdNav), - EnableHardwareAcceleration = true, - LogDirectory = DirectoryUtilities.GetLogDirectory(), - LogVerbosity = this.userSettingService.GetUserSetting<int>(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<int>(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<int>(UserSettingConstants.ProcessIsolationPort)); + this.serverUrl = string.Format("http://127.0.0.1:{0}/", this.port); - // Set Process Priority - switch ((ProcessPriority)this.userSettingService.GetUserSetting<int>(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<int>( + 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<bool>(UserSettingConstants.DisableLibDvdNav), + EnableHardwareAcceleration = true, + LogDirectory = DirectoryUtilities.GetLogDirectory(), + LogVerbosity = this.userSettingService.GetUserSetting<int>(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 @@ -3664,6 +3664,15 @@ namespace HandBrakeWPF.Properties { } /// <summary> + /// Looks up a localized string similar to ( Windows 10 Only! ). + /// </summary> + public static string OptionsView_Win10Only { + get { + return ResourceManager.GetString("OptionsView_Win10Only", resourceCulture); + } + } + + /// <summary> /// Looks up a localized string similar to Default network port for worker:. /// </summary> public static string OptionsView_WorkerDefaultPort { 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.</value> <data name="PictureSettingsView_RotateAndCrop" xml:space="preserve">
<value>Rotate and Crop</value>
</data>
+ <data name="OptionsView_Win10Only" xml:space="preserve">
+ <value>( Windows 10 Only! )</value>
+ </data>
</root>
\ 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;
/// <summary>
/// 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 @@ </StackPanel>
</StackPanel>
- <StackPanel Orientation="Vertical" Visibility="{Binding IsProcessIsolationAllowed, Converter={StaticResource boolToVisConverter}}">
+ <StackPanel Orientation="Vertical" Visibility="{Binding IsProcessIsolationAllowed, Converter={StaticResource boolToVisConverter}}" IsEnabled="{Binding IsWindows10}">
- <TextBlock Text="{x:Static Properties:Resources.OptionsView_ProcessIsolation}" FontSize="14" Margin="0,10,0,10"/>
+ <StackPanel Orientation="Horizontal">
+ <TextBlock Text="{x:Static Properties:Resources.OptionsView_ProcessIsolation}" FontSize="14" Margin="0,10,0,10"/>
+ <TextBlock Text="{x:Static Properties:Resources.OptionsView_Win10Only}" Margin="15,10,0,10" Visibility="{Binding IsWindows10, Converter={StaticResource boolToVisConverter}, ConverterParameter=true}" />
+ </StackPanel>
+
<TextBlock Text="{x:Static Properties:Resources.OptionsView_ProcessIsolation_Warning1}" Margin="10,0,0,0" TextWrapping="Wrap" FontStyle="Italic" />
<TextBlock Text="{x:Static Properties:Resources.OptionsView_ProcessIsolation_Warning2}" Margin="10,0,0,0" TextWrapping="Wrap" FontStyle="Italic" />
|