From b75147e4c0b8a0bbefbe288ea0b8632642ae156e Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Wed, 12 Jul 2023 14:20:55 +0200 Subject: [PATCH] Added temp mail check --- .../Services/Background/TempMailService.cs | 37 +++++++++++++++++++ Moonlight/App/Services/UserService.cs | 9 ++++- Moonlight/Program.cs | 4 +- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 Moonlight/App/Services/Background/TempMailService.cs diff --git a/Moonlight/App/Services/Background/TempMailService.cs b/Moonlight/App/Services/Background/TempMailService.cs new file mode 100644 index 00000000..57832d42 --- /dev/null +++ b/Moonlight/App/Services/Background/TempMailService.cs @@ -0,0 +1,37 @@ +using System.Net.Mail; +using Moonlight.App.Helpers; + +namespace Moonlight.App.Services.Background; + +public class TempMailService +{ + private string[] Domains = Array.Empty(); + + public TempMailService() + { + Task.Run(Init); + } + + private async Task Init() + { + var client = new HttpClient(); + var text = await client.GetStringAsync("https://raw.githubusercontent.com/disposable-email-domains/disposable-email-domains/master/disposable_email_blocklist.conf"); + + Domains = text + .Split("\n") + .Select(x => x.Trim()) + .ToArray(); + + Logger.Info($"Fetched {Domains.Length} temp mail domains"); + } + + public Task IsTempMail(string mail) + { + var address = new MailAddress(mail); + + if (Domains.Contains(address.Host)) + return Task.FromResult(true); + + return Task.FromResult(false); + } +} \ No newline at end of file diff --git a/Moonlight/App/Services/UserService.cs b/Moonlight/App/Services/UserService.cs index 2a611cb9..cab1b644 100644 --- a/Moonlight/App/Services/UserService.cs +++ b/Moonlight/App/Services/UserService.cs @@ -5,6 +5,7 @@ using Moonlight.App.Exceptions; using Moonlight.App.Helpers; using Moonlight.App.Models.Misc; using Moonlight.App.Repositories; +using Moonlight.App.Services.Background; using Moonlight.App.Services.Mail; using Moonlight.App.Services.Sessions; @@ -19,6 +20,7 @@ public class UserService private readonly IpLocateService IpLocateService; private readonly DateTimeService DateTimeService; private readonly ConfigService ConfigService; + private readonly TempMailService TempMailService; private readonly string JwtSecret; @@ -29,7 +31,8 @@ public class UserService MailService mailService, IdentityService identityService, IpLocateService ipLocateService, - DateTimeService dateTimeService) + DateTimeService dateTimeService, + TempMailService tempMailService) { UserRepository = userRepository; TotpService = totpService; @@ -38,6 +41,7 @@ public class UserService IdentityService = identityService; IpLocateService = ipLocateService; DateTimeService = dateTimeService; + TempMailService = tempMailService; JwtSecret = configService .Get() @@ -48,6 +52,9 @@ public class UserService { if (ConfigService.Get().Moonlight.Auth.DenyRegister) throw new DisplayException("This operation was disabled"); + + if (await TempMailService.IsTempMail(email)) + throw new DisplayException("This email is blacklisted"); // Check if the email is already taken var emailTaken = UserRepository.Get().FirstOrDefault(x => x.Email == email) != null; diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index fb850669..c8850aa3 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -103,8 +103,6 @@ namespace Moonlight } } - Logger.Info($"Working dir: {Directory.GetCurrentDirectory()}"); - Logger.Info("Running pre-init tasks"); var databaseCheckupService = new DatabaseCheckupService(configService); @@ -246,6 +244,7 @@ namespace Moonlight builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); // Other builder.Services.AddSingleton(); @@ -291,6 +290,7 @@ namespace Moonlight _ = app.Services.GetRequiredService(); _ = app.Services.GetRequiredService(); _ = app.Services.GetRequiredService(); + _ = app.Services.GetRequiredService(); _ = app.Services.GetRequiredService();