2017-07-14 10:28:59 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2017-07-14 10:28:59 +02:00
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
using System.Collections.Generic;
|
2021-12-23 15:21:05 +01:00
|
|
|
|
using System.Globalization;
|
2020-02-21 15:11:35 +01:00
|
|
|
|
using System.IO;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
using System.Linq;
|
2021-05-11 01:32:50 +02:00
|
|
|
|
using System.Threading;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
using CefSharp;
|
2019-01-08 14:30:38 +01:00
|
|
|
|
using CefSharp.WinForms;
|
2019-08-30 12:30:00 +02:00
|
|
|
|
using SafeExamBrowser.Applications.Contracts.Events;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Browser.Contracts;
|
2019-09-06 08:13:27 +02:00
|
|
|
|
using SafeExamBrowser.Browser.Contracts.Events;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
using SafeExamBrowser.Browser.Events;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Configuration.Contracts;
|
2020-10-05 23:37:23 +02:00
|
|
|
|
using SafeExamBrowser.Configuration.Contracts.Cryptography;
|
2021-04-16 10:47:10 +02:00
|
|
|
|
using SafeExamBrowser.Core.Contracts.Resources.Icons;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.I18n.Contracts;
|
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2023-03-08 00:01:20 +01:00
|
|
|
|
using SafeExamBrowser.Settings;
|
2019-12-18 08:24:55 +01:00
|
|
|
|
using SafeExamBrowser.Settings.Browser.Proxy;
|
2019-09-06 09:39:28 +02:00
|
|
|
|
using SafeExamBrowser.Settings.Logging;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.UserInterface.Contracts;
|
2020-01-22 15:16:11 +01:00
|
|
|
|
using SafeExamBrowser.UserInterface.Contracts.FileSystemDialog;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.UserInterface.Contracts.MessageBox;
|
2020-09-21 19:22:15 +02:00
|
|
|
|
using SafeExamBrowser.WindowsApi.Contracts;
|
2019-09-06 09:39:28 +02:00
|
|
|
|
using BrowserSettings = SafeExamBrowser.Settings.Browser.BrowserSettings;
|
2017-07-14 10:28:59 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Browser
|
|
|
|
|
{
|
2019-08-30 12:30:00 +02:00
|
|
|
|
public class BrowserApplication : IBrowserApplication
|
2017-07-14 10:28:59 +02:00
|
|
|
|
{
|
2022-02-04 13:41:11 +01:00
|
|
|
|
private int windowIdCounter = default;
|
2018-08-31 07:49:41 +02:00
|
|
|
|
|
2021-10-18 12:06:10 +02:00
|
|
|
|
private readonly AppConfig appConfig;
|
2024-03-04 14:27:49 +01:00
|
|
|
|
private readonly Clipboard clipboard;
|
2021-10-18 12:06:10 +02:00
|
|
|
|
private readonly IFileSystemDialog fileSystemDialog;
|
|
|
|
|
private readonly IHashAlgorithm hashAlgorithm;
|
|
|
|
|
private readonly IKeyGenerator keyGenerator;
|
|
|
|
|
private readonly IModuleLogger logger;
|
|
|
|
|
private readonly IMessageBox messageBox;
|
|
|
|
|
private readonly INativeMethods nativeMethods;
|
2023-03-08 00:01:20 +01:00
|
|
|
|
private readonly SessionMode sessionMode;
|
2021-10-18 12:06:10 +02:00
|
|
|
|
private readonly BrowserSettings settings;
|
|
|
|
|
private readonly IText text;
|
|
|
|
|
private readonly IUserInterfaceFactory uiFactory;
|
2022-02-04 13:41:11 +01:00
|
|
|
|
private readonly List<BrowserWindow> windows;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
|
2019-12-02 15:48:06 +01:00
|
|
|
|
public bool AutoStart { get; private set; }
|
|
|
|
|
public IconResource Icon { get; private set; }
|
|
|
|
|
public Guid Id { get; private set; }
|
|
|
|
|
public string Name { get; private set; }
|
|
|
|
|
public string Tooltip { get; private set; }
|
2019-08-30 12:30:00 +02:00
|
|
|
|
|
2018-06-21 07:56:25 +02:00
|
|
|
|
public event DownloadRequestedEventHandler ConfigurationDownloadRequested;
|
2021-11-24 08:42:07 +01:00
|
|
|
|
public event LoseFocusRequestedEventHandler LoseFocusRequested;
|
2019-12-19 15:02:40 +01:00
|
|
|
|
public event TerminationRequestedEventHandler TerminationRequested;
|
2023-11-01 13:52:39 +01:00
|
|
|
|
public event UserIdentifierDetectedEventHandler UserIdentifierDetected;
|
2020-07-29 23:39:05 +02:00
|
|
|
|
public event WindowsChangedEventHandler WindowsChanged;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
|
2019-08-30 12:30:00 +02:00
|
|
|
|
public BrowserApplication(
|
2018-06-29 09:50:20 +02:00
|
|
|
|
AppConfig appConfig,
|
2018-02-15 15:42:54 +01:00
|
|
|
|
BrowserSettings settings,
|
2020-01-22 15:16:11 +01:00
|
|
|
|
IFileSystemDialog fileSystemDialog,
|
2020-10-05 23:37:23 +02:00
|
|
|
|
IHashAlgorithm hashAlgorithm,
|
2021-10-18 12:06:10 +02:00
|
|
|
|
IKeyGenerator keyGenerator,
|
2019-01-17 11:12:17 +01:00
|
|
|
|
IMessageBox messageBox,
|
2018-08-31 15:29:36 +02:00
|
|
|
|
IModuleLogger logger,
|
2023-03-08 00:01:20 +01:00
|
|
|
|
INativeMethods nativeMethods,
|
|
|
|
|
SessionMode sessionMode,
|
2018-01-17 14:08:39 +01:00
|
|
|
|
IText text,
|
|
|
|
|
IUserInterfaceFactory uiFactory)
|
2017-07-24 17:31:28 +02:00
|
|
|
|
{
|
2018-06-29 09:50:20 +02:00
|
|
|
|
this.appConfig = appConfig;
|
2024-03-04 14:27:49 +01:00
|
|
|
|
this.clipboard = new Clipboard(logger.CloneFor(nameof(Clipboard)), settings);
|
2020-01-22 15:16:11 +01:00
|
|
|
|
this.fileSystemDialog = fileSystemDialog;
|
2020-10-05 23:37:23 +02:00
|
|
|
|
this.hashAlgorithm = hashAlgorithm;
|
2021-10-18 12:06:10 +02:00
|
|
|
|
this.keyGenerator = keyGenerator;
|
2018-03-08 15:27:12 +01:00
|
|
|
|
this.logger = logger;
|
2019-01-17 11:12:17 +01:00
|
|
|
|
this.messageBox = messageBox;
|
2021-10-18 12:06:10 +02:00
|
|
|
|
this.nativeMethods = nativeMethods;
|
2023-03-08 00:01:20 +01:00
|
|
|
|
this.sessionMode = sessionMode;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
this.settings = settings;
|
2017-07-31 20:22:53 +02:00
|
|
|
|
this.text = text;
|
2017-07-24 17:31:28 +02:00
|
|
|
|
this.uiFactory = uiFactory;
|
2022-02-04 13:41:11 +01:00
|
|
|
|
this.windows = new List<BrowserWindow>();
|
2017-07-24 17:31:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-13 16:47:18 +02:00
|
|
|
|
public void Focus(bool forward)
|
|
|
|
|
{
|
|
|
|
|
windows.ForEach(window =>
|
|
|
|
|
{
|
|
|
|
|
window.Focus(forward);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-13 11:04:36 +01:00
|
|
|
|
public IEnumerable<IBrowserWindow> GetWindows()
|
2019-11-28 17:22:04 +01:00
|
|
|
|
{
|
2024-02-13 11:04:36 +01:00
|
|
|
|
return new List<IBrowserWindow>(windows);
|
2019-11-28 17:22:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:28 +02:00
|
|
|
|
public void Initialize()
|
|
|
|
|
{
|
2020-02-17 12:10:04 +01:00
|
|
|
|
logger.Info("Starting initialization...");
|
|
|
|
|
|
2018-03-02 15:41:04 +01:00
|
|
|
|
var cefSettings = InitializeCefSettings();
|
2019-01-22 09:07:34 +01:00
|
|
|
|
var success = Cef.Initialize(cefSettings, true, default(IApp));
|
2017-07-25 09:02:32 +02:00
|
|
|
|
|
2019-11-15 16:00:03 +01:00
|
|
|
|
InitializeApplicationInfo();
|
2018-08-31 07:49:41 +02:00
|
|
|
|
|
2019-08-30 12:30:00 +02:00
|
|
|
|
if (success)
|
|
|
|
|
{
|
2023-03-08 22:24:29 +01:00
|
|
|
|
InitializeIntegrityKeys();
|
2021-07-01 18:54:43 +02:00
|
|
|
|
|
2020-02-17 12:10:04 +01:00
|
|
|
|
if (settings.DeleteCookiesOnStartup)
|
|
|
|
|
{
|
|
|
|
|
DeleteCookies();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-08 22:24:29 +01:00
|
|
|
|
if (settings.UseTemporaryDownAndUploadDirectory)
|
|
|
|
|
{
|
|
|
|
|
CreateTemporaryDownAndUploadDirectory();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 12:30:00 +02:00
|
|
|
|
logger.Info("Initialized browser.");
|
|
|
|
|
}
|
|
|
|
|
else
|
2017-07-25 09:02:32 +02:00
|
|
|
|
{
|
2019-01-18 09:58:14 +01:00
|
|
|
|
throw new Exception("Failed to initialize browser!");
|
2017-07-25 09:02:32 +02:00
|
|
|
|
}
|
2017-07-24 17:31:28 +02:00
|
|
|
|
}
|
2017-07-14 10:28:59 +02:00
|
|
|
|
|
2018-11-15 08:45:17 +01:00
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2022-02-04 13:41:11 +01:00
|
|
|
|
CreateNewWindow();
|
2018-11-15 08:45:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:28 +02:00
|
|
|
|
public void Terminate()
|
|
|
|
|
{
|
2020-02-17 12:10:04 +01:00
|
|
|
|
logger.Info("Initiating termination...");
|
2021-05-11 01:32:50 +02:00
|
|
|
|
AwaitReady();
|
2020-02-17 12:10:04 +01:00
|
|
|
|
|
2022-02-04 13:41:11 +01:00
|
|
|
|
foreach (var window in windows)
|
2017-07-27 11:46:31 +02:00
|
|
|
|
{
|
2022-02-04 13:41:11 +01:00
|
|
|
|
window.Closed -= Window_Closed;
|
|
|
|
|
window.Close();
|
|
|
|
|
logger.Info($"Closed browser window #{window.Id}.");
|
2017-07-27 11:46:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-01 18:54:43 +02:00
|
|
|
|
if (settings.UseTemporaryDownAndUploadDirectory)
|
|
|
|
|
{
|
|
|
|
|
DeleteTemporaryDownAndUploadDirectory();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 12:10:04 +01:00
|
|
|
|
if (settings.DeleteCookiesOnShutdown)
|
|
|
|
|
{
|
|
|
|
|
DeleteCookies();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 17:31:28 +02:00
|
|
|
|
Cef.Shutdown();
|
2019-01-18 09:58:14 +01:00
|
|
|
|
logger.Info("Terminated browser.");
|
2020-02-21 15:11:35 +01:00
|
|
|
|
|
|
|
|
|
if (settings.DeleteCacheOnShutdown && settings.DeleteCookiesOnShutdown)
|
|
|
|
|
{
|
|
|
|
|
DeleteCache();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Retained browser cache.");
|
|
|
|
|
}
|
2017-07-24 17:31:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 01:32:50 +02:00
|
|
|
|
private void AwaitReady()
|
|
|
|
|
{
|
|
|
|
|
// We apparently need to let the browser finish any pending work before attempting to reset or terminate it, especially if the
|
|
|
|
|
// reset or termination is initiated automatically (e.g. by a quit URL). Otherwise, the engine will crash on some occasions, seemingly
|
|
|
|
|
// when it can't finish handling its events (like ChromiumWebBrowser.LoadError).
|
|
|
|
|
|
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 13:59:36 +01:00
|
|
|
|
private void CreateNewWindow(PopupRequestedEventArgs args = default)
|
2017-07-28 09:12:17 +02:00
|
|
|
|
{
|
2022-02-04 13:41:11 +01:00
|
|
|
|
var id = ++windowIdCounter;
|
|
|
|
|
var isMainWindow = windows.Count == 0;
|
2022-02-23 13:59:36 +01:00
|
|
|
|
var startUrl = GenerateStartUrl();
|
2022-02-04 13:41:11 +01:00
|
|
|
|
var windowLogger = logger.CloneFor($"Browser Window #{id}");
|
|
|
|
|
var window = new BrowserWindow(
|
2021-10-18 12:06:10 +02:00
|
|
|
|
appConfig,
|
2024-03-04 14:27:49 +01:00
|
|
|
|
clipboard,
|
2021-10-18 12:06:10 +02:00
|
|
|
|
fileSystemDialog,
|
|
|
|
|
hashAlgorithm,
|
2022-02-23 13:59:36 +01:00
|
|
|
|
id,
|
|
|
|
|
isMainWindow,
|
2021-10-18 12:06:10 +02:00
|
|
|
|
keyGenerator,
|
2022-02-04 13:41:11 +01:00
|
|
|
|
windowLogger,
|
2022-02-23 13:59:36 +01:00
|
|
|
|
messageBox,
|
2023-03-08 00:01:20 +01:00
|
|
|
|
sessionMode,
|
2022-02-23 13:59:36 +01:00
|
|
|
|
settings,
|
|
|
|
|
startUrl,
|
2021-10-18 12:06:10 +02:00
|
|
|
|
text,
|
2022-02-23 13:59:36 +01:00
|
|
|
|
uiFactory);
|
2017-07-28 09:12:17 +02:00
|
|
|
|
|
2022-02-04 13:41:11 +01:00
|
|
|
|
window.Closed += Window_Closed;
|
2022-02-23 13:59:36 +01:00
|
|
|
|
window.ConfigurationDownloadRequested += (f, a) => ConfigurationDownloadRequested?.Invoke(f, a);
|
2022-02-04 13:41:11 +01:00
|
|
|
|
window.PopupRequested += Window_PopupRequested;
|
|
|
|
|
window.ResetRequested += Window_ResetRequested;
|
2023-11-01 13:52:39 +01:00
|
|
|
|
window.UserIdentifierDetected += (i) => UserIdentifierDetected?.Invoke(i);
|
2022-02-04 13:41:11 +01:00
|
|
|
|
window.TerminationRequested += () => TerminationRequested?.Invoke();
|
2021-11-24 08:42:07 +01:00
|
|
|
|
window.LoseFocusRequested += (forward) => LoseFocusRequested?.Invoke(forward);
|
2018-03-14 11:13:30 +01:00
|
|
|
|
|
2022-02-23 13:59:36 +01:00
|
|
|
|
window.InitializeControl();
|
2022-02-04 13:41:11 +01:00
|
|
|
|
windows.Add(window);
|
2018-08-31 07:49:41 +02:00
|
|
|
|
|
2022-02-23 13:59:36 +01:00
|
|
|
|
if (args != default(PopupRequestedEventArgs))
|
|
|
|
|
{
|
|
|
|
|
args.Window = window;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
window.InitializeWindow();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 13:41:11 +01:00
|
|
|
|
logger.Info($"Created browser window #{window.Id}.");
|
2019-11-28 17:22:04 +01:00
|
|
|
|
WindowsChanged?.Invoke();
|
2017-07-28 09:12:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-01 18:54:43 +02:00
|
|
|
|
private void CreateTemporaryDownAndUploadDirectory()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
settings.DownAndUploadDirectory = Path.Combine(appConfig.TemporaryDirectory, Path.GetRandomFileName());
|
|
|
|
|
Directory.CreateDirectory(settings.DownAndUploadDirectory);
|
|
|
|
|
logger.Info($"Created temporary down- and upload directory.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to create temporary down- and upload directory!", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DeleteTemporaryDownAndUploadDirectory()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(settings.DownAndUploadDirectory, true);
|
|
|
|
|
logger.Info("Deleted temporary down- and upload directory.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to delete temporary down- and upload directory!", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 15:11:35 +01:00
|
|
|
|
private void DeleteCache()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.Delete(appConfig.BrowserCachePath, true);
|
|
|
|
|
logger.Info("Deleted browser cache.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to delete browser cache!", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 12:10:04 +01:00
|
|
|
|
private void DeleteCookies()
|
|
|
|
|
{
|
|
|
|
|
var callback = new TaskDeleteCookiesCallback();
|
|
|
|
|
|
|
|
|
|
callback.Task.ContinueWith(task =>
|
|
|
|
|
{
|
|
|
|
|
if (!task.IsCompleted || task.Result == TaskDeleteCookiesCallback.InvalidNoOfCookiesDeleted)
|
|
|
|
|
{
|
|
|
|
|
logger.Warn("Failed to delete cookies!");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Debug($"Deleted {task.Result} cookies.");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (Cef.GetGlobalCookieManager().DeleteCookies(callback: callback))
|
|
|
|
|
{
|
|
|
|
|
logger.Debug("Successfully initiated cookie deletion.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn("Failed to initiate cookie deletion!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 12:55:20 +02:00
|
|
|
|
private string GenerateStartUrl()
|
|
|
|
|
{
|
|
|
|
|
var url = settings.StartUrl;
|
|
|
|
|
|
|
|
|
|
if (settings.UseQueryParameter)
|
|
|
|
|
{
|
2020-09-24 17:01:23 +02:00
|
|
|
|
if (url.Contains("?") && settings.StartUrlQuery?.Length > 1 && Uri.TryCreate(url, UriKind.Absolute, out var uri))
|
|
|
|
|
{
|
|
|
|
|
url = url.Replace(uri.Query, $"{uri.Query}&{settings.StartUrlQuery.Substring(1)}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
url = $"{url}{settings.StartUrlQuery}";
|
|
|
|
|
}
|
2020-09-24 12:55:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 12:10:04 +01:00
|
|
|
|
private void InitializeApplicationInfo()
|
|
|
|
|
{
|
|
|
|
|
AutoStart = true;
|
|
|
|
|
Icon = new BrowserIconResource();
|
|
|
|
|
Id = Guid.NewGuid();
|
|
|
|
|
Name = text.Get(TextKey.Browser_Name);
|
|
|
|
|
Tooltip = text.Get(TextKey.Browser_Tooltip);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-02 15:41:04 +01:00
|
|
|
|
private CefSettings InitializeCefSettings()
|
|
|
|
|
{
|
2019-01-23 08:12:15 +01:00
|
|
|
|
var warning = logger.LogLevel == LogLevel.Warning;
|
|
|
|
|
var error = logger.LogLevel == LogLevel.Error;
|
2020-02-17 15:44:22 +01:00
|
|
|
|
var cefSettings = new CefSettings();
|
2019-12-18 08:09:59 +01:00
|
|
|
|
|
2021-12-23 15:21:05 +01:00
|
|
|
|
cefSettings.AcceptLanguageList = CultureInfo.CurrentUICulture.Name;
|
2020-02-17 15:44:22 +01:00
|
|
|
|
cefSettings.CachePath = appConfig.BrowserCachePath;
|
2019-05-08 15:36:48 +02:00
|
|
|
|
cefSettings.CefCommandLineArgs.Add("touch-events", "enabled");
|
2020-02-17 15:44:22 +01:00
|
|
|
|
cefSettings.LogFile = appConfig.BrowserLogFilePath;
|
|
|
|
|
cefSettings.LogSeverity = error ? LogSeverity.Error : (warning ? LogSeverity.Warning : LogSeverity.Info);
|
2020-02-25 10:41:55 +01:00
|
|
|
|
cefSettings.PersistSessionCookies = !settings.DeleteCookiesOnStartup || !settings.DeleteCookiesOnShutdown;
|
2020-02-17 15:44:22 +01:00
|
|
|
|
cefSettings.UserAgent = InitializeUserAgent();
|
2019-05-08 15:36:48 +02:00
|
|
|
|
|
2024-02-27 10:42:02 +01:00
|
|
|
|
if (!settings.AllowPageZoom)
|
|
|
|
|
{
|
|
|
|
|
cefSettings.CefCommandLineArgs.Add("disable-pinch");
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 11:15:28 +01:00
|
|
|
|
if (!settings.AllowPdfReader)
|
|
|
|
|
{
|
2020-03-05 09:38:26 +01:00
|
|
|
|
cefSettings.CefCommandLineArgs.Add("disable-pdf-extension");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!settings.AllowSpellChecking)
|
|
|
|
|
{
|
|
|
|
|
cefSettings.CefCommandLineArgs.Add("disable-spell-checking");
|
2020-01-30 11:15:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-16 10:47:10 +02:00
|
|
|
|
cefSettings.CefCommandLineArgs.Add("enable-media-stream");
|
2021-07-12 16:36:01 +02:00
|
|
|
|
cefSettings.CefCommandLineArgs.Add("enable-usermedia-screen-capturing");
|
|
|
|
|
cefSettings.CefCommandLineArgs.Add("use-fake-ui-for-media-stream");
|
2021-04-16 10:47:10 +02:00
|
|
|
|
|
2020-02-17 15:44:22 +01:00
|
|
|
|
InitializeProxySettings(cefSettings);
|
|
|
|
|
|
2021-12-23 15:23:54 +01:00
|
|
|
|
logger.Debug($"Accept Language: {cefSettings.AcceptLanguageList}");
|
2020-02-17 12:10:04 +01:00
|
|
|
|
logger.Debug($"Cache Path: {cefSettings.CachePath}");
|
|
|
|
|
logger.Debug($"Engine Version: Chromium {Cef.ChromiumVersion}, CEF {Cef.CefVersion}, CefSharp {Cef.CefSharpVersion}");
|
|
|
|
|
logger.Debug($"Log File: {cefSettings.LogFile}");
|
|
|
|
|
logger.Debug($"Log Severity: {cefSettings.LogSeverity}.");
|
|
|
|
|
logger.Debug($"PDF Reader: {(settings.AllowPdfReader ? "Enabled" : "Disabled")}.");
|
|
|
|
|
logger.Debug($"Session Persistence: {(cefSettings.PersistSessionCookies ? "Enabled" : "Disabled")}.");
|
2018-08-31 07:49:41 +02:00
|
|
|
|
|
2018-03-02 15:41:04 +01:00
|
|
|
|
return cefSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-08 22:24:29 +01:00
|
|
|
|
private void InitializeIntegrityKeys()
|
|
|
|
|
{
|
|
|
|
|
logger.Debug($"Browser Exam Key (BEK) transmission is {(settings.SendBrowserExamKey ? "enabled" : "disabled")}.");
|
|
|
|
|
logger.Debug($"Configuration Key (CK) transmission is {(settings.SendConfigurationKey ? "enabled" : "disabled")}.");
|
|
|
|
|
|
|
|
|
|
if (settings.CustomBrowserExamKey != default)
|
|
|
|
|
{
|
|
|
|
|
keyGenerator.UseCustomBrowserExamKey(settings.CustomBrowserExamKey);
|
|
|
|
|
logger.Debug($"The browser application will be using a custom browser exam key.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Debug($"The browser application will be using the default browser exam key.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 08:09:59 +01:00
|
|
|
|
private void InitializeProxySettings(CefSettings cefSettings)
|
|
|
|
|
{
|
|
|
|
|
if (settings.Proxy.Policy == ProxyPolicy.Custom)
|
|
|
|
|
{
|
|
|
|
|
if (settings.Proxy.AutoConfigure)
|
|
|
|
|
{
|
|
|
|
|
cefSettings.CefCommandLineArgs.Add("proxy-pac-url", settings.Proxy.AutoConfigureUrl);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (settings.Proxy.AutoDetect)
|
|
|
|
|
{
|
|
|
|
|
cefSettings.CefCommandLineArgs.Add("proxy-auto-detect", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (settings.Proxy.BypassList.Any())
|
|
|
|
|
{
|
|
|
|
|
cefSettings.CefCommandLineArgs.Add("proxy-bypass-list", string.Join(";", settings.Proxy.BypassList));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (settings.Proxy.Proxies.Any())
|
|
|
|
|
{
|
|
|
|
|
var proxies = new List<string>();
|
|
|
|
|
|
|
|
|
|
foreach (var proxy in settings.Proxy.Proxies)
|
|
|
|
|
{
|
|
|
|
|
proxies.Add($"{ToScheme(proxy.Protocol)}={proxy.Host}:{proxy.Port}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cefSettings.CefCommandLineArgs.Add("proxy-server", string.Join(";", proxies));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-17 12:10:04 +01:00
|
|
|
|
private string InitializeUserAgent()
|
|
|
|
|
{
|
|
|
|
|
var osVersion = $"{Environment.OSVersion.Version.Major}.{Environment.OSVersion.Version.Minor}";
|
|
|
|
|
var sebVersion = $"SEB/{appConfig.ProgramInformationalVersion}";
|
2020-02-19 15:32:38 +01:00
|
|
|
|
var userAgent = default(string);
|
2020-02-17 12:10:04 +01:00
|
|
|
|
|
|
|
|
|
if (settings.UseCustomUserAgent)
|
|
|
|
|
{
|
2020-02-19 15:32:38 +01:00
|
|
|
|
userAgent = $"{settings.CustomUserAgent} {sebVersion}";
|
2020-02-17 12:10:04 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-02-19 15:32:38 +01:00
|
|
|
|
userAgent = $"Mozilla/5.0 (Windows NT {osVersion}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{Cef.ChromiumVersion} {sebVersion}";
|
2020-02-17 12:10:04 +01:00
|
|
|
|
}
|
2020-02-19 15:32:38 +01:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(settings.UserAgentSuffix))
|
|
|
|
|
{
|
|
|
|
|
userAgent = $"{userAgent} {settings.UserAgentSuffix}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return userAgent;
|
2020-02-17 12:10:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 08:09:59 +01:00
|
|
|
|
private string ToScheme(ProxyProtocol protocol)
|
|
|
|
|
{
|
|
|
|
|
switch (protocol)
|
|
|
|
|
{
|
|
|
|
|
case ProxyProtocol.Ftp:
|
|
|
|
|
return Uri.UriSchemeFtp;
|
|
|
|
|
case ProxyProtocol.Http:
|
|
|
|
|
return Uri.UriSchemeHttp;
|
|
|
|
|
case ProxyProtocol.Https:
|
|
|
|
|
return Uri.UriSchemeHttps;
|
|
|
|
|
case ProxyProtocol.Socks:
|
|
|
|
|
return "socks";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new NotImplementedException($"Mapping for proxy protocol '{protocol}' is not yet implemented!");
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 13:41:11 +01:00
|
|
|
|
private void Window_Closed(int id)
|
2019-01-17 11:12:17 +01:00
|
|
|
|
{
|
2022-02-04 13:41:11 +01:00
|
|
|
|
windows.Remove(windows.First(i => i.Id == id));
|
|
|
|
|
WindowsChanged?.Invoke();
|
|
|
|
|
logger.Info($"Window #{id} has been closed.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Window_PopupRequested(PopupRequestedEventArgs args)
|
|
|
|
|
{
|
2022-02-23 13:59:36 +01:00
|
|
|
|
logger.Info($"Received request to create new window...");
|
|
|
|
|
CreateNewWindow(args);
|
2019-01-17 11:12:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 13:41:11 +01:00
|
|
|
|
private void Window_ResetRequested()
|
2020-09-21 19:22:15 +02:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Attempting to reset browser...");
|
2021-05-11 01:32:50 +02:00
|
|
|
|
AwaitReady();
|
2020-09-21 19:22:15 +02:00
|
|
|
|
|
2022-02-04 13:41:11 +01:00
|
|
|
|
foreach (var window in windows)
|
2020-09-21 19:22:15 +02:00
|
|
|
|
{
|
2022-02-04 13:41:11 +01:00
|
|
|
|
window.Closed -= Window_Closed;
|
|
|
|
|
window.Close();
|
|
|
|
|
logger.Info($"Closed browser window #{window.Id}.");
|
2020-09-21 19:22:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-04 13:41:11 +01:00
|
|
|
|
windows.Clear();
|
2020-09-21 19:22:15 +02:00
|
|
|
|
WindowsChanged?.Invoke();
|
|
|
|
|
|
|
|
|
|
if (settings.DeleteCookiesOnStartup && settings.DeleteCookiesOnShutdown)
|
|
|
|
|
{
|
|
|
|
|
DeleteCookies();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nativeMethods.EmptyClipboard();
|
2022-02-04 13:41:11 +01:00
|
|
|
|
CreateNewWindow();
|
2020-09-21 19:22:15 +02:00
|
|
|
|
logger.Info("Successfully reset browser.");
|
|
|
|
|
}
|
2017-07-14 10:28:59 +02:00
|
|
|
|
}
|
|
|
|
|
}
|