Skip to content

Commit 4a070ab

Browse files
committed
3.9.5
fix rc4 fill with randBytes add auth_aes128_md5 & auth_aes128_sha1
1 parent 1e4b97d commit 4a070ab

File tree

8 files changed

+87
-44
lines changed

8 files changed

+87
-44
lines changed

shadowsocks-csharp/Controller/Local.cs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,15 @@ class Handler
166166
protected bool is_obfs_sendback = false;
167167

168168
protected bool connectionTCPIdle, connectionUDPIdle, remoteTCPIdle, remoteUDPIdle;
169-
protected int remoteRecvCount = 0;
169+
//protected int remoteRecvCount = 0;
170170
protected int connectionRecvCount = 0;
171171

172172
protected SpeedTester speedTester = new SpeedTester();
173173
protected int lastErrCode;
174174
protected Random random = new Random();
175175
protected System.Timers.Timer timer;
176176
protected object timerLock = new object();
177+
protected DateTime lastTimerSetTime;
177178

178179
enum ConnectState
179180
{
@@ -206,31 +207,42 @@ private void ResetTimeout(Double time)
206207
return;
207208

208209
cfg.try_keep_alive = 0;
209-
lock (timerLock)
210+
211+
if (time <= 0)
210212
{
211-
if (time <= 0)
213+
if (timer != null)
212214
{
213-
if (timer != null)
215+
lock (timerLock)
214216
{
215-
timer.Enabled = false;
216-
timer.Elapsed -= timer_Elapsed;
217-
timer.Dispose();
218-
timer = null;
217+
if (timer != null)
218+
{
219+
timer.Enabled = false;
220+
timer.Elapsed -= timer_Elapsed;
221+
timer.Dispose();
222+
timer = null;
223+
}
219224
}
220225
}
221-
else
226+
}
227+
else
228+
{
229+
if (lastTimerSetTime != null && (DateTime.Now - lastTimerSetTime).TotalMilliseconds > 500)
222230
{
223-
if (timer == null)
231+
lock (timerLock)
224232
{
225-
timer = new System.Timers.Timer(time * 1000.0);
226-
timer.Elapsed += timer_Elapsed;
227-
}
228-
else
229-
{
230-
timer.Interval = time * 1000.0;
231-
timer.Stop();
233+
if (timer == null)
234+
{
235+
timer = new System.Timers.Timer(time * 1000.0);
236+
timer.Elapsed += timer_Elapsed;
237+
}
238+
else
239+
{
240+
timer.Interval = time * 1000.0;
241+
timer.Stop();
242+
}
243+
timer.Start();
244+
lastTimerSetTime = DateTime.Now;
232245
}
233-
timer.Start();
234246
}
235247
}
236248
}
@@ -521,7 +533,7 @@ private void BeginConnect(IPAddress ipAddress, int serverPort)
521533
remote.GetSocket().SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
522534
try
523535
{
524-
remote.SetEncryptor(EncryptorFactory.GetEncryptor(server.method, server.password));
536+
remote.CreateEncryptor(server.method, server.password);
525537
}
526538
catch
527539
{
@@ -539,7 +551,7 @@ private void BeginConnect(IPAddress ipAddress, int serverPort)
539551
SocketType.Dgram, ProtocolType.Udp);
540552
remoteUDP.GetSocket().Bind(new IPEndPoint(ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0));
541553

542-
remoteUDP.SetEncryptor(EncryptorFactory.GetEncryptor(server.method, server.password));
554+
remoteUDP.CreateEncryptor(server.method, server.password);
543555
remoteUDP.SetProtocol(ObfsFactory.GetObfs(server.protocol));
544556
remoteUDP.SetObfs(ObfsFactory.GetObfs(server.obfs));
545557
if (server.server_udp_port == 0 || cfg.socks5RemotePort != 0)
@@ -668,9 +680,11 @@ public void Close()
668680
}
669681
closed = true;
670682
}
683+
Thread.Sleep(100);
671684
for (int i = 0; i < 10; ++i)
672685
{
673-
if (remoteRecvCount <= 0 && connectionRecvCount <= 0)
686+
if (//remoteRecvCount <= 0 &&
687+
connectionRecvCount <= 0)
674688
break;
675689
Thread.Sleep(10 * (i + 1) * (i + 1));
676690
}
@@ -1291,7 +1305,7 @@ private void UDPoverTCPConnectionSend(byte[] send_buffer, int bytesToSend)
12911305

12921306
private void PipeRemoteReceiveCallback(IAsyncResult ar)
12931307
{
1294-
Interlocked.Increment(ref remoteRecvCount);
1308+
//Interlocked.Increment(ref remoteRecvCount);
12951309
bool final_close = false;
12961310
try
12971311
{
@@ -1363,7 +1377,7 @@ private void PipeRemoteReceiveCallback(IAsyncResult ar)
13631377
}
13641378
finally
13651379
{
1366-
Interlocked.Decrement(ref remoteRecvCount);
1380+
//Interlocked.Decrement(ref remoteRecvCount);
13671381
if (final_close)
13681382
{
13691383
Close();
@@ -1375,7 +1389,7 @@ private void PipeRemoteReceiveCallback(IAsyncResult ar)
13751389
private void PipeRemoteUDPReceiveCallback(IAsyncResult ar)
13761390
{
13771391
bool final_close = false;
1378-
Interlocked.Decrement(ref remoteRecvCount);
1392+
//Interlocked.Decrement(ref remoteRecvCount);
13791393
try
13801394
{
13811395
if (closed)
@@ -1426,7 +1440,7 @@ private void PipeRemoteUDPReceiveCallback(IAsyncResult ar)
14261440
}
14271441
finally
14281442
{
1429-
Interlocked.Decrement(ref remoteRecvCount);
1443+
//Interlocked.Decrement(ref remoteRecvCount);
14301444
if (final_close)
14311445
{
14321446
Close();

shadowsocks-csharp/Controller/ProxySocket.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ProxySocket
2525

2626
protected IEncryptor _encryptor;
2727
protected object _encryptionLock = new object();
28+
protected string _password;
2829
//protected object _decryptionLock = new object();
2930
public IObfs _protocol;
3031
public IObfs _obfs;
@@ -82,13 +83,6 @@ public bool isProtocolSendback
8283
{
8384
get
8485
{
85-
//Dictionary<string, int> protocols = new Dictionary<string, int>();
86-
//protocols["auth_aes128_sha1"] = 1;
87-
//if (protocols.ContainsKey(_protocol.Name()))
88-
//{
89-
// return true;
90-
//}
91-
//return false;
9286
return _protocol.isAlwaysSendback();
9387
}
9488
}
@@ -151,9 +145,10 @@ public void EndConnect(IAsyncResult ar)
151145
_socket.EndConnect(ar);
152146
}
153147

154-
public void SetEncryptor(IEncryptor encryptor)
148+
public void CreateEncryptor(string method, string password)
155149
{
156-
_encryptor = encryptor;
150+
_encryptor = EncryptorFactory.GetEncryptor(method, password);
151+
_password = password;
157152
}
158153

159154
public void SetProtocol(IObfs protocol)
@@ -184,9 +179,9 @@ public void SetObfsPlugin(Server server, int head_len)
184179
if (_proxy_server != null)
185180
server_addr = _proxy_server;
186181
_protocol.SetServerInfo(new ServerInfo(server_addr, server.server_port, "", server.getProtocolData(),
187-
_encryptor.getIV(), _encryptor.getKey(), head_len, mss));
182+
_encryptor.getIV(), _password, _encryptor.getKey(), head_len, mss));
188183
_obfs.SetServerInfo(new ServerInfo(server_addr, server.server_port, server.obfsparam??"", server.getObfsData(),
189-
_encryptor.getIV(), _encryptor.getKey(), head_len, mss));
184+
_encryptor.getIV(), _password, _encryptor.getKey(), head_len, mss));
190185
}
191186

192187
public IAsyncResult BeginReceive(byte[] buffer, int size, SocketFlags flags, AsyncCallback callback, object state)

shadowsocks-csharp/Controller/UpdateChecker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class UpdateChecker
2121

2222
public const string Name = "ShadowsocksR";
2323
public const string Copyright = "Copyright © BreakWall 2016. Fork from Shadowsocks by clowwindy";
24-
public const string Version = "3.9.4";
24+
public const string Version = "3.9.5";
2525
public const string FullVersion = Version + "";
2626

2727
private static bool UseProxy = true;

shadowsocks-csharp/Encryption/IVEncryptor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outl
167167

168168
public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
169169
{
170-
if (_decryptIVReceived < ivLen)
170+
if (_decryptIVReceived <= ivLen)
171171
{
172172
int start_pos = ivLen;
173173
if (_decryptIVReceived + length < ivLen)
@@ -199,6 +199,7 @@ public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outl
199199

200200
if (outlength > 0)
201201
{
202+
_decryptIVReceived += outlength;
202203
lock (tempbuf)
203204
{
204205
// C# could be multi-threaded

shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public MbedTLSEncryptor(string method, string password)
3434
{ "camellia-128-cfb", new EncryptorInfo(16, 16, true, CIPHER_CAMELLIA, "CAMELLIA-128-CFB128") },
3535
{ "camellia-192-cfb", new EncryptorInfo(24, 16, true, CIPHER_CAMELLIA, "CAMELLIA-192-CFB128") },
3636
{ "camellia-256-cfb", new EncryptorInfo(32, 16, true, CIPHER_CAMELLIA, "CAMELLIA-256-CFB128") },
37-
{ "rc4", new EncryptorInfo(16, 16, false, CIPHER_RC4, "ARC4-128") },
37+
{ "rc4", new EncryptorInfo(16, 0, true, CIPHER_RC4, "ARC4-128") },
3838
{ "rc4-md5", new EncryptorInfo(16, 16, true, CIPHER_RC4, "ARC4-128") },
3939
{ "rc4-md5-6", new EncryptorInfo(16, 6, true, CIPHER_RC4, "ARC4-128") },
4040
};

shadowsocks-csharp/Obfs/Auth.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ public void PackData(byte[] data, int datalength, byte[] outdata, out int outlen
232232
Array.Copy(data, 0, outdata, rand_len + 2, datalength);
233233
outdata[0] = (byte)(outlength >> 8);
234234
outdata[1] = (byte)(outlength);
235+
{
236+
byte[] rnd_data = new byte[rand_len];
237+
random.NextBytes(rnd_data);
238+
rnd_data.CopyTo(outdata, 2);
239+
}
235240
if (rand_len < 128)
236241
{
237242
outdata[2] = (byte)(rand_len);
@@ -252,6 +257,11 @@ public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int ou
252257
int data_offset = rand_len + 4 + 2;
253258
outlength = data_offset + datalength + 12 + 10;
254259
AuthData authData = (AuthData)this.Server.data;
260+
{
261+
byte[] rnd_data = new byte[rand_len];
262+
random.NextBytes(rnd_data);
263+
rnd_data.CopyTo(outdata, data_offset - rand_len);
264+
}
255265
lock (authData)
256266
{
257267
if (authData.connectionID > 0xFF000000)
@@ -447,6 +457,11 @@ public void PackData(byte[] data, int datalength, byte[] outdata, out int outlen
447457
outdata[1] = (byte)(outlength);
448458
ulong crc32 = Util.CRC32.CalcCRC32(outdata, 2);
449459
BitConverter.GetBytes((ushort)crc32).CopyTo(outdata, 2);
460+
{
461+
byte[] rnd_data = new byte[rand_len];
462+
random.NextBytes(rnd_data);
463+
rnd_data.CopyTo(outdata, 4);
464+
}
450465
if (rand_len < 128)
451466
{
452467
outdata[4] = (byte)(rand_len);
@@ -467,6 +482,11 @@ public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int ou
467482
int data_offset = rand_len + 4 + 2;
468483
outlength = data_offset + datalength + 12 + 10;
469484
AuthData authData = (AuthData)this.Server.data;
485+
{
486+
byte[] rnd_data = new byte[rand_len];
487+
random.NextBytes(rnd_data);
488+
rnd_data.CopyTo(outdata, data_offset - rand_len);
489+
}
470490
lock (authData)
471491
{
472492
if (authData.connectionID > 0xFF000000)
@@ -677,6 +697,11 @@ public void PackData(byte[] data, int datalength, byte[] outdata, out int outlen
677697
outdata[1] = (byte)(outlength >> 8);
678698
ulong crc32 = Util.CRC32.CalcCRC32(outdata, 2);
679699
BitConverter.GetBytes((ushort)crc32).CopyTo(outdata, 2);
700+
{
701+
byte[] rnd_data = new byte[rand_len];
702+
random.NextBytes(rnd_data);
703+
rnd_data.CopyTo(outdata, 4);
704+
}
680705
if (rand_len < 128)
681706
{
682707
outdata[4] = (byte)(rand_len);
@@ -958,6 +983,11 @@ public void PackData(byte[] data, int datalength, byte[] outdata, out int outlen
958983
byte[] key = new byte[user_key.Length + 4];
959984
user_key.CopyTo(key, 0);
960985
BitConverter.GetBytes(pack_id).CopyTo(key, key.Length - 4);
986+
{
987+
byte[] rnd_data = new byte[rand_len];
988+
random.NextBytes(rnd_data);
989+
rnd_data.CopyTo(outdata, 4);
990+
}
961991

962992
{
963993
HMAC sha1 = CreateHMAC(key);
@@ -1045,12 +1075,12 @@ public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int ou
10451075
Array.Copy(sha1data, 0, encrypt, 20, 4);
10461076
}
10471077
{
1048-
byte[] rnd = new byte[3];
1078+
byte[] rnd = new byte[1];
10491079
random.NextBytes(rnd);
10501080
rnd.CopyTo(outdata, 0);
10511081
HMAC sha1 = CreateHMAC(key);
1052-
byte[] sha1data = sha1.ComputeHash(rnd, 0, 3);
1053-
Array.Copy(sha1data, 0, outdata, 3, 4);
1082+
byte[] sha1data = sha1.ComputeHash(rnd, 0, rnd.Length);
1083+
Array.Copy(sha1data, 0, outdata, rnd.Length, 7 - rnd.Length);
10541084
}
10551085
encrypt.CopyTo(outdata, 7);
10561086
Array.Copy(data, 0, outdata, data_offset, datalength);

shadowsocks-csharp/Obfs/IObfs.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ public class ServerInfo
1919
public int tcp_mss;
2020
public byte[] iv;
2121
public byte[] key;
22+
public string key_str;
2223
public int head_len;
2324

24-
public ServerInfo(string host, int port, string param, object data, byte[] iv, byte[] key, int head_len, int tcp_mss)
25+
public ServerInfo(string host, int port, string param, object data, byte[] iv, string key_str, byte[] key, int head_len, int tcp_mss)
2526
{
2627
this.host = host;
2728
this.port = port;
2829
this.param = param;
2930
this.data = data;
3031
this.iv = iv;
3132
this.key = key;
33+
this.key_str = key_str;
3234
this.head_len = head_len;
3335
this.tcp_mss = tcp_mss;
3436
}

shadowsocks-csharp/View/ConfigForm.Designer.cs

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)