|
|
@@ -9,96 +9,30 @@ using Shadowsocks.Model; |
|
|
|
namespace Shadowsocks.Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
class Local
|
|
|
|
class Local : Listener.Service
|
|
|
|
{
|
|
|
|
private Configuration _config;
|
|
|
|
private bool _shareOverLAN;
|
|
|
|
//private Encryptor encryptor;
|
|
|
|
Socket _listener;
|
|
|
|
public Local(Configuration config)
|
|
|
|
{
|
|
|
|
this._config = config;
|
|
|
|
_shareOverLAN = config.shareOverLan;
|
|
|
|
//this.encryptor = new Encryptor(config.method, config.password);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
public bool GoodForMe(byte[] firstPacket, int length)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Create a TCP/IP socket.
|
|
|
|
_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
_listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
|
|
|
IPEndPoint localEndPoint = null;
|
|
|
|
if (_shareOverLAN)
|
|
|
|
{
|
|
|
|
localEndPoint = new IPEndPoint(IPAddress.Any, _config.localPort);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
localEndPoint = new IPEndPoint(IPAddress.Loopback, _config.localPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bind the socket to the local endpoint and listen for incoming connections.
|
|
|
|
_listener.Bind(localEndPoint);
|
|
|
|
_listener.Listen(100);
|
|
|
|
|
|
|
|
|
|
|
|
// Start an asynchronous socket to listen for connections.
|
|
|
|
Console.WriteLine("Shadowsocks started");
|
|
|
|
_listener.BeginAccept(
|
|
|
|
new AsyncCallback(AcceptCallback),
|
|
|
|
_listener);
|
|
|
|
}
|
|
|
|
catch(SocketException)
|
|
|
|
{
|
|
|
|
_listener.Close();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
{
|
|
|
|
_listener.Close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AcceptCallback(IAsyncResult ar)
|
|
|
|
|
|
|
|
public void Handle(byte[] firstPacket, int length, Socket socket)
|
|
|
|
{
|
|
|
|
Socket listener = (Socket)ar.AsyncState;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Socket conn = listener.EndAccept(ar);
|
|
|
|
conn.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
|
|
|
|
|
|
|
|
Handler handler = new Handler();
|
|
|
|
handler.connection = conn;
|
|
|
|
Server server = _config.GetCurrentServer();
|
|
|
|
handler.encryptor = EncryptorFactory.GetEncryptor(server.method, server.password);
|
|
|
|
handler.server = server;
|
|
|
|
|
|
|
|
handler.Start();
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
//Console.WriteLine(e.Message);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
listener.BeginAccept(
|
|
|
|
new AsyncCallback(AcceptCallback),
|
|
|
|
listener);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
//Console.WriteLine(e.Message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
|
|
|
|
Handler handler = new Handler();
|
|
|
|
handler.connection = socket;
|
|
|
|
Server server = _config.GetCurrentServer();
|
|
|
|
handler.encryptor = EncryptorFactory.GetEncryptor(server.method, server.password);
|
|
|
|
handler.server = server;
|
|
|
|
|
|
|
|
handler.Start(firstPacket, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Handler
|
|
|
@@ -109,6 +43,9 @@ namespace Shadowsocks.Controller |
|
|
|
// Client socket.
|
|
|
|
public Socket remote;
|
|
|
|
public Socket connection;
|
|
|
|
|
|
|
|
private byte[] _firstPacket;
|
|
|
|
private int _firstPacketLength;
|
|
|
|
// Size of receive buffer.
|
|
|
|
public const int RecvSize = 16384;
|
|
|
|
public const int BufferSize = RecvSize + 32;
|
|
|
@@ -129,8 +66,10 @@ namespace Shadowsocks.Controller |
|
|
|
private object encryptionLock = new object();
|
|
|
|
private object decryptionLock = new object();
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
public void Start(byte[] firstPacket, int length)
|
|
|
|
{
|
|
|
|
this._firstPacket = firstPacket;
|
|
|
|
this._firstPacketLength = length;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// TODO async resolving
|
|
|
@@ -241,33 +180,15 @@ namespace Shadowsocks.Controller |
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
connection.BeginReceive(connetionRecvBuffer, 0, 256, 0,
|
|
|
|
new AsyncCallback(HandshakeReceiveCallback), null);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Logging.LogUsefulException(e);
|
|
|
|
this.Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void HandshakeReceiveCallback(IAsyncResult ar)
|
|
|
|
{
|
|
|
|
if (closed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int bytesRead = connection.EndReceive(ar);
|
|
|
|
int bytesRead = _firstPacketLength;
|
|
|
|
|
|
|
|
if (bytesRead > 1)
|
|
|
|
{
|
|
|
|
byte[] response = { 5, 0 };
|
|
|
|
if (connetionRecvBuffer[0] != 5)
|
|
|
|
if (_firstPacket[0] != 5)
|
|
|
|
{
|
|
|
|
// reject socks 4
|
|
|
|
response = new byte[]{ 0, 91 };
|
|
|
|
response = new byte[] { 0, 91 };
|
|
|
|
Console.WriteLine("socks 5 protocol error");
|
|
|
|
}
|
|
|
|
connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(HandshakeSendCallback), null);
|
|
|
|