Skip to content

Commit e9a9c5e

Browse files
Pass ILoggerFactory to Unobtanium ProxyServer for logging integration
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
1 parent 055b006 commit e9a9c5e

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

DevProxy/ApiControllers/ProxyController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ namespace DevProxy.ApiControllers;
1414
[ApiController]
1515
[Route("[controller]")]
1616
#pragma warning disable CA1515 // required for the API controller
17-
public sealed class ProxyController(IProxyStateController proxyStateController, IProxyConfiguration proxyConfiguration) : ControllerBase
17+
public sealed class ProxyController(IProxyStateController proxyStateController, IProxyConfiguration proxyConfiguration, ILoggerFactory loggerFactory) : ControllerBase
1818
#pragma warning restore CA1515
1919
{
2020
private readonly IProxyStateController _proxyStateController = proxyStateController;
2121
private readonly IProxyConfiguration _proxyConfiguration = proxyConfiguration;
22+
private readonly ILoggerFactory _loggerFactory = loggerFactory;
2223

2324
[HttpGet]
2425
public ProxyInfo Get() => ProxyInfo.From(_proxyStateController.ProxyState, _proxyConfiguration);
@@ -114,6 +115,9 @@ public IActionResult GetRootCertificate([FromQuery][Required] string format)
114115
return ValidationProblem(ModelState);
115116
}
116117

118+
// Ensure ProxyServer is initialized with LoggerFactory for Unobtanium logging
119+
ProxyEngine.EnsureProxyServerInitialized(_loggerFactory);
120+
117121
var certificate = ProxyEngine.ProxyServer.CertificateManager.RootCertificate;
118122
if (certificate == null)
119123
{

DevProxy/Commands/CertCommand.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ namespace DevProxy.Commands;
1414
sealed class CertCommand : Command
1515
{
1616
private readonly ILogger _logger;
17+
private readonly ILoggerFactory _loggerFactory;
1718
private readonly Option<bool> _forceOption = new("--force", "-f")
1819
{
1920
Description = "Don't prompt for confirmation when removing the certificate"
2021
};
2122

22-
public CertCommand(ILogger<CertCommand> logger) :
23+
public CertCommand(ILogger<CertCommand> logger, ILoggerFactory loggerFactory) :
2324
base("cert", "Manage the Dev Proxy certificate")
2425
{
2526
_logger = logger;
27+
_loggerFactory = loggerFactory;
2628

2729
ConfigureCommand();
2830
}
@@ -49,6 +51,9 @@ private async Task EnsureCertAsync()
4951

5052
try
5153
{
54+
// Ensure ProxyServer is initialized with LoggerFactory for Unobtanium logging
55+
ProxyEngine.EnsureProxyServerInitialized(_loggerFactory);
56+
5257
_logger.LogInformation("Ensuring certificate exists and is trusted...");
5358
await ProxyEngine.ProxyServer.CertificateManager.EnsureRootCertificateAsync();
5459
_logger.LogInformation("DONE");
@@ -79,6 +84,9 @@ public void RemoveCert(ParseResult parseResult)
7984

8085
_logger.LogInformation("Uninstalling the root certificate...");
8186

87+
// Ensure ProxyServer is initialized with LoggerFactory for Unobtanium logging
88+
ProxyEngine.EnsureProxyServerInitialized(_loggerFactory);
89+
8290
RemoveTrustedCertificateOnMac();
8391
ProxyEngine.ProxyServer.CertificateManager.RemoveTrustedRootCertificate(machineTrusted: false);
8492

DevProxy/Proxy/ProxyEngine.cs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ sealed class ProxyEngine(
3030
IProxyConfiguration proxyConfiguration,
3131
ISet<UrlToWatch> urlsToWatch,
3232
IProxyStateController proxyController,
33-
ILogger<ProxyEngine> logger) : BackgroundService, IDisposable
33+
ILogger<ProxyEngine> logger,
34+
ILoggerFactory loggerFactory) : BackgroundService, IDisposable
3435
{
3536
private readonly IEnumerable<IPlugin> _plugins = plugins;
3637
private readonly ILogger _logger = logger;
3738
private readonly IProxyConfiguration _config = proxyConfiguration;
3839

39-
internal static ProxyServer ProxyServer { get; private set; }
40+
internal static ProxyServer ProxyServer { get; private set; } = null!;
41+
private static bool _isProxyServerInitialized;
42+
private static readonly object _initLock = new();
4043
private ExplicitProxyEndPoint? _explicitEndPoint;
4144
// lists of URLs to watch, used for intercepting requests
4245
private readonly ISet<UrlToWatch> _urlsToWatch = urlsToWatch;
@@ -56,23 +59,49 @@ sealed class ProxyEngine(
5659

5760
static ProxyEngine()
5861
{
59-
ProxyServer = new();
60-
ProxyServer.CertificateManager.PfxFilePath = Environment.GetEnvironmentVariable("DEV_PROXY_CERT_PATH") ?? string.Empty;
61-
ProxyServer.CertificateManager.RootCertificateName = "Dev Proxy CA";
62-
ProxyServer.CertificateManager.CertificateStorage = new CertificateDiskCache();
63-
// we need to change this to a value lower than 397
64-
// to avoid the ERR_CERT_VALIDITY_TOO_LONG error in Edge
65-
ProxyServer.CertificateManager.CertificateValidDays = 365;
66-
67-
using var joinableTaskContext = new JoinableTaskContext();
68-
var joinableTaskFactory = new JoinableTaskFactory(joinableTaskContext);
69-
_ = joinableTaskFactory.Run(async () => await ProxyServer.CertificateManager.LoadOrCreateRootCertificateAsync());
62+
// ProxyServer initialization moved to EnsureProxyServerInitialized
63+
// to enable passing ILoggerFactory for Unobtanium logging
64+
}
65+
66+
// Ensure ProxyServer is initialized with the given ILoggerFactory
67+
// This method can be called from multiple places (ProxyEngine, CertCommand, etc.)
68+
internal static void EnsureProxyServerInitialized(ILoggerFactory? loggerFactory = null)
69+
{
70+
if (_isProxyServerInitialized)
71+
{
72+
return;
73+
}
74+
75+
lock (_initLock)
76+
{
77+
if (_isProxyServerInitialized)
78+
{
79+
return;
80+
}
81+
82+
ProxyServer = new(loggerFactory: loggerFactory);
83+
ProxyServer.CertificateManager.PfxFilePath = Environment.GetEnvironmentVariable("DEV_PROXY_CERT_PATH") ?? string.Empty;
84+
ProxyServer.CertificateManager.RootCertificateName = "Dev Proxy CA";
85+
ProxyServer.CertificateManager.CertificateStorage = new CertificateDiskCache();
86+
// we need to change this to a value lower than 397
87+
// to avoid the ERR_CERT_VALIDITY_TOO_LONG error in Edge
88+
ProxyServer.CertificateManager.CertificateValidDays = 365;
89+
90+
using var joinableTaskContext = new JoinableTaskContext();
91+
var joinableTaskFactory = new JoinableTaskFactory(joinableTaskContext);
92+
_ = joinableTaskFactory.Run(async () => await ProxyServer.CertificateManager.LoadOrCreateRootCertificateAsync());
93+
94+
_isProxyServerInitialized = true;
95+
}
7096
}
7197

7298
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
7399
{
74100
_cancellationToken = stoppingToken;
75101

102+
// Initialize ProxyServer with LoggerFactory for Unobtanium logging
103+
EnsureProxyServerInitialized(loggerFactory);
104+
76105
Debug.Assert(ProxyServer is not null, "Proxy server is not initialized");
77106

78107
if (!_urlsToWatch.Any())

0 commit comments

Comments
 (0)