diff --git a/KeePassNatMsg/KeePassNatMsgExt.cs b/KeePassNatMsg/KeePassNatMsgExt.cs index dab0325..2bc9cb5 100644 --- a/KeePassNatMsg/KeePassNatMsgExt.cs +++ b/KeePassNatMsg/KeePassNatMsgExt.cs @@ -22,7 +22,7 @@ namespace KeePassNatMsg { - public sealed class KeePassNatMsgExt : Plugin + public sealed class KeePassNatMsgExt : Plugin, IDisposable { /// @@ -381,5 +381,22 @@ internal JObject GeneratePassword() } public static string GetVersion() => KeePassXcVersion.ToString(); + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposing) + { + // dispose managed resources + IDisposable disposable = (IDisposable)_listener; + disposable?.Dispose(); + } + // free native resources + } } } diff --git a/KeePassNatMsg/Protocol/Crypto/Nacl.cs b/KeePassNatMsg/Protocol/Crypto/Nacl.cs index 72efc04..0e9db05 100644 --- a/KeePassNatMsg/Protocol/Crypto/Nacl.cs +++ b/KeePassNatMsg/Protocol/Crypto/Nacl.cs @@ -49,8 +49,13 @@ public class TweetNaCl public static readonly Int32 SignBytes = 64; + [Serializable] public class InvalidSignatureException : CryptographicException { } + + [Serializable] public class InvalidCipherTextException : CryptographicException { } + + [Serializable] public class InvalidEncryptionKeypair : CryptographicException { } /// diff --git a/KeePassNatMsg/Protocol/Listener/SocketReadState.cs b/KeePassNatMsg/Protocol/Listener/SocketReadState.cs index 9b49c16..0505ba6 100644 --- a/KeePassNatMsg/Protocol/Listener/SocketReadState.cs +++ b/KeePassNatMsg/Protocol/Listener/SocketReadState.cs @@ -1,9 +1,10 @@ -using System.Net.Sockets; +using System; +using System.Net.Sockets; using System.Threading; namespace KeePassNatMsg.Protocol.Listener { - internal sealed class SocketReadState + internal sealed class SocketReadState : IDisposable { public Socket Socket; public bool Active; @@ -15,5 +16,21 @@ public SocketReadState() WaitHandle = new ManualResetEventSlim(false); Active = true; } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposing) + { + // dispose managed resources + WaitHandle.Dispose(); + } + // free native resources + } } } diff --git a/KeePassNatMsg/Protocol/Listener/UdpListener.cs b/KeePassNatMsg/Protocol/Listener/UdpListener.cs index 2e22bb9..9ba0246 100644 --- a/KeePassNatMsg/Protocol/Listener/UdpListener.cs +++ b/KeePassNatMsg/Protocol/Listener/UdpListener.cs @@ -5,7 +5,7 @@ namespace KeePassNatMsg.Protocol.Listener { - public sealed class UdpListener + public sealed class UdpListener : IDisposable { private readonly Thread _thread; private UdpClient _client; @@ -47,6 +47,22 @@ public void Send(string msg, IPEndPoint ep) _client.Send(data, data.Length, ep); } + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposing) + { + // dispose managed resources + _client.Close(); + } + // free native resources + } + private void Run() { _ep = new IPEndPoint(IPAddress.Any, _port); diff --git a/KeePassNatMsg/Protocol/Listener/UnixSocketListener.cs b/KeePassNatMsg/Protocol/Listener/UnixSocketListener.cs index 68df568..e258a02 100644 --- a/KeePassNatMsg/Protocol/Listener/UnixSocketListener.cs +++ b/KeePassNatMsg/Protocol/Listener/UnixSocketListener.cs @@ -6,7 +6,7 @@ namespace KeePassNatMsg.Protocol.Listener { - public class UnixSocketListener : IListener + public class UnixSocketListener : IListener, IDisposable { private const string SocketName = "kpxc_server"; @@ -46,6 +46,23 @@ public void Stop() DeleteSocketFile(); } + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposing) + { + // dispose managed resources + _socket.Close(); + _cts.Dispose(); + } + // free native resources + } + private void RunThread() { DeleteSocketFile();