2019-05-29 10:52:46 +02:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2019 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-06-06 15:44:03 +02:00
|
|
|
|
using System;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using System.Collections.Generic;
|
2019-06-06 15:44:03 +02:00
|
|
|
|
using System.IO;
|
2019-06-19 15:40:21 +02:00
|
|
|
|
using System.Security.AccessControl;
|
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
using System.Threading;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using SafeExamBrowser.Communication.Hosts;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
using SafeExamBrowser.Communication.Proxies;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Core.OperationModel;
|
2019-06-06 15:44:03 +02:00
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Contracts.Service;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using SafeExamBrowser.Core.OperationModel;
|
|
|
|
|
using SafeExamBrowser.Core.Operations;
|
2019-06-06 15:44:03 +02:00
|
|
|
|
using SafeExamBrowser.Logging;
|
2019-06-07 15:26:03 +02:00
|
|
|
|
using SafeExamBrowser.Service.Communication;
|
2019-06-18 10:18:56 +02:00
|
|
|
|
using SafeExamBrowser.Service.Operations;
|
2019-06-06 15:44:03 +02:00
|
|
|
|
|
2019-05-29 10:52:46 +02:00
|
|
|
|
namespace SafeExamBrowser.Service
|
|
|
|
|
{
|
|
|
|
|
internal class CompositionRoot
|
|
|
|
|
{
|
2019-06-06 15:44:03 +02:00
|
|
|
|
private ILogger logger;
|
|
|
|
|
|
|
|
|
|
internal IServiceController ServiceController { get; private set; }
|
|
|
|
|
|
2019-05-29 10:52:46 +02:00
|
|
|
|
internal void BuildObjectGraph()
|
|
|
|
|
{
|
2019-06-07 15:26:03 +02:00
|
|
|
|
const string SERVICE_ADDRESS = "net.pipe://localhost/safeexambrowser/service";
|
|
|
|
|
const int FIVE_SECONDS = 5000;
|
2019-06-06 15:44:03 +02:00
|
|
|
|
|
|
|
|
|
InitializeLogging();
|
|
|
|
|
|
2019-06-18 10:18:56 +02:00
|
|
|
|
var proxyFactory = new ProxyFactory(new ProxyObjectFactory(), new ModuleLogger(logger, nameof(ProxyFactory)));
|
2019-06-07 15:26:03 +02:00
|
|
|
|
var serviceHost = new ServiceHost(SERVICE_ADDRESS, new HostObjectFactory(), new ModuleLogger(logger, nameof(ServiceHost)), FIVE_SECONDS);
|
|
|
|
|
var sessionContext = new SessionContext();
|
|
|
|
|
|
|
|
|
|
var bootstrapOperations = new Queue<IOperation>();
|
2019-06-18 10:18:56 +02:00
|
|
|
|
var sessionOperations = new Queue<IOperation>();
|
2019-06-07 15:26:03 +02:00
|
|
|
|
|
2019-06-20 10:55:24 +02:00
|
|
|
|
bootstrapOperations.Enqueue(new RestoreOperation(logger));
|
2019-06-07 15:26:03 +02:00
|
|
|
|
bootstrapOperations.Enqueue(new CommunicationHostOperation(serviceHost, logger));
|
2019-06-18 10:18:56 +02:00
|
|
|
|
bootstrapOperations.Enqueue(new ServiceEventCleanupOperation(logger, sessionContext));
|
2019-06-07 15:26:03 +02:00
|
|
|
|
|
2019-06-19 15:40:21 +02:00
|
|
|
|
sessionOperations.Enqueue(new SessionInitializationOperation(logger, LogWriterFactory, ServiceEventFactory, serviceHost, sessionContext));
|
2019-06-20 10:55:24 +02:00
|
|
|
|
sessionOperations.Enqueue(new LockdownOperation(logger, sessionContext));
|
2019-06-18 10:18:56 +02:00
|
|
|
|
sessionOperations.Enqueue(new SessionActivationOperation(logger, sessionContext));
|
2019-06-07 15:26:03 +02:00
|
|
|
|
|
|
|
|
|
var bootstrapSequence = new OperationSequence(logger, bootstrapOperations);
|
2019-06-18 10:18:56 +02:00
|
|
|
|
var sessionSequence = new OperationSequence(logger, sessionOperations);
|
2019-06-07 15:26:03 +02:00
|
|
|
|
|
2019-06-18 10:18:56 +02:00
|
|
|
|
ServiceController = new ServiceController(logger, bootstrapSequence, sessionSequence, serviceHost, sessionContext);
|
2019-06-06 15:44:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void LogStartupInformation()
|
|
|
|
|
{
|
|
|
|
|
logger.Log($"# Service started at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
|
|
|
|
|
logger.Log(string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void LogShutdownInformation()
|
|
|
|
|
{
|
|
|
|
|
logger?.Log($"# Service terminated at {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeLogging()
|
|
|
|
|
{
|
|
|
|
|
var appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), nameof(SafeExamBrowser));
|
|
|
|
|
var logFolder = Path.Combine(appDataFolder, "Logs");
|
|
|
|
|
var logFilePrefix = DateTime.Now.ToString("yyyy-MM-dd\\_HH\\hmm\\mss\\s");
|
|
|
|
|
var logFilePath = Path.Combine(logFolder, $"{logFilePrefix}_Service.log");
|
|
|
|
|
var logFileWriter = new LogFileWriter(new DefaultLogFormatter(), logFilePath);
|
2019-05-29 10:52:46 +02:00
|
|
|
|
|
2019-06-07 15:26:03 +02:00
|
|
|
|
logger = new Logger();
|
2019-06-06 15:44:03 +02:00
|
|
|
|
logger.LogLevel = LogLevel.Debug;
|
|
|
|
|
logger.Subscribe(logFileWriter);
|
2019-06-07 15:26:03 +02:00
|
|
|
|
logFileWriter.Initialize();
|
2019-05-29 10:52:46 +02:00
|
|
|
|
}
|
2019-06-18 15:51:35 +02:00
|
|
|
|
|
2019-06-19 15:40:21 +02:00
|
|
|
|
private ILogObserver LogWriterFactory(string filePath)
|
2019-06-18 15:51:35 +02:00
|
|
|
|
{
|
|
|
|
|
var writer = new LogFileWriter(new DefaultLogFormatter(), filePath);
|
|
|
|
|
|
|
|
|
|
writer.Initialize();
|
|
|
|
|
|
|
|
|
|
return writer;
|
|
|
|
|
}
|
2019-06-19 15:40:21 +02:00
|
|
|
|
|
|
|
|
|
private EventWaitHandle ServiceEventFactory(string eventName)
|
|
|
|
|
{
|
|
|
|
|
var securityIdentifier = new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null);
|
|
|
|
|
var accessRule = new EventWaitHandleAccessRule(securityIdentifier, EventWaitHandleRights.Synchronize, AccessControlType.Allow);
|
|
|
|
|
var security = new EventWaitHandleSecurity();
|
|
|
|
|
|
|
|
|
|
security.AddAccessRule(accessRule);
|
|
|
|
|
|
|
|
|
|
return new EventWaitHandle(false, EventResetMode.AutoReset, eventName, out _, security);
|
|
|
|
|
}
|
2019-05-29 10:52:46 +02:00
|
|
|
|
}
|
|
|
|
|
}
|