Skip to content

Commit

Permalink
add AEAD support
Browse files Browse the repository at this point in the history
  • Loading branch information
wongsyrone committed Mar 31, 2017
1 parent 317c64f commit 3de1409
Show file tree
Hide file tree
Showing 20 changed files with 1,354 additions and 589 deletions.
14 changes: 13 additions & 1 deletion shadowsocks-csharp/Controller/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Net.Sockets;
using System.Net;
using System.Diagnostics;

using System.Text;
using Shadowsocks.Util;

namespace Shadowsocks.Controller
Expand Down Expand Up @@ -70,6 +70,18 @@ public static void Debug(object o)
WriteToLogFile("[D] " + o);
}

[Conditional("DEBUG")]
public static void Dump(string tag, byte[] arr, int length)
{
var sb = new StringBuilder($"{Environment.NewLine}{tag}: ");
for (int i = 0; i < length - 1; i++) {
sb.Append($"0x{arr[i]:X2}, ");
}
sb.Append($"0x{arr[length - 1]:X2}");
sb.Append(Environment.NewLine);
Debug(sb.ToString());
}

[Conditional("DEBUG")]
public static void Debug(EndPoint local, EndPoint remote, int len, string header = null, string tailer = null)
{
Expand Down
331 changes: 206 additions & 125 deletions shadowsocks-csharp/Controller/Service/TCPRelay.cs

Large diffs are not rendered by default.

28 changes: 16 additions & 12 deletions shadowsocks-csharp/Controller/Service/UDPRelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;

using Shadowsocks.Controller.Strategy;
using Shadowsocks.Encryption;
using Shadowsocks.Model;
Expand All @@ -13,15 +12,16 @@ namespace Shadowsocks.Controller
class UDPRelay : Listener.Service
{
private ShadowsocksController _controller;
private LRUCache<IPEndPoint, UDPHandler> _cache;

// TODO: choose a smart number
private LRUCache<IPEndPoint, UDPHandler> _cache = new LRUCache<IPEndPoint, UDPHandler>(512);

public long outbound = 0;
public long inbound = 0;

public UDPRelay(ShadowsocksController controller)
{
this._controller = controller;
this._cache = new LRUCache<IPEndPoint, UDPHandler>(512); // todo: choose a smart number
}

public override bool Handle(byte[] firstPacket, int length, Socket socket, object state)
Expand Down Expand Up @@ -78,12 +78,12 @@ public UDPHandler(Socket local, Server server, IPEndPoint localEndPoint)

public void Send(byte[] data, int length)
{
IEncryptor encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password, _server.auth, true);
byte[] dataIn = new byte[length - 3 + IVEncryptor.ONETIMEAUTH_BYTES];
IEncryptor encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password);
byte[] dataIn = new byte[length - 3];
Array.Copy(data, 3, dataIn, 0, length - 3);
byte[] dataOut = new byte[length - 3 + 16 + IVEncryptor.ONETIMEAUTH_BYTES];
byte[] dataOut = new byte[length - 3 + 16];
int outlen;
encryptor.Encrypt(dataIn, length - 3, dataOut, out outlen);
encryptor.EncryptUDP(dataIn, length - 3, dataOut, out outlen);
Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay");
_remote?.SendTo(dataOut, outlen, SocketFlags.None, _remoteEndPoint);
}
Expand All @@ -106,14 +106,15 @@ public void RecvFromCallback(IAsyncResult ar)
byte[] dataOut = new byte[bytesRead];
int outlen;

IEncryptor encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password, _server.auth, true);
encryptor.Decrypt(_buffer, bytesRead, dataOut, out outlen);
IEncryptor encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password);
encryptor.DecryptUDP(_buffer, bytesRead, dataOut, out outlen);

byte[] sendBuf = new byte[outlen + 3];
Array.Copy(dataOut, 0, sendBuf, 3, outlen);

Logging.Debug(_localEndPoint, _remoteEndPoint, outlen, "UDP Relay");
_local?.SendTo(sendBuf, outlen + 3, 0, _localEndPoint);

Receive();
}
catch (ObjectDisposedException)
Expand All @@ -126,6 +127,8 @@ public void RecvFromCallback(IAsyncResult ar)
}
finally
{
// No matter success or failed, we keep receiving

}
}

Expand All @@ -143,13 +146,12 @@ public void Close()
{
// TODO: need more think about handle other Exceptions, or should remove this catch().
}
finally
{
}
}
}
}

#region LRU cache

// cc by-sa 3.0 http://stackoverflow.com/a/3719378/1124054
class LRUCache<K, V> where V : UDPRelay.UDPHandler
{
Expand Down Expand Up @@ -212,4 +214,6 @@ public LRUCacheItem(K k, V v)
public K key;
public V value;
}

#endregion
}
Loading

0 comments on commit 3de1409

Please sign in to comment.