Skip to content

Commit

Permalink
3.9.5
Browse files Browse the repository at this point in the history
fix rc4
fill with randBytes
add auth_aes128_md5 & auth_aes128_sha1
  • Loading branch information
breakwa11 committed Oct 12, 2016
1 parent 1e4b97d commit 4a070ab
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 44 deletions.
64 changes: 39 additions & 25 deletions shadowsocks-csharp/Controller/Local.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,15 @@ class Handler
protected bool is_obfs_sendback = false;

protected bool connectionTCPIdle, connectionUDPIdle, remoteTCPIdle, remoteUDPIdle;
protected int remoteRecvCount = 0;
//protected int remoteRecvCount = 0;
protected int connectionRecvCount = 0;

protected SpeedTester speedTester = new SpeedTester();
protected int lastErrCode;
protected Random random = new Random();
protected System.Timers.Timer timer;
protected object timerLock = new object();
protected DateTime lastTimerSetTime;

enum ConnectState
{
Expand Down Expand Up @@ -206,31 +207,42 @@ private void ResetTimeout(Double time)
return;

cfg.try_keep_alive = 0;
lock (timerLock)

if (time <= 0)
{
if (time <= 0)
if (timer != null)
{
if (timer != null)
lock (timerLock)
{
timer.Enabled = false;
timer.Elapsed -= timer_Elapsed;
timer.Dispose();
timer = null;
if (timer != null)
{
timer.Enabled = false;
timer.Elapsed -= timer_Elapsed;
timer.Dispose();
timer = null;
}
}
}
else
}
else
{
if (lastTimerSetTime != null && (DateTime.Now - lastTimerSetTime).TotalMilliseconds > 500)
{
if (timer == null)
lock (timerLock)
{
timer = new System.Timers.Timer(time * 1000.0);
timer.Elapsed += timer_Elapsed;
}
else
{
timer.Interval = time * 1000.0;
timer.Stop();
if (timer == null)
{
timer = new System.Timers.Timer(time * 1000.0);
timer.Elapsed += timer_Elapsed;
}
else
{
timer.Interval = time * 1000.0;
timer.Stop();
}
timer.Start();
lastTimerSetTime = DateTime.Now;
}
timer.Start();
}
}
}
Expand Down Expand Up @@ -521,7 +533,7 @@ private void BeginConnect(IPAddress ipAddress, int serverPort)
remote.GetSocket().SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
try
{
remote.SetEncryptor(EncryptorFactory.GetEncryptor(server.method, server.password));
remote.CreateEncryptor(server.method, server.password);
}
catch
{
Expand All @@ -539,7 +551,7 @@ private void BeginConnect(IPAddress ipAddress, int serverPort)
SocketType.Dgram, ProtocolType.Udp);
remoteUDP.GetSocket().Bind(new IPEndPoint(ipAddress.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddress.IPv6Any : IPAddress.Any, 0));

remoteUDP.SetEncryptor(EncryptorFactory.GetEncryptor(server.method, server.password));
remoteUDP.CreateEncryptor(server.method, server.password);
remoteUDP.SetProtocol(ObfsFactory.GetObfs(server.protocol));
remoteUDP.SetObfs(ObfsFactory.GetObfs(server.obfs));
if (server.server_udp_port == 0 || cfg.socks5RemotePort != 0)
Expand Down Expand Up @@ -668,9 +680,11 @@ public void Close()
}
closed = true;
}
Thread.Sleep(100);
for (int i = 0; i < 10; ++i)
{
if (remoteRecvCount <= 0 && connectionRecvCount <= 0)
if (//remoteRecvCount <= 0 &&
connectionRecvCount <= 0)
break;
Thread.Sleep(10 * (i + 1) * (i + 1));
}
Expand Down Expand Up @@ -1291,7 +1305,7 @@ private void UDPoverTCPConnectionSend(byte[] send_buffer, int bytesToSend)

private void PipeRemoteReceiveCallback(IAsyncResult ar)
{
Interlocked.Increment(ref remoteRecvCount);
//Interlocked.Increment(ref remoteRecvCount);
bool final_close = false;
try
{
Expand Down Expand Up @@ -1363,7 +1377,7 @@ private void PipeRemoteReceiveCallback(IAsyncResult ar)
}
finally
{
Interlocked.Decrement(ref remoteRecvCount);
//Interlocked.Decrement(ref remoteRecvCount);
if (final_close)
{
Close();
Expand All @@ -1375,7 +1389,7 @@ private void PipeRemoteReceiveCallback(IAsyncResult ar)
private void PipeRemoteUDPReceiveCallback(IAsyncResult ar)
{
bool final_close = false;
Interlocked.Decrement(ref remoteRecvCount);
//Interlocked.Decrement(ref remoteRecvCount);
try
{
if (closed)
Expand Down Expand Up @@ -1426,7 +1440,7 @@ private void PipeRemoteUDPReceiveCallback(IAsyncResult ar)
}
finally
{
Interlocked.Decrement(ref remoteRecvCount);
//Interlocked.Decrement(ref remoteRecvCount);
if (final_close)
{
Close();
Expand Down
17 changes: 6 additions & 11 deletions shadowsocks-csharp/Controller/ProxySocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ProxySocket

protected IEncryptor _encryptor;
protected object _encryptionLock = new object();
protected string _password;
//protected object _decryptionLock = new object();
public IObfs _protocol;
public IObfs _obfs;
Expand Down Expand Up @@ -82,13 +83,6 @@ public bool isProtocolSendback
{
get
{
//Dictionary<string, int> protocols = new Dictionary<string, int>();
//protocols["auth_aes128_sha1"] = 1;
//if (protocols.ContainsKey(_protocol.Name()))
//{
// return true;
//}
//return false;
return _protocol.isAlwaysSendback();
}
}
Expand Down Expand Up @@ -151,9 +145,10 @@ public void EndConnect(IAsyncResult ar)
_socket.EndConnect(ar);
}

public void SetEncryptor(IEncryptor encryptor)
public void CreateEncryptor(string method, string password)
{
_encryptor = encryptor;
_encryptor = EncryptorFactory.GetEncryptor(method, password);
_password = password;
}

public void SetProtocol(IObfs protocol)
Expand Down Expand Up @@ -184,9 +179,9 @@ public void SetObfsPlugin(Server server, int head_len)
if (_proxy_server != null)
server_addr = _proxy_server;
_protocol.SetServerInfo(new ServerInfo(server_addr, server.server_port, "", server.getProtocolData(),
_encryptor.getIV(), _encryptor.getKey(), head_len, mss));
_encryptor.getIV(), _password, _encryptor.getKey(), head_len, mss));
_obfs.SetServerInfo(new ServerInfo(server_addr, server.server_port, server.obfsparam??"", server.getObfsData(),
_encryptor.getIV(), _encryptor.getKey(), head_len, mss));
_encryptor.getIV(), _password, _encryptor.getKey(), head_len, mss));
}

public IAsyncResult BeginReceive(byte[] buffer, int size, SocketFlags flags, AsyncCallback callback, object state)
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks-csharp/Controller/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class UpdateChecker

public const string Name = "ShadowsocksR";
public const string Copyright = "Copyright © BreakWall 2016. Fork from Shadowsocks by clowwindy";
public const string Version = "3.9.4";
public const string Version = "3.9.5";
public const string FullVersion = Version + "";

private static bool UseProxy = true;
Expand Down
3 changes: 2 additions & 1 deletion shadowsocks-csharp/Encryption/IVEncryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outl

public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outlength)
{
if (_decryptIVReceived < ivLen)
if (_decryptIVReceived <= ivLen)
{
int start_pos = ivLen;
if (_decryptIVReceived + length < ivLen)
Expand Down Expand Up @@ -199,6 +199,7 @@ public override void Decrypt(byte[] buf, int length, byte[] outbuf, out int outl

if (outlength > 0)
{
_decryptIVReceived += outlength;
lock (tempbuf)
{
// C# could be multi-threaded
Expand Down
2 changes: 1 addition & 1 deletion shadowsocks-csharp/Encryption/MbedTLSEncryptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public MbedTLSEncryptor(string method, string password)
{ "camellia-128-cfb", new EncryptorInfo(16, 16, true, CIPHER_CAMELLIA, "CAMELLIA-128-CFB128") },
{ "camellia-192-cfb", new EncryptorInfo(24, 16, true, CIPHER_CAMELLIA, "CAMELLIA-192-CFB128") },
{ "camellia-256-cfb", new EncryptorInfo(32, 16, true, CIPHER_CAMELLIA, "CAMELLIA-256-CFB128") },
{ "rc4", new EncryptorInfo(16, 16, false, CIPHER_RC4, "ARC4-128") },
{ "rc4", new EncryptorInfo(16, 0, true, CIPHER_RC4, "ARC4-128") },
{ "rc4-md5", new EncryptorInfo(16, 16, true, CIPHER_RC4, "ARC4-128") },
{ "rc4-md5-6", new EncryptorInfo(16, 6, true, CIPHER_RC4, "ARC4-128") },
};
Expand Down
36 changes: 33 additions & 3 deletions shadowsocks-csharp/Obfs/Auth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public void PackData(byte[] data, int datalength, byte[] outdata, out int outlen
Array.Copy(data, 0, outdata, rand_len + 2, datalength);
outdata[0] = (byte)(outlength >> 8);
outdata[1] = (byte)(outlength);
{
byte[] rnd_data = new byte[rand_len];
random.NextBytes(rnd_data);
rnd_data.CopyTo(outdata, 2);
}
if (rand_len < 128)
{
outdata[2] = (byte)(rand_len);
Expand All @@ -252,6 +257,11 @@ public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int ou
int data_offset = rand_len + 4 + 2;
outlength = data_offset + datalength + 12 + 10;
AuthData authData = (AuthData)this.Server.data;
{
byte[] rnd_data = new byte[rand_len];
random.NextBytes(rnd_data);
rnd_data.CopyTo(outdata, data_offset - rand_len);
}
lock (authData)
{
if (authData.connectionID > 0xFF000000)
Expand Down Expand Up @@ -447,6 +457,11 @@ public void PackData(byte[] data, int datalength, byte[] outdata, out int outlen
outdata[1] = (byte)(outlength);
ulong crc32 = Util.CRC32.CalcCRC32(outdata, 2);
BitConverter.GetBytes((ushort)crc32).CopyTo(outdata, 2);
{
byte[] rnd_data = new byte[rand_len];
random.NextBytes(rnd_data);
rnd_data.CopyTo(outdata, 4);
}
if (rand_len < 128)
{
outdata[4] = (byte)(rand_len);
Expand All @@ -467,6 +482,11 @@ public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int ou
int data_offset = rand_len + 4 + 2;
outlength = data_offset + datalength + 12 + 10;
AuthData authData = (AuthData)this.Server.data;
{
byte[] rnd_data = new byte[rand_len];
random.NextBytes(rnd_data);
rnd_data.CopyTo(outdata, data_offset - rand_len);
}
lock (authData)
{
if (authData.connectionID > 0xFF000000)
Expand Down Expand Up @@ -677,6 +697,11 @@ public void PackData(byte[] data, int datalength, byte[] outdata, out int outlen
outdata[1] = (byte)(outlength >> 8);
ulong crc32 = Util.CRC32.CalcCRC32(outdata, 2);
BitConverter.GetBytes((ushort)crc32).CopyTo(outdata, 2);
{
byte[] rnd_data = new byte[rand_len];
random.NextBytes(rnd_data);
rnd_data.CopyTo(outdata, 4);
}
if (rand_len < 128)
{
outdata[4] = (byte)(rand_len);
Expand Down Expand Up @@ -958,6 +983,11 @@ public void PackData(byte[] data, int datalength, byte[] outdata, out int outlen
byte[] key = new byte[user_key.Length + 4];
user_key.CopyTo(key, 0);
BitConverter.GetBytes(pack_id).CopyTo(key, key.Length - 4);
{
byte[] rnd_data = new byte[rand_len];
random.NextBytes(rnd_data);
rnd_data.CopyTo(outdata, 4);
}

{
HMAC sha1 = CreateHMAC(key);
Expand Down Expand Up @@ -1045,12 +1075,12 @@ public void PackAuthData(byte[] data, int datalength, byte[] outdata, out int ou
Array.Copy(sha1data, 0, encrypt, 20, 4);
}
{
byte[] rnd = new byte[3];
byte[] rnd = new byte[1];
random.NextBytes(rnd);
rnd.CopyTo(outdata, 0);
HMAC sha1 = CreateHMAC(key);
byte[] sha1data = sha1.ComputeHash(rnd, 0, 3);
Array.Copy(sha1data, 0, outdata, 3, 4);
byte[] sha1data = sha1.ComputeHash(rnd, 0, rnd.Length);
Array.Copy(sha1data, 0, outdata, rnd.Length, 7 - rnd.Length);
}
encrypt.CopyTo(outdata, 7);
Array.Copy(data, 0, outdata, data_offset, datalength);
Expand Down
4 changes: 3 additions & 1 deletion shadowsocks-csharp/Obfs/IObfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ public class ServerInfo
public int tcp_mss;
public byte[] iv;
public byte[] key;
public string key_str;
public int head_len;

public ServerInfo(string host, int port, string param, object data, byte[] iv, byte[] key, int head_len, int tcp_mss)
public ServerInfo(string host, int port, string param, object data, byte[] iv, string key_str, byte[] key, int head_len, int tcp_mss)
{
this.host = host;
this.port = port;
this.param = param;
this.data = data;
this.iv = iv;
this.key = key;
this.key_str = key_str;
this.head_len = head_len;
this.tcp_mss = tcp_mss;
}
Expand Down
3 changes: 2 additions & 1 deletion shadowsocks-csharp/View/ConfigForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4a070ab

Please sign in to comment.