Skip to content

Commit

Permalink
fix: fix typo, optimize file search code
Browse files Browse the repository at this point in the history
  • Loading branch information
nidbCN committed Dec 9, 2024
1 parent 237c2a7 commit acacf7e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Configs/CertConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
public record CertConfig
{
public required string CertSearchPath { get; init; }

// ReSharper disable once StringLiteralTypo
public string CertFileName { get; set; } = "fullchain.pem";
public string PrivateKeyFileName { get; set; } = "privkey.pem";

public bool RecursionSearch { get; init; } = true;
public uint IntervalHour { get; init; } = 24;
public uint CacheTimeoutMin { get; init; } = 30;
Expand Down
28 changes: 17 additions & 11 deletions Services/CertScanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace AliCdnSSLWorker.Services;
public class CertScanService
{
private readonly ILogger<CertScanService> _logger;
private readonly CertConfig _options;
private readonly IOptions<CertConfig> _options;
private DateTime _lastScan;
private readonly HashSet<string> _domainList;

Expand All @@ -17,10 +17,9 @@ public class CertScanService
public CertScanService(ILogger<CertScanService> logger, IOptions<CertConfig> options)
{
_logger = logger;
_options = options;

_options = options.Value ?? throw new ArgumentNullException(nameof(options));

_domainList = _options.DomainList;
_domainList = _options.Value.DomainList;

ScanCertAsync().GetAwaiter().GetResult();
}
Expand All @@ -30,7 +29,7 @@ public CertScanService(ILogger<CertScanService> logger, IOptions<CertConfig> opt
ArgumentNullException.ThrowIfNull(domain);

var time = DateTime.Now - _lastScan;
if (time > TimeSpan.FromMinutes(_options.CacheTimeoutMin))
if (time > TimeSpan.FromMinutes(_options.Value.CacheTimeoutMin))
{
ScanCertAsync().Wait();
_lastScan = DateTime.Now;
Expand All @@ -42,21 +41,28 @@ public CertScanService(ILogger<CertScanService> logger, IOptions<CertConfig> opt

private async Task ScanCertAsync()
{
var dir = new DirectoryInfo(_options.CertSearchPath);
var dir = new DirectoryInfo(_options.Value.CertSearchPath);
if (!dir.Exists)
{
_logger.LogError("Dir {d} isn't exists!", dir.FullName);
return;
}

var dirList = dir.GetDirectories();
var dirList = _options.Value.RecursionSearch
? dir.GetDirectories()
: [dir];

foreach (var subDir in dirList)
{
// ReSharper disable once StringLiteralTypo
var certFile = new FileInfo(Path.Combine(subDir.FullName, "fullchain.pem"));
var privateKeyFile = new FileInfo(Path.Combine(subDir.FullName, "privkey.pem"));
var certFile = subDir
.GetFiles()
.FirstOrDefault(f => f.Name == _options.Value.CertFileName);

var privateKeyFile = subDir
.GetFiles()
.FirstOrDefault(f => f.Name == _options.Value.PrivateKeyFileName);

if (!certFile.Exists || !privateKeyFile.Exists)
if (certFile is null || privateKeyFile is null)
{
_logger.LogWarning("Can not found cert or private key file, skip {d}", subDir.Name);
continue;
Expand Down

0 comments on commit acacf7e

Please sign in to comment.