Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Challenge fails with Connection refused (challenge type http-01) #37

Merged
merged 4 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public class LetsEncryptOptions
/// </summary>
public RenewalFailMode RenewalFailMode { get; set; } = RenewalFailMode.LogAndContinue;

/// <summary>
/// Gets or sets the <see cref="Certes.KeyAlgorithm"/> used to request a new LetsEncrypt certificate.
/// </summary>
public KeyAlgorithm KeyAlgorithm { get; set; } = KeyAlgorithm.ES256;
/// <summary>
/// Gets or sets the <see cref="Certes.KeyAlgorithm"/> used to request a new LetsEncrypt certificate.
/// </summary>
public KeyAlgorithm KeyAlgorithm { get; set; } = KeyAlgorithm.ES256;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using FluffySpoon.AspNet.LetsEncrypt.Certificates;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using static FluffySpoon.AspNet.LetsEncrypt.Certificates.CertificateRenewalStatus;

Expand All @@ -14,6 +15,7 @@ public class LetsEncryptRenewalService : ILetsEncryptRenewalService
private readonly ICertificateProvider _certificateProvider;
private readonly IEnumerable<ICertificateRenewalLifecycleHook> _lifecycleHooks;
private readonly ILogger<ILetsEncryptRenewalService> _logger;
private readonly IApplicationLifetime _lifetime;
private readonly SemaphoreSlim _semaphoreSlim;
private readonly LetsEncryptOptions _options;

Expand All @@ -22,11 +24,13 @@ public class LetsEncryptRenewalService : ILetsEncryptRenewalService
public LetsEncryptRenewalService(
ICertificateProvider certificateProvider,
IEnumerable<ICertificateRenewalLifecycleHook> lifecycleHooks,
IApplicationLifetime lifetime,
ILogger<ILetsEncryptRenewalService> logger,
LetsEncryptOptions options)
{
_certificateProvider = certificateProvider;
_lifecycleHooks = lifecycleHooks;
_lifetime = lifetime;
_logger = logger;
_options = options;
_semaphoreSlim = new SemaphoreSlim(1);
Expand All @@ -45,10 +49,12 @@ public async Task StartAsync(CancellationToken cancellationToken)
" which means that the LetsEncrypt certificate will never renew.");
}

_lifetime.ApplicationStarted.Register(() => OnApplicationStarted(cancellationToken));

foreach (var lifecycleHook in _lifecycleHooks)
await lifecycleHook.OnStartAsync();

_timer = new Timer(async state => await RunOnceWithErrorHandlingAsync(), null, TimeSpan.Zero, TimeSpan.FromHours(1));
_timer = new Timer(async state => await RunOnceWithErrorHandlingAsync(), null, Timeout.InfiniteTimeSpan, TimeSpan.FromHours(1));
}

public async Task StopAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -93,19 +99,22 @@ public async Task RunOnceAsync()

private async Task RunOnceWithErrorHandlingAsync()
{
try
{
try {
await RunOnceAsync();
_timer?.Change(TimeSpan.FromHours(1), TimeSpan.FromHours(1));
}
catch (Exception e) when (_options.RenewalFailMode != RenewalFailMode.Unhandled)
{
} catch (Exception e) when (_options.RenewalFailMode != RenewalFailMode.Unhandled) {
_logger.LogWarning(e, $"Exception occured renewing certificates: '{e.Message}.'");
if (_options.RenewalFailMode == RenewalFailMode.LogAndRetry)
if (_options.RenewalFailMode == RenewalFailMode.LogAndRetry) {
_timer?.Change(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
}
}
}

private void OnApplicationStarted(CancellationToken t) {
_logger.LogInformation("Application started");
_timer?.Change(TimeSpan.Zero, TimeSpan.FromHours(1));
}

public void Dispose()
{
_timer?.Dispose();
Expand Down
3 changes: 2 additions & 1 deletion src/FluffySpoon.AspNet.LetsEncrypt/RegistrationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ public static void AddFluffySpoonLetsEncrypt(
services.AddSingleton<ILetsEncryptClientFactory, LetsEncryptClientFactory>();
services.AddSingleton<ICertificateValidator, CertificateValidator>();
services.AddSingleton<ICertificateProvider, CertificateProvider>();

services.AddTransient<ILetsEncryptRenewalService, LetsEncryptRenewalService>();
services.AddTransient<IHostedService, LetsEncryptRenewalService>();
services.AddTransient<IHostedService, LetsEncryptRenewalService>();
}

public static void UseFluffySpoonLetsEncrypt(
Expand Down