mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-20 13:34:37 +01:00
Disable plugins if they crash the Dashboard page (#6099)
This commit is contained in:
parent
a5ab68ab02
commit
372688b723
5 changed files with 57 additions and 2 deletions
|
@ -291,6 +291,7 @@ namespace BTCPayServer.Hosting
|
|||
});
|
||||
services.TryAddSingleton<BTCPayNetworkProvider>();
|
||||
|
||||
services.AddExceptionHandler<PluginExceptionHandler>();
|
||||
services.TryAddSingleton<AppService>();
|
||||
services.AddTransient<PluginService>();
|
||||
services.AddSingleton<PluginHookService>();
|
||||
|
|
|
@ -293,6 +293,7 @@ namespace BTCPayServer.Hosting
|
|||
|
||||
app.UseStatusCodePagesWithReExecute("/errors/{0}");
|
||||
|
||||
app.UseExceptionHandler("/errors/{0}");
|
||||
app.UsePayServer();
|
||||
app.UseRouting();
|
||||
app.UseCors();
|
||||
|
|
52
BTCPayServer/Plugins/PluginExceptionHandler.cs
Normal file
52
BTCPayServer/Plugins/PluginExceptionHandler.cs
Normal file
|
@ -0,0 +1,52 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Configuration;
|
||||
using BTCPayServer.Logging;
|
||||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Hosting.Internal;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace BTCPayServer.Plugins
|
||||
{
|
||||
public class PluginExceptionHandler : IExceptionHandler
|
||||
{
|
||||
readonly string _pluginDir;
|
||||
readonly IHostApplicationLifetime _applicationLifetime;
|
||||
private readonly Logs _logs;
|
||||
|
||||
public PluginExceptionHandler(IOptions<DataDirectories> options, IHostApplicationLifetime applicationLifetime, Logs logs)
|
||||
{
|
||||
_applicationLifetime = applicationLifetime;
|
||||
_logs = logs;
|
||||
_pluginDir = options.Value.PluginDir;
|
||||
}
|
||||
public ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!GetDisablePluginIfCrash(httpContext) ||
|
||||
!PluginManager.IsExceptionByPlugin(exception, out var pluginName))
|
||||
return ValueTask.FromResult(false);
|
||||
_logs.Configuration.LogError(exception, $"Unhandled exception caused by plugin '{pluginName}', disabling it and restarting...");
|
||||
_ = Task.Delay(3000).ContinueWith((t) => _applicationLifetime.StopApplication());
|
||||
// Returning true here means we will see Error 500 error message.
|
||||
// Returning false means that the user will see a stacktrace.
|
||||
return ValueTask.FromResult(false);
|
||||
}
|
||||
|
||||
internal static bool GetDisablePluginIfCrash(HttpContext httpContext)
|
||||
{
|
||||
return httpContext.Items.TryGetValue("DisablePluginIfCrash", out object renderingDashboard) ||
|
||||
renderingDashboard is not true;
|
||||
}
|
||||
internal static void SetDisablePluginIfCrash(HttpContext httpContext)
|
||||
{
|
||||
httpContext.Items.TryAdd("DisablePluginIfCrash", true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -84,7 +84,7 @@
|
|||
@plugin
|
||||
@if (version != null)
|
||||
{
|
||||
<span>({version})</span>
|
||||
<span>(@version)</span>
|
||||
}
|
||||
</span>
|
||||
<form asp-action="UnInstallPlugin" asp-route-plugin="@plugin">
|
||||
|
|
|
@ -9,7 +9,8 @@
|
|||
@using BTCPayServer.Client
|
||||
@model StoreDashboardViewModel
|
||||
@{
|
||||
ViewData.SetActivePage(StoreNavPages.Dashboard, Model.StoreName, Model.StoreId);
|
||||
BTCPayServer.Plugins.PluginExceptionHandler.SetDisablePluginIfCrash(Context);
|
||||
ViewData.SetActivePage(StoreNavPages.Dashboard, Model.StoreName, Model.StoreId);
|
||||
var store = ViewContext.HttpContext.GetStoreData();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue