Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/Zetian.Relay/Client/SmtpRelayClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Zetian.Protocol;
using Zetian.Relay.Abstractions;
using Zetian.Relay.Models;
using Zetian.Relay.Services;

namespace Zetian.Relay.Client
{
Expand Down Expand Up @@ -62,13 +63,27 @@
using CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(Timeout);

await _tcpClient.ConnectAsync(Host, Port).ConfigureAwait(false);
await _tcpClient.ConnectAsync(Host, Port, cts.Token).ConfigureAwait(false);

_stream = _tcpClient.GetStream();

if (EnableSsl)
{
await UpgradeToSslAsync(cts.Token).ConfigureAwait(false);
try
{
await UpgradeToSslAsync(cts.Token).ConfigureAwait(false);
}
catch (Exception)
{
_logger.LogInformation("Failed to connect as SMTPS on {Host}:{Port}", Host, Port);

_stream.Close();
await _stream.DisposeAsync();

_tcpClient = new TcpClient();
await _tcpClient.ConnectAsync(Host, Port, cts.Token).ConfigureAwait(false);
_stream = _tcpClient.GetStream();
}
}

_reader = new StreamReader(_stream, Encoding.ASCII);
Expand All @@ -84,6 +99,15 @@
// Send EHLO
await SendEhloAsync(cts.Token).ConfigureAwait(false);

// Upgrade the connection to STARTTLS if allowed
if (EnableSsl && _stream is not SslStream && _serverCapabilities?.ContainsKey("STARTTLS") == true)
{
await UpgradeToStartTlsAsync(cts.Token).ConfigureAwait(false);

_reader = new StreamReader(_stream, Encoding.ASCII);
_writer = new StreamWriter(_stream, Encoding.ASCII) { AutoFlush = true };
}

_logger.LogInformation("Connected to {Host}:{Port}", Host, Port);
}
catch (Exception ex)
Expand Down Expand Up @@ -386,7 +410,7 @@

private async Task UpgradeToSslAsync(CancellationToken cancellationToken)
{
SslStream sslStream = new(_stream, false, ValidateServerCertificate);

Check warning on line 413 in src/Zetian.Relay/Client/SmtpRelayClient.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp, Release, Zetian.slnx)

Possible null reference argument for parameter 'innerStream' in 'SslStream.SslStream(Stream innerStream, bool leaveInnerStreamOpen, RemoteCertificateValidationCallback? userCertificateValidationCallback)'.

await sslStream.AuthenticateAsClientAsync(
Host,
Expand All @@ -398,6 +422,18 @@
_logger.LogDebug("SSL/TLS connection established");
}

private async Task UpgradeToStartTlsAsync(CancellationToken cancellationToken)
{
await SendCommandAsync("STARTTLS", cancellationToken).ConfigureAwait(false);

SmtpResponse response = await ReadResponseAsync(cancellationToken).ConfigureAwait(false);

if (response.IsSuccess)
{
await UpgradeToSslAsync(cancellationToken);
}
}

private async Task AuthPlainAsync(CancellationToken cancellationToken)
{
if (Credentials == null)
Expand Down
5 changes: 5 additions & 0 deletions src/Zetian.Relay/Services/RelayService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ private async Task<bool> CanRelayAsync(ISmtpSession session, ISmtpMessage messag
return config;
}

if (Configuration.DefaultSmartHost?.Host == host && Configuration.DefaultSmartHost?.Port == port)
{
return Configuration.DefaultSmartHost;
}

// Create default configuration
return new SmartHostConfiguration
{
Expand Down
Loading