diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index 8eb2384b..64663c2c 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -151,30 +151,30 @@ namespace Shadowsocks.Controller return; } closed = true; - } - if (connection != null) - { - try - { - connection.Shutdown(SocketShutdown.Send); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - } - } - if (remote != null) - { - try + if (connection != null) { - remote.Shutdown(SocketShutdown.Send); + try + { + connection.Shutdown(SocketShutdown.Send); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } } - catch (SocketException e) + if (remote != null) { - Console.WriteLine(e.Message); + try + { + remote.Shutdown(SocketShutdown.Send); + } + catch (SocketException e) + { + Console.WriteLine(e.Message); + } } + ((IDisposable)encryptor).Dispose(); } - ((IDisposable)encryptor).Dispose(); } private void ConnectCallback(IAsyncResult ar) diff --git a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs index 3b00c6cb..9bfa3dde 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs @@ -165,38 +165,38 @@ namespace Shadowsocks.Encrypt public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength) { - if (_encryptCtx == null) + lock (this) { - randBytes(outbuf, ivLen); - InitCipher(ref _encryptCtx, outbuf, true); - outlength = length + ivLen; - lock (tempbuf) + if (_disposed) { + outbuf = null; + outlength = 0; + return; + } + if (_encryptCtx == null) + { + randBytes(outbuf, ivLen); + InitCipher(ref _encryptCtx, outbuf, true); + outlength = length + ivLen; // C# could be multi-threaded - lock (_encryptCtx) + switch (_cipher) { - switch (_cipher) - { - case CIPHER_AES: - PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, tempbuf); - break; - case CIPHER_BF: - PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, tempbuf); - break; - case CIPHER_RC4: - PolarSSL.arc4_crypt(_encryptCtx, length, buf, tempbuf); - break; - } - outlength = length + ivLen; - Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, outlength); + case CIPHER_AES: + PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, tempbuf); + break; + case CIPHER_BF: + PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, tempbuf); + break; + case CIPHER_RC4: + PolarSSL.arc4_crypt(_encryptCtx, length, buf, tempbuf); + break; } + outlength = length + ivLen; + Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, outlength); } - } - else - { - outlength = length; - lock (_encryptCtx) + else { + outlength = length; switch (_cipher) { case CIPHER_AES: @@ -215,36 +215,36 @@ namespace Shadowsocks.Encrypt public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength) { - if (_decryptCtx == null) + lock (this) { - InitCipher(ref _decryptCtx, buf, false); - outlength = length - ivLen; - lock (tempbuf) + if (_disposed) { + outbuf = null; + outlength = 0; + return; + } + if (_decryptCtx == null) + { + InitCipher(ref _decryptCtx, buf, false); + outlength = length - ivLen; // C# could be multi-threaded Buffer.BlockCopy(buf, ivLen, tempbuf, 0, length - ivLen); - lock (_decryptCtx) + switch (_cipher) { - switch (_cipher) - { - case CIPHER_AES: - PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length - ivLen, ref _decryptIVOffset, _decryptIV, tempbuf, outbuf); - break; - case CIPHER_BF: - PolarSSL.blowfish_crypt_cfb64(_decryptCtx, PolarSSL.BLOWFISH_DECRYPT, length - ivLen, ref _decryptIVOffset, _decryptIV, tempbuf, outbuf); - break; - case CIPHER_RC4: - PolarSSL.arc4_crypt(_decryptCtx, length - ivLen, tempbuf, outbuf); - break; - } + case CIPHER_AES: + PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length - ivLen, ref _decryptIVOffset, _decryptIV, tempbuf, outbuf); + break; + case CIPHER_BF: + PolarSSL.blowfish_crypt_cfb64(_decryptCtx, PolarSSL.BLOWFISH_DECRYPT, length - ivLen, ref _decryptIVOffset, _decryptIV, tempbuf, outbuf); + break; + case CIPHER_RC4: + PolarSSL.arc4_crypt(_decryptCtx, length - ivLen, tempbuf, outbuf); + break; } } - } - else - { - outlength = length; - lock (_decryptCtx) + else { + outlength = length; switch (_cipher) { case CIPHER_AES: @@ -284,13 +284,10 @@ namespace Shadowsocks.Encrypt return; } _disposed = true; - } - if (disposing) - { - if (_encryptCtx != null) + if (disposing) { - lock (_encryptCtx) + if (_encryptCtx != null) { switch (_cipher) { @@ -306,10 +303,7 @@ namespace Shadowsocks.Encrypt } _encryptCtx = null; } - } - if (_decryptCtx != null) - { - lock (_decryptCtx) + if (_decryptCtx != null) { switch (_cipher) {