move local to listener service
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
|
||||
public void Handle(byte[] firstPacket, int length, Socket socket)
|
||||
{
|
||||
_listener.Close();
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
public void AcceptCallback(IAsyncResult ar)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Shadowsocks.Controller
|
||||
|
||||
private Thread _ramThread;
|
||||
|
||||
private Local local;
|
||||
private Listener _listener;
|
||||
private PACServer pacServer;
|
||||
private Configuration _config;
|
||||
private PolipoRunner polipoRunner;
|
||||
@@ -77,6 +77,7 @@ namespace Shadowsocks.Controller
|
||||
public void SaveServers(List<Server> servers, int localPort)
|
||||
{
|
||||
_config.configs = servers;
|
||||
_config.localPort = localPort;
|
||||
SaveConfig(_config);
|
||||
}
|
||||
|
||||
@@ -142,9 +143,9 @@ namespace Shadowsocks.Controller
|
||||
return;
|
||||
}
|
||||
stopped = true;
|
||||
if (local != null)
|
||||
if (_listener != null)
|
||||
{
|
||||
local.Stop();
|
||||
_listener.Stop();
|
||||
}
|
||||
if (polipoRunner != null)
|
||||
{
|
||||
@@ -204,9 +205,9 @@ namespace Shadowsocks.Controller
|
||||
|
||||
pacServer.Stop();
|
||||
|
||||
if (local != null)
|
||||
if (_listener != null)
|
||||
{
|
||||
local.Stop();
|
||||
_listener.Stop();
|
||||
}
|
||||
|
||||
// don't put polipoRunner.Start() before pacServer.Stop()
|
||||
@@ -218,8 +219,11 @@ namespace Shadowsocks.Controller
|
||||
{
|
||||
polipoRunner.Start(_config);
|
||||
|
||||
local = new Local(_config);
|
||||
local.Start();
|
||||
Local local = new Local(_config);
|
||||
List<Listener.Service> services = new List<Listener.Service>();
|
||||
services.Add(local);
|
||||
_listener = new Listener(services);
|
||||
_listener.Start(_config);
|
||||
pacServer.Start(_config);
|
||||
}
|
||||
catch (Exception e)
|
||||
|
||||
@@ -127,6 +127,7 @@
|
||||
<Compile Include="Controller\FileManager.cs" />
|
||||
<Compile Include="Controller\GFWListUpdater.cs" />
|
||||
<Compile Include="Controller\I18N.cs" />
|
||||
<Compile Include="Controller\Listener.cs" />
|
||||
<Compile Include="Controller\Logging.cs" />
|
||||
<Compile Include="Controller\UpdateChecker.cs" />
|
||||
<Compile Include="Encryption\EncryptorBase.cs" />
|
||||
|
||||
Reference in New Issue
Block a user