2020-07-13 22:57:19 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2020-07-13 22:57:19 +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;
|
2020-07-31 14:37:12 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-07-13 22:57:19 +02:00
|
|
|
|
using System.Collections.Generic;
|
2020-07-22 18:11:51 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-07-31 14:37:12 +02:00
|
|
|
|
using System.Timers;
|
2020-07-22 18:11:51 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2020-07-27 15:58:30 +02:00
|
|
|
|
using SafeExamBrowser.Configuration.Contracts;
|
2023-03-02 23:48:11 +01:00
|
|
|
|
using SafeExamBrowser.Configuration.Contracts.Cryptography;
|
2020-07-22 18:11:51 +02:00
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2020-07-13 22:57:19 +02:00
|
|
|
|
using SafeExamBrowser.Server.Contracts;
|
2020-07-28 19:56:25 +02:00
|
|
|
|
using SafeExamBrowser.Server.Contracts.Data;
|
2020-08-01 17:55:18 +02:00
|
|
|
|
using SafeExamBrowser.Server.Contracts.Events;
|
2024-02-01 17:36:11 +01:00
|
|
|
|
using SafeExamBrowser.Server.Contracts.Events.Proctoring;
|
2020-07-22 18:11:51 +02:00
|
|
|
|
using SafeExamBrowser.Server.Data;
|
2023-02-24 21:33:26 +01:00
|
|
|
|
using SafeExamBrowser.Server.Requests;
|
2020-07-13 22:57:19 +02:00
|
|
|
|
using SafeExamBrowser.Settings.Server;
|
2022-01-14 13:33:35 +01:00
|
|
|
|
using SafeExamBrowser.SystemComponents.Contracts;
|
2022-04-19 18:21:29 +02:00
|
|
|
|
using SafeExamBrowser.SystemComponents.Contracts.Network;
|
2020-12-03 19:47:17 +01:00
|
|
|
|
using SafeExamBrowser.SystemComponents.Contracts.PowerSupply;
|
2020-08-01 17:55:18 +02:00
|
|
|
|
using Timer = System.Timers.Timer;
|
2020-07-13 22:57:19 +02:00
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Server
|
|
|
|
|
{
|
2020-07-31 14:37:12 +02:00
|
|
|
|
public class ServerProxy : ILogObserver, IServerProxy
|
2020-07-13 22:57:19 +02:00
|
|
|
|
{
|
2022-01-14 13:33:35 +01:00
|
|
|
|
private readonly AppConfig appConfig;
|
|
|
|
|
private readonly FileSystem fileSystem;
|
|
|
|
|
private readonly ConcurrentQueue<string> instructionConfirmations;
|
2023-03-02 23:48:11 +01:00
|
|
|
|
private readonly IKeyGenerator keyGenerator;
|
2022-01-14 13:33:35 +01:00
|
|
|
|
private readonly ILogger logger;
|
|
|
|
|
private readonly ConcurrentQueue<ILogContent> logContent;
|
|
|
|
|
private readonly Timer logTimer;
|
|
|
|
|
private readonly Parser parser;
|
|
|
|
|
private readonly Timer pingTimer;
|
|
|
|
|
private readonly IPowerSupply powerSupply;
|
|
|
|
|
private readonly ISystemInfo systemInfo;
|
|
|
|
|
private readonly IUserInfo userInfo;
|
2022-04-19 18:21:29 +02:00
|
|
|
|
private readonly INetworkAdapter networkAdapter;
|
2022-01-14 13:33:35 +01:00
|
|
|
|
|
2020-07-22 18:11:51 +02:00
|
|
|
|
private ApiVersion1 api;
|
2023-02-24 21:33:26 +01:00
|
|
|
|
private int currentHandId;
|
2022-09-02 14:56:49 +02:00
|
|
|
|
private int currentLockScreenId;
|
2020-07-29 23:39:05 +02:00
|
|
|
|
private string examId;
|
2022-02-03 12:15:54 +01:00
|
|
|
|
private HttpClient httpClient;
|
2022-09-02 14:56:49 +02:00
|
|
|
|
private int notificationId;
|
2020-07-31 14:37:12 +02:00
|
|
|
|
private int pingNumber;
|
2023-03-02 23:48:11 +01:00
|
|
|
|
private bool powerSupplyConnected;
|
|
|
|
|
private int powerSupplyValue;
|
2020-07-22 18:11:51 +02:00
|
|
|
|
private ServerSettings settings;
|
2023-03-02 23:48:11 +01:00
|
|
|
|
private int wirelessNetworkValue;
|
2020-07-22 18:11:51 +02:00
|
|
|
|
|
2021-09-17 10:47:02 +02:00
|
|
|
|
public event ServerEventHandler HandConfirmed;
|
2022-08-31 14:11:19 +02:00
|
|
|
|
public event ServerEventHandler LockScreenConfirmed;
|
2021-04-12 10:59:31 +02:00
|
|
|
|
public event ProctoringConfigurationReceivedEventHandler ProctoringConfigurationReceived;
|
|
|
|
|
public event ProctoringInstructionReceivedEventHandler ProctoringInstructionReceived;
|
2020-08-01 17:55:18 +02:00
|
|
|
|
public event TerminationRequestedEventHandler TerminationRequested;
|
2022-08-31 14:11:19 +02:00
|
|
|
|
public event LockScreenRequestedEventHandler LockScreenRequested;
|
2020-08-01 17:55:18 +02:00
|
|
|
|
|
2020-12-03 19:47:17 +01:00
|
|
|
|
public ServerProxy(
|
|
|
|
|
AppConfig appConfig,
|
2023-03-02 23:48:11 +01:00
|
|
|
|
IKeyGenerator keyGenerator,
|
2020-12-03 19:47:17 +01:00
|
|
|
|
ILogger logger,
|
2022-01-14 13:33:35 +01:00
|
|
|
|
ISystemInfo systemInfo,
|
|
|
|
|
IUserInfo userInfo,
|
2022-02-03 12:15:54 +01:00
|
|
|
|
IPowerSupply powerSupply = default,
|
2022-04-19 18:21:29 +02:00
|
|
|
|
INetworkAdapter networkAdapter = default)
|
2020-07-13 22:57:19 +02:00
|
|
|
|
{
|
2020-07-22 18:11:51 +02:00
|
|
|
|
this.api = new ApiVersion1();
|
2020-07-27 15:58:30 +02:00
|
|
|
|
this.appConfig = appConfig;
|
2023-03-02 23:48:11 +01:00
|
|
|
|
this.keyGenerator = keyGenerator;
|
2021-04-12 10:59:31 +02:00
|
|
|
|
this.fileSystem = new FileSystem(appConfig, logger);
|
|
|
|
|
this.instructionConfirmations = new ConcurrentQueue<string>();
|
2020-12-03 19:47:17 +01:00
|
|
|
|
this.logger = logger;
|
2021-04-12 10:59:31 +02:00
|
|
|
|
this.logContent = new ConcurrentQueue<ILogContent>();
|
2021-08-03 14:56:58 +02:00
|
|
|
|
this.logTimer = new Timer();
|
2022-04-19 18:21:29 +02:00
|
|
|
|
this.networkAdapter = networkAdapter;
|
2021-04-12 10:59:31 +02:00
|
|
|
|
this.parser = new Parser(logger);
|
2021-08-03 14:56:58 +02:00
|
|
|
|
this.pingTimer = new Timer();
|
2020-12-03 19:47:17 +01:00
|
|
|
|
this.powerSupply = powerSupply;
|
2022-01-14 13:33:35 +01:00
|
|
|
|
this.systemInfo = systemInfo;
|
|
|
|
|
this.userInfo = userInfo;
|
2020-07-22 18:11:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
public ServerResponse ConfirmLockScreen()
|
|
|
|
|
{
|
|
|
|
|
var request = new ConfirmLockScreenRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(currentLockScreenId, out var message);
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"Successfully sent notification confirmation for lock screen #{currentLockScreenId}.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error($"Failed to send notification confirmation for lock screen #{currentLockScreenId}!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ServerResponse(success, message);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 19:56:25 +02:00
|
|
|
|
public ServerResponse Connect()
|
2020-07-22 18:11:51 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new ApiRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(out api, out var message);
|
2020-07-22 18:11:51 +02:00
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
if (success)
|
2020-07-22 18:11:51 +02:00
|
|
|
|
{
|
|
|
|
|
logger.Info("Successfully loaded server API.");
|
2023-02-24 21:33:26 +01:00
|
|
|
|
success = new OAuth2TokenRequest(api, httpClient, logger, parser, settings).TryExecute(out message);
|
2020-07-22 18:11:51 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to load server API!");
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 19:56:25 +02:00
|
|
|
|
return new ServerResponse(success, message);
|
2020-07-13 22:57:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ServerResponse Disconnect()
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new DisconnectionRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(out var message);
|
2020-07-31 13:24:42 +02:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Successfully terminated connection.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to terminate connection!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ServerResponse(success, message);
|
2020-07-13 22:57:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 12:15:54 +01:00
|
|
|
|
public ServerResponse<IEnumerable<Exam>> GetAvailableExams(string examId = default)
|
2020-07-13 22:57:19 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new AvailableExamsRequest(api, appConfig, httpClient, logger, parser, settings, systemInfo, userInfo);
|
|
|
|
|
var success = request.TryExecute(examId, out var exams, out var message);
|
2020-07-22 18:11:51 +02:00
|
|
|
|
|
2020-07-27 15:58:30 +02:00
|
|
|
|
if (success)
|
2020-07-22 18:11:51 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
logger.Info("Successfully retrieved available exams.");
|
2020-07-22 18:11:51 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
logger.Error("Failed to retrieve available exams!");
|
2020-07-22 18:11:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 15:58:30 +02:00
|
|
|
|
return new ServerResponse<IEnumerable<Exam>>(success, exams, message);
|
2020-07-13 22:57:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ServerResponse<Uri> GetConfigurationFor(Exam exam)
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new ExamConfigurationRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(exam, out var content, out var message);
|
2020-07-27 15:58:30 +02:00
|
|
|
|
var uri = default(Uri);
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Successfully retrieved exam configuration.");
|
2020-07-22 18:11:51 +02:00
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
success = fileSystem.TrySaveFile(content, out uri);
|
2020-07-27 15:58:30 +02:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info($"Successfully saved exam configuration as '{uri}'.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to save exam configuration!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to retrieve exam configuration!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ServerResponse<Uri>(success, uri, message);
|
2020-07-22 18:11:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 19:56:25 +02:00
|
|
|
|
public ConnectionInfo GetConnectionInfo()
|
|
|
|
|
{
|
|
|
|
|
return new ConnectionInfo
|
|
|
|
|
{
|
|
|
|
|
Api = JsonConvert.SerializeObject(api),
|
2023-02-24 21:33:26 +01:00
|
|
|
|
ConnectionToken = BaseRequest.ConnectionToken,
|
|
|
|
|
Oauth2Token = BaseRequest.Oauth2Token
|
2020-07-28 19:56:25 +02:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-22 18:11:51 +02:00
|
|
|
|
public void Initialize(ServerSettings settings)
|
|
|
|
|
{
|
|
|
|
|
this.settings = settings;
|
2022-04-19 18:21:29 +02:00
|
|
|
|
|
2022-02-03 12:15:54 +01:00
|
|
|
|
httpClient = new HttpClient();
|
2020-07-22 18:11:51 +02:00
|
|
|
|
httpClient.BaseAddress = new Uri(settings.ServerUrl);
|
|
|
|
|
|
|
|
|
|
if (settings.RequestTimeout > 0)
|
|
|
|
|
{
|
|
|
|
|
httpClient.Timeout = TimeSpan.FromMilliseconds(settings.RequestTimeout);
|
|
|
|
|
}
|
2020-07-13 22:57:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 23:39:05 +02:00
|
|
|
|
public void Initialize(string api, string connectionToken, string examId, string oauth2Token, ServerSettings settings)
|
2020-07-28 19:56:25 +02:00
|
|
|
|
{
|
|
|
|
|
this.api = JsonConvert.DeserializeObject<ApiVersion1>(api);
|
2020-07-29 23:39:05 +02:00
|
|
|
|
this.examId = examId;
|
2023-02-24 21:33:26 +01:00
|
|
|
|
|
|
|
|
|
BaseRequest.ConnectionToken = connectionToken;
|
|
|
|
|
BaseRequest.Oauth2Token = oauth2Token;
|
2020-07-28 19:56:25 +02:00
|
|
|
|
|
|
|
|
|
Initialize(settings);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
public ServerResponse LockScreen(string text = null)
|
2021-09-17 10:47:02 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new LockScreenRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(currentLockScreenId = ++notificationId, text, out var message);
|
2021-09-17 10:47:02 +02:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
logger.Info($"Successfully sent notification for lock screen #{currentLockScreenId}.");
|
2021-09-17 10:47:02 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
logger.Error($"Failed to send notification for lock screen #{currentLockScreenId}!");
|
2021-09-17 10:47:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
return new ServerResponse(success, message);
|
2021-09-17 10:47:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
public ServerResponse LowerHand()
|
2022-08-31 14:11:19 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new LowerHandRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(currentHandId, out var message);
|
2022-08-31 14:11:19 +02:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
logger.Info("Successfully sent lower hand notification.");
|
2022-08-31 14:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
logger.Error("Failed to send lower hand notification!");
|
2022-08-31 14:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
return new ServerResponse(success, message);
|
2022-08-31 14:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 14:37:12 +02:00
|
|
|
|
public void Notify(ILogContent content)
|
|
|
|
|
{
|
|
|
|
|
logContent.Enqueue(content);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
public ServerResponse RaiseHand(string text = null)
|
2021-09-17 10:47:02 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new RaiseHandRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(currentHandId = ++notificationId, text, out var message);
|
2021-09-17 10:47:02 +02:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Successfully sent raise hand notification.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to send raise hand notification!");
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
return new ServerResponse(success, message);
|
2022-08-31 14:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-08 22:24:29 +01:00
|
|
|
|
public ServerResponse<string> SendSelectedExam(Exam exam)
|
2023-02-24 15:48:54 +01:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new SelectExamRequest(api, httpClient, logger, parser, settings);
|
2023-03-08 22:24:29 +01:00
|
|
|
|
var success = request.TryExecute(exam, out var message, out var appSignatureKeySalt, out var browserExamKey);
|
2023-02-24 15:48:54 +01:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Successfully sent selected exam.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to send selected exam!");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-08 22:24:29 +01:00
|
|
|
|
if (success && appSignatureKeySalt != default)
|
2023-02-24 15:48:54 +01:00
|
|
|
|
{
|
|
|
|
|
logger.Info("App signature key salt detected, performing key exchange...");
|
2023-03-08 22:24:29 +01:00
|
|
|
|
success = TrySendAppSignatureKey(appSignatureKeySalt, out message);
|
2023-02-24 15:48:54 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Info("No app signature key salt detected, skipping key exchange.");
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-08 22:24:29 +01:00
|
|
|
|
if (browserExamKey != default)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Custom browser exam key detected.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ServerResponse<string>(success, browserExamKey, message);
|
2023-02-24 15:48:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 13:52:39 +01:00
|
|
|
|
public ServerResponse SendUserIdentifier(string identifier)
|
2020-07-13 22:57:19 +02:00
|
|
|
|
{
|
2023-11-01 13:52:39 +01:00
|
|
|
|
var request = new UserIdentifierRequest(api, httpClient, logger, parser, settings);
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var success = request.TryExecute(examId, identifier, out var message);
|
2020-07-31 13:24:42 +02:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
2023-11-01 13:52:39 +01:00
|
|
|
|
logger.Info("Successfully sent user identifier.");
|
2020-07-31 13:24:42 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-11-01 13:52:39 +01:00
|
|
|
|
logger.Error("Failed to send user identifier!");
|
2020-07-31 13:24:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new ServerResponse(success, message);
|
2020-07-22 18:11:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-29 23:39:05 +02:00
|
|
|
|
public void StartConnectivity()
|
|
|
|
|
{
|
2020-07-31 14:37:12 +02:00
|
|
|
|
foreach (var item in logger.GetLog())
|
|
|
|
|
{
|
|
|
|
|
logContent.Enqueue(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Subscribe(this);
|
2021-08-03 14:56:58 +02:00
|
|
|
|
logTimer.AutoReset = false;
|
|
|
|
|
logTimer.Elapsed += LogTimer_Elapsed;
|
|
|
|
|
logTimer.Interval = 500;
|
|
|
|
|
logTimer.Start();
|
2020-08-03 14:41:25 +02:00
|
|
|
|
logger.Info("Started sending log items.");
|
|
|
|
|
|
2021-08-03 14:56:58 +02:00
|
|
|
|
pingTimer.AutoReset = false;
|
|
|
|
|
pingTimer.Elapsed += PingTimer_Elapsed;
|
2022-01-03 17:38:15 +01:00
|
|
|
|
pingTimer.Interval = settings.PingInterval;
|
2021-08-03 14:56:58 +02:00
|
|
|
|
pingTimer.Start();
|
2020-12-03 19:48:00 +01:00
|
|
|
|
logger.Info("Started sending pings.");
|
2020-12-03 19:47:17 +01:00
|
|
|
|
|
2022-04-19 18:21:29 +02:00
|
|
|
|
if (powerSupply != default && networkAdapter != default)
|
2020-12-03 19:47:17 +01:00
|
|
|
|
{
|
|
|
|
|
powerSupply.StatusChanged += PowerSupply_StatusChanged;
|
2022-04-19 18:21:29 +02:00
|
|
|
|
networkAdapter.Changed += NetworkAdapter_Changed;
|
2020-12-03 19:47:17 +01:00
|
|
|
|
logger.Info("Started monitoring system components.");
|
|
|
|
|
}
|
2020-07-29 23:39:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopConnectivity()
|
|
|
|
|
{
|
2022-04-19 18:21:29 +02:00
|
|
|
|
if (powerSupply != default && networkAdapter != default)
|
2020-12-03 19:47:17 +01:00
|
|
|
|
{
|
|
|
|
|
powerSupply.StatusChanged -= PowerSupply_StatusChanged;
|
2022-04-19 18:21:29 +02:00
|
|
|
|
networkAdapter.Changed -= NetworkAdapter_Changed;
|
2020-12-03 19:47:17 +01:00
|
|
|
|
logger.Info("Stopped monitoring system components.");
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-31 14:37:12 +02:00
|
|
|
|
logger.Unsubscribe(this);
|
2021-08-03 14:56:58 +02:00
|
|
|
|
logTimer.Stop();
|
|
|
|
|
logTimer.Elapsed -= LogTimer_Elapsed;
|
2020-08-03 14:41:25 +02:00
|
|
|
|
logger.Info("Stopped sending log items.");
|
2020-07-31 14:37:12 +02:00
|
|
|
|
|
2021-08-03 14:56:58 +02:00
|
|
|
|
pingTimer.Stop();
|
|
|
|
|
pingTimer.Elapsed -= PingTimer_Elapsed;
|
2020-08-03 14:41:25 +02:00
|
|
|
|
logger.Info("Stopped sending pings.");
|
2020-07-31 14:37:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 14:56:58 +02:00
|
|
|
|
private void LogTimer_Elapsed(object sender, ElapsedEventArgs args)
|
2020-07-31 14:37:12 +02:00
|
|
|
|
{
|
2021-08-03 14:56:58 +02:00
|
|
|
|
try
|
2020-08-01 17:55:18 +02:00
|
|
|
|
{
|
2021-08-03 14:56:58 +02:00
|
|
|
|
while (!logContent.IsEmpty)
|
2020-08-01 17:55:18 +02:00
|
|
|
|
{
|
|
|
|
|
if (logContent.TryDequeue(out var c) && c is ILogMessage message)
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
new LogRequest(api, httpClient, logger, parser, settings).TryExecute(message);
|
2020-08-01 17:55:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 19:47:17 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2021-08-03 14:56:58 +02:00
|
|
|
|
logger.Error("Failed to send log!", e);
|
2020-12-03 19:47:17 +01:00
|
|
|
|
}
|
2021-08-03 14:56:58 +02:00
|
|
|
|
|
|
|
|
|
logTimer.Start();
|
2020-12-03 19:47:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 14:56:58 +02:00
|
|
|
|
private void PingTimer_Elapsed(object sender, ElapsedEventArgs args)
|
2020-08-01 17:55:18 +02:00
|
|
|
|
{
|
2020-07-31 14:37:12 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
instructionConfirmations.TryDequeue(out var confirmation);
|
2021-04-12 10:59:31 +02:00
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new PingRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(++pingNumber, out var content, out var message, confirmation);
|
2020-07-31 14:37:12 +02:00
|
|
|
|
|
2020-08-01 19:31:27 +02:00
|
|
|
|
if (success)
|
2020-07-31 14:37:12 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
if (parser.TryParseInstruction(content, out var attributes, out var instruction, out var instructionConfirmation))
|
2020-08-01 19:31:27 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
HandleInstruction(attributes, instruction);
|
2021-04-12 10:59:31 +02:00
|
|
|
|
|
2022-02-03 12:15:54 +01:00
|
|
|
|
if (instructionConfirmation != default)
|
2021-04-12 10:59:31 +02:00
|
|
|
|
{
|
|
|
|
|
instructionConfirmations.Enqueue(instructionConfirmation);
|
|
|
|
|
}
|
2020-08-01 19:31:27 +02:00
|
|
|
|
}
|
2020-07-31 14:37:12 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-09-04 15:14:55 +02:00
|
|
|
|
logger.Warn($"Failed to send ping: {message}!");
|
2020-07-31 14:37:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to send ping!", e);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 14:56:58 +02:00
|
|
|
|
pingTimer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void PowerSupply_StatusChanged(IPowerSupplyStatus status)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var value = Convert.ToInt32(status.BatteryCharge * 100);
|
2023-03-13 18:13:42 +01:00
|
|
|
|
var statusChanged = powerSupplyConnected != status.IsOnline;
|
|
|
|
|
var valueChanged = powerSupplyValue != value;
|
2021-08-03 14:56:58 +02:00
|
|
|
|
|
2023-03-13 18:13:42 +01:00
|
|
|
|
if (statusChanged || valueChanged)
|
2021-08-03 14:56:58 +02:00
|
|
|
|
{
|
2023-03-02 23:48:11 +01:00
|
|
|
|
var request = new PowerSupplyRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(status, powerSupplyConnected, powerSupplyValue, out var message);
|
2023-02-24 21:33:26 +01:00
|
|
|
|
|
2023-03-02 23:48:11 +01:00
|
|
|
|
if (success)
|
|
|
|
|
{
|
2023-03-13 18:13:42 +01:00
|
|
|
|
powerSupplyConnected = status.IsOnline;
|
2023-03-02 23:48:11 +01:00
|
|
|
|
powerSupplyValue = value;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-09-04 15:14:55 +02:00
|
|
|
|
logger.Warn($"Failed to send power supply status: {message}!");
|
2023-03-02 23:48:11 +01:00
|
|
|
|
}
|
2021-11-10 12:02:01 +01:00
|
|
|
|
}
|
2021-08-03 14:56:58 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to send power supply status!", e);
|
|
|
|
|
}
|
2020-07-29 23:39:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 18:21:29 +02:00
|
|
|
|
private void NetworkAdapter_Changed()
|
2020-12-03 19:47:17 +01:00
|
|
|
|
{
|
2020-12-10 17:42:50 +01:00
|
|
|
|
const int NOT_CONNECTED = -1;
|
|
|
|
|
|
2020-12-03 19:47:17 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-04-19 18:21:29 +02:00
|
|
|
|
var network = networkAdapter.GetWirelessNetworks().FirstOrDefault(n => n.Status == ConnectionStatus.Connected);
|
2023-03-13 18:13:42 +01:00
|
|
|
|
var statusChanged = network == default && wirelessNetworkValue != NOT_CONNECTED;
|
|
|
|
|
var valueChanged = network != default && Math.Abs(network.SignalStrength - wirelessNetworkValue) >= 5;
|
2020-12-03 19:47:17 +01:00
|
|
|
|
|
2023-03-13 18:13:42 +01:00
|
|
|
|
if (statusChanged || valueChanged)
|
2020-12-03 19:47:17 +01:00
|
|
|
|
{
|
2023-03-02 23:48:11 +01:00
|
|
|
|
var request = new NetworkAdapterRequest(api, httpClient, logger, parser, settings);
|
|
|
|
|
var success = request.TryExecute(network, out var message);
|
2020-12-03 19:47:17 +01:00
|
|
|
|
|
2023-03-02 23:48:11 +01:00
|
|
|
|
if (success)
|
2020-12-10 17:42:50 +01:00
|
|
|
|
{
|
2023-03-02 23:48:11 +01:00
|
|
|
|
wirelessNetworkValue = network?.SignalStrength ?? NOT_CONNECTED;
|
2020-12-10 17:42:50 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-09-04 15:14:55 +02:00
|
|
|
|
logger.Warn($"Failed to send wireless status: {message}!");
|
2020-12-10 17:42:50 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 19:47:17 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to send wireless status!", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 21:33:26 +01:00
|
|
|
|
private void HandleInstruction(Attributes attributes, string instruction)
|
2022-07-27 15:26:42 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
switch (instruction)
|
2022-07-27 15:26:42 +02:00
|
|
|
|
{
|
2023-02-24 21:33:26 +01:00
|
|
|
|
case Instructions.LOCK_SCREEN:
|
|
|
|
|
Task.Run(() => LockScreenRequested?.Invoke(attributes.Message));
|
|
|
|
|
break;
|
|
|
|
|
case Instructions.NOTIFICATION_CONFIRM when attributes.Type == AttributeType.LockScreen:
|
|
|
|
|
Task.Run(() => LockScreenConfirmed?.Invoke());
|
|
|
|
|
break;
|
|
|
|
|
case Instructions.NOTIFICATION_CONFIRM when attributes.Type == AttributeType.Hand:
|
|
|
|
|
Task.Run(() => HandConfirmed?.Invoke());
|
|
|
|
|
break;
|
|
|
|
|
case Instructions.PROCTORING:
|
|
|
|
|
Task.Run(() => ProctoringInstructionReceived?.Invoke(attributes.Instruction));
|
|
|
|
|
break;
|
|
|
|
|
case Instructions.PROCTORING_RECONFIGURATION:
|
|
|
|
|
Task.Run(() => ProctoringConfigurationReceived?.Invoke(attributes.AllowChat, attributes.ReceiveAudio, attributes.ReceiveVideo));
|
|
|
|
|
break;
|
|
|
|
|
case Instructions.QUIT:
|
|
|
|
|
Task.Run(() => TerminationRequested?.Invoke());
|
|
|
|
|
break;
|
2022-07-27 15:26:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 23:48:11 +01:00
|
|
|
|
private bool TrySendAppSignatureKey(string salt, out string message)
|
2023-02-24 15:48:54 +01:00
|
|
|
|
{
|
2023-03-02 23:48:11 +01:00
|
|
|
|
var appSignatureKey = keyGenerator.CalculateAppSignatureKey(BaseRequest.ConnectionToken, salt);
|
2023-02-24 21:33:26 +01:00
|
|
|
|
var request = new AppSignatureKeyRequest(api, httpClient, logger, parser, settings);
|
2023-03-02 23:48:11 +01:00
|
|
|
|
var success = request.TryExecute(appSignatureKey, out message);
|
2023-02-24 15:48:54 +01:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
logger.Info("Successfully sent app signature key.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Error("Failed to send app signature key!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
2020-07-13 22:57:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|