Merge pull request #318 from Moonlight-Panel/AddBotCheck

Implemented a basic bot check
This commit is contained in:
Marcel Baumgartner 2023-09-18 18:59:02 +02:00 committed by GitHub
commit 84d39c1c75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 10 deletions

View file

@ -116,6 +116,14 @@ public class ConfigV1
[JsonProperty("DenyRegister")] [JsonProperty("DenyRegister")]
[Description("Prevent every new user to register")] [Description("Prevent every new user to register")]
public bool DenyRegister { get; set; } = false; public bool DenyRegister { get; set; } = false;
[JsonProperty("CheckForBots")]
[Description("Check for bots when a user has been registered")]
public bool CheckForBots { get; set; } = true;
[JsonProperty("BlockLinuxUsers")]
[Description("Blocks linux users from registering")]
public bool BlockLinuxUsers { get; set; } = false;
} }
public class CleanupData public class CleanupData

View file

@ -3,6 +3,7 @@ using JWT.Algorithms;
using JWT.Builder; using JWT.Builder;
using JWT.Exceptions; using JWT.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.JSInterop;
using Moonlight.App.Database.Entities; using Moonlight.App.Database.Entities;
using Moonlight.App.Helpers; using Moonlight.App.Helpers;
using Moonlight.App.Perms; using Moonlight.App.Perms;
@ -16,6 +17,7 @@ public class IdentityService
private readonly Repository<User> UserRepository; private readonly Repository<User> UserRepository;
private readonly CookieService CookieService; private readonly CookieService CookieService;
private readonly IHttpContextAccessor HttpContextAccessor; private readonly IHttpContextAccessor HttpContextAccessor;
private readonly IJSRuntime JsRuntime;
private readonly string Secret; private readonly string Secret;
public User User { get; private set; } public User User { get; private set; }
@ -29,11 +31,13 @@ public class IdentityService
CookieService cookieService, CookieService cookieService,
Repository<User> userRepository, Repository<User> userRepository,
IHttpContextAccessor httpContextAccessor, IHttpContextAccessor httpContextAccessor,
ConfigService configService) ConfigService configService,
IJSRuntime jsRuntime)
{ {
CookieService = cookieService; CookieService = cookieService;
UserRepository = userRepository; UserRepository = userRepository;
HttpContextAccessor = httpContextAccessor; HttpContextAccessor = httpContextAccessor;
JsRuntime = jsRuntime;
Secret = configService Secret = configService
.Get() .Get()
@ -260,4 +264,21 @@ public class IdentityService
Permissions.IsReadyOnly = true; Permissions.IsReadyOnly = true;
} }
public async Task<bool> GetBotStatus()
{
var webDriverStatus = await JsRuntime
.InvokeAsync<bool>("moonlight.utils.getWebDriverStatus");
if (webDriverStatus)
return true;
var languagesStatus = await JsRuntime
.InvokeAsync<bool>("moonlight.utils.getLanguagesStatus");
if (languagesStatus)
return true;
return false;
}
} }

View file

@ -61,6 +61,19 @@ public class UserService
Logger.Warn($"A user tried to use a blacklisted domain to register. Email: '{email}'", "security"); Logger.Warn($"A user tried to use a blacklisted domain to register. Email: '{email}'", "security");
throw new DisplayException("This email is blacklisted"); throw new DisplayException("This email is blacklisted");
} }
if (ConfigService.Get().Moonlight.Auth.BlockLinuxUsers && IdentityService.Device.Contains("Linux"))
throw new DisplayException("This operation was disabled");
if (ConfigService.Get().Moonlight.Auth.CheckForBots)
{
var isABot = await IdentityService.GetBotStatus();
if (isABot)
{
throw new DisplayException("This operation was disabled");
}
}
// Check if the email is already taken // Check if the email is already taken
var emailTaken = UserRepository.Get().FirstOrDefault(x => x.Email == email) != null; var emailTaken = UserRepository.Get().FirstOrDefault(x => x.Email == email) != null;

View file

@ -82,7 +82,7 @@ public class Startup
return; return;
} }
if(ConfigService.DebugMode || uri.HostNameType == UriHostNameType.IPv4) if(ConfigService.DebugMode || uri.HostNameType == UriHostNameType.IPv4 || !ConfigService.Get().Moonlight.LetsEncrypt.Enable)
await WebApplication.RunAsync(); await WebApplication.RunAsync();
else else
await WebApplication.RunAsync(ConfigService.Get().Moonlight.AppUrl); await WebApplication.RunAsync(ConfigService.Get().Moonlight.AppUrl);

View file

@ -292,6 +292,20 @@
}, },
showNotification: function (title, text, img) { showNotification: function (title, text, img) {
let notification = new Notification(title, {body: text, icon: img}); let notification = new Notification(title, {body: text, icon: img});
},
getWebDriverStatus: function () {
if (navigator.webdriver)
return true;
else
return false;
},
getLanguagesStatus: function()
{
if (!navigator.languages || navigator.languages.length === 0) {
return false;
} else {
return true;
}
} }
}, },
loading: { loading: {
@ -314,20 +328,18 @@
} }
}); });
}, },
checkConnection: async function(url, threshold) { checkConnection: async function (url, threshold) {
const start = performance.now(); const start = performance.now();
try try {
{ const response = await fetch(url, {mode: 'no-cors'});
const response = await fetch(url, { mode: 'no-cors' });
const latency = performance.now() - start; const latency = performance.now() - start;
if (latency > threshold) if (latency > threshold) {
{
moonlight.toasts.warning(`High latency detected: ${latency}ms. Moonlight might feel laggy. Please check your internet connection`); moonlight.toasts.warning(`High latency detected: ${latency}ms. Moonlight might feel laggy. Please check your internet connection`);
} }
} catch (error) {
} }
catch (error) {}
} }
}, },
flashbang: { flashbang: {
@ -429,7 +441,7 @@
const systemZoom = width / window.screen.availWidth; const systemZoom = width / window.screen.availWidth;
const left = (width - w) / 2 / systemZoom + dualScreenLeft const left = (width - w) / 2 / systemZoom + dualScreenLeft
const top = (height - h) / 2 / systemZoom + dualScreenTop const top = (height - h) / 2 / systemZoom + dualScreenTop
const newWindow = window.open(url, title,`scrollbars=yes,width=${w / systemZoom},height=${h / systemZoom},top=${top},left=${left}`) const newWindow = window.open(url, title, `scrollbars=yes,width=${w / systemZoom},height=${h / systemZoom},top=${top},left=${left}`)
if (window.focus) newWindow.focus(); if (window.focus) newWindow.focus();
} }
} }