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;
|
|
|
|
|
using System.Threading;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
using SafeExamBrowser.Contracts.Behaviour.Operations;
|
|
|
|
|
using SafeExamBrowser.Contracts.Communication;
|
|
|
|
|
using SafeExamBrowser.Contracts.Configuration;
|
|
|
|
|
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-02-08 13:32:48 +01:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Runtime.Behaviour.Operations
|
|
|
|
|
{
|
|
|
|
|
internal abstract class SessionSequenceOperation : IOperation
|
|
|
|
|
{
|
2018-02-12 12:21:55 +01:00
|
|
|
|
private bool sessionRunning;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
private IClientProxy client;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
private IConfigurationRepository configuration;
|
|
|
|
|
private ILogger logger;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
private IProcessFactory processFactory;
|
|
|
|
|
private IRuntimeHost runtimeHost;
|
|
|
|
|
private IServiceProxy service;
|
|
|
|
|
private ISession session;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
|
|
|
|
|
public bool Abort { get; private set; }
|
|
|
|
|
public IProgressIndicator ProgressIndicator { private get; set; }
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
public SessionSequenceOperation(
|
|
|
|
|
IClientProxy client,
|
|
|
|
|
IConfigurationRepository configuration,
|
|
|
|
|
ILogger logger,
|
|
|
|
|
IProcessFactory processFactory,
|
|
|
|
|
IRuntimeHost runtimeHost,
|
|
|
|
|
IServiceProxy service)
|
2018-02-08 13:32:48 +01:00
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
this.client = client;
|
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;
|
|
|
|
|
this.runtimeHost = runtimeHost;
|
|
|
|
|
this.service = service;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void Perform();
|
|
|
|
|
public abstract void Repeat();
|
|
|
|
|
public abstract void Revert();
|
|
|
|
|
|
|
|
|
|
protected void StartSession()
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Starting new session...");
|
|
|
|
|
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_StartSession, true);
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
session = configuration.InitializeSession();
|
|
|
|
|
runtimeHost.StartupToken = session.StartupToken;
|
2018-02-08 13:32:48 +01:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.StartSession(session.Id, configuration.CurrentSettings);
|
2018-02-08 13:32:48 +01:00
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
StartClient();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
service.StopSession(session.Id);
|
|
|
|
|
logger.Error("Failed to start client!", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sessionRunning)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"Successfully started new session with identifier '{session.Id}'.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Abort = true;
|
|
|
|
|
logger.Info($"Failed to start new session! Aborting...");
|
|
|
|
|
}
|
2018-02-08 13:32:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void StopSession()
|
|
|
|
|
{
|
2018-02-12 12:21:55 +01:00
|
|
|
|
if (sessionRunning)
|
2018-02-08 13:32:48 +01:00
|
|
|
|
{
|
2018-02-14 15:26:05 +01:00
|
|
|
|
logger.Info($"Stopping session with identifier '{session.Id}'...");
|
2018-02-08 13:32:48 +01:00
|
|
|
|
ProgressIndicator?.UpdateText(TextKey.ProgressIndicator_StopSession, true);
|
|
|
|
|
|
2018-02-14 15:26:05 +01:00
|
|
|
|
service.StopSession(session.Id);
|
2018-02-08 13:32:48 +01:00
|
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
|
// - Terminate client (or does it terminate itself?)
|
|
|
|
|
|
2018-02-12 12:21:55 +01:00
|
|
|
|
sessionRunning = false;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
logger.Info($"Successfully stopped session with identifier '{session.Id}'.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartClient()
|
|
|
|
|
{
|
2018-02-15 15:42:54 +01:00
|
|
|
|
const int TEN_SECONDS = 10000;
|
|
|
|
|
|
2018-02-16 13:15:16 +01:00
|
|
|
|
var clientStarted = false;
|
|
|
|
|
var clientReadyEvent = new AutoResetEvent(false);
|
|
|
|
|
var clientReadyEventHandler = new CommunicationEventHandler(() => clientReadyEvent.Set());
|
2018-02-14 15:26:05 +01:00
|
|
|
|
var clientExecutable = configuration.RuntimeInfo.ClientExecutablePath;
|
2018-02-15 15:42:54 +01:00
|
|
|
|
var clientLogFile = $"{'"' + configuration.RuntimeInfo.ClientLogFile + '"'}";
|
2018-02-14 15:26:05 +01:00
|
|
|
|
var hostUri = configuration.RuntimeInfo.RuntimeAddress;
|
|
|
|
|
var token = session.StartupToken.ToString("D");
|
|
|
|
|
|
2018-02-16 13:15:16 +01:00
|
|
|
|
runtimeHost.ClientReady += clientReadyEventHandler;
|
2018-02-15 15:42:54 +01:00
|
|
|
|
session.ClientProcess = processFactory.StartNew(clientExecutable, clientLogFile, hostUri, token);
|
|
|
|
|
|
2018-02-16 13:15:16 +01:00
|
|
|
|
clientStarted = clientReadyEvent.WaitOne(TEN_SECONDS);
|
|
|
|
|
runtimeHost.ClientReady -= clientReadyEventHandler;
|
2018-02-14 15:26:05 +01:00
|
|
|
|
|
2018-02-15 15:42:54 +01:00
|
|
|
|
// TODO: Check if client process alive!
|
|
|
|
|
if (clientStarted)
|
2018-02-14 15:26:05 +01:00
|
|
|
|
{
|
2018-02-15 15:42:54 +01:00
|
|
|
|
if (client.Connect(session.StartupToken))
|
2018-02-14 15:26:05 +01:00
|
|
|
|
{
|
2018-02-15 15:42:54 +01:00
|
|
|
|
var response = client.RequestAuthentication();
|
|
|
|
|
|
|
|
|
|
// TODO: Further integrity checks necessary?
|
2018-02-16 13:15:16 +01:00
|
|
|
|
if (session.ClientProcess.Id == response?.ProcessId)
|
2018-02-15 15:42:54 +01:00
|
|
|
|
{
|
|
|
|
|
sessionRunning = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to verify client integrity!");
|
|
|
|
|
}
|
2018-02-14 15:26:05 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-02-15 15:42:54 +01:00
|
|
|
|
logger.Error("Failed to connect to client!");
|
2018-02-14 15:26:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-02-15 15:42:54 +01:00
|
|
|
|
logger.Error($"Failed to start client within {TEN_SECONDS / 1000} seconds!");
|
2018-02-08 13:32:48 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|