diff --git a/Continuity.AuthServer/ChannelSession.cs b/Continuity.AuthServer/ChannelSession.cs index 77ae088..14903d3 100644 --- a/Continuity.AuthServer/ChannelSession.cs +++ b/Continuity.AuthServer/ChannelSession.cs @@ -23,12 +23,13 @@ public class ChannelSession : TcpSession private readonly ICryptoTransform _encryptor; private readonly ILogger _logger; private readonly IMediator _mediator; + private bool _disposed; public ChannelSession(TcpServer server, IMediator mediator, ILogger logger) : base(server) { _mediator = mediator; _logger = logger; - var aes = Aes.Create(); + using var aes = Aes.Create(); aes.Key = _key; aes.IV = _iv; aes.Padding = PaddingMode.None; @@ -63,6 +64,28 @@ public class ChannelSession : TcpSession } } + protected override void Dispose(bool disposingManagedResources) + { + if (_disposed) + { + return; + } + + _disposed = true; + if (disposingManagedResources) + { + _decryptor.Dispose(); + _encryptor.Dispose(); + } + + base.Dispose(disposingManagedResources); + } + + protected virtual void ThrowIfDisposed() + { + ObjectDisposedException.ThrowIf(_disposed, GetType()); + } + private static byte[] Decrypt(byte[] buffer) { for (var i = 0; i < buffer.Length; ++i)