Merge pull request #352 from Moonlight-Panel/v2_AntiAdBlocker

Implemented basic adblocker prevention
This commit is contained in:
Marcel Baumgartner 2024-01-05 11:44:18 +01:00 committed by GitHub
commit a2a9a6e21d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 3 deletions

View file

@ -17,6 +17,14 @@ public class ConfigV1
[JsonProperty("Store")] public StoreData Store { get; set; } = new(); [JsonProperty("Store")] public StoreData Store { get; set; } = new();
[JsonProperty("Theme")] public ThemeData Theme { get; set; } = new(); [JsonProperty("Theme")] public ThemeData Theme { get; set; } = new();
[JsonProperty("Advertisement")] public AdvertisementData Advertisement { get; set; } = new();
public class AdvertisementData
{
[JsonProperty("PreventAdBlockers")]
[Description("This prevents users from using ad blockers while using moonlight. (Note: The detection might not always work)")]
public bool PreventAdBlockers { get; set; }
}
public class ThemeData public class ThemeData
{ {
[JsonProperty("EnableDefault")] public bool EnableDefault { get; set; } = true; [JsonProperty("EnableDefault")] public bool EnableDefault { get; set; } = true;

View file

@ -0,0 +1,29 @@
using Microsoft.JSInterop;
using Moonlight.App.Helpers;
namespace Moonlight.App.Services.Interop;
public class AdBlockService
{
private readonly IJSRuntime JsRuntime;
public AdBlockService(IJSRuntime jsRuntime)
{
JsRuntime = jsRuntime;
}
public async Task<bool> Detect()
{
try
{
return await JsRuntime.InvokeAsync<bool>("moonlight.utils.vendo"); // lat. vendo = advertisement xd
}
catch (Exception e)
{
Logger.Warn("An unexpected error occured while trying to detect possible ad blockers");
Logger.Warn(e);
return false;
}
}
}

View file

@ -64,6 +64,7 @@ builder.Services.AddScoped<ToastService>();
builder.Services.AddScoped<ModalService>(); builder.Services.AddScoped<ModalService>();
builder.Services.AddScoped<AlertService>(); builder.Services.AddScoped<AlertService>();
builder.Services.AddScoped<FileDownloadService>(); builder.Services.AddScoped<FileDownloadService>();
builder.Services.AddScoped<AdBlockService>();
// Services / Store // Services / Store
builder.Services.AddScoped<StoreService>(); builder.Services.AddScoped<StoreService>();

View file

@ -0,0 +1,16 @@
<div class="w-100">
<div class="card-body">
<div class="text-start mb-8">
<h1 class="text-dark mb-3 fs-3x">
AdBlocker detected
</h1>
<div class="text-gray-400 fw-semibold fs-6">
We have detected that you are using an adblocker. Disable your adblocker or add this site as an exception and reload the page to proceed
</div>
</div>
<div class="d-flex flex-stack">
<a href="javascript:location.reload()" class="btn btn-primary me-2 flex-shrink-0">I have disabled my adblocker</a>
</div>
</div>
</div>

View file

@ -1,6 +1,4 @@
@inject NavigationManager Navigation <div class="w-100">
<div class="w-100">
<div class="card-body"> <div class="card-body">
<div class="text-start mb-8"> <div class="text-start mb-8">
<h1 class="text-dark mb-3 fs-3x"> <h1 class="text-dark mb-3 fs-3x">

View file

@ -14,6 +14,7 @@
@inject SessionService SessionService @inject SessionService SessionService
@inject NavigationManager Navigation @inject NavigationManager Navigation
@inject ConnectionService ConnectionService @inject ConnectionService ConnectionService
@inject AdBlockService AdBlockService
@{ @{
var url = new Uri(Navigation.Uri); var url = new Uri(Navigation.Uri);
@ -41,6 +42,12 @@
<RestartAlert/> <RestartAlert/>
</OverlayLayout> </OverlayLayout>
} }
else if (ConfigService.Get().Advertisement.PreventAdBlockers && AdBlockerDetected)
{
<OverlayLayout>
<AdBlockAlert />
</OverlayLayout>
}
else else
{ {
<DefaultLayout> <DefaultLayout>
@ -100,6 +107,7 @@ else
{ {
private bool Initialized = false; private bool Initialized = false;
private bool RestartLock = false; private bool RestartLock = false;
private bool AdBlockerDetected = false;
private Session? MySession; private Session? MySession;
@ -148,6 +156,9 @@ else
await SessionService.Register(MySession); await SessionService.Register(MySession);
if(ConfigService.Get().Advertisement.PreventAdBlockers)
AdBlockerDetected = await AdBlockService.Detect();
Initialized = true; Initialized = true;
await InvokeAsync(StateHasChanged); await InvokeAsync(StateHasChanged);

View file

@ -120,6 +120,25 @@ window.moonlight = {
anchorElement.click(); anchorElement.click();
anchorElement.remove(); anchorElement.remove();
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
},
vendo: function ()
{
try
{
var request = new XMLHttpRequest();
request.open("GET", "https://pagead2.googlesyndication.com/pagead/js/aidsbygoogle.js?client=ca-pub-1234567890123456", false);
request.send();
if(request.status === 404)
return false;
return true;
}
catch (e)
{
return false;
}
} }
}, },
textEditor: { textEditor: {