Beispiel #1
0
        public async Task Invoke(HttpContext context)
        {
            var instanceLogger = AcmeCertificateManager.GetInstanceLogger();

            if (instanceLogger == null)
            {
                throw new NullReferenceException("Instance Logger null on AcmeCertificationManger.");
            }

            try
            {
                context.Response.Headers.Add("Date", DateTime.UtcNow.ToJSONString());

                var requestPath = context.Request.PathBase + context.Request.Path;

                _storage.Init(_settings, instanceLogger);

                instanceLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Verbose, "AcmeResponseMiddleware_Invoke", $"Received request", requestPath.Value.ToKVP("requestPath"));

                if (requestPath.StartsWithSegments(AcmeResponsePath, out PathString requestPathId))
                {
                    var challenge = requestPathId.Value.TrimStart('/');
                    instanceLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Verbose, "AcmeResponseMiddleware_Invoke", $"Received request", challenge.ToKVP("challenge"), requestPath.Value.ToKVP("requestPath"));

                    var response = await _storage.GetResponseAsync(challenge);

                    if (!string.IsNullOrEmpty(response))
                    {
                        instanceLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Verbose, "AcmeResponseMiddleware_Invoke", "Found challenge and sent response", response.ToKVP("response"), challenge.ToKVP("challenge"));

                        context.Response.ContentType = "text/plain";
                        context.Response.StatusCode  = 200;
                        await context.Response.WriteAsync(response);
                    }
                    else
                    {
                        instanceLogger.AddError("AcmeResponseMiddleware_Invoke", "Could not find challenge", challenge.ToKVP("challenge"));

                        context.Response.StatusCode = 404;
                    }
                }
                else
                {
                    await _next.Invoke(context);
                }
            }
            catch (Exception ex)
            {
                instanceLogger.AddException("AcmeResponseMiddleware_Invoke", ex, context.Request.Path.ToString().ToKVP("path"));
            }
        }
Beispiel #2
0
        public async Task Invoke(HttpContext context)
        {
            var requestPath = context.Request.PathBase + context.Request.Path;

            _logger.LogWarning($"Received request {requestPath}");

            if (requestPath.StartsWithSegments(AcmeResponsePath, out PathString requestPathId))
            {
                var challenge = requestPathId.Value.TrimStart('/');
                if (_settings.Diagnostics)
                {
                    _logger.LogWarning($"[AcmeResponseMiddleWare] Acme challenge received on {requestPath}, challenge id = {challenge}");
                }

                var response = await _storage.GetResponseAsync(challenge);

                if (!string.IsNullOrEmpty(response))
                {
                    if (_settings.Diagnostics)
                    {
                        _logger.LogWarning($"[AcmeResponseMiddleWare] Acme challenge response found: {response}");
                    }
                    context.Response.ContentType = "text/plain";
                    context.Response.StatusCode  = 200;
                    await context.Response.WriteAsync(response);
                }
                else
                {
                    if (_settings.Diagnostics)
                    {
                        _logger.LogError($"[AcmeResponseMiddleWare] !!!! Error: Acme challenge response for challenge id {challenge} NOT FOUND!");
                    }
                    context.Response.StatusCode = 404;
                }
            }
            else
            {
                await _next.Invoke(context);
            }
        }