Skip to content

Commit

Permalink
Add ignore SSL errors options
Browse files Browse the repository at this point in the history
Move some web browser client options to a new section called "Advanced"
  • Loading branch information
Voltstro committed Oct 16, 2024
1 parent e9c8f53 commit ba16d25
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 34 deletions.
30 changes: 25 additions & 5 deletions src/Packages/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Enable or disable WebRTC
/// </summary>
[Tooltip("Enable or disable WebRTC")] public bool webRtc;

/// <summary>
/// Enable or disable local storage
/// </summary>
Expand All @@ -134,6 +129,12 @@ public Resolution Resolution
/// Proxy Settings
/// </summary>
[Tooltip("Proxy settings")] public ProxySettings proxySettings;

/// <summary>
/// Enable or disable WebRTC
/// </summary>
[Header("Advanced")]
[Tooltip("Enable or disable WebRTC")] public bool webRtc;

/// <summary>
/// Enable or disable remote debugging
Expand All @@ -159,6 +160,18 @@ public Resolution Resolution
[Tooltip("Manager for JS methods")]
public JsMethodManager jsMethodManager = new();

/// <summary>
/// Will ignore SSL errors on provided domains in <see cref="ignoreSslErrorsDomains"/>
/// </summary>
[Tooltip("Will ignore SSL errors on provided domains in ignoreSSLErrorsDomains")]
public bool ignoreSslErrors = false;

/// <summary>
/// Domains to ignore SSL errors on if <see cref="ignoreSslErrors"/> is enabled
/// </summary>
[Tooltip("Domains to ignore SSL errors on if ignoreSSLErrors is enabled")]
public string[] ignoreSslErrorsDomains;

/// <summary>
/// The <see cref="CommunicationLayer" /> to use
/// </summary>
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ public class UwbCefEnginePopupInfo : EnginePopupInfo
/// </summary>
/// <param name="proxySettings"></param>
/// <param name="popupManager"></param>
public UwbCefEnginePopupInfo(EnginePopupManager popupManager, ProxySettings proxySettings, ref CefClient client)
/// <param name="client"></param>
/// <param name="ignoreSslErrors"></param>
/// <param name="ignoreSslErrorsDomains"></param>
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ public class UwbCefPopupClient : CefClient
/// </summary>
/// <param name="proxySettings"></param>
/// <param name="onShutdown"></param>
public UwbCefPopupClient(ProxySettings proxySettings, Action onShutdown)
/// <param name="ignoreSslErrors"></param>
/// <param name="ignoreSslErrorsDomains"></param>
public UwbCefPopupClient(
ProxySettings proxySettings,
Action onShutdown,
bool ignoreSslErrors,
string[] ignoreSslErrorsDomains)
{
requestHandler = new UwbCefRequestHandler(proxySettings);
requestHandler = new UwbCefRequestHandler(proxySettings, ignoreSslErrors, ignoreSslErrorsDomains);
lifeSpanHandler = new UwbCefPopupLifeSpanHandler(onShutdown);
}

Expand Down
7 changes: 4 additions & 3 deletions src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
{
Expand All @@ -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
Expand Down
28 changes: 19 additions & 9 deletions src/UnityWebBrowser.Engine.Cef/Shared/Browser/UwbCefClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,6 +61,8 @@ public UwbCefClient(
PopupAction popupAction,
EnginePopupManager popupManager,
ProxySettings proxySettings,
bool ignoreSslErrors,
string[] ignoreSslErrorsDomains,
ClientControlsActions clientControlsActions,
ILogger mainLogger,
ILogger browserConsoleLogger)
Expand All @@ -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<string, IMessageBase>
{
Expand Down Expand Up @@ -306,7 +313,7 @@ public Vector2 GetMouseScrollPosition()
/// Loads HTML content
/// </summary>
/// <param name="html"></param>
public unsafe void LoadHtml(string html)
public void LoadHtml(string html)
{
html = CefRuntime.Base64Encode(Encoding.UTF8.GetBytes(html));
html = CefRuntime.UriEncode(html, false);
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CefBrowser> AfterCreated;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand All @@ -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;
}
}
10 changes: 5 additions & 5 deletions src/UnityWebBrowser.UnityProject/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,39 +167,39 @@
"depth": 0,
"source": "local",
"dependencies": {
"dev.voltstro.unitywebbrowser": "2.2.0"
"dev.voltstro.unitywebbrowser": "2.2.1"
}
},
"dev.voltstro.unitywebbrowser.engine.cef.linux.x64": {
"version": "file:../../Packages/UnityWebBrowser.Engine.Cef.Linux-x64",
"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": {
"version": "file:../../Packages/UnityWebBrowser.Engine.Cef.MacOS-arm64",
"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": {
"version": "file:../../Packages/UnityWebBrowser.Engine.Cef.MacOS-x64",
"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": {
"version": "file:../../Packages/UnityWebBrowser.Engine.Cef.Win-x64",
"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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,7 +63,7 @@ public class LaunchArguments
/// <summary>
/// Remote debugging allowed origins
/// </summary>
public string[] RemoteDebuggingAllowedOrigins { get; init; }
public string[]? RemoteDebuggingAllowedOrigins { get; init; }

/// <summary>
/// The <see cref="Color" /> to use for the background
Expand All @@ -77,7 +78,7 @@ public class LaunchArguments
/// <summary>
/// The path you should use for your cache
/// </summary>
public FileInfo CachePath { get; init; }
public FileInfo? CachePath { get; init; }

/// <summary>
/// Should we use a proxy or direct
Expand All @@ -87,12 +88,22 @@ public class LaunchArguments
/// <summary>
/// Username of the proxy
/// </summary>
public string ProxyUsername { get; init; }
public string? ProxyUsername { get; init; }

/// <summary>
/// Password of the proxy
/// </summary>
public string ProxyPassword { get; init; }
public string? ProxyPassword { get; init; }

/// <summary>
/// Will ignore SSL errors on provided domains in <see cref="IgnoreSslErrorsDomains"/>
/// </summary>
public bool IgnoreSslErrors { get; init; }

/// <summary>
/// Domains to ignore if <see cref="IgnoreSslErrors"/> is enabled
/// </summary>
public string[]? IgnoreSslErrorsDomains { get; set; }

/// <summary>
/// The path you should log browser events to
Expand Down
Loading

0 comments on commit ba16d25

Please sign in to comment.