From acf2f807a864110b948ceec1d6b3b5c62b995715 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Tue, 11 Nov 2014 20:31:10 +0800 Subject: [PATCH 01/20] experimental --- shadowsocks-csharp/Encrypt/EncryptorFactory.cs | 2 +- shadowsocks-csharp/Encrypt/PolarSSL.cs | 2 +- shadowsocks-csharp/Program.cs | 2 +- shadowsocks-csharp/shadowsocks-csharp.csproj | 5 ++--- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/shadowsocks-csharp/Encrypt/EncryptorFactory.cs b/shadowsocks-csharp/Encrypt/EncryptorFactory.cs index dbb45d33..59111a69 100644 --- a/shadowsocks-csharp/Encrypt/EncryptorFactory.cs +++ b/shadowsocks-csharp/Encrypt/EncryptorFactory.cs @@ -10,7 +10,7 @@ namespace Shadowsocks.Encrypt return new TableEncryptor(method, password); } - return new OpenSSLEncryptor(method, password); + return new PolarSSLEncryptor(method, password); } } } diff --git a/shadowsocks-csharp/Encrypt/PolarSSL.cs b/shadowsocks-csharp/Encrypt/PolarSSL.cs index 0eab93c7..1ce44439 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSL.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSL.cs @@ -7,7 +7,7 @@ namespace Shadowsocks.Encrypt { public class PolarSSL { - const string DLLNAME = "polarssl"; + const string DLLNAME = "libpolarssl"; public const int AES_CTX_SIZE = 8 + 4 * 68; public const int AES_ENCRYPT = 1; diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index d730419a..df44a09e 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -45,7 +45,7 @@ namespace Shadowsocks { Console.WriteLine(e.ToString()); } - LoadLibrary(dllPath); + //LoadLibrary(dllPath); #if !DEBUG Logging.OpenLogFile(); diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 7629f4fc..d081645c 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -10,7 +10,7 @@ Properties Shadowsocks Shadowsocks - v2.0 + v4.0 512 @@ -21,8 +21,7 @@ 3.5 - - + Client publish\ true Disk From 1defa4a59a80b115cecb2f8d06d80299301610ff Mon Sep 17 00:00:00 2001 From: clowwindy Date: Tue, 11 Nov 2014 21:51:46 +0800 Subject: [PATCH 02/20] fix index out of range --- shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs | 2 +- shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs b/shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs index 7abdb375..bcea60a3 100755 --- a/shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs +++ b/shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs @@ -40,7 +40,7 @@ namespace Shadowsocks.Encrypt outlength = length + ivLen; OpenSSL.EVP_CipherUpdate(_encryptCtx, tempbuf, out outlength, buf, length); outlength = length + ivLen; - Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, outlength); + Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, length); } else { diff --git a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs index 68293a8a..b959565e 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs @@ -192,7 +192,7 @@ namespace Shadowsocks.Encrypt break; } outlength = length + ivLen; - Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, outlength); + Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, length); } } } From bbbde8a64d0277955a67f9cd50b575ca7bd0faa1 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Tue, 11 Nov 2014 21:52:26 +0800 Subject: [PATCH 03/20] add unit test for encryption --- test/UnitTest.cs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/UnitTest.cs b/test/UnitTest.cs index c23f4c75..46f9b662 100755 --- a/test/UnitTest.cs +++ b/test/UnitTest.cs @@ -1,6 +1,9 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Shadowsocks.Controller; +using Shadowsocks.Encrypt; +using System.Threading; +using System.Collections.Generic; namespace test { @@ -18,5 +21,72 @@ namespace test Assert.IsTrue(UpdateChecker.CompareVersion("2.3.1", "2.4") < 0); Assert.IsTrue(UpdateChecker.CompareVersion("1.3.2", "1.3.1") > 0); } + + [TestMethod] + public void TestEncryption() + { + // run it once before the multi-threading test to initialize global tables + RunSingleEncryptionThread(); + + List threads = new List(); + for (int i = 0; i < 10; i++) + { + Thread t = new Thread(new ThreadStart(RunSingleEncryptionThread)); + threads.Add(t); + t.Start(); + } + foreach (Thread t in threads) + { + t.Join(); + } + Assert.IsFalse(encryptionFailed); + } + + private static bool encryptionFailed = false; + private static object locker = new object(); + + private void RunSingleEncryptionThread() + { + try + { + for (int i = 0; i < 1000; i++) + { + var random = new Random(); + IEncryptor encryptor; + IEncryptor decryptor; + lock (locker) + { + encryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!"); + decryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!"); + } + byte[] plain = new byte[16384]; + byte[] cipher = new byte[plain.Length + 16]; + byte[] plain2 = new byte[plain.Length + 16]; + int outLen = 0; + int outLen2 = 0; + random.NextBytes(plain); + //lock (locker) + //{ + encryptor.Encrypt(plain, plain.Length, cipher, out outLen); + decryptor.Decrypt(cipher, outLen, plain2, out outLen2); + Assert.AreEqual(plain.Length, outLen2); + encryptor.Encrypt(plain, 1000, cipher, out outLen); + decryptor.Decrypt(cipher, outLen, plain2, out outLen2); + Assert.AreEqual(1000, outLen2); + encryptor.Encrypt(plain, 12333, cipher, out outLen); + decryptor.Decrypt(cipher, outLen, plain2, out outLen2); + //} + Assert.AreEqual(12333, outLen2); + for (int j = 0; j < plain.Length; j++) + { + Assert.AreEqual(plain[j], plain2[j]); + } + } + } + catch + { + encryptionFailed = true; + } + } } } From aa8e6558d5781ad392bd52abd61f11268cecbf0a Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 01:51:11 +0800 Subject: [PATCH 04/20] move polarssl context to unmanaged heap; fix #32 --- shadowsocks-csharp/Encrypt/PolarSSL.cs | 26 +-- shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs | 267 ++++++++++++------------ test/UnitTest.cs | 27 ++- test/test.csproj | 3 + 4 files changed, 160 insertions(+), 163 deletions(-) diff --git a/shadowsocks-csharp/Encrypt/PolarSSL.cs b/shadowsocks-csharp/Encrypt/PolarSSL.cs index 1ce44439..2ccf35ac 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSL.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSL.cs @@ -7,38 +7,38 @@ namespace Shadowsocks.Encrypt { public class PolarSSL { - const string DLLNAME = "libpolarssl"; + const string DLLNAME = "polarssl"; public const int AES_CTX_SIZE = 8 + 4 * 68; public const int AES_ENCRYPT = 1; public const int AES_DECRYPT = 0; [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static void aes_init(byte[] ctx); + public extern static void aes_init(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static void aes_free(byte[] ctx); + public extern static void aes_free(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int aes_setkey_enc(byte[] ctx, byte[] key, int keysize); + public extern static int aes_setkey_enc(IntPtr ctx, byte[] key, int keysize); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int aes_crypt_cfb128(byte[] ctx, int mode, int length, ref int iv_off, byte[] iv, byte[] input, byte[] output); + public extern static int aes_crypt_cfb128(IntPtr ctx, int mode, int length, byte[] iv_off, byte[] iv, byte[] input, byte[] output); public const int ARC4_CTX_SIZE = 264; [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static void arc4_init(byte[] ctx); + public extern static void arc4_init(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static void arc4_free(byte[] ctx); + public extern static void arc4_free(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static void arc4_setup(byte[] ctx, byte[] key, int keysize); + public extern static void arc4_setup(IntPtr ctx, byte[] key, int keysize); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int arc4_crypt(byte[] ctx, int length, byte[] input, byte[] output); + public extern static int arc4_crypt(IntPtr ctx, int length, byte[] input, byte[] output); public const int BLOWFISH_CTX_SIZE = 4168; @@ -46,16 +46,16 @@ namespace Shadowsocks.Encrypt public const int BLOWFISH_DECRYPT = 0; [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static void blowfish_init(byte[] ctx); + public extern static void blowfish_init(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static void blowfish_free(byte[] ctx); + public extern static void blowfish_free(IntPtr ctx); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int blowfish_setkey(byte[] ctx, byte[] key, int keysize); + public extern static int blowfish_setkey(IntPtr ctx, byte[] key, int keysize); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int blowfish_crypt_cfb64(byte[] ctx, int mode, int length, ref int iv_off, byte[] iv, byte[] input, byte[] output); + public extern static int blowfish_crypt_cfb64(IntPtr ctx, int mode, int length, byte[] iv_off, byte[] iv, byte[] input, byte[] output); } } diff --git a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs index b959565e..c10ae6d4 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs @@ -27,12 +27,12 @@ namespace Shadowsocks.Encrypt private int _cipher; private int[] _cipherInfo; private byte[] _key; - private byte[] _encryptCtx; - private byte[] _decryptCtx; + private IntPtr _encryptCtx = IntPtr.Zero; + private IntPtr _decryptCtx = IntPtr.Zero; private byte[] _encryptIV; private byte[] _decryptIV; - private int _encryptIVOffset; - private int _decryptIVOffset; + private byte[] _encryptIVOffset; + private byte[] _decryptIVOffset; private string _method; private int keyLen; private int ivLen; @@ -100,63 +100,65 @@ namespace Shadowsocks.Encrypt } } - private void InitCipher(ref byte[] ctx, byte[] iv, bool isCipher) + private void InitCipher(ref IntPtr ctx, byte[] iv, bool isCipher) { - ctx = new byte[_cipherInfo[3]]; - lock (ctx) + ctx = Marshal.AllocHGlobal(_cipherInfo[3]); + byte[] realkey; + if (_method == "rc4-md5") { - byte[] realkey; - if (_method == "rc4-md5") + byte[] temp = new byte[keyLen + ivLen]; + realkey = new byte[keyLen]; + Array.Copy(_key, 0, temp, 0, keyLen); + Array.Copy(iv, 0, temp, keyLen, ivLen); + realkey = MD5.Create().ComputeHash(temp); + } + else + { + realkey = _key; + } + if (_cipher == CIPHER_AES) + { + PolarSSL.aes_init(ctx); + // PolarSSL takes key length by bit + // since we'll use CFB mode, here we both do enc, not dec + PolarSSL.aes_setkey_enc(ctx, realkey, keyLen * 8); + if (isCipher) { - byte[] temp = new byte[keyLen + ivLen]; - realkey = new byte[keyLen]; - Array.Copy(_key, 0, temp, 0, keyLen); - Array.Copy(iv, 0, temp, keyLen, ivLen); - realkey = MD5.Create().ComputeHash(temp); + _encryptIV = new byte[ivLen]; + _encryptIVOffset = new byte[8]; + Array.Copy(iv, _encryptIV, ivLen); } else { - realkey = _key; - } - if (_cipher == CIPHER_AES) - { - PolarSSL.aes_init(ctx); - // PolarSSL takes key length by bit - // since we'll use CFB mode, here we both do enc, not dec - PolarSSL.aes_setkey_enc(ctx, realkey, keyLen * 8); - if (isCipher) - { - _encryptIV = new byte[ivLen]; - Array.Copy(iv, _encryptIV, ivLen); - } - else - { - _decryptIV = new byte[ivLen]; - Array.Copy(iv, _decryptIV, ivLen); - } + _decryptIV = new byte[ivLen]; + _decryptIVOffset = new byte[8]; + Array.Copy(iv, _decryptIV, ivLen); } - else if (_cipher == CIPHER_BF) + } + else if (_cipher == CIPHER_BF) + { + PolarSSL.blowfish_init(ctx); + // PolarSSL takes key length by bit + PolarSSL.blowfish_setkey(ctx, realkey, keyLen * 8); + if (isCipher) { - PolarSSL.blowfish_init(ctx); - // PolarSSL takes key length by bit - PolarSSL.blowfish_setkey(ctx, realkey, keyLen * 8); - if (isCipher) - { - _encryptIV = new byte[ivLen]; - Array.Copy(iv, _encryptIV, ivLen); - } - else - { - _decryptIV = new byte[ivLen]; - Array.Copy(iv, _decryptIV, ivLen); - } + _encryptIV = new byte[ivLen]; + _encryptIVOffset = new byte[8]; + Array.Copy(iv, _encryptIV, ivLen); } - else if (_cipher == CIPHER_RC4) + else { - PolarSSL.arc4_init(ctx); - PolarSSL.arc4_setup(ctx, realkey, keyLen); + _decryptIV = new byte[ivLen]; + _decryptIVOffset = new byte[8]; + Array.Copy(iv, _decryptIV, ivLen); } } + else if (_cipher == CIPHER_RC4) + { + PolarSSL.arc4_init(ctx); + // PolarSSL RC4 takes key length by byte + PolarSSL.arc4_setup(ctx, realkey, keyLen); + } } @@ -165,7 +167,7 @@ namespace Shadowsocks.Encrypt public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength) { - if (_encryptCtx == null) + if (_encryptCtx == IntPtr.Zero) { randBytes(outbuf, ivLen); InitCipher(ref _encryptCtx, outbuf, true); @@ -173,34 +175,6 @@ namespace Shadowsocks.Encrypt lock (tempbuf) { // C# could be multi-threaded - lock (_encryptCtx) - { - if (_disposed) - { - throw new ObjectDisposedException(this.ToString()); - } - 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, length); - } - } - } - else - { - outlength = length; - lock (_encryptCtx) - { if (_disposed) { throw new ObjectDisposedException(this.ToString()); @@ -208,22 +182,45 @@ namespace Shadowsocks.Encrypt switch (_cipher) { case CIPHER_AES: - PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, outbuf); + PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, _encryptIVOffset, _encryptIV, buf, tempbuf); break; case CIPHER_BF: - PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, outbuf); + PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, _encryptIVOffset, _encryptIV, buf, tempbuf); break; case CIPHER_RC4: - PolarSSL.arc4_crypt(_encryptCtx, length, buf, outbuf); + PolarSSL.arc4_crypt(_encryptCtx, length, buf, tempbuf); break; } + outlength = length + ivLen; + Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, length); + + } + } + else + { + outlength = length; + if (_disposed) + { + throw new ObjectDisposedException(this.ToString()); + } + switch (_cipher) + { + case CIPHER_AES: + PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, _encryptIVOffset, _encryptIV, buf, outbuf); + break; + case CIPHER_BF: + PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, _encryptIVOffset, _encryptIV, buf, outbuf); + break; + case CIPHER_RC4: + PolarSSL.arc4_crypt(_encryptCtx, length, buf, outbuf); + break; } } } public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength) { - if (_decryptCtx == null) + if (_decryptCtx == IntPtr.Zero) { InitCipher(ref _decryptCtx, buf, false); outlength = length - ivLen; @@ -231,32 +228,6 @@ namespace Shadowsocks.Encrypt { // C# could be multi-threaded Buffer.BlockCopy(buf, ivLen, tempbuf, 0, length - ivLen); - lock (_decryptCtx) - { - if (_disposed) - { - throw new ObjectDisposedException(this.ToString()); - } - 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; - } - } - } - } - else - { - outlength = length; - lock (_decryptCtx) - { if (_disposed) { throw new ObjectDisposedException(this.ToString()); @@ -264,17 +235,37 @@ namespace Shadowsocks.Encrypt switch (_cipher) { case CIPHER_AES: - PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length, ref _decryptIVOffset, _decryptIV, buf, outbuf); + PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length - ivLen, _decryptIVOffset, _decryptIV, tempbuf, outbuf); break; case CIPHER_BF: - PolarSSL.blowfish_crypt_cfb64(_decryptCtx, PolarSSL.BLOWFISH_DECRYPT, length, ref _decryptIVOffset, _decryptIV, buf, outbuf); + PolarSSL.blowfish_crypt_cfb64(_decryptCtx, PolarSSL.BLOWFISH_DECRYPT, length - ivLen, _decryptIVOffset, _decryptIV, tempbuf, outbuf); break; case CIPHER_RC4: - PolarSSL.arc4_crypt(_decryptCtx, length, buf, outbuf); + PolarSSL.arc4_crypt(_decryptCtx, length - ivLen, tempbuf, outbuf); break; } } } + else + { + outlength = length; + if (_disposed) + { + throw new ObjectDisposedException(this.ToString()); + } + switch (_cipher) + { + case CIPHER_AES: + PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length, _decryptIVOffset, _decryptIV, buf, outbuf); + break; + case CIPHER_BF: + PolarSSL.blowfish_crypt_cfb64(_decryptCtx, PolarSSL.BLOWFISH_DECRYPT, length, _decryptIVOffset, _decryptIV, buf, outbuf); + break; + case CIPHER_RC4: + PolarSSL.arc4_crypt(_decryptCtx, length, buf, outbuf); + break; + } + } } #region IDisposable @@ -304,41 +295,39 @@ namespace Shadowsocks.Encrypt if (disposing) { - if (_encryptCtx != null) + if (_encryptCtx != IntPtr.Zero) { - lock (_encryptCtx) + switch (_cipher) { - switch (_cipher) - { - case CIPHER_AES: - PolarSSL.aes_free(_encryptCtx); - break; - case CIPHER_BF: - PolarSSL.blowfish_free(_encryptCtx); - break; - case CIPHER_RC4: - PolarSSL.arc4_free(_encryptCtx); - break; - } + case CIPHER_AES: + PolarSSL.aes_free(_encryptCtx); + break; + case CIPHER_BF: + PolarSSL.blowfish_free(_encryptCtx); + break; + case CIPHER_RC4: + PolarSSL.arc4_free(_encryptCtx); + break; } + Marshal.FreeHGlobal(_encryptCtx); + _encryptCtx = IntPtr.Zero; } - if (_decryptCtx != null) + if (_decryptCtx != IntPtr.Zero) { - lock (_decryptCtx) + switch (_cipher) { - switch (_cipher) - { - case CIPHER_AES: - PolarSSL.aes_free(_decryptCtx); - break; - case CIPHER_BF: - PolarSSL.blowfish_free(_decryptCtx); - break; - case CIPHER_RC4: - PolarSSL.arc4_free(_decryptCtx); - break; - } + case CIPHER_AES: + PolarSSL.aes_free(_decryptCtx); + break; + case CIPHER_BF: + PolarSSL.blowfish_free(_decryptCtx); + break; + case CIPHER_RC4: + PolarSSL.arc4_free(_decryptCtx); + break; } + Marshal.FreeHGlobal(_decryptCtx); + _decryptCtx = IntPtr.Zero; } } } diff --git a/test/UnitTest.cs b/test/UnitTest.cs index 46f9b662..166620f5 100755 --- a/test/UnitTest.cs +++ b/test/UnitTest.cs @@ -27,7 +27,6 @@ namespace test { // run it once before the multi-threading test to initialize global tables RunSingleEncryptionThread(); - List threads = new List(); for (int i = 0; i < 10; i++) { @@ -54,11 +53,8 @@ namespace test var random = new Random(); IEncryptor encryptor; IEncryptor decryptor; - lock (locker) - { - encryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!"); - decryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!"); - } + encryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!"); + decryptor = new PolarSSLEncryptor("aes-256-cfb", "barfoo!"); byte[] plain = new byte[16384]; byte[] cipher = new byte[plain.Length + 16]; byte[] plain2 = new byte[plain.Length + 16]; @@ -70,22 +66,31 @@ namespace test encryptor.Encrypt(plain, plain.Length, cipher, out outLen); decryptor.Decrypt(cipher, outLen, plain2, out outLen2); Assert.AreEqual(plain.Length, outLen2); + for (int j = 0; j < plain.Length; j++) + { + Assert.AreEqual(plain[j], plain2[j]); + } encryptor.Encrypt(plain, 1000, cipher, out outLen); decryptor.Decrypt(cipher, outLen, plain2, out outLen2); Assert.AreEqual(1000, outLen2); + for (int j = 0; j < outLen2; j++) + { + Assert.AreEqual(plain[j], plain2[j]); + } encryptor.Encrypt(plain, 12333, cipher, out outLen); decryptor.Decrypt(cipher, outLen, plain2, out outLen2); + Assert.AreEqual(12333, outLen2); + for (int j = 0; j < outLen2; j++) + { + Assert.AreEqual(plain[j], plain2[j]); + } //} - Assert.AreEqual(12333, outLen2); - for (int j = 0; j < plain.Length; j++) - { - Assert.AreEqual(plain[j], plain2[j]); - } } } catch { encryptionFailed = true; + throw; } } } diff --git a/test/test.csproj b/test/test.csproj index 235846d0..d7420145 100755 --- a/test/test.csproj +++ b/test/test.csproj @@ -26,6 +26,9 @@ bin\x86\Release\ x86 + + + From 519232211c69393aa814a984ef57fe6bc8fb8085 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 01:57:32 +0800 Subject: [PATCH 05/20] iv_off back to ref int --- shadowsocks-csharp/Encrypt/PolarSSL.cs | 4 ++-- shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs | 24 ++++++++++-------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/shadowsocks-csharp/Encrypt/PolarSSL.cs b/shadowsocks-csharp/Encrypt/PolarSSL.cs index 2ccf35ac..ac1592f0 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSL.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSL.cs @@ -23,7 +23,7 @@ namespace Shadowsocks.Encrypt public extern static int aes_setkey_enc(IntPtr ctx, byte[] key, int keysize); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int aes_crypt_cfb128(IntPtr ctx, int mode, int length, byte[] iv_off, byte[] iv, byte[] input, byte[] output); + public extern static int aes_crypt_cfb128(IntPtr ctx, int mode, int length, ref int iv_off, byte[] iv, byte[] input, byte[] output); public const int ARC4_CTX_SIZE = 264; @@ -55,7 +55,7 @@ namespace Shadowsocks.Encrypt public extern static int blowfish_setkey(IntPtr ctx, byte[] key, int keysize); [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int blowfish_crypt_cfb64(IntPtr ctx, int mode, int length, byte[] iv_off, byte[] iv, byte[] input, byte[] output); + public extern static int blowfish_crypt_cfb64(IntPtr ctx, int mode, int length, ref int iv_off, byte[] iv, byte[] input, byte[] output); } } diff --git a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs index c10ae6d4..7b369130 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs @@ -31,8 +31,8 @@ namespace Shadowsocks.Encrypt private IntPtr _decryptCtx = IntPtr.Zero; private byte[] _encryptIV; private byte[] _decryptIV; - private byte[] _encryptIVOffset; - private byte[] _decryptIVOffset; + private int _encryptIVOffset = 0; + private int _decryptIVOffset = 0; private string _method; private int keyLen; private int ivLen; @@ -125,13 +125,11 @@ namespace Shadowsocks.Encrypt if (isCipher) { _encryptIV = new byte[ivLen]; - _encryptIVOffset = new byte[8]; Array.Copy(iv, _encryptIV, ivLen); } else { _decryptIV = new byte[ivLen]; - _decryptIVOffset = new byte[8]; Array.Copy(iv, _decryptIV, ivLen); } } @@ -143,13 +141,11 @@ namespace Shadowsocks.Encrypt if (isCipher) { _encryptIV = new byte[ivLen]; - _encryptIVOffset = new byte[8]; Array.Copy(iv, _encryptIV, ivLen); } else { _decryptIV = new byte[ivLen]; - _decryptIVOffset = new byte[8]; Array.Copy(iv, _decryptIV, ivLen); } } @@ -182,10 +178,10 @@ namespace Shadowsocks.Encrypt switch (_cipher) { case CIPHER_AES: - PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, _encryptIVOffset, _encryptIV, buf, tempbuf); + 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, _encryptIVOffset, _encryptIV, buf, tempbuf); + 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); @@ -206,10 +202,10 @@ namespace Shadowsocks.Encrypt switch (_cipher) { case CIPHER_AES: - PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, _encryptIVOffset, _encryptIV, buf, outbuf); + PolarSSL.aes_crypt_cfb128(_encryptCtx, PolarSSL.AES_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, outbuf); break; case CIPHER_BF: - PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, _encryptIVOffset, _encryptIV, buf, outbuf); + PolarSSL.blowfish_crypt_cfb64(_encryptCtx, PolarSSL.BLOWFISH_ENCRYPT, length, ref _encryptIVOffset, _encryptIV, buf, outbuf); break; case CIPHER_RC4: PolarSSL.arc4_crypt(_encryptCtx, length, buf, outbuf); @@ -235,10 +231,10 @@ namespace Shadowsocks.Encrypt switch (_cipher) { case CIPHER_AES: - PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length - ivLen, _decryptIVOffset, _decryptIV, tempbuf, outbuf); + 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, _decryptIVOffset, _decryptIV, tempbuf, outbuf); + 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); @@ -256,10 +252,10 @@ namespace Shadowsocks.Encrypt switch (_cipher) { case CIPHER_AES: - PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length, _decryptIVOffset, _decryptIV, buf, outbuf); + PolarSSL.aes_crypt_cfb128(_decryptCtx, PolarSSL.AES_DECRYPT, length, ref _decryptIVOffset, _decryptIV, buf, outbuf); break; case CIPHER_BF: - PolarSSL.blowfish_crypt_cfb64(_decryptCtx, PolarSSL.BLOWFISH_DECRYPT, length, _decryptIVOffset, _decryptIV, buf, outbuf); + PolarSSL.blowfish_crypt_cfb64(_decryptCtx, PolarSSL.BLOWFISH_DECRYPT, length, ref _decryptIVOffset, _decryptIV, buf, outbuf); break; case CIPHER_RC4: PolarSSL.arc4_crypt(_decryptCtx, length, buf, outbuf); From ed2722a993bddf6f72d9ada1e0c0415045ccb5d9 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 02:24:50 +0800 Subject: [PATCH 06/20] back to polarssl --- shadowsocks-csharp/Controller/Local.cs | 26 +++++++++++----------- shadowsocks-csharp/Program.cs | 6 ++--- .../Properties/Resources.Designer.cs | 10 --------- shadowsocks-csharp/Properties/Resources.resx | 3 --- 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index 8bdbd15f..73c78021 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -141,7 +141,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -173,7 +173,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); } } if (remote != null) @@ -185,7 +185,7 @@ namespace Shadowsocks.Controller } catch (SocketException e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); } } ((IDisposable)encryptor).Dispose(); @@ -205,7 +205,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -219,7 +219,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -248,7 +248,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -271,7 +271,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -295,7 +295,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -313,7 +313,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -341,7 +341,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -368,7 +368,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -383,7 +383,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } @@ -398,7 +398,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); this.Close(); } } diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index df44a09e..2115c77a 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -36,16 +36,16 @@ namespace Shadowsocks return; } string tempPath = Path.GetTempPath(); - string dllPath = tempPath + "/libeay32.dll"; + string dllPath = tempPath + "/polarssl.dll"; try { - FileManager.UncompressFile(dllPath, Resources.libeay32_dll); + FileManager.UncompressFile(dllPath, Resources.polarssl_dll); } catch (IOException e) { Console.WriteLine(e.ToString()); } - //LoadLibrary(dllPath); + LoadLibrary(dllPath); #if !DEBUG Logging.OpenLogFile(); diff --git a/shadowsocks-csharp/Properties/Resources.Designer.cs b/shadowsocks-csharp/Properties/Resources.Designer.cs index 65b2d0a2..4f9edeba 100755 --- a/shadowsocks-csharp/Properties/Resources.Designer.cs +++ b/shadowsocks-csharp/Properties/Resources.Designer.cs @@ -63,16 +63,6 @@ namespace Shadowsocks.Properties { /// /// Looks up a localized resource of type System.Byte[]. /// - internal static byte[] libeay32_dll { - get { - object obj = ResourceManager.GetObject("libeay32_dll", resourceCulture); - return ((byte[])(obj)); - } - } - - /// - /// Looks up a localized resource of type System.Byte[]. - /// internal static byte[] polarssl_dll { get { object obj = ResourceManager.GetObject("polarssl_dll", resourceCulture); diff --git a/shadowsocks-csharp/Properties/Resources.resx b/shadowsocks-csharp/Properties/Resources.resx index f196cbea..a644b5f9 100755 --- a/shadowsocks-csharp/Properties/Resources.resx +++ b/shadowsocks-csharp/Properties/Resources.resx @@ -118,9 +118,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - ..\Data\libeay32.dll.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - ..\Data\polarssl.dll.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 From 58ba4e28e7b42535bfcd0df1b893880aa5b5d993 Mon Sep 17 00:00:00 2001 From: GhostFlying Date: Mon, 10 Nov 2014 12:48:30 +0800 Subject: [PATCH 07/20] enable proxy for all connections --- shadowsocks-csharp/Controller/SystemProxy.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/shadowsocks-csharp/Controller/SystemProxy.cs b/shadowsocks-csharp/Controller/SystemProxy.cs index 884c7260..a578752a 100755 --- a/shadowsocks-csharp/Controller/SystemProxy.cs +++ b/shadowsocks-csharp/Controller/SystemProxy.cs @@ -35,6 +35,8 @@ namespace Shadowsocks.Controller registry.SetValue("ProxyServer", ""); registry.SetValue("AutoConfigURL", "http://127.0.0.1:8090/pac?t=" + GetTimestamp(DateTime.Now)); SystemProxy.NotifyIE(); + //Must Notify IE first, or the connections do not chanage + CopyProxySettingFromLan(); } catch (Exception) { @@ -54,6 +56,7 @@ namespace Shadowsocks.Controller registry.SetValue("ProxyServer", ""); registry.SetValue("AutoConfigURL", ""); SystemProxy.NotifyIE(); + CopyProxySettingFromLan(); } catch (Exception) { @@ -62,6 +65,25 @@ namespace Shadowsocks.Controller } } + private static void CopyProxySettingFromLan() + { + RegistryKey registry = + Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Connections", + true); + var defulatValue = registry.GetValue("DefaultConnectionSettings"); + var connections = registry.GetValueNames(); + foreach (String each in connections){ + if (!(each.Equals("DefaultConnectionSettings") + || each.Equals("LAN Connection") + || each.Equals("SavedLegacySettings"))) + { + //set all the connections's proxy as the lan + registry.SetValue(each, defulatValue); + } + } + NotifyIE(); + } + private static String GetTimestamp(DateTime value) { return value.ToString("yyyyMMddHHmmssffff"); From 8c90452b90064eff8e0a351133e0169116a02f40 Mon Sep 17 00:00:00 2001 From: kookxiang Date: Mon, 10 Nov 2014 15:04:47 +0800 Subject: [PATCH 08/20] option to start on boot --- shadowsocks-csharp/View/ConfigForm.Designer.cs | 30 ++++++++++++------ shadowsocks-csharp/View/ConfigForm.cs | 44 ++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 6b246c66..6ed80c1d 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -65,6 +65,7 @@ this.AddButton = new System.Windows.Forms.Button(); this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); + this.autoStartup = new System.Windows.Forms.MenuItem(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); @@ -299,6 +300,7 @@ // this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.enableItem, + this.autoStartup, this.ShareOverLANItem, this.ServersItem, this.menuItem4, @@ -308,6 +310,7 @@ this.aboutItem, this.menuItem3, this.quitItem}); + this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup); // // enableItem // @@ -317,13 +320,13 @@ // // ShareOverLANItem // - this.ShareOverLANItem.Index = 1; + this.ShareOverLANItem.Index = 2; this.ShareOverLANItem.Text = "Share over LAN"; this.ShareOverLANItem.Click += new System.EventHandler(this.ShareOverLANItem_Click); // // ServersItem // - this.ServersItem.Index = 2; + this.ServersItem.Index = 3; this.ServersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.SeperatorItem, this.ConfigItem}); @@ -342,41 +345,41 @@ // // menuItem4 // - this.menuItem4.Index = 3; + this.menuItem4.Index = 4; this.menuItem4.Text = "-"; // // editPACFileItem // - this.editPACFileItem.Index = 4; + this.editPACFileItem.Index = 5; this.editPACFileItem.Text = "Edit &PAC File..."; this.editPACFileItem.Click += new System.EventHandler(this.EditPACFileItem_Click); // // QRCodeItem // - this.QRCodeItem.Index = 5; + this.QRCodeItem.Index = 6; this.QRCodeItem.Text = "Show &QRCode..."; this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click); // // ShowLogItem // - this.ShowLogItem.Index = 6; + this.ShowLogItem.Index = 7; this.ShowLogItem.Text = "Show Logs..."; this.ShowLogItem.Click += new System.EventHandler(this.ShowLogItem_Click); // // aboutItem // - this.aboutItem.Index = 7; + this.aboutItem.Index = 8; this.aboutItem.Text = "About..."; this.aboutItem.Click += new System.EventHandler(this.AboutItem_Click); // // menuItem3 // - this.menuItem3.Index = 8; + this.menuItem3.Index = 9; this.menuItem3.Text = "-"; // // quitItem // - this.quitItem.Index = 9; + this.quitItem.Index = 10; this.quitItem.Text = "&Quit"; this.quitItem.Click += new System.EventHandler(this.Quit_Click); // @@ -435,7 +438,13 @@ this.ServersListBox.Size = new System.Drawing.Size(286, 264); this.ServersListBox.TabIndex = 5; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); - // + // + // autoStartup + // + this.autoStartup.Index = 1; + this.autoStartup.Text = "Start on boot"; + this.autoStartup.Click += new System.EventHandler(this.autoStartup_Click); + // // ConfigForm // this.AcceptButton = this.OKButton; @@ -508,6 +517,7 @@ private System.Windows.Forms.MenuItem QRCodeItem; private System.Windows.Forms.MenuItem ShowLogItem; private System.Windows.Forms.MenuItem ShareOverLANItem; + private System.Windows.Forms.MenuItem autoStartup; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index f992222a..74cac77a 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -5,6 +5,7 @@ using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; +using Microsoft.Win32; using Shadowsocks.Controller; using Shadowsocks.Model; using Shadowsocks.Properties; @@ -377,5 +378,48 @@ namespace Shadowsocks.View qrCodeForm.Icon = this.Icon; qrCodeForm.Show(); } + + private bool setAutoStartup(bool enabled) { + try { + string path = Application.ExecutablePath; + RegistryKey runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); + if (enabled) { + runKey.SetValue("Shadowsocks-CSharp", path); + } else { + runKey.DeleteValue("Shadowsocks-CSharp"); + } + runKey.Close(); + return true; + } catch (Exception e) { + return false; + } + } + + private bool checkAutoStartup() { + try { + string path = Application.ExecutablePath; + RegistryKey runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); + string[] runList = runKey.GetValueNames(); + runKey.Close(); + foreach(string item in runList){ + if (item.Equals("Shadowsocks-CSharp")) + return true; + } + return false; + } catch (Exception e) { + return false; + } + } + + private void autoStartup_Click(object sender, EventArgs e) { + autoStartup.Checked = !autoStartup.Checked; + if (!setAutoStartup(autoStartup.Checked)) { + //MessageBox.Show("Failed to edit registry"); + } + } + + private void contextMenu1_Popup(object sender, EventArgs e) { + autoStartup.Checked = checkAutoStartup(); + } } } From c75941fe148277888a80a8451415b9690b8ff2b4 Mon Sep 17 00:00:00 2001 From: kookxiang Date: Mon, 10 Nov 2014 21:53:45 +0800 Subject: [PATCH 09/20] config path --- shadowsocks-csharp/Model/Configuration.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 65b0a3b6..3ae77dff 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Text; +using System.Windows.Forms; namespace Shadowsocks.Model { @@ -10,6 +11,7 @@ namespace Shadowsocks.Model { public Configuration() { + Configuration.CONFIG_FILE = Application.StartupPath + @"\gui-config.json"; } public List configs; From 3d4262d11d1c9723f2cd24d8ef9468304c27b8d8 Mon Sep 17 00:00:00 2001 From: kookxiang Date: Wed, 12 Nov 2014 14:14:03 +0800 Subject: [PATCH 10/20] move code to controller/autostartup --- shadowsocks-csharp/Controller/AutoStartup.cs | 39 ++++++++++++++++++++++++++++ shadowsocks-csharp/View/ConfigForm.cs | 38 +++------------------------ shadowsocks-csharp/shadowsocks-csharp.csproj | 1 + 3 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 shadowsocks-csharp/Controller/AutoStartup.cs diff --git a/shadowsocks-csharp/Controller/AutoStartup.cs b/shadowsocks-csharp/Controller/AutoStartup.cs new file mode 100644 index 00000000..cc7e0b2b --- /dev/null +++ b/shadowsocks-csharp/Controller/AutoStartup.cs @@ -0,0 +1,39 @@ +using System; +using System.Windows.Forms; +using Microsoft.Win32; + +namespace Shadowsocks.Controller { + class AutoStartup { + public static bool Set(bool enabled) { + try { + string path = Application.ExecutablePath; + RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); + if (enabled) { + runKey.SetValue("Shadowsocks", path); + } else { + runKey.DeleteValue("Shadowsocks"); + } + runKey.Close(); + return true; + } catch (Exception) { + return false; + } + } + + public static bool Check() { + try { + string path = Application.ExecutablePath; + RegistryKey runKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); + string[] runList = runKey.GetValueNames(); + runKey.Close(); + foreach (string item in runList) { + if (item.Equals("Shadowsocks")) + return true; + } + return false; + } catch (Exception) { + return false; + } + } + } +} diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 74cac77a..21d17487 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -379,47 +379,15 @@ namespace Shadowsocks.View qrCodeForm.Show(); } - private bool setAutoStartup(bool enabled) { - try { - string path = Application.ExecutablePath; - RegistryKey runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); - if (enabled) { - runKey.SetValue("Shadowsocks-CSharp", path); - } else { - runKey.DeleteValue("Shadowsocks-CSharp"); - } - runKey.Close(); - return true; - } catch (Exception e) { - return false; - } - } - - private bool checkAutoStartup() { - try { - string path = Application.ExecutablePath; - RegistryKey runKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); - string[] runList = runKey.GetValueNames(); - runKey.Close(); - foreach(string item in runList){ - if (item.Equals("Shadowsocks-CSharp")) - return true; - } - return false; - } catch (Exception e) { - return false; - } - } - private void autoStartup_Click(object sender, EventArgs e) { autoStartup.Checked = !autoStartup.Checked; - if (!setAutoStartup(autoStartup.Checked)) { - //MessageBox.Show("Failed to edit registry"); + if (!AutoStartup.Set(autoStartup.Checked)) { + MessageBox.Show("Failed to edit registry"); } } private void contextMenu1_Popup(object sender, EventArgs e) { - autoStartup.Checked = checkAutoStartup(); + autoStartup.Checked = AutoStartup.Check(); } } } diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index d081645c..533186eb 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -70,6 +70,7 @@ + From ef050cd3c3b567b1ab72582bad3ba8a6152f3f2c Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 21:27:27 +0800 Subject: [PATCH 11/20] check close actively --- shadowsocks-csharp/Controller/Local.cs | 71 +++++++++++++++++++++++++++--- shadowsocks-csharp/Controller/PACServer.cs | 7 ++- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index 73c78021..97f21474 100755 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -115,6 +115,9 @@ namespace Shadowsocks.Controller private bool connectionShutdown = false; private bool remoteShutdown = false; private bool closed = false; + + private object encryptionLock = new object(); + private object decryptionLock = new object(); public void Start() { @@ -188,11 +191,21 @@ namespace Shadowsocks.Controller Console.WriteLine(e); } } - ((IDisposable)encryptor).Dispose(); + lock (encryptionLock) + { + lock (decryptionLock) + { + ((IDisposable)encryptor).Dispose(); + } + } } private void ConnectCallback(IAsyncResult ar) { + if (closed) + { + return; + } try { // Complete the connection. @@ -212,6 +225,10 @@ namespace Shadowsocks.Controller private void HandshakeReceive() { + if (closed) + { + return; + } try { connection.BeginReceive(connetionRecvBuffer, 0, 256, 0, @@ -226,6 +243,10 @@ namespace Shadowsocks.Controller private void HandshakeReceiveCallback(IAsyncResult ar) { + if (closed) + { + return; + } try { int bytesRead = connection.EndReceive(ar); @@ -255,6 +276,10 @@ namespace Shadowsocks.Controller private void HandshakeSendCallback(IAsyncResult ar) { + if (closed) + { + return; + } try { connection.EndSend(ar); @@ -278,6 +303,10 @@ namespace Shadowsocks.Controller private void handshakeReceive2Callback(IAsyncResult ar) { + if (closed) + { + return; + } try { int bytesRead = connection.EndReceive(ar); @@ -303,6 +332,10 @@ namespace Shadowsocks.Controller private void StartPipe(IAsyncResult ar) { + if (closed) + { + return; + } try { connection.EndReceive(ar); @@ -320,7 +353,10 @@ namespace Shadowsocks.Controller private void PipeRemoteReceiveCallback(IAsyncResult ar) { - + if (closed) + { + return; + } try { int bytesRead = remote.EndReceive(ar); @@ -328,7 +364,14 @@ namespace Shadowsocks.Controller if (bytesRead > 0) { int bytesToSend; - encryptor.Decrypt(remoteRecvBuffer, bytesRead, remoteSendBuffer, out bytesToSend); + lock (decryptionLock) + { + if (closed) + { + return; + } + encryptor.Decrypt(remoteRecvBuffer, bytesRead, remoteSendBuffer, out bytesToSend); + } connection.BeginSend(remoteSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeConnectionSendCallback), null); } else @@ -348,7 +391,10 @@ namespace Shadowsocks.Controller private void PipeConnectionReceiveCallback(IAsyncResult ar) { - + if (closed) + { + return; + } try { int bytesRead = connection.EndReceive(ar); @@ -356,7 +402,14 @@ namespace Shadowsocks.Controller if (bytesRead > 0) { int bytesToSend; - encryptor.Encrypt(connetionRecvBuffer, bytesRead, connetionSendBuffer, out bytesToSend); + lock (encryptionLock) + { + if (closed) + { + return; + } + encryptor.Encrypt(connetionRecvBuffer, bytesRead, connetionSendBuffer, out bytesToSend); + } remote.BeginSend(connetionSendBuffer, 0, bytesToSend, 0, new AsyncCallback(PipeRemoteSendCallback), null); } else @@ -375,6 +428,10 @@ namespace Shadowsocks.Controller private void PipeRemoteSendCallback(IAsyncResult ar) { + if (closed) + { + return; + } try { remote.EndSend(ar); @@ -390,6 +447,10 @@ namespace Shadowsocks.Controller private void PipeConnectionSendCallback(IAsyncResult ar) { + if (closed) + { + return; + } try { connection.EndSend(ar); diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index 422089dc..ced6b3f1 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -87,9 +87,12 @@ namespace Shadowsocks.Controller conn.BeginReceive(new byte[1024], 0, 1024, 0, new AsyncCallback(ReceiveCallback), conn); } + catch (ObjectDisposedException) + { + } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); } } @@ -152,7 +155,7 @@ Connection: Close } catch (Exception e) { - Console.WriteLine(e.Message); + Console.WriteLine(e); conn.Close(); } } From 3b5777d830524b82bfb5daeff453f4485454cff1 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 21:40:31 +0800 Subject: [PATCH 12/20] lint code --- shadowsocks-csharp/Model/Configuration.cs | 5 - shadowsocks-csharp/View/ConfigForm.Designer.cs | 169 +++++++++++-------------- shadowsocks-csharp/View/ConfigForm.cs | 11 +- shadowsocks-csharp/View/ConfigForm.resx | 8 +- 4 files changed, 85 insertions(+), 108 deletions(-) diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index 3ae77dff..db02b840 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -9,11 +9,6 @@ namespace Shadowsocks.Model [Serializable] public class Configuration { - public Configuration() - { - Configuration.CONFIG_FILE = Application.StartupPath + @"\gui-config.json"; - } - public List configs; public int index; public bool enabled; diff --git a/shadowsocks-csharp/View/ConfigForm.Designer.cs b/shadowsocks-csharp/View/ConfigForm.Designer.cs index 6ed80c1d..1d5f7236 100755 --- a/shadowsocks-csharp/View/ConfigForm.Designer.cs +++ b/shadowsocks-csharp/View/ConfigForm.Designer.cs @@ -49,6 +49,7 @@ this.panel1 = new System.Windows.Forms.Panel(); this.contextMenu1 = new System.Windows.Forms.ContextMenu(); this.enableItem = new System.Windows.Forms.MenuItem(); + this.AutoStartupItem = new System.Windows.Forms.MenuItem(); this.ShareOverLANItem = new System.Windows.Forms.MenuItem(); this.ServersItem = new System.Windows.Forms.MenuItem(); this.SeperatorItem = new System.Windows.Forms.MenuItem(); @@ -65,7 +66,7 @@ this.AddButton = new System.Windows.Forms.Button(); this.ServerGroupBox = new System.Windows.Forms.GroupBox(); this.ServersListBox = new System.Windows.Forms.ListBox(); - this.autoStartup = new System.Windows.Forms.MenuItem(); + this.menuItem1 = new System.Windows.Forms.MenuItem(); this.tableLayoutPanel1.SuspendLayout(); this.panel1.SuspendLayout(); this.panel3.SuspendLayout(); @@ -91,10 +92,10 @@ this.tableLayoutPanel1.Controls.Add(this.PasswordTextBox, 1, 2); this.tableLayoutPanel1.Controls.Add(this.label5, 0, 3); this.tableLayoutPanel1.Controls.Add(this.EncryptionSelect, 1, 3); - this.tableLayoutPanel1.Location = new System.Drawing.Point(12, 31); - this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(7, 7, 7, 7); + this.tableLayoutPanel1.Location = new System.Drawing.Point(8, 21); + this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; - this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(7, 7, 7, 7); + this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(5, 5, 5, 5); this.tableLayoutPanel1.RowCount = 6; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); @@ -102,16 +103,15 @@ this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); - this.tableLayoutPanel1.Size = new System.Drawing.Size(360, 232); + this.tableLayoutPanel1.Size = new System.Drawing.Size(242, 167); this.tableLayoutPanel1.TabIndex = 0; // // RemarksTextBox // this.RemarksTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); - this.RemarksTextBox.Location = new System.Drawing.Point(110, 194); - this.RemarksTextBox.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.RemarksTextBox.Location = new System.Drawing.Point(74, 139); this.RemarksTextBox.Name = "RemarksTextBox"; - this.RemarksTextBox.Size = new System.Drawing.Size(238, 26); + this.RemarksTextBox.Size = new System.Drawing.Size(160, 20); this.RemarksTextBox.TabIndex = 10; this.RemarksTextBox.WordWrap = false; // @@ -119,10 +119,9 @@ // this.label6.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label6.AutoSize = true; - this.label6.Location = new System.Drawing.Point(27, 197); - this.label6.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0); + this.label6.Location = new System.Drawing.Point(19, 142); this.label6.Name = "label6"; - this.label6.Size = new System.Drawing.Size(73, 20); + this.label6.Size = new System.Drawing.Size(49, 13); this.label6.TabIndex = 9; this.label6.Text = "Remarks"; // @@ -130,10 +129,9 @@ // this.label1.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(26, 15); - this.label1.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0); + this.label1.Location = new System.Drawing.Point(17, 11); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(74, 20); + this.label1.Size = new System.Drawing.Size(51, 13); this.label1.TabIndex = 0; this.label1.Text = "Server IP"; // @@ -141,20 +139,18 @@ // this.label2.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(12, 51); - this.label2.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0); + this.label2.Location = new System.Drawing.Point(8, 37); this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(88, 20); + this.label2.Size = new System.Drawing.Size(60, 13); this.label2.TabIndex = 1; this.label2.Text = "Server Port"; // // ProxyPortTextBox // this.ProxyPortTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); - this.ProxyPortTextBox.Location = new System.Drawing.Point(110, 158); - this.ProxyPortTextBox.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.ProxyPortTextBox.Location = new System.Drawing.Point(74, 113); this.ProxyPortTextBox.Name = "ProxyPortTextBox"; - this.ProxyPortTextBox.Size = new System.Drawing.Size(238, 26); + this.ProxyPortTextBox.Size = new System.Drawing.Size(160, 20); this.ProxyPortTextBox.TabIndex = 4; this.ProxyPortTextBox.WordWrap = false; // @@ -162,10 +158,9 @@ // this.label4.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label4.AutoSize = true; - this.label4.Location = new System.Drawing.Point(20, 161); - this.label4.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0); + this.label4.Location = new System.Drawing.Point(13, 116); this.label4.Name = "label4"; - this.label4.Size = new System.Drawing.Size(80, 20); + this.label4.Size = new System.Drawing.Size(55, 13); this.label4.TabIndex = 3; this.label4.Text = "Proxy Port"; // @@ -173,41 +168,37 @@ // this.label3.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(22, 87); - this.label3.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0); + this.label3.Location = new System.Drawing.Point(15, 63); this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(78, 20); + this.label3.Size = new System.Drawing.Size(53, 13); this.label3.TabIndex = 2; this.label3.Text = "Password"; // // IPTextBox // this.IPTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); - this.IPTextBox.Location = new System.Drawing.Point(110, 12); - this.IPTextBox.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.IPTextBox.Location = new System.Drawing.Point(74, 8); this.IPTextBox.Name = "IPTextBox"; - this.IPTextBox.Size = new System.Drawing.Size(238, 26); + this.IPTextBox.Size = new System.Drawing.Size(160, 20); this.IPTextBox.TabIndex = 0; this.IPTextBox.WordWrap = false; // // ServerPortTextBox // this.ServerPortTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); - this.ServerPortTextBox.Location = new System.Drawing.Point(110, 48); - this.ServerPortTextBox.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.ServerPortTextBox.Location = new System.Drawing.Point(74, 34); this.ServerPortTextBox.Name = "ServerPortTextBox"; - this.ServerPortTextBox.Size = new System.Drawing.Size(238, 26); + this.ServerPortTextBox.Size = new System.Drawing.Size(160, 20); this.ServerPortTextBox.TabIndex = 1; this.ServerPortTextBox.WordWrap = false; // // PasswordTextBox // this.PasswordTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right))); - this.PasswordTextBox.Location = new System.Drawing.Point(110, 84); - this.PasswordTextBox.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.PasswordTextBox.Location = new System.Drawing.Point(74, 60); this.PasswordTextBox.Name = "PasswordTextBox"; this.PasswordTextBox.PasswordChar = '*'; - this.PasswordTextBox.Size = new System.Drawing.Size(238, 26); + this.PasswordTextBox.Size = new System.Drawing.Size(160, 20); this.PasswordTextBox.TabIndex = 2; this.PasswordTextBox.WordWrap = false; // @@ -215,10 +206,9 @@ // this.label5.Anchor = System.Windows.Forms.AnchorStyles.Right; this.label5.AutoSize = true; - this.label5.Location = new System.Drawing.Point(16, 124); - this.label5.Margin = new System.Windows.Forms.Padding(5, 0, 5, 0); + this.label5.Location = new System.Drawing.Point(11, 90); this.label5.Name = "label5"; - this.label5.Size = new System.Drawing.Size(84, 20); + this.label5.Size = new System.Drawing.Size(57, 13); this.label5.TabIndex = 8; this.label5.Text = "Encryption"; // @@ -229,7 +219,7 @@ this.EncryptionSelect.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.EncryptionSelect.FormattingEnabled = true; this.EncryptionSelect.ImeMode = System.Windows.Forms.ImeMode.NoControl; - this.EncryptionSelect.ItemHeight = 20; + this.EncryptionSelect.ItemHeight = 13; this.EncryptionSelect.Items.AddRange(new object[] { "table", "rc4-md5", @@ -238,10 +228,9 @@ "aes-128-cfb", "bf-cfb", "rc4"}); - this.EncryptionSelect.Location = new System.Drawing.Point(110, 120); - this.EncryptionSelect.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.EncryptionSelect.Location = new System.Drawing.Point(74, 86); this.EncryptionSelect.Name = "EncryptionSelect"; - this.EncryptionSelect.Size = new System.Drawing.Size(238, 28); + this.EncryptionSelect.Size = new System.Drawing.Size(160, 21); this.EncryptionSelect.TabIndex = 3; // // notifyIcon1 @@ -254,8 +243,7 @@ this.panel2.Anchor = System.Windows.Forms.AnchorStyles.Top; this.panel2.AutoSize = true; this.panel2.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; - this.panel2.Location = new System.Drawing.Point(246, 263); - this.panel2.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.panel2.Location = new System.Drawing.Point(164, 175); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(0, 0); this.panel2.TabIndex = 1; @@ -263,10 +251,9 @@ // OKButton // this.OKButton.DialogResult = System.Windows.Forms.DialogResult.OK; - this.OKButton.Location = new System.Drawing.Point(6, 6); - this.OKButton.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.OKButton.Location = new System.Drawing.Point(4, 4); this.OKButton.Name = "OKButton"; - this.OKButton.Size = new System.Drawing.Size(113, 35); + this.OKButton.Size = new System.Drawing.Size(75, 23); this.OKButton.TabIndex = 8; this.OKButton.Text = "OK"; this.OKButton.UseVisualStyleBackColor = true; @@ -275,10 +262,9 @@ // MyCancelButton // this.MyCancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.MyCancelButton.Location = new System.Drawing.Point(130, 6); - this.MyCancelButton.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.MyCancelButton.Location = new System.Drawing.Point(87, 4); this.MyCancelButton.Name = "MyCancelButton"; - this.MyCancelButton.Size = new System.Drawing.Size(113, 35); + this.MyCancelButton.Size = new System.Drawing.Size(75, 23); this.MyCancelButton.TabIndex = 9; this.MyCancelButton.Text = "Cancel"; this.MyCancelButton.UseVisualStyleBackColor = true; @@ -290,27 +276,27 @@ this.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.panel1.Controls.Add(this.MyCancelButton); this.panel1.Controls.Add(this.OKButton); - this.panel1.Location = new System.Drawing.Point(469, 371); + this.panel1.Location = new System.Drawing.Point(313, 247); this.panel1.Margin = new System.Windows.Forms.Padding(0); this.panel1.Name = "panel1"; - this.panel1.Size = new System.Drawing.Size(248, 46); + this.panel1.Size = new System.Drawing.Size(165, 30); this.panel1.TabIndex = 1; // // contextMenu1 // this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.enableItem, - this.autoStartup, - this.ShareOverLANItem, this.ServersItem, - this.menuItem4, + this.menuItem1, + this.AutoStartupItem, + this.ShareOverLANItem, this.editPACFileItem, + this.menuItem4, this.QRCodeItem, this.ShowLogItem, this.aboutItem, this.menuItem3, this.quitItem}); - this.contextMenu1.Popup += new System.EventHandler(this.contextMenu1_Popup); // // enableItem // @@ -318,15 +304,21 @@ this.enableItem.Text = "&Enable"; this.enableItem.Click += new System.EventHandler(this.EnableItem_Click); // + // AutoStartupItem + // + this.AutoStartupItem.Index = 3; + this.AutoStartupItem.Text = "Start on Boot"; + this.AutoStartupItem.Click += new System.EventHandler(this.AutoStartupItem_Click); + // // ShareOverLANItem // - this.ShareOverLANItem.Index = 2; + this.ShareOverLANItem.Index = 4; this.ShareOverLANItem.Text = "Share over LAN"; this.ShareOverLANItem.Click += new System.EventHandler(this.ShareOverLANItem_Click); // // ServersItem // - this.ServersItem.Index = 3; + this.ServersItem.Index = 1; this.ServersItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.SeperatorItem, this.ConfigItem}); @@ -345,41 +337,41 @@ // // menuItem4 // - this.menuItem4.Index = 4; + this.menuItem4.Index = 6; this.menuItem4.Text = "-"; // // editPACFileItem // - this.editPACFileItem.Index = 5; + this.editPACFileItem.Index = 5; this.editPACFileItem.Text = "Edit &PAC File..."; this.editPACFileItem.Click += new System.EventHandler(this.EditPACFileItem_Click); // // QRCodeItem // - this.QRCodeItem.Index = 6; + this.QRCodeItem.Index = 7; this.QRCodeItem.Text = "Show &QRCode..."; this.QRCodeItem.Click += new System.EventHandler(this.QRCodeItem_Click); // // ShowLogItem // - this.ShowLogItem.Index = 7; + this.ShowLogItem.Index = 8; this.ShowLogItem.Text = "Show Logs..."; this.ShowLogItem.Click += new System.EventHandler(this.ShowLogItem_Click); // // aboutItem // - this.aboutItem.Index = 8; + this.aboutItem.Index = 9; this.aboutItem.Text = "About..."; this.aboutItem.Click += new System.EventHandler(this.AboutItem_Click); // // menuItem3 // - this.menuItem3.Index = 9; + this.menuItem3.Index = 10; this.menuItem3.Text = "-"; // // quitItem // - this.quitItem.Index = 10; + this.quitItem.Index = 11; this.quitItem.Text = "&Quit"; this.quitItem.Click += new System.EventHandler(this.Quit_Click); // @@ -388,18 +380,17 @@ this.panel3.AutoSize = true; this.panel3.Controls.Add(this.DeleteButton); this.panel3.Controls.Add(this.AddButton); - this.panel3.Location = new System.Drawing.Point(18, 330); + this.panel3.Location = new System.Drawing.Point(12, 220); this.panel3.Margin = new System.Windows.Forms.Padding(0); this.panel3.Name = "panel3"; - this.panel3.Size = new System.Drawing.Size(288, 46); + this.panel3.Size = new System.Drawing.Size(192, 31); this.panel3.TabIndex = 5; // // DeleteButton // - this.DeleteButton.Location = new System.Drawing.Point(150, 6); - this.DeleteButton.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.DeleteButton.Location = new System.Drawing.Point(100, 4); this.DeleteButton.Name = "DeleteButton"; - this.DeleteButton.Size = new System.Drawing.Size(133, 35); + this.DeleteButton.Size = new System.Drawing.Size(89, 23); this.DeleteButton.TabIndex = 7; this.DeleteButton.Text = "&Delete"; this.DeleteButton.UseVisualStyleBackColor = true; @@ -407,10 +398,9 @@ // // AddButton // - this.AddButton.Location = new System.Drawing.Point(6, 6); - this.AddButton.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.AddButton.Location = new System.Drawing.Point(4, 4); this.AddButton.Name = "AddButton"; - this.AddButton.Size = new System.Drawing.Size(133, 35); + this.AddButton.Size = new System.Drawing.Size(89, 23); this.AddButton.TabIndex = 6; this.AddButton.Text = "&Add"; this.AddButton.UseVisualStyleBackColor = true; @@ -419,11 +409,9 @@ // ServerGroupBox // this.ServerGroupBox.Controls.Add(this.tableLayoutPanel1); - this.ServerGroupBox.Location = new System.Drawing.Point(334, 18); - this.ServerGroupBox.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.ServerGroupBox.Location = new System.Drawing.Point(223, 12); this.ServerGroupBox.Name = "ServerGroupBox"; - this.ServerGroupBox.Padding = new System.Windows.Forms.Padding(5, 5, 5, 5); - this.ServerGroupBox.Size = new System.Drawing.Size(383, 307); + this.ServerGroupBox.Size = new System.Drawing.Size(255, 205); this.ServerGroupBox.TabIndex = 6; this.ServerGroupBox.TabStop = false; this.ServerGroupBox.Text = "Server"; @@ -431,35 +419,31 @@ // ServersListBox // this.ServersListBox.FormattingEnabled = true; - this.ServersListBox.ItemHeight = 20; - this.ServersListBox.Location = new System.Drawing.Point(18, 18); - this.ServersListBox.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); + this.ServersListBox.Location = new System.Drawing.Point(12, 12); this.ServersListBox.Name = "ServersListBox"; - this.ServersListBox.Size = new System.Drawing.Size(286, 264); + this.ServersListBox.Size = new System.Drawing.Size(192, 173); this.ServersListBox.TabIndex = 5; this.ServersListBox.SelectedIndexChanged += new System.EventHandler(this.ServersListBox_SelectedIndexChanged); - // - // autoStartup - // - this.autoStartup.Index = 1; - this.autoStartup.Text = "Start on boot"; - this.autoStartup.Click += new System.EventHandler(this.autoStartup_Click); - // + // + // menuItem1 + // + this.menuItem1.Index = 2; + this.menuItem1.Text = "-"; + // // ConfigForm // this.AcceptButton = this.OKButton; - this.AutoScaleDimensions = new System.Drawing.SizeF(144F, 144F); + this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi; this.AutoSize = true; this.CancelButton = this.MyCancelButton; - this.ClientSize = new System.Drawing.Size(733, 430); + this.ClientSize = new System.Drawing.Size(489, 287); this.Controls.Add(this.ServersListBox); this.Controls.Add(this.ServerGroupBox); this.Controls.Add(this.panel1); this.Controls.Add(this.panel3); this.Controls.Add(this.panel2); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; - this.Margin = new System.Windows.Forms.Padding(5, 5, 5, 5); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "ConfigForm"; @@ -517,7 +501,8 @@ private System.Windows.Forms.MenuItem QRCodeItem; private System.Windows.Forms.MenuItem ShowLogItem; private System.Windows.Forms.MenuItem ShareOverLANItem; - private System.Windows.Forms.MenuItem autoStartup; + private System.Windows.Forms.MenuItem AutoStartupItem; + private System.Windows.Forms.MenuItem menuItem1; } } diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index 21d17487..c1bfff63 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -185,6 +185,7 @@ namespace Shadowsocks.View UpdateServersMenu(); enableItem.Checked = _modifiedConfiguration.enabled; ShareOverLANItem.Checked = _modifiedConfiguration.shareOverLan; + AutoStartupItem.Checked = AutoStartup.Check(); } private void UpdateServersMenu() @@ -379,15 +380,11 @@ namespace Shadowsocks.View qrCodeForm.Show(); } - private void autoStartup_Click(object sender, EventArgs e) { - autoStartup.Checked = !autoStartup.Checked; - if (!AutoStartup.Set(autoStartup.Checked)) { + private void AutoStartupItem_Click(object sender, EventArgs e) { + AutoStartupItem.Checked = !AutoStartupItem.Checked; + if (!AutoStartup.Set(AutoStartupItem.Checked)) { MessageBox.Show("Failed to edit registry"); } } - - private void contextMenu1_Popup(object sender, EventArgs e) { - autoStartup.Checked = AutoStartup.Check(); - } } } diff --git a/shadowsocks-csharp/View/ConfigForm.resx b/shadowsocks-csharp/View/ConfigForm.resx index dc4a77ba..3dc5188a 100755 --- a/shadowsocks-csharp/View/ConfigForm.resx +++ b/shadowsocks-csharp/View/ConfigForm.resx @@ -112,15 +112,15 @@ 2.0 - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + 17, 17 - + 146, 19 \ No newline at end of file From 078ae6c0325ba8966b9f188444f1f5c6fa80b7c4 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 22:00:52 +0800 Subject: [PATCH 13/20] bump --- shadowsocks-csharp/Properties/AssemblyInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shadowsocks-csharp/Properties/AssemblyInfo.cs b/shadowsocks-csharp/Properties/AssemblyInfo.cs index a5fe27d4..8b0d229e 100755 --- a/shadowsocks-csharp/Properties/AssemblyInfo.cs +++ b/shadowsocks-csharp/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值, // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.0.7")] +[assembly: AssemblyVersion("2.0.8")] // [assembly: AssemblyFileVersion("2.0.0")] From 2fc3cb920d653f83a56768958d715686e602d2e2 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 22:02:34 +0800 Subject: [PATCH 14/20] back to .net 2.0 --- shadowsocks-csharp/shadowsocks-csharp.csproj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 533186eb..0b312e57 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -10,7 +10,7 @@ Properties Shadowsocks Shadowsocks - v4.0 + v2.0 512 @@ -21,7 +21,8 @@ 3.5 - Client + + publish\ true Disk From b2e066a888f3aff45a2796ce4c01830e2f8da5dc Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 22:04:25 +0800 Subject: [PATCH 15/20] update changes --- CHANGES | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES b/CHANGES index 781044c9..ccd57a0b 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,9 @@ +2.0.8 2014-11-12 +- Fix data corruption +- Set proxy for PPPoE +- Auto Startup Option +- Support high DPI screens + 2.0.7 2014-11-11 - Use OpenSSL for now From cec8378fea9c6804dfd95b5d075aea540d232014 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 22:38:11 +0800 Subject: [PATCH 16/20] add commented code of auto proxy type choosing, not enabled now --- shadowsocks-csharp/Controller/PACServer.cs | 38 ++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/shadowsocks-csharp/Controller/PACServer.cs b/shadowsocks-csharp/Controller/PACServer.cs index ced6b3f1..7b39252d 100755 --- a/shadowsocks-csharp/Controller/PACServer.cs +++ b/shadowsocks-csharp/Controller/PACServer.cs @@ -15,6 +15,7 @@ namespace Shadowsocks.Controller { private static int PORT = 8090; private static string PAC_FILE = "pac.txt"; + private static Configuration config; Socket _listener; FileSystemWatcher watcher; @@ -25,6 +26,7 @@ namespace Shadowsocks.Controller { try { + config = configuration; // Create a TCP/IP socket. _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); @@ -84,8 +86,14 @@ namespace Shadowsocks.Controller new AsyncCallback(AcceptCallback), listener); - conn.BeginReceive(new byte[1024], 0, 1024, 0, - new AsyncCallback(ReceiveCallback), conn); + byte[] buf = new byte[2048]; + object[] state = new object[] { + conn, + buf + }; + + conn.BeginReceive(buf, 0, 1024, 0, + new AsyncCallback(ReceiveCallback), state); } catch (ObjectDisposedException) { @@ -123,7 +131,10 @@ namespace Shadowsocks.Controller private void ReceiveCallback(IAsyncResult ar) { - Socket conn = (Socket)ar.AsyncState; + object[] state = (object[])ar.AsyncState; + + Socket conn = (Socket)state[0]; + byte[] requestBuf = (byte[])state[1]; try { int bytesRead = conn.EndReceive(ar); @@ -132,7 +143,7 @@ namespace Shadowsocks.Controller IPEndPoint localEndPoint = (IPEndPoint)conn.LocalEndPoint; - string proxy = "PROXY " + localEndPoint.Address + ":8123;"; + string proxy = GetPACAddress(requestBuf, localEndPoint); pac = pac.Replace("__PROXY__", proxy); @@ -189,5 +200,24 @@ Connection: Close PACFileChanged(this, new EventArgs()); } } + + private string GetPACAddress(byte[] requestBuf, IPEndPoint localEndPoint) + { + string proxy = "PROXY " + localEndPoint.Address + ":8123;"; + //try + //{ + // string requestString = Encoding.UTF8.GetString(requestBuf); + // if (requestString.IndexOf("AppleWebKit") >= 0) + // { + // string address = "" + localEndPoint.Address + ":" + config.GetCurrentServer().local_port; + // proxy = "SOCKS5 " + address + "; SOCKS " + address + ";"; + // } + //} + //catch (Exception e) + //{ + // Console.WriteLine(e); + //} + return proxy; + } } } From 7626d07ad8ecd95c04d76b49bf8559f7b238d915 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 22:47:04 +0800 Subject: [PATCH 17/20] fix #43 --- shadowsocks-csharp/Data/polipo_config.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/shadowsocks-csharp/Data/polipo_config.txt b/shadowsocks-csharp/Data/polipo_config.txt index fc500543..5a18557f 100755 --- a/shadowsocks-csharp/Data/polipo_config.txt +++ b/shadowsocks-csharp/Data/polipo_config.txt @@ -4,3 +4,6 @@ socksParentProxy = "127.0.0.1:__SOCKS_PORT__" socksProxyType = socks5 diskCacheRoot = "" localDocumentRoot = "" + +allowedPorts = 1-65535 +tunnelAllowedPorts = 1-65535 \ No newline at end of file From b8252d11e4c9cea7734c4da7eb5a59e438fd0847 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Wed, 12 Nov 2014 23:19:02 +0800 Subject: [PATCH 18/20] remove OpenSSL --- shadowsocks-csharp/Encrypt/OpenSSL.cs | 45 ------- shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs | 170 ------------------------- shadowsocks-csharp/Encrypt/PolarSSL.cs | 23 +++- shadowsocks-csharp/Program.cs | 16 --- shadowsocks-csharp/shadowsocks-csharp.csproj | 2 - 5 files changed, 22 insertions(+), 234 deletions(-) delete mode 100755 shadowsocks-csharp/Encrypt/OpenSSL.cs delete mode 100755 shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs diff --git a/shadowsocks-csharp/Encrypt/OpenSSL.cs b/shadowsocks-csharp/Encrypt/OpenSSL.cs deleted file mode 100755 index 8d7c31e4..00000000 --- a/shadowsocks-csharp/Encrypt/OpenSSL.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using System.Text; - -namespace Shadowsocks.Encrypt -{ - public class OpenSSL - { - const string DLLNAME = "libeay32"; - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static void OpenSSL_add_all_ciphers(); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static IntPtr EVP_md5(); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int EVP_BytesToKey(IntPtr type, IntPtr md, IntPtr salt, byte[] data, int datal, int count, byte[] key, byte[] iv); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int RAND_bytes(byte[] buf, int num); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static IntPtr EVP_get_cipherbyname(byte[] name); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static IntPtr EVP_CIPHER_CTX_new(); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int EVP_CipherInit_ex(IntPtr ctx, IntPtr type, IntPtr impl, byte[] key, byte[] iv, int enc); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int EVP_CIPHER_CTX_cleanup(IntPtr a); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int EVP_CIPHER_CTX_free(IntPtr a); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static int EVP_CipherUpdate(IntPtr ctx, byte[] outb, out int outl, byte[] inb, int inl); - - [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] - public extern static IntPtr MD5(byte[] d, long n, byte[] md); - } -} diff --git a/shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs b/shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs deleted file mode 100755 index bcea60a3..00000000 --- a/shadowsocks-csharp/Encrypt/OpenSSLEncryptor.cs +++ /dev/null @@ -1,170 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using System.Text; - -namespace Shadowsocks.Encrypt -{ - public class OpenSSLEncryptor - : EncryptorBase, IDisposable - { - static Dictionary ciphers = new Dictionary { - {"aes-128-cfb", new int[]{16, 16}}, - {"aes-192-cfb", new int[]{24, 16}}, - {"aes-256-cfb", new int[]{32, 16}}, - {"bf-cfb", new int[]{16, 8}}, - {"rc4", new int[]{16, 0}}, - {"rc4-md5", new int[]{16, 16}}, - }; - - static OpenSSLEncryptor() - { - OpenSSL.OpenSSL_add_all_ciphers(); - } - - public OpenSSLEncryptor(string method, string password) - : base(method, password) - { - InitKey(method, password); - } - - - static byte[] tempbuf = new byte[32768]; - - public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength) - { - if (_encryptCtx == IntPtr.Zero) - { - OpenSSL.RAND_bytes(outbuf, ivLen); - InitCipher(ref _encryptCtx, outbuf, true); - outlength = length + ivLen; - OpenSSL.EVP_CipherUpdate(_encryptCtx, tempbuf, out outlength, buf, length); - outlength = length + ivLen; - Buffer.BlockCopy(tempbuf, 0, outbuf, ivLen, length); - } - else - { - outlength = length; - OpenSSL.EVP_CipherUpdate(_encryptCtx, outbuf, out outlength, buf, length); - } - } - - public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength) - { - if (_decryptCtx == IntPtr.Zero) - { - InitCipher(ref _decryptCtx, buf, false); - outlength = length - ivLen; - Buffer.BlockCopy(buf, ivLen, tempbuf, 0, length - ivLen); - OpenSSL.EVP_CipherUpdate(_decryptCtx, outbuf, out outlength, tempbuf, length - ivLen); - } - else - { - outlength = length; - OpenSSL.EVP_CipherUpdate(_decryptCtx, outbuf, out outlength, buf, length); - } - } - - private static readonly Dictionary CachedKeys = new Dictionary(); - private byte[] _key; - private IntPtr _encryptCtx; - private IntPtr _decryptCtx; - private IntPtr _cipher; - private string _method; - private int keyLen; - private int ivLen; - - private void InitKey(string method, string password) - { - method = method.ToLower(); - _method = method; - string k = method + ":" + password; - if (method == "rc4-md5") - { - method = "rc4"; - } - _cipher = OpenSSL.EVP_get_cipherbyname(System.Text.Encoding.UTF8.GetBytes(method)); - if (_cipher == null) - { - throw new Exception("method not found"); - } - keyLen = ciphers[_method][0]; - ivLen = ciphers[_method][1]; - if (CachedKeys.ContainsKey(k)) - { - _key = CachedKeys[k]; - } - else - { - byte[] passbuf = Encoding.UTF8.GetBytes(password); - _key = new byte[32]; - byte[] iv = new byte[16]; - OpenSSL.EVP_BytesToKey(_cipher, OpenSSL.EVP_md5(), IntPtr.Zero, passbuf, passbuf.Length, 1, _key, iv); - CachedKeys[k] = _key; - } - } - - private void InitCipher(ref IntPtr ctx, byte[] iv, bool isCipher) - { - ctx = OpenSSL.EVP_CIPHER_CTX_new(); - int enc = isCipher ? 1 : 0; - byte[] realkey; - IntPtr r = IntPtr.Zero; - if (_method == "rc4-md5") - { - byte[] temp = new byte[keyLen + ivLen]; - realkey = new byte[keyLen]; - Array.Copy(_key, 0, temp, 0, keyLen); - Array.Copy(iv, 0, temp, keyLen, ivLen); - r = OpenSSL.MD5(temp, keyLen + ivLen, null); - Marshal.Copy(r, realkey, 0, keyLen); - } - else - { - realkey = _key; - } - OpenSSL.EVP_CipherInit_ex(ctx, _cipher, IntPtr.Zero, realkey, iv, enc); - } - - #region IDisposable - private bool _disposed; - - public override void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - ~OpenSSLEncryptor() - { - Dispose(false); - } - - protected virtual void Dispose(bool disposing) - { - if (!_disposed) - { - if (disposing) - { - - } - - if (_encryptCtx.ToInt64() != 0) - { - OpenSSL.EVP_CIPHER_CTX_cleanup(_encryptCtx); - OpenSSL.EVP_CIPHER_CTX_free(_encryptCtx); - _encryptCtx = IntPtr.Zero; - } - if (_decryptCtx.ToInt64() != 0) - { - OpenSSL.EVP_CIPHER_CTX_cleanup(_decryptCtx); - OpenSSL.EVP_CIPHER_CTX_free(_decryptCtx); - _decryptCtx = IntPtr.Zero; - } - - _disposed = true; - } - } - #endregion - } -} diff --git a/shadowsocks-csharp/Encrypt/PolarSSL.cs b/shadowsocks-csharp/Encrypt/PolarSSL.cs index ac1592f0..193e4ab0 100755 --- a/shadowsocks-csharp/Encrypt/PolarSSL.cs +++ b/shadowsocks-csharp/Encrypt/PolarSSL.cs @@ -1,5 +1,8 @@ -using System; +using Shadowsocks.Controller; +using Shadowsocks.Properties; +using System; using System.Collections.Generic; +using System.IO; using System.Runtime.InteropServices; using System.Text; @@ -13,6 +16,24 @@ namespace Shadowsocks.Encrypt public const int AES_ENCRYPT = 1; public const int AES_DECRYPT = 0; + static PolarSSL() + { + string tempPath = Path.GetTempPath(); + string dllPath = tempPath + "/polarssl.dll"; + try + { + FileManager.UncompressFile(dllPath, Resources.polarssl_dll); + } + catch (IOException e) + { + Console.WriteLine(e.ToString()); + } + LoadLibrary(dllPath); + } + + [DllImport("Kernel32.dll")] + private static extern IntPtr LoadLibrary(string path); + [DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)] public extern static void aes_init(IntPtr ctx); diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index 2115c77a..b2834ed6 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -4,9 +4,6 @@ using Shadowsocks.View; using System; using System.Collections.Generic; using System.Diagnostics; -using System.IO; -using System.Reflection; -using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; @@ -14,8 +11,6 @@ namespace Shadowsocks { static class Program { - [DllImport("Kernel32.dll")] - private static extern IntPtr LoadLibrary(string path); /// /// 应用程序的主入口点。 @@ -35,17 +30,6 @@ namespace Shadowsocks MessageBox.Show("Shadowsocks is already running.\n\nFind Shadowsocks icon in your notify tray."); return; } - string tempPath = Path.GetTempPath(); - string dllPath = tempPath + "/polarssl.dll"; - try - { - FileManager.UncompressFile(dllPath, Resources.polarssl_dll); - } - catch (IOException e) - { - Console.WriteLine(e.ToString()); - } - LoadLibrary(dllPath); #if !DEBUG Logging.OpenLogFile(); diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 0b312e57..41515225 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -77,8 +77,6 @@ - - From 0d78051318072e0a4362a185812466b3688512ea Mon Sep 17 00:00:00 2001 From: clowwindy Date: Thu, 13 Nov 2014 22:24:51 +0800 Subject: [PATCH 19/20] fix #60 --- shadowsocks-csharp/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index b2834ed6..7b0d6df0 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -4,6 +4,7 @@ using Shadowsocks.View; using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Threading; using System.Windows.Forms; @@ -30,7 +31,7 @@ namespace Shadowsocks MessageBox.Show("Shadowsocks is already running.\n\nFind Shadowsocks icon in your notify tray."); return; } - + Directory.SetCurrentDirectory(Application.StartupPath); #if !DEBUG Logging.OpenLogFile(); #endif From 3d7b820d3e40a2da9b3ed3f59b8de19a6b26a4f8 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Thu, 13 Nov 2014 22:28:19 +0800 Subject: [PATCH 20/20] bump --- CHANGES | 4 ++++ shadowsocks-csharp/Properties/AssemblyInfo.cs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index ccd57a0b..2f8955b3 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +2.0.9 2014-11-13 +- Fix startup path +- Fix allowed port range for polipo + 2.0.8 2014-11-12 - Fix data corruption - Set proxy for PPPoE diff --git a/shadowsocks-csharp/Properties/AssemblyInfo.cs b/shadowsocks-csharp/Properties/AssemblyInfo.cs index 8b0d229e..90a614c5 100755 --- a/shadowsocks-csharp/Properties/AssemblyInfo.cs +++ b/shadowsocks-csharp/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值, // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("2.0.8")] +[assembly: AssemblyVersion("2.0.9")] // [assembly: AssemblyFileVersion("2.0.0")]