share over LAN

This commit is contained in:
clowwindy
2014-11-09 13:31:33 +08:00
parent 492869fd5c
commit e4ff27a1fc
8 changed files with 122 additions and 67 deletions
+18 -26
View File
@@ -11,12 +11,14 @@ namespace Shadowsocks.Controller
class Local
{
private Server config;
private Server _server;
private bool _shareOverLAN;
//private Encryptor encryptor;
Socket _listener;
public Local(Server config)
public Local(Configuration config)
{
this.config = config;
this._server = config.GetCurrentServer();
_shareOverLAN = config.shareOverLan;
//this.encryptor = new Encryptor(config.method, config.password);
}
@@ -25,9 +27,17 @@ namespace Shadowsocks.Controller
try
{
// Create a TCP/IP socket.
_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(0, config.local_port);
_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, _server.local_port);
}
else
{
localEndPoint = new IPEndPoint(IPAddress.Loopback, _server.local_port);
}
// Bind the socket to the local endpoint and listen for incoming connections.
_listener.Bind(localEndPoint);
@@ -58,37 +68,19 @@ namespace Shadowsocks.Controller
{
try
{
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
//if (!listener.Connected)
//{
// return;
//}
Socket conn = listener.EndAccept(ar);
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
// Create the state object.
Handler handler = new Handler();
handler.connection = conn;
//if (encryptor.method == "table")
//{
// handler.encryptor = encryptor;
//}
//else
//{
// handler.encryptor = new Encryptor(config.method, config.password);
//}
handler.encryptor = EncryptorFactory.GetEncryptor(config.method, config.password);
handler.config = config;
handler.encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password);
handler.config = _server;
handler.Start();
//handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
// new AsyncCallback(ReadCallback), state);
}
catch
{
+42 -17
View File
@@ -1,4 +1,5 @@
using Shadowsocks.Properties;
using Shadowsocks.Model;
using Shadowsocks.Properties;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -12,28 +13,51 @@ namespace Shadowsocks.Controller
{
class PACServer
{
private static int PORT = 8090;
private static string PAC_FILE = "pac.txt";
Socket listener;
Socket _listener;
FileSystemWatcher watcher;
public event EventHandler PACFileChanged;
public void Start()
public void Start(Configuration configuration)
{
// Create a TCP/IP socket.
listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint localEndPoint = new IPEndPoint(0, 8090);
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 (configuration.shareOverLan)
{
localEndPoint = new IPEndPoint(IPAddress.Any, PORT);
}
else
{
localEndPoint = new IPEndPoint(IPAddress.Loopback, PORT);
}
// Bind the socket to the local endpoint and listen for incoming connections.
listener.Bind(localEndPoint);
listener.Listen(100);
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
// Bind the socket to the local endpoint and listen for incoming connections.
_listener.Bind(localEndPoint);
_listener.Listen(100);
_listener.BeginAccept(
new AsyncCallback(AcceptCallback),
_listener);
WatchPacFile();
WatchPacFile();
}
catch (SocketException)
{
_listener.Close();
throw;
}
}
public void Stop()
{
_listener.Close();
_listener = null;
}
public string TouchPACFile()
@@ -55,10 +79,10 @@ namespace Shadowsocks.Controller
try
{
Socket listener = (Socket)ar.AsyncState;
Socket conn = listener.EndAccept(ar);
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
Socket conn = listener.EndAccept(ar);
conn.BeginReceive(new byte[1024], 0, 1024, 0,
new AsyncCallback(ReceiveCallback), conn);
@@ -92,7 +116,6 @@ namespace Shadowsocks.Controller
return System.Text.Encoding.UTF8.GetString(buffer, 0, n);
}
}
WatchPacFile();
}
private void ReceiveCallback(IAsyncResult ar)
@@ -104,7 +127,9 @@ namespace Shadowsocks.Controller
string pac = GetPACContent();
string proxy = "PROXY 127.0.0.1:8123;";
IPEndPoint localEndPoint = (IPEndPoint)conn.LocalEndPoint;
string proxy = "PROXY " + localEndPoint.Address + ":8123;";
pac = pac.Replace("__PROXY__", proxy);
@@ -13,8 +13,9 @@ namespace Shadowsocks.Controller
{
private Process _process;
public void Start(Server config)
public void Start(Configuration configuration)
{
Server server = configuration.GetCurrentServer();
if (_process == null)
{
Process[] existingPolipo = Process.GetProcessesByName("ss_polipo");
@@ -31,8 +32,9 @@ namespace Shadowsocks.Controller
}
}
string temppath = Path.GetTempPath();
string polipoConfig = Resources.polipo_config;
polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", config.local_port.ToString());
string polipoConfig = Resources.polipo_config;
polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", server.local_port.ToString());
polipoConfig = polipoConfig.Replace("__POLIPO_BIND_IP__", configuration.shareOverLan ? "0.0.0.0" : "127.0.0.1");
FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig));
FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe);
@@ -45,7 +47,6 @@ namespace Shadowsocks.Controller
_process.StartInfo.CreateNoWindow = true;
_process.StartInfo.RedirectStandardOutput = true;
_process.StartInfo.RedirectStandardError = true;
//process.StandardOutput
_process.Start();
}
}
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Shadowsocks.Controller
{
@@ -25,6 +26,7 @@ namespace Shadowsocks.Controller
public event EventHandler ConfigChanged;
public event EventHandler EnableStatusChanged;
public event EventHandler ShareOverLANStatusChanged;
// when user clicked Edit PAC, and PAC file has already created
public event EventHandler<PathEventArgs> PACFileReadyToOpen;
@@ -33,14 +35,14 @@ namespace Shadowsocks.Controller
{
_config = Configuration.Load();
polipoRunner = new PolipoRunner();
polipoRunner.Start(_config.GetCurrentServer());
local = new Local(_config.GetCurrentServer());
polipoRunner.Start(_config);
local = new Local(_config);
try
{
local.Start();
pacServer = new PACServer();
pacServer.PACFileChanged += pacServer_PACFileChanged;
pacServer.Start();
pacServer.Start(_config);
}
catch (Exception e)
{
@@ -58,9 +60,9 @@ namespace Shadowsocks.Controller
local.Stop();
polipoRunner.Stop();
polipoRunner.Start(_config.GetCurrentServer());
polipoRunner.Start(_config);
local = new Local(_config.GetCurrentServer());
local = new Local(_config);
local.Start();
if (ConfigChanged != null)
@@ -92,6 +94,18 @@ namespace Shadowsocks.Controller
}
}
public void ToggleShareOverLAN(bool enabled)
{
_config.shareOverLan = enabled;
SaveConfig(_config);
pacServer.Stop();
pacServer.Start(_config);
if (ShareOverLANStatusChanged != null)
{
ShareOverLANStatusChanged(this, new EventArgs());
}
}
public void Stop()
{
if (stopped)