private Task <ScanTestMediaServerResult> DoScan(MediaServerInfo mediaServer, CancellationToken cancellationToken) { try { return(new ScanTestMediaServer(Options).Run(mediaServer, cancellationToken)); } catch (Exception ex) { return(Task.FromResult(ScanTestMediaServerResult.Fail(mediaServer, ex))); } }
private async Task <ScanTestMediaServerResult> Scan(string mediaServerId, CancellationToken cancellationToken) { var attempt = 1; while (true) { Console.Error.WriteLine(); Console.Error.WriteLine($"Test Attempt #{attempt}"); // get current info var mediaServer = await GetMediaServer(mediaServerId).ConfigureAwait(false); if (mediaServer == null) { Console.Error.WriteLine("Media Server is gone. Skipping..."); return(ScanTestMediaServerResult.Skip(mediaServer, "Media Server is gone.")); } // don't test inactive Media Servers if (!mediaServer.Active) { Console.Error.WriteLine("Media Server is inactive. Skipping..."); return(ScanTestMediaServerResult.Skip(mediaServer, "Media Server is inactive.")); } // don't test draining Media Servers if (mediaServer.Draining) { Console.Error.WriteLine("Media Server is draining. Skipping..."); return(ScanTestMediaServerResult.Skip(mediaServer, "Media Server is draining.")); } // don't test over-capacity Media Servers if (mediaServer.OverCapacity) { Console.Error.WriteLine("Media Server is over-capacity. Skipping..."); return(ScanTestMediaServerResult.Skip(mediaServer, "Media Server is over-capacity.")); } // scan Media Server var result = await DoScan(mediaServer, cancellationToken).ConfigureAwait(false); // check for success if (result.State != ScanTestState.Fail) { return(result); } // check for acceptable failures if (result.Exception is MediaServerMismatchException) { if (await MediaServerIsGone(mediaServer.Id).ConfigureAwait(false)) { Console.Error.WriteLine("Media Server has unregistered. Skipping..."); return(ScanTestMediaServerResult.Skip(mediaServer, "Media Server has unregistered.")); } else if (await MediaServerWouldBeOverCapacity(mediaServer.Id).ConfigureAwait(false)) { Console.Error.WriteLine("Media Server would be over-capacity. Skipping..."); return(ScanTestMediaServerResult.Skip(mediaServer, "Media Server would be over-capacity.")); } } // check for max attempts if (attempt >= Options.MaxAttempts) { return(result); } attempt++; // time to try again await Task.Delay(Options.AttemptInterval *Constants.MillisecondsPerSecond).ConfigureAwait(false); } }
public async Task <ScanTestMediaServerResult> Run(MediaServerInfo mediaServer, CancellationToken cancellationToken) { var result = ScanTestMediaServerResult.Pass(mediaServer); try { await RegisterClient(cancellationToken).ConfigureAwait(false); try { await JoinChannel(cancellationToken).ConfigureAwait(false); // test a variety of scenarios foreach (var scenario in new[] { ScanTestScenario.Host, ScanTestScenario.Stun, ScanTestScenario.TurnUdp, ScanTestScenario.TurnTcp, ScanTestScenario.Turns }) { if (Options.ShouldTest(scenario)) { try { TimeSpan?certificateValidFor = null; if (scenario.RequiresTls()) { // set certificate check TcpSocket.ClientSslAuthenticate = (sslStream, targetHost, enabledSslProtocols) => { sslStream.AuthenticateAsClient(targetHost, null, enabledSslProtocols, false); if (sslStream.RemoteCertificate is X509Certificate2 remoteCertificate) { certificateValidFor = remoteCertificate.NotAfter - DateTime.UtcNow; if (ServerCertificates.TryAdd(targetHost, remoteCertificate)) { var daysRemaining = Math.Max(0, certificateValidFor.Value.TotalDays); Console.Error.WriteLine(string.Join(Environment.NewLine, new[] { $"TLS certificate subject: {remoteCertificate.Subject}", $"TLS certificate issuer: {remoteCertificate.Issuer}", $"TLS certificate issued: {remoteCertificate.NotBefore:yyyy-MM-ddTHH\\:mm\\:ss}", $"TLS certificate expiry: {remoteCertificate.NotAfter:yyyy-MM-ddTHH\\:mm\\:ss} ({(int)daysRemaining} day(s) remaining)", $"TLS certificate thumbprint: {remoteCertificate.Thumbprint}" })); } } }; } try { await OpenConnection(mediaServer.Id, scenario, cancellationToken).ConfigureAwait(false); // pass result.SetScenarioResult(scenario, ScanTestScenarioResult.Pass(scenario, certificateValidFor)); } catch (Exception ex) { // fail result.SetScenarioResult(scenario, ScanTestScenarioResult.Fail(scenario, ex)); } finally { await CloseConnection().ConfigureAwait(false); } }