2018-02-08 13:32:48 +01:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
|
|
|
|
*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
using System.Threading;
|
2018-06-21 07:56:25 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Events;
|
2018-03-15 14:32:07 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Hosts;
|
|
|
|
|
using SafeExamBrowser.Contracts.Communication.Proxies;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
2018-08-31 10:06:27 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Core.OperationModel;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.I18n;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.UserInterface;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.WindowsApi;
|
2018-07-06 15:57:38 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.WindowsApi.Events;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Runtime.Operations
|
2018-02-08 13:32:48 +01:00
|
|
|
|
{
|
2018-03-21 10:23:15 +01:00
|
|
|
|
internal class ClientOperation : IOperation
|
2018-02-08 13:32:48 +01:00
|
|
|
|
{
|
2018-03-21 15:28:59 +01:00
|
|
|
|
private readonly int timeout_ms;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
protected IConfigurationRepository configuration;
|
|
|
|
|
protected ILogger logger;
|
|
|
|
|
protected IProcessFactory processFactory;
|
|
|
|
|
protected IProxyFactory proxyFactory;
|
|
|
|
|
protected IRuntimeHost runtimeHost;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
protected static IProcess ClientProcess { get; private set; }
|
|
|
|
|
protected static IClientProxy ClientProxy { get; private set; }
|
|
|
|
|
public IProgressIndicator ProgressIndicator { protected get; set; }
|
2018-02-08 13:32:48 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
public ClientOperation(
|
2018-02-14 15:26:05 +01:00
|
|
|
|
IConfigurationRepository configuration,
|
|
|
|
|
ILogger logger,
|
|
|
|
|
IProcessFactory processFactory,
|
2018-03-14 11:04:28 +01:00
|
|
|
|
IProxyFactory proxyFactory,
|
2018-03-21 15:28:59 +01:00
|
|
|
|
IRuntimeHost runtimeHost,
|
|
|
|
|
int timeout_ms)
|
2018-02-08 13:32:48 +01:00
|
|
|
|
{
|
|
|
|
|
this.configuration = configuration;
|
|
|
|
|
this.logger = logger;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
this.processFactory = processFactory;
|
2018-03-14 11:04:28 +01:00
|
|
|
|
this.proxyFactory = proxyFactory;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
this.runtimeHost = runtimeHost;
|
2018-03-21 15:28:59 +01:00
|
|
|
|
this.timeout_ms = timeout_ms;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
public virtual OperationResult Perform()
|
2018-02-08 13:32:48 +01:00
|
|
|
|
{
|
2018-03-21 10:23:15 +01:00
|
|
|
|
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_StartClient, true);
|
2018-02-08 13:32:48 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
var success = TryStartClient();
|
2018-02-14 15:26:05 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
if (success)
|
2018-02-14 15:26:05 +01:00
|
|
|
|
{
|
2018-03-21 10:23:15 +01:00
|
|
|
|
logger.Info($"Successfully started new client instance.");
|
2018-02-14 15:26:05 +01:00
|
|
|
|
}
|
2018-03-21 10:23:15 +01:00
|
|
|
|
else
|
2018-02-08 13:32:48 +01:00
|
|
|
|
{
|
2018-03-21 10:23:15 +01:00
|
|
|
|
logger.Error($"Failed to start new client instance! Aborting procedure...");
|
2018-02-14 15:26:05 +01:00
|
|
|
|
}
|
2018-02-28 15:49:06 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
return success ? OperationResult.Success : OperationResult.Failed;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
public virtual OperationResult Repeat()
|
2018-03-14 11:04:28 +01:00
|
|
|
|
{
|
2018-03-21 10:23:15 +01:00
|
|
|
|
return Perform();
|
2018-03-14 11:04:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
public virtual void Revert()
|
2018-03-14 11:04:28 +01:00
|
|
|
|
{
|
2018-03-21 10:23:15 +01:00
|
|
|
|
if (ClientProcess != null && !ClientProcess.HasTerminated)
|
|
|
|
|
{
|
|
|
|
|
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_StopClient, true);
|
|
|
|
|
TryStopClient();
|
|
|
|
|
}
|
2018-03-14 11:04:28 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
protected bool TryStartClient()
|
2018-02-14 15:26:05 +01:00
|
|
|
|
{
|
2018-02-20 15:15:26 +01:00
|
|
|
|
var clientReady = false;
|
2018-02-16 13:15:16 +01:00
|
|
|
|
var clientReadyEvent = new AutoResetEvent(false);
|
|
|
|
|
var clientReadyEventHandler = new CommunicationEventHandler(() => clientReadyEvent.Set());
|
2018-03-21 15:28:59 +01:00
|
|
|
|
|
2018-06-29 09:50:20 +02:00
|
|
|
|
var clientExecutable = configuration.AppConfig.ClientExecutablePath;
|
|
|
|
|
var clientLogFile = $"{'"' + configuration.AppConfig.ClientLogFile + '"'}";
|
|
|
|
|
var hostUri = configuration.AppConfig.RuntimeAddress;
|
2018-03-14 11:04:28 +01:00
|
|
|
|
var token = configuration.CurrentSession.StartupToken.ToString("D");
|
2018-02-14 15:26:05 +01:00
|
|
|
|
|
2018-03-14 11:04:28 +01:00
|
|
|
|
logger.Info("Starting new client process...");
|
2018-02-16 13:15:16 +01:00
|
|
|
|
runtimeHost.ClientReady += clientReadyEventHandler;
|
2018-03-21 10:23:15 +01:00
|
|
|
|
ClientProcess = processFactory.StartNew(clientExecutable, clientLogFile, hostUri, token);
|
2018-02-15 15:42:54 +01:00
|
|
|
|
|
2018-02-22 10:00:18 +01:00
|
|
|
|
logger.Info("Waiting for client to complete initialization...");
|
2018-03-21 15:28:59 +01:00
|
|
|
|
clientReady = clientReadyEvent.WaitOne(timeout_ms);
|
2018-02-16 13:15:16 +01:00
|
|
|
|
runtimeHost.ClientReady -= clientReadyEventHandler;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
|
2018-02-22 10:00:18 +01:00
|
|
|
|
if (!clientReady)
|
2018-02-14 15:26:05 +01:00
|
|
|
|
{
|
2018-03-21 15:28:59 +01:00
|
|
|
|
logger.Error($"Failed to start client within {timeout_ms / 1000} seconds!");
|
2018-02-22 10:00:18 +01:00
|
|
|
|
|
|
|
|
|
return false;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
}
|
2018-02-22 10:00:18 +01:00
|
|
|
|
|
2018-03-14 11:13:30 +01:00
|
|
|
|
logger.Info("Client has been successfully started and initialized. Creating communication proxy for client host...");
|
2018-06-29 09:50:20 +02:00
|
|
|
|
ClientProxy = proxyFactory.CreateClientProxy(configuration.AppConfig.ClientAddress);
|
2018-02-22 10:00:18 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
if (!ClientProxy.Connect(configuration.CurrentSession.StartupToken))
|
2018-02-14 15:26:05 +01:00
|
|
|
|
{
|
2018-02-22 10:00:18 +01:00
|
|
|
|
logger.Error("Failed to connect to client!");
|
|
|
|
|
|
|
|
|
|
return false;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
}
|
2018-02-22 10:00:18 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
logger.Info("Connection with client has been established. Requesting authentication...");
|
2018-02-22 10:00:18 +01:00
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
var communication = ClientProxy.RequestAuthentication();
|
|
|
|
|
var response = communication.Value;
|
2018-02-22 10:00:18 +01:00
|
|
|
|
|
2018-08-10 13:23:24 +02:00
|
|
|
|
if (!communication.Success || ClientProcess.Id != response?.ProcessId)
|
2018-02-22 10:00:18 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to verify client integrity!");
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 11:04:28 +01:00
|
|
|
|
logger.Info("Authentication of client has been successful, client is ready to operate.");
|
2018-02-22 10:00:18 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
configuration.CurrentSession.ClientProcess = ClientProcess;
|
|
|
|
|
configuration.CurrentSession.ClientProxy = ClientProxy;
|
|
|
|
|
|
2018-02-22 10:00:18 +01:00
|
|
|
|
return true;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
}
|
2018-02-20 15:15:26 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
protected bool TryStopClient()
|
2018-02-20 15:15:26 +01:00
|
|
|
|
{
|
2018-03-21 10:23:15 +01:00
|
|
|
|
var success = false;
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
var disconnected = false;
|
|
|
|
|
var disconnectedEvent = new AutoResetEvent(false);
|
|
|
|
|
var disconnectedEventHandler = new CommunicationEventHandler(() => disconnectedEvent.Set());
|
|
|
|
|
|
|
|
|
|
var terminated = false;
|
|
|
|
|
var terminatedEvent = new AutoResetEvent(false);
|
|
|
|
|
var terminatedEventHandler = new ProcessTerminatedEventHandler((_) => terminatedEvent.Set());
|
|
|
|
|
|
|
|
|
|
runtimeHost.ClientDisconnected += disconnectedEventHandler;
|
2018-03-21 10:23:15 +01:00
|
|
|
|
ClientProcess.Terminated += terminatedEventHandler;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
|
2018-02-22 10:00:18 +01:00
|
|
|
|
logger.Info("Instructing client to initiate shutdown procedure.");
|
2018-03-21 10:23:15 +01:00
|
|
|
|
ClientProxy.InitiateShutdown();
|
2018-02-22 10:00:18 +01:00
|
|
|
|
|
|
|
|
|
logger.Info("Disconnecting from client communication host.");
|
2018-03-21 10:23:15 +01:00
|
|
|
|
ClientProxy.Disconnect();
|
2018-02-20 15:15:26 +01:00
|
|
|
|
|
2018-02-22 10:00:18 +01:00
|
|
|
|
logger.Info("Waiting for client to disconnect from runtime communication host...");
|
2018-03-21 15:28:59 +01:00
|
|
|
|
disconnected = disconnectedEvent.WaitOne(timeout_ms);
|
2018-02-22 10:00:18 +01:00
|
|
|
|
|
|
|
|
|
if (!disconnected)
|
|
|
|
|
{
|
2018-03-21 15:28:59 +01:00
|
|
|
|
logger.Error($"Client failed to disconnect within {timeout_ms / 1000} seconds!");
|
2018-02-22 10:00:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Info("Waiting for client process to terminate...");
|
2018-03-21 15:28:59 +01:00
|
|
|
|
terminated = terminatedEvent.WaitOne(timeout_ms);
|
2018-02-20 15:15:26 +01:00
|
|
|
|
|
2018-02-22 10:00:18 +01:00
|
|
|
|
if (!terminated)
|
|
|
|
|
{
|
2018-03-21 15:28:59 +01:00
|
|
|
|
logger.Error($"Client failed to terminate within {timeout_ms / 1000} seconds!");
|
2018-02-22 10:00:18 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 15:15:26 +01:00
|
|
|
|
runtimeHost.ClientDisconnected -= disconnectedEventHandler;
|
2018-03-21 10:23:15 +01:00
|
|
|
|
ClientProcess.Terminated -= terminatedEventHandler;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
|
|
|
|
|
if (disconnected && terminated)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Client has been successfully terminated.");
|
2018-03-21 10:23:15 +01:00
|
|
|
|
success = true;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-02-27 15:28:54 +01:00
|
|
|
|
logger.Warn("Attempting to kill client process since graceful termination failed!");
|
2018-03-21 10:23:15 +01:00
|
|
|
|
success = TryKillClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
configuration.CurrentSession.ClientProcess = null;
|
|
|
|
|
configuration.CurrentSession.ClientProxy = null;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
2018-03-21 10:23:15 +01:00
|
|
|
|
|
|
|
|
|
return success;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
protected bool TryKillClient(int attempt = 0)
|
2018-02-20 15:15:26 +01:00
|
|
|
|
{
|
|
|
|
|
const int MAX_ATTEMPTS = 5;
|
|
|
|
|
|
|
|
|
|
if (attempt == MAX_ATTEMPTS)
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to kill client process within {MAX_ATTEMPTS} attempts!");
|
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
return false;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
logger.Info($"Killing client process with ID = {ClientProcess.Id}.");
|
|
|
|
|
ClientProcess.Kill();
|
2018-02-20 15:15:26 +01:00
|
|
|
|
|
2018-03-21 10:23:15 +01:00
|
|
|
|
if (ClientProcess.HasTerminated)
|
2018-02-20 15:15:26 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Client process has terminated.");
|
2018-03-21 10:23:15 +01:00
|
|
|
|
|
|
|
|
|
return true;
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Warn("Failed to kill client process. Trying again...");
|
2018-03-21 10:23:15 +01:00
|
|
|
|
|
2018-03-21 15:28:59 +01:00
|
|
|
|
return TryKillClient(++attempt);
|
2018-02-20 15:15:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-08 13:32:48 +01:00
|
|
|
|
}
|
|
|
|
|
}
|