Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SessionId to SessionContext, fix missing timeout for starttls #235

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 19 additions & 3 deletions Src/SmtpServer/IO/SecurableDuplexPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,27 @@ internal SecurableDuplexPipe(Stream stream, Action disposeAction)
/// <returns>A task that asynchronously performs the operation.</returns>
public async Task UpgradeAsync(X509Certificate certificate, SslProtocols protocols, CancellationToken cancellationToken = default)
{
var stream = new SslStream(_stream, true);
var sslStream = new SslStream(_stream, true);

await stream.AuthenticateAsServerAsync(certificate, false, protocols, true).ConfigureAwait(false);
try
{
var sslServerAuthenticationOptions = new SslServerAuthenticationOptions
{
ServerCertificate = certificate,
ClientCertificateRequired = false,
EnabledSslProtocols = protocols,
CertificateRevocationCheckMode = X509RevocationMode.Online
};

_stream = stream;
await sslStream.AuthenticateAsServerAsync(sslServerAuthenticationOptions, cancellationToken);
}
catch
{
sslStream.Dispose();
throw;
}

_stream = sslStream;

Input = PipeReader.Create(_stream);
Output = PipeWriter.Create(_stream);
Expand Down
5 changes: 5 additions & 0 deletions Src/SmtpServer/ISessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ namespace SmtpServer
/// </summary>
public interface ISessionContext
{
/// <summary>
/// A unique Id for the Session
/// </summary>
public Guid SessionId { get; }

/// <summary>
/// Fired when a command is about to execute.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Src/SmtpServer/SmtpServer.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<AssemblyName>SmtpServer</AssemblyName>
<RootNamespace>SmtpServer</RootNamespace>
Expand Down
4 changes: 4 additions & 0 deletions Src/SmtpServer/SmtpSessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace SmtpServer
{
internal sealed class SmtpSessionContext : ISessionContext
{
/// <inheritdoc />
public Guid SessionId { get; private set; }

/// <inheritdoc />
public event EventHandler<SmtpCommandEventArgs> CommandExecuting;

Expand All @@ -27,6 +30,7 @@ internal sealed class SmtpSessionContext : ISessionContext
/// <param name="endpointDefinition">The endpoint definition.</param>
internal SmtpSessionContext(IServiceProvider serviceProvider, ISmtpServerOptions options, IEndpointDefinition endpointDefinition)
{
SessionId = Guid.NewGuid();
ServiceProvider = serviceProvider;
ServerOptions = options;
EndpointDefinition = endpointDefinition;
Expand Down
50 changes: 24 additions & 26 deletions Src/SmtpServer/SmtpSessionManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -9,8 +9,7 @@ namespace SmtpServer
internal sealed class SmtpSessionManager
{
readonly SmtpServer _smtpServer;
readonly HashSet<SmtpSessionHandle> _sessions = new HashSet<SmtpSessionHandle>();
readonly object _sessionsLock = new object();
readonly ConcurrentDictionary<Guid, SmtpSessionHandle> _sessions = new ConcurrentDictionary<Guid, SmtpSessionHandle>();

internal SmtpSessionManager(SmtpServer smtpServer)
{
Expand All @@ -20,16 +19,13 @@ internal SmtpSessionManager(SmtpServer smtpServer)
internal void Run(SmtpSessionContext sessionContext, CancellationToken cancellationToken)
{
var handle = new SmtpSessionHandle(new SmtpSession(sessionContext), sessionContext);
Add(handle);

handle.CompletionTask = RunAsync(handle, cancellationToken);
var smtpSessionTask = RunAsync(handle, cancellationToken).ContinueWith(task =>
{
Remove(handle);
});

// ReSharper disable once MethodSupportsCancellation
handle.CompletionTask.ContinueWith(
task =>
{
Remove(handle);
});
handle.CompletionTask = smtpSessionTask;
}

async Task RunAsync(SmtpSessionHandle handle, CancellationToken cancellationToken)
Expand Down Expand Up @@ -79,30 +75,18 @@ async Task UpgradeAsync(SmtpSessionHandle handle, CancellationToken cancellation

internal Task WaitAsync()
{
IReadOnlyList<Task> tasks;

lock (_sessionsLock)
{
tasks = _sessions.Select(session => session.CompletionTask).ToList();
}

var tasks = _sessions.Values.Select(session => session.CompletionTask).ToList().AsReadOnly();
return Task.WhenAll(tasks);
}

void Add(SmtpSessionHandle handle)
{
lock (_sessionsLock)
{
_sessions.Add(handle);
}
_sessions.TryAdd(handle.SessionContext.SessionId, handle);
}

void Remove(SmtpSessionHandle handle)
{
lock (_sessionsLock)
{
_sessions.Remove(handle);
}
_sessions.TryRemove(handle.SessionContext.SessionId, out _);
}

class SmtpSessionHandle
Expand All @@ -118,6 +102,20 @@ public SmtpSessionHandle(SmtpSession session, SmtpSessionContext sessionContext)
public SmtpSessionContext SessionContext { get; }

public Task CompletionTask { get; set; }

public override bool Equals(object obj)
tinohager marked this conversation as resolved.
Show resolved Hide resolved
{
if (obj is SmtpSessionHandle other)
{
return SessionContext.SessionId == other.SessionContext.SessionId;
}
return false;
}

public override int GetHashCode()
{
return SessionContext.SessionId.GetHashCode();
}
}
}
}
Loading