refactor
This commit is contained in:
parent
7b147569e0
commit
d32207fbf7
6 changed files with 30 additions and 51 deletions
|
|
@ -32,6 +32,7 @@ public class GraphSender(GraphConfig config, ILogger<GraphSender> logger)
|
|||
if (_cachedAccessToken == null || InvalidToken(_cachedAccessToken.Value))
|
||||
{
|
||||
_cachedAccessToken = _cachedCredentials.GetToken(_tokenRequestContext, cancellationToken);
|
||||
|
||||
_logger.LogInformation("Requested new Azure Access Token");
|
||||
|
||||
// convert token => jwt
|
||||
|
|
@ -69,6 +70,7 @@ public class GraphSender(GraphConfig config, ILogger<GraphSender> logger)
|
|||
var sentFile = new FileInfo(Path.Combine(Common.SentDir.FullName, spoolFile.Name));
|
||||
spoolFile.MoveTo(sentFile.FullName);
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
_logger.LogInformation("Sent: {Path}", sentFile.FullName);
|
||||
|
||||
return resp;
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ public partial class GraphSmtpSession(GraphSender graphSender, ILogger<GraphSmtp
|
|||
|
||||
await File.WriteAllBytesAsync(spoolFile.FullName, data, cancellationToken);
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
_logger.LogInformation("Spooled: {Path}", spoolFile.FullName);
|
||||
|
||||
return spoolFile;
|
||||
|
|
|
|||
|
|
@ -129,7 +129,7 @@ internal partial class Program
|
|||
if (string.IsNullOrWhiteSpace(graphConfig.SenderUpn))
|
||||
throw new ArgumentNullException(graphConfig.SenderUpn, "{Graph:SenderUpn} cannot be null");
|
||||
|
||||
if (!GraphHelper.ValidateEmailRegex().Match(graphConfig.SenderUpn).Success)
|
||||
if (!GraphHelper.ValidateEmailRegex().IsMatch(graphConfig.SenderUpn))
|
||||
throw new ArgumentNullException(graphConfig.SenderUpn, "{Graph:SenderUpn} invalid format");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ public class SmtpServer<TSession>(IServiceScopeFactory scopeFactory, ILogger<Smt
|
|||
IsRunning = true;
|
||||
|
||||
// log
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
_logger.LogInformation("SMTP listening on {HOST}:{PORT}", config.Host, config.Port);
|
||||
|
||||
// handler loop
|
||||
|
|
@ -46,7 +47,8 @@ public class SmtpServer<TSession>(IServiceScopeFactory scopeFactory, ILogger<Smt
|
|||
catch (OperationCanceledException) { }
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.ToString());
|
||||
if (_logger.IsEnabled(LogLevel.Error))
|
||||
_logger.LogError("ERROR: [{ERR}]", ex.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using SmtpRelay.Graph;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
|
@ -33,6 +32,7 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
// set tcp options
|
||||
socket.NoDelay = true;
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
_logger.LogInformation("[{REP}] Connected", socket.RemoteEndPoint);
|
||||
|
||||
try
|
||||
|
|
@ -49,7 +49,9 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
break;
|
||||
|
||||
var line = Encoding.ASCII.GetString(rawLine);
|
||||
_logger.LogInformation("[{REP}] > {LINE}", socket.RemoteEndPoint, line);
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Debug))
|
||||
_logger.LogDebug("[{REP}] > {LINE}", socket.RemoteEndPoint, line);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
|
|
@ -77,9 +79,11 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (_logger.IsEnabled(LogLevel.Error))
|
||||
_logger.LogError("[{REP}] Error: {MSG}", socket.RemoteEndPoint, ex.ToString());
|
||||
}
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
_logger.LogInformation("[{REP}] Disconnected", socket.RemoteEndPoint);
|
||||
}
|
||||
|
||||
|
|
@ -354,17 +358,12 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
var rawData = await ReadToSpanAsync(socket, DataDelimiterSpan, true, cancellationToken);
|
||||
|
||||
await OnProcessDataAsync(rawData, cancellationToken);
|
||||
|
||||
// dot unstuff
|
||||
//if (response.StartsWith(".."))
|
||||
// response = response[1..];
|
||||
|
||||
//var response = Encoding.ASCII.GetString(rawData);
|
||||
//Console.WriteLine(JsonSerializer.Serialize(response));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.ToString());
|
||||
if (_logger.IsEnabled(LogLevel.Error))
|
||||
_logger.LogError("[{REP}] Error: {MSG}", socket.RemoteEndPoint, ex.ToString());
|
||||
|
||||
throw new InvalidOperationException(ex.ToString());
|
||||
}
|
||||
|
||||
|
|
@ -379,8 +378,6 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
|
||||
private async Task HandleAuthPlainAsync(Socket socket, SmtpConfig config, string? raw, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogCritical("AUTH PLAIN");
|
||||
|
||||
// PLAIN == base64
|
||||
string payloadB64 = raw ?? string.Empty;
|
||||
|
||||
|
|
@ -393,6 +390,7 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
var rawLine = await ReadToSpanAsync(socket, CrlnSpan, true, cancellationToken);
|
||||
payloadB64 = Encoding.ASCII.GetString(rawLine ?? throw new InvalidDataException("null")).Trim() ?? "";
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
_logger.LogInformation("[{REP}] > {LINE}", socket.RemoteEndPoint, rawLine);
|
||||
}
|
||||
|
||||
|
|
@ -411,13 +409,10 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
return;
|
||||
}
|
||||
|
||||
var authzid = parts[0];
|
||||
//var authzid = parts[0];
|
||||
var user = parts[1];
|
||||
var pass = parts[2];
|
||||
|
||||
_logger.LogCritical(user);
|
||||
_logger.LogCritical(pass);
|
||||
|
||||
if (ValidateUser(config, user, pass))
|
||||
{
|
||||
_authenticated = true;
|
||||
|
|
@ -432,8 +427,6 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
|
||||
private async Task HandleAuthLoginAsync(Socket socket, SmtpConfig config, string? raw, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogCritical("AUTH LOGIN");
|
||||
|
||||
string? user;
|
||||
string? pass;
|
||||
|
||||
|
|
@ -454,6 +447,7 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
var rawLine = await ReadToSpanAsync(socket, CrlnSpan, true, cancellationToken);
|
||||
var response = Encoding.ASCII.GetString(rawLine ?? throw new InvalidDataException("null")).Trim() ?? "";
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
_logger.LogInformation("[{REP}] > {LINE}", socket.RemoteEndPoint, rawLine);
|
||||
|
||||
if (!SmtpHelper.TryBase64(response, out var ub))
|
||||
|
|
@ -509,13 +503,6 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
// cut out unsed buffer
|
||||
var unusedBuffer = bufferMemory.Slice(_dataOffset, _bufferOffset - _dataOffset);
|
||||
|
||||
// DEBUG ONLY (no aot)
|
||||
//_logger.LogTrace(_dataOffset.ToString());
|
||||
//_logger.LogTrace(_bufferOffset.ToString());
|
||||
|
||||
//var dbgLine = Encoding.ASCII.GetString(unusedBuffer.Span);
|
||||
//_logger.LogTrace(JsonSerializer.Serialize(dbgLine));
|
||||
|
||||
// find escape sequence if theres more left, before fetching new data
|
||||
var escapeIndex = unusedBuffer.Span.IndexOf(until.Span);
|
||||
if (escapeIndex == -1)
|
||||
|
|
@ -595,6 +582,7 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
{
|
||||
_writeLock.Release();
|
||||
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
_logger.LogInformation("[{REP}] < {LINE}", socket.RemoteEndPoint, line);
|
||||
}
|
||||
}
|
||||
|
|
@ -612,20 +600,6 @@ public abstract partial class SmtpSessionBase(ILogger<SmtpSessionBase> logger)
|
|||
|
||||
private static bool ValidateReceiver(SmtpConfig config, string receiver) => SmtpHelper.MatchesAny(receiver, config.AllowedReceivers);
|
||||
|
||||
private async Task<FileInfo> SpoolToFileAsync(ReadOnlyMemory<byte> data, CancellationToken cancellationToken)
|
||||
{
|
||||
Directory.CreateDirectory(Common.SpoolDir.FullName);
|
||||
|
||||
var path = Path.Combine(Common.SpoolDir.FullName, $"{Guid.NewGuid()}.eml");
|
||||
var spoolFile = new FileInfo(path);
|
||||
|
||||
await File.WriteAllBytesAsync(spoolFile.FullName, data, cancellationToken);
|
||||
|
||||
_logger.LogInformation("Spooled: {Path}", spoolFile.FullName);
|
||||
|
||||
return spoolFile;
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"^MAIL\s+FROM:\s*<([^>]+)>(?:\s+(.*))?$", RegexOptions.IgnoreCase)]
|
||||
public static partial Regex MailFromRegex();
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
},
|
||||
"Whitelist": {
|
||||
"Senders": [
|
||||
"me2@example.local"
|
||||
"me@example.local"
|
||||
],
|
||||
"Receivers": [
|
||||
"noreply@example.local",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue