From ba16d253087f19bbcf7e166315d2f818ef8bb8a1 Mon Sep 17 00:00:00 2001 From: Voltstro Date: Wed, 16 Oct 2024 21:46:46 +1000 Subject: [PATCH] Add ignore SSL errors options Move some web browser client options to a new section called "Advanced" --- .../Runtime/Core/WebBrowserClient.cs | 30 +++++++++++++++---- .../Main/Core/CefEngineControlsManager.cs | 2 ++ .../Browser/Popups/UwbCefEnginePopupInfo.cs | 12 ++++++-- .../Browser/Popups/UwbCefPopupClient.cs | 10 +++++-- .../Shared/Browser/UwbCefApp.cs | 7 +++-- .../Shared/Browser/UwbCefClient.cs | 28 +++++++++++------ .../Shared/Browser/UwbCefLifespanHandler.cs | 14 +++++++-- .../Shared/Browser/UwbCefRequestHandler.cs | 27 ++++++++++++++++- .../Packages/packages-lock.json | 10 +++---- .../Core/EngineEntryPoint.cs | 3 ++ .../Core/LaunchArguments.cs | 19 +++++++++--- .../Core/LaunchArgumentsBinder.cs | 12 ++++++++ .../Core/LaunchArgumentsParser.cs | 12 ++++++++ 13 files changed, 152 insertions(+), 34 deletions(-) diff --git a/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs b/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs index dc2c450..5361fa2 100644 --- a/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs +++ b/src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs @@ -114,11 +114,6 @@ public Resolution Resolution [Tooltip("Enable or disable incognito/private mode. When true, no profile-specific data is persisted to disk, but cache is still used to persist installation-specific data.")] public bool incognitoMode; - /// - /// Enable or disable WebRTC - /// - [Tooltip("Enable or disable WebRTC")] public bool webRtc; - /// /// Enable or disable local storage /// @@ -134,6 +129,12 @@ public Resolution Resolution /// Proxy Settings /// [Tooltip("Proxy settings")] public ProxySettings proxySettings; + + /// + /// Enable or disable WebRTC + /// + [Header("Advanced")] + [Tooltip("Enable or disable WebRTC")] public bool webRtc; /// /// Enable or disable remote debugging @@ -159,6 +160,18 @@ public Resolution Resolution [Tooltip("Manager for JS methods")] public JsMethodManager jsMethodManager = new(); + /// + /// Will ignore SSL errors on provided domains in + /// + [Tooltip("Will ignore SSL errors on provided domains in ignoreSSLErrorsDomains")] + public bool ignoreSslErrors = false; + + /// + /// Domains to ignore SSL errors on if is enabled + /// + [Tooltip("Domains to ignore SSL errors on if ignoreSSLErrors is enabled")] + public string[] ignoreSslErrorsDomains; + /// /// The to use /// @@ -385,6 +398,13 @@ internal void Init() if (!string.IsNullOrWhiteSpace(proxySettings.Password)) argsBuilder.AppendArgument("proxy-password", proxySettings.Password, true); } + + //Ignore ssl errors + if (ignoreSslErrors) + { + argsBuilder.AppendArgument("ignore-ssl-errors", true); + argsBuilder.AppendArgument("ignore-ssl-errors-domains", string.Join(",", ignoreSslErrorsDomains)); + } //Make sure not to include this, its for testing #if UWB_ENGINE_PRJ //Define for backup, cause I am dumb as fuck and gonna accidentally include this in a release build one day diff --git a/src/UnityWebBrowser.Engine.Cef/Main/Core/CefEngineControlsManager.cs b/src/UnityWebBrowser.Engine.Cef/Main/Core/CefEngineControlsManager.cs index 019e558..6d4abdf 100644 --- a/src/UnityWebBrowser.Engine.Cef/Main/Core/CefEngineControlsManager.cs +++ b/src/UnityWebBrowser.Engine.Cef/Main/Core/CefEngineControlsManager.cs @@ -222,6 +222,8 @@ public void Init(ClientControlsActions clientControlsActions, EnginePopupManager launchArguments.PopupAction, popupManager, new ProxySettings(launchArguments.ProxyUsername, launchArguments.ProxyPassword, launchArguments.ProxyEnabled), + launchArguments.IgnoreSslErrors, + launchArguments.IgnoreSslErrorsDomains, clientControlsActions, mainLogger, browserConsoleLogger); diff --git a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/Popups/UwbCefEnginePopupInfo.cs b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/Popups/UwbCefEnginePopupInfo.cs index 10abad5..905d148 100644 --- a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/Popups/UwbCefEnginePopupInfo.cs +++ b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/Popups/UwbCefEnginePopupInfo.cs @@ -24,12 +24,20 @@ public class UwbCefEnginePopupInfo : EnginePopupInfo /// /// /// - public UwbCefEnginePopupInfo(EnginePopupManager popupManager, ProxySettings proxySettings, ref CefClient client) + /// + /// + /// + public UwbCefEnginePopupInfo( + EnginePopupManager popupManager, + ProxySettings proxySettings, + ref CefClient client, + bool ignoreSslErrors, + string[] ignoreSslErrorsDomains) { this.popupManager = popupManager; //Create a new client for it, and properly create the window - popupClient = new UwbCefPopupClient(proxySettings, DisposeNoClose); + popupClient = new UwbCefPopupClient(proxySettings, DisposeNoClose, ignoreSslErrors, ignoreSslErrorsDomains); client = popupClient; } diff --git a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/Popups/UwbCefPopupClient.cs b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/Popups/UwbCefPopupClient.cs index c0c2b55..4f53ff7 100644 --- a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/Popups/UwbCefPopupClient.cs +++ b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/Popups/UwbCefPopupClient.cs @@ -22,9 +22,15 @@ public class UwbCefPopupClient : CefClient /// /// /// - public UwbCefPopupClient(ProxySettings proxySettings, Action onShutdown) + /// + /// + public UwbCefPopupClient( + ProxySettings proxySettings, + Action onShutdown, + bool ignoreSslErrors, + string[] ignoreSslErrorsDomains) { - requestHandler = new UwbCefRequestHandler(proxySettings); + requestHandler = new UwbCefRequestHandler(proxySettings, ignoreSslErrors, ignoreSslErrorsDomains); lifeSpanHandler = new UwbCefPopupLifeSpanHandler(onShutdown); } diff --git a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefApp.cs b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefApp.cs index 48f5408..ae70f45 100644 --- a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefApp.cs +++ b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefApp.cs @@ -3,6 +3,7 @@ // // This project is under the MIT license. See the LICENSE.md file for more details. +#nullable enable using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core; using Xilium.CefGlue; @@ -16,9 +17,9 @@ public class UwbCefApp : CefApp private readonly bool mediaStreamingEnabled; private readonly bool noProxyServer; private readonly bool remoteDebugging; - private readonly string[] remoteDebuggingOrigins; + private readonly string[]? remoteDebuggingOrigins; - private UwbCefBrowserProcessHandler browserProcessHandler; + private UwbCefBrowserProcessHandler browserProcessHandler = null!; public UwbCefApp(LaunchArguments launchArguments) { @@ -36,7 +37,7 @@ protected override void OnBeforeCommandLineProcessing(string processType, CefCom if (mediaStreamingEnabled && !commandLine.HasSwitch("--enable-media-stream")) commandLine.AppendSwitch("--enable-media-stream"); - if (remoteDebugging && !commandLine.HasSwitch("--remote-allow-origins")) + if (remoteDebugging && remoteDebuggingOrigins != null && !commandLine.HasSwitch("--remote-allow-origins")) commandLine.AppendSwitch("--remote-allow-origins", string.Join(',', remoteDebuggingOrigins)); #if LINUX || MACOS diff --git a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefClient.cs b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefClient.cs index 1736f6e..e5c7df1 100644 --- a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefClient.cs +++ b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefClient.cs @@ -46,6 +46,8 @@ internal class UwbCefClient : CefClient, IDisposable private UwbCefPopupClient devToolsClient; //Dev Tools + private readonly bool ignoreSslErrors; + private readonly string[] ignoreSslErrorsDomains; private CefWindowInfo devToolsWindowInfo; //State of mouse click events that needs to be persisted for dragging @@ -59,6 +61,8 @@ public UwbCefClient( PopupAction popupAction, EnginePopupManager popupManager, ProxySettings proxySettings, + bool ignoreSslErrors, + string[] ignoreSslErrorsDomains, ClientControlsActions clientControlsActions, ILogger mainLogger, ILogger browserConsoleLogger) @@ -72,16 +76,19 @@ public UwbCefClient( //Setup our handlers loadHandler = new UwbCefLoadHandler(this); renderHandler = new UwbCefRenderHandler(this, size); - lifespanHandler = new UwbCefLifespanHandler(popupAction, popupManager, proxySettings); + lifespanHandler = new UwbCefLifespanHandler(popupAction, popupManager, proxySettings, ignoreSslErrors, ignoreSslErrorsDomains); lifespanHandler.AfterCreated += cefBrowser => { browser = cefBrowser; browserHost = cefBrowser.GetHost(); }; displayHandler = new UwbCefDisplayHandler(this, mainLogger, browserConsoleLogger); - requestHandler = new UwbCefRequestHandler(proxySettings); + requestHandler = new UwbCefRequestHandler(proxySettings, ignoreSslErrors, ignoreSslErrorsDomains); contextMenuHandler = new UwbCefContextMenuHandler(); + this.ignoreSslErrors = ignoreSslErrors; + this.ignoreSslErrorsDomains = ignoreSslErrorsDomains; + //Create message types messageTypes = new Dictionary { @@ -306,7 +313,7 @@ public Vector2 GetMouseScrollPosition() /// Loads HTML content /// /// - public unsafe void LoadHtml(string html) + public void LoadHtml(string html) { html = CefRuntime.Base64Encode(Encoding.UTF8.GetBytes(html)); html = CefRuntime.UriEncode(html, false); @@ -351,12 +358,15 @@ public void OpenDevTools() if (devToolsWindowInfo == null) { devToolsWindowInfo = CefWindowInfo.Create(); - devToolsClient = new UwbCefPopupClient(proxySettings, () => - { - devToolsWindowInfo = null; - devToolsClient = null; - devToolsBrowserSettings = null; - }); + devToolsClient = new UwbCefPopupClient( + proxySettings, () => + { + devToolsWindowInfo = null; + devToolsClient = null; + devToolsBrowserSettings = null; + }, + ignoreSslErrors, + ignoreSslErrorsDomains); devToolsBrowserSettings = new CefBrowserSettings(); } diff --git a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefLifespanHandler.cs b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefLifespanHandler.cs index f4fe4ea..48e0757 100644 --- a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefLifespanHandler.cs +++ b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefLifespanHandler.cs @@ -22,13 +22,21 @@ public class UwbCefLifespanHandler : CefLifeSpanHandler private readonly EnginePopupManager popupManager; private readonly ProxySettings proxySettings; + private readonly bool ignoreSslErrors; + private readonly string[] ignoreSslErrorsDomains; - public UwbCefLifespanHandler(PopupAction popupAction, EnginePopupManager enginePopupManager, - ProxySettings proxySettings) + public UwbCefLifespanHandler( + PopupAction popupAction, + EnginePopupManager enginePopupManager, + ProxySettings proxySettings, + bool ignoreSslErrors, + string[] ignoreSslErrorsDomains) { this.proxySettings = proxySettings; this.popupAction = popupAction; popupManager = enginePopupManager; + this.ignoreSslErrors = ignoreSslErrors; + this.ignoreSslErrorsDomains = ignoreSslErrorsDomains; } public event Action AfterCreated; @@ -52,7 +60,7 @@ protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string case PopupAction.Ignore: break; case PopupAction.OpenExternalWindow: - popupManager.OnPopup(new UwbCefEnginePopupInfo(popupManager, proxySettings, ref client)); + popupManager.OnPopup(new UwbCefEnginePopupInfo(popupManager, proxySettings, ref client, ignoreSslErrors, ignoreSslErrorsDomains)); return false; case PopupAction.Redirect: frame.LoadUrl(targetUrl); diff --git a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefRequestHandler.cs b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefRequestHandler.cs index 7956b20..45ad9a7 100644 --- a/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefRequestHandler.cs +++ b/src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefRequestHandler.cs @@ -3,6 +3,9 @@ // // This project is under the MIT license. See the LICENSE.md file for more details. +using System.Linq; +using System.Threading.Tasks; +using UnityWebBrowser.Engine.Cef.Core; using VoltstroStudios.UnityWebBrowser.Shared; using Xilium.CefGlue; @@ -14,10 +17,14 @@ namespace UnityWebBrowser.Engine.Cef.Shared.Browser; public class UwbCefRequestHandler : CefRequestHandler { private readonly ProxySettings proxySettings; + private readonly bool ignoreSslErrors; + private readonly string[] ignoreSslErrorsDomains; - public UwbCefRequestHandler(ProxySettings proxySettings) + public UwbCefRequestHandler(ProxySettings proxySettings, bool ignoreSslErrors, string[] ignoreSslErrorsDomains) { this.proxySettings = proxySettings; + this.ignoreSslErrors = ignoreSslErrors; + this.ignoreSslErrorsDomains = ignoreSslErrorsDomains; } protected override CefResourceRequestHandler GetResourceRequestHandler(CefBrowser browser, CefFrame frame, @@ -35,4 +42,22 @@ protected override bool GetAuthCredentials(CefBrowser browser, string originUrl, return base.GetAuthCredentials(browser, originUrl, isProxy, host, port, realm, scheme, callback); } + + protected override bool OnCertificateError(CefBrowser browser, CefErrorCode certError, string requestUrl, CefSslInfo sslInfo, + CefCallback callback) + { + if (ignoreSslErrors && ignoreSslErrorsDomains != null) + { + requestUrl = requestUrl!.ToLower(); + bool contains = ignoreSslErrorsDomains.Any(x => requestUrl.Contains(x)); + if(contains) + callback!.Continue(); + else + callback!.Cancel(); + + return true; + } + + return false; + } } \ No newline at end of file diff --git a/src/UnityWebBrowser.UnityProject/Packages/packages-lock.json b/src/UnityWebBrowser.UnityProject/Packages/packages-lock.json index a65e545..4e436c1 100644 --- a/src/UnityWebBrowser.UnityProject/Packages/packages-lock.json +++ b/src/UnityWebBrowser.UnityProject/Packages/packages-lock.json @@ -167,7 +167,7 @@ "depth": 0, "source": "local", "dependencies": { - "dev.voltstro.unitywebbrowser": "2.2.0" + "dev.voltstro.unitywebbrowser": "2.2.1" } }, "dev.voltstro.unitywebbrowser.engine.cef.linux.x64": { @@ -175,7 +175,7 @@ "depth": 0, "source": "local", "dependencies": { - "dev.voltstro.unitywebbrowser.engine.cef": "2.2.0-128.4.9" + "dev.voltstro.unitywebbrowser.engine.cef": "2.2.1-129.0.11" } }, "dev.voltstro.unitywebbrowser.engine.cef.macos.arm64": { @@ -183,7 +183,7 @@ "depth": 0, "source": "local", "dependencies": { - "dev.voltstro.unitywebbrowser.engine.cef": "2.2.0-128.4.9" + "dev.voltstro.unitywebbrowser.engine.cef": "2.2.1-129.0.11" } }, "dev.voltstro.unitywebbrowser.engine.cef.macos.x64": { @@ -191,7 +191,7 @@ "depth": 0, "source": "local", "dependencies": { - "dev.voltstro.unitywebbrowser.engine.cef": "2.2.0-128.4.9" + "dev.voltstro.unitywebbrowser.engine.cef": "2.2.1-129.0.11" } }, "dev.voltstro.unitywebbrowser.engine.cef.win.x64": { @@ -199,7 +199,7 @@ "depth": 0, "source": "local", "dependencies": { - "dev.voltstro.unitywebbrowser.engine.cef": "2.2.0-128.4.9" + "dev.voltstro.unitywebbrowser.engine.cef": "2.2.1-129.0.11" } }, "dev.voltstro.unitywebbrowser.unix-support": { diff --git a/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/EngineEntryPoint.cs b/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/EngineEntryPoint.cs index 949420b..8aa2086 100644 --- a/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/EngineEntryPoint.cs +++ b/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/EngineEntryPoint.cs @@ -89,6 +89,9 @@ public int Main(string[] args) ClientControlsActions = new ClientControlsActions(); PopupManager = new EnginePopupManager(); + if(parsedArgs.IgnoreSslErrors && parsedArgs.IgnoreSslErrorsDomains != null) + engineLogger.LogWarning("Ignore Ssl Errors is enabled! Proceed with caution on these domains: {Domains}", string.Join(", ", parsedArgs.IgnoreSslErrorsDomains)); + EarlyInit(parsedArgs, args); EntryPoint(parsedArgs, args); diff --git a/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArguments.cs b/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArguments.cs index 70a5c9b..9a54072 100644 --- a/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArguments.cs +++ b/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArguments.cs @@ -3,6 +3,7 @@ // // This project is under the MIT license. See the LICENSE.md file for more details. +#nullable enable using System.IO; using VoltstroStudios.UnityWebBrowser.Shared; using VoltstroStudios.UnityWebBrowser.Shared.Popups; @@ -62,7 +63,7 @@ public class LaunchArguments /// /// Remote debugging allowed origins /// - public string[] RemoteDebuggingAllowedOrigins { get; init; } + public string[]? RemoteDebuggingAllowedOrigins { get; init; } /// /// The to use for the background @@ -77,7 +78,7 @@ public class LaunchArguments /// /// The path you should use for your cache /// - public FileInfo CachePath { get; init; } + public FileInfo? CachePath { get; init; } /// /// Should we use a proxy or direct @@ -87,12 +88,22 @@ public class LaunchArguments /// /// Username of the proxy /// - public string ProxyUsername { get; init; } + public string? ProxyUsername { get; init; } /// /// Password of the proxy /// - public string ProxyPassword { get; init; } + public string? ProxyPassword { get; init; } + + /// + /// Will ignore SSL errors on provided domains in + /// + public bool IgnoreSslErrors { get; init; } + + /// + /// Domains to ignore if is enabled + /// + public string[]? IgnoreSslErrorsDomains { get; set; } /// /// The path you should log browser events to diff --git a/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArgumentsBinder.cs b/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArgumentsBinder.cs index a9db4ef..706efe4 100644 --- a/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArgumentsBinder.cs +++ b/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArgumentsBinder.cs @@ -41,6 +41,10 @@ internal class LaunchArgumentsBinder : BinderBase private readonly Option proxyServer; private readonly Option proxyUsername; private readonly Option proxyPassword; + + //Ignore SSL Error Settings + private readonly Option ignoreSslErrors; + private readonly Option ignoreSslErrorsDomains; //Logging private readonly Option logPath; @@ -71,6 +75,8 @@ public LaunchArgumentsBinder( Option proxyServer, Option proxyUsername, Option proxyPassword, + Option ignoreSslErrors, + Option ignoreSslErrorsDomains, Option logPath, Option logSeverity, Option communicationLayerName, @@ -97,6 +103,9 @@ public LaunchArgumentsBinder( this.proxyServer = proxyServer; this.proxyUsername = proxyUsername; this.proxyPassword = proxyPassword; + + this.ignoreSslErrors = ignoreSslErrors; + this.ignoreSslErrorsDomains = ignoreSslErrorsDomains; this.logPath = logPath; this.logSeverity = logSeverity; @@ -133,6 +142,9 @@ protected override LaunchArguments GetBoundValue(BindingContext bindingContext) ProxyUsername = bindingContext.ParseResult.GetValueForOption(proxyUsername), ProxyPassword = bindingContext.ParseResult.GetValueForOption(proxyPassword), + IgnoreSslErrors = bindingContext.ParseResult.GetValueForOption(ignoreSslErrors), + IgnoreSslErrorsDomains = bindingContext.ParseResult.GetValueForOption(ignoreSslErrorsDomains), + LogPath = bindingContext.ParseResult.GetValueForOption(logPath), LogSeverity = bindingContext.ParseResult.GetValueForOption(logSeverity), diff --git a/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArgumentsParser.cs b/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArgumentsParser.cs index f432feb..4212feb 100644 --- a/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArgumentsParser.cs +++ b/src/VoltstroStudios.UnityWebBrowser.Engine.Shared/Core/LaunchArgumentsParser.cs @@ -80,6 +80,14 @@ public LaunchArgumentsParser() () => null, "The password to use in the proxy auth"); + //Ignore SSL Errors + Option ignoreSslErrors = new("-ignore-ssl-errors", + () => false, + "Will ignore SSL errors on provided domains in ignoreSSLErrorsDomains"); + Option ignoreSslErrorsDomains = new("-ignore-ssl-errors-domains", + () => null, + "Domains to ignore SSL errors on if ignoreSSLErrors is enabled"); + //Logging Option logPath = new("-log-path", () => new FileInfo("engine.log"), @@ -121,6 +129,8 @@ public LaunchArgumentsParser() proxyServer, proxyUsername, proxyPassword, + ignoreSslErrors, + ignoreSslErrorsDomains, logPath, logSeverity, communicationLayerName, @@ -150,6 +160,8 @@ public LaunchArgumentsParser() proxyServer, proxyUsername, proxyPassword, + ignoreSslErrors, + ignoreSslErrorsDomains, logPath, logSeverity, communicationLayerName,