Skip to content

Commit

Permalink
feat(mail): only exit when EmailConfirmationRequired enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Sep 2, 2024
1 parent 5bf30c9 commit d4a5258
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/GZCTF/Repositories/GameRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public Task<Game[]> GetGames(int count, int skip, CancellationToken token) =>
Context.Games.OrderByDescending(g => g.Id).Skip(skip).Take(count).ToArrayAsync(token);

public void FlushGameInfoCache() => cache.Remove(CacheKey.BasicGameInfo);

// By xfoxfu & GZTimeWalker @ 2022/04/03
// Refactored by GZTimeWalker @ 2024/08/31
public async Task<ScoreboardModel> GenScoreboard(Game game, CancellationToken token = default)
Expand Down Expand Up @@ -235,7 +235,7 @@ public async Task<ScoreboardModel> GenScoreboard(Game game, CancellationToken to
game.BloodBonus.SecondBloodFactor,
game.BloodBonus.ThirdBloodFactor
];

foreach (var item in submissions.OrderBy(s => s.SubmitTimeUtc))
{
var challenge = challenges[item.Id];
Expand Down
3 changes: 3 additions & 0 deletions src/GZCTF/Resources/Program.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1038,4 +1038,7 @@
<data name="MailSender_ConnectedToSmtp" xml:space="preserve">
<value>Successfully connected to SMTP server: {0}</value>
</data>
<data name="MailSender_InvalidEmailConfig" xml:space="preserve">
<value>Invalid email configuration, but email authentication is enabled</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/GZCTF/Resources/Program.ja-JP.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1038,4 +1038,7 @@
<data name="MailSender_ConnectedToSmtp" xml:space="preserve">
<value>SMTPサーバーへの接続に成功しました:{0}</value>
</data>
<data name="MailSender_InvalidEmailConfig" xml:space="preserve">
<value>無効な電子メール設定ですが、電子メール認証は有効です</value>
</data>
</root>
3 changes: 3 additions & 0 deletions src/GZCTF/Resources/Program.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1038,4 +1038,7 @@
<data name="MailSender_ConnectedToSmtp" xml:space="preserve">
<value>成功连接到 SMTP 服务器:{0}</value>
</data>
<data name="MailSender_InvalidEmailConfig" xml:space="preserve">
<value>无效的邮件配置,但邮件验证已启用</value>
</data>
</root>
31 changes: 21 additions & 10 deletions src/GZCTF/Services/Mail/MailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public sealed class MailSender : IMailSender, IDisposable
bool _disposed;

public MailSender(
IOptionsSnapshot<AccountPolicy> accountPolicy,
IOptions<EmailConfig> options,
ILogger<MailSender> logger)
{
Expand Down Expand Up @@ -53,9 +54,16 @@ public MailSender(
=> errors is SslPolicyErrors.None || options.Value.Smtp?.BypassCertVerify is true;

if (!TestSmtpClient())
ExitWithFatalMessage(Program.StaticLocalizer[nameof(Resources.Program.MailSender_InvalidRequest)]);
{
if (accountPolicy.Value.EmailConfirmationRequired)
ExitWithFatalMessage(StaticLocalizer[nameof(Resources.Program.MailSender_InvalidEmailConfig)]);

_smtpClient.Dispose();
_smtpClient = null;
return;
}

_logger.SystemLog(Program.StaticLocalizer[nameof(Resources.Program.MailSender_ConnectedToSmtp),
_logger.SystemLog(StaticLocalizer[nameof(Resources.Program.MailSender_ConnectedToSmtp),
$"{_options.Smtp.Host}:{_options.Smtp.Port}"], TaskStatus.Success, LogLevel.Debug);

Task.Factory.StartNew(MailSenderWorker, _cancellationToken, TaskCreationOptions.LongRunning,
Expand All @@ -75,6 +83,9 @@ public void Dispose()

async Task<bool> SendEmailAsync(string subject, string content, string to)
{
if (_smtpClient is null)
return false;

using var msg = new MimeMessage();
msg.From.Add(new MailboxAddress(_options!.SendMailAddress, _options.SendMailAddress));
msg.To.Add(new MailboxAddress(to, to));
Expand All @@ -83,16 +94,16 @@ async Task<bool> SendEmailAsync(string subject, string content, string to)

try
{
await _smtpClient!.SendAsync(msg, _cancellationToken);
await _smtpClient.SendAsync(msg, _cancellationToken);

_logger.SystemLog(Program.StaticLocalizer[nameof(Resources.Program.MailSender_SendMail), to],
_logger.SystemLog(StaticLocalizer[nameof(Resources.Program.MailSender_SendMail), to],
TaskStatus.Success, LogLevel.Information);
return true;
}
catch (Exception e)
{
_logger.LogError(e, "{msg}",
Program.StaticLocalizer[nameof(Resources.Program.MailSender_MailSendFailed)]);
StaticLocalizer[nameof(Resources.Program.MailSender_MailSendFailed)]);
return false;
}
}
Expand All @@ -116,7 +127,7 @@ public async Task SendMailContent(MailContent content)
var title = $"{content.Title} - {content.Platform}";

if (!await SendEmailAsync(title, emailContent, content.Email))
_logger.SystemLog(Program.StaticLocalizer[nameof(Resources.Program.MailSender_MailSendFailed)],
_logger.SystemLog(StaticLocalizer[nameof(Resources.Program.MailSender_MailSendFailed)],
TaskStatus.Failed);
}

Expand Down Expand Up @@ -161,7 +172,7 @@ await _smtpClient.AuthenticateAsync(_options!.UserName, _options.Password,
_mailQueue.Clear();

_logger.LogError(e, "{msg}",
Program.StaticLocalizer[nameof(Resources.Program.MailSender_MailSendFailed)]);
StaticLocalizer[nameof(Resources.Program.MailSender_MailSendFailed)]);
}
finally
{
Expand All @@ -178,7 +189,7 @@ bool EnqueueMailTask(string? userName, string? email, string? resetLink, MailTyp

if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(resetLink))
{
_logger.SystemLog(Program.StaticLocalizer[nameof(Resources.Program.MailSender_InvalidRequest)],
_logger.SystemLog(StaticLocalizer[nameof(Resources.Program.MailSender_InvalidRequest)],
TaskStatus.Failed);
return false;
}
Expand All @@ -205,8 +216,8 @@ bool TestSmtpClient(CancellationToken token = default)
}
catch (Exception e)
{
_logger.LogError(e, "{msg}",
Program.StaticLocalizer[nameof(Resources.Program.MailSender_MailSendFailed)]);
_logger.LogDebug(e, "{msg}",
StaticLocalizer[nameof(Resources.Program.MailSender_MailSendFailed)]);
return false;
}
}
Expand Down

0 comments on commit d4a5258

Please sign in to comment.