@@ -22,22 +22,29 @@ namespace shadowsocks_csharp.Controller | |||||
public void Start() | public void Start() | ||||
{ | { | ||||
try | |||||
{ | |||||
// Create a TCP/IP socket. | |||||
listener = new Socket(AddressFamily.InterNetwork, | |||||
SocketType.Stream, ProtocolType.Tcp); | |||||
IPEndPoint localEndPoint = new IPEndPoint(0, config.local_port); | |||||
// Create a TCP/IP socket. | |||||
listener = new Socket(AddressFamily.InterNetwork, | |||||
SocketType.Stream, ProtocolType.Tcp); | |||||
IPEndPoint localEndPoint = new IPEndPoint(0, config.local_port); | |||||
// Bind the socket to the local endpoint and listen for incoming connections. | |||||
listener.Bind(localEndPoint); | |||||
listener.Listen(100); | |||||
// 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); | |||||
// Start an asynchronous socket to listen for connections. | |||||
Console.WriteLine("Shadowsocks started"); | |||||
listener.BeginAccept( | |||||
new AsyncCallback(AcceptCallback), | |||||
listener); | |||||
} | |||||
catch(SocketException) | |||||
{ | |||||
listener.Close(); | |||||
throw; | |||||
} | |||||
} | } | ||||
@@ -11,6 +11,8 @@ namespace shadowsocks_csharp.Controller | |||||
{ | { | ||||
class PACServer | class PACServer | ||||
{ | { | ||||
private static string PAC_FILE = "pac.txt"; | |||||
Socket listener; | Socket listener; | ||||
public void Start() | public void Start() | ||||
{ | { | ||||
@@ -28,6 +30,19 @@ namespace shadowsocks_csharp.Controller | |||||
listener); | listener); | ||||
} | } | ||||
public string TouchPACFile() | |||||
{ | |||||
if (File.Exists(PAC_FILE)) | |||||
{ | |||||
return PAC_FILE; | |||||
} | |||||
else | |||||
{ | |||||
FileManager.UncompressFile(PAC_FILE, Resources.proxy_pac_txt); | |||||
return PAC_FILE; | |||||
} | |||||
} | |||||
public void AcceptCallback(IAsyncResult ar) | public void AcceptCallback(IAsyncResult ar) | ||||
{ | { | ||||
@@ -51,21 +66,29 @@ namespace shadowsocks_csharp.Controller | |||||
private string getPACContent() | private string getPACContent() | ||||
{ | { | ||||
// TODO try pac.txt in current directory | // TODO try pac.txt in current directory | ||||
byte[] pacGZ = Resources.proxy_pac_txt; | |||||
byte[] buffer = new byte[1024 * 1024]; // builtin pac gzip size: maximum 1M | |||||
int n; | |||||
using (GZipStream input = new GZipStream(new MemoryStream(pacGZ), | |||||
CompressionMode.Decompress, false)) | |||||
if (File.Exists(PAC_FILE)) | |||||
{ | { | ||||
n = input.Read(buffer, 0, buffer.Length); | |||||
if (n == 0) | |||||
return File.ReadAllText(PAC_FILE, Encoding.UTF8); | |||||
} | |||||
else | |||||
{ | |||||
byte[] pacGZ = Resources.proxy_pac_txt; | |||||
byte[] buffer = new byte[1024 * 1024]; // builtin pac gzip size: maximum 1M | |||||
int n; | |||||
using (GZipStream input = new GZipStream(new MemoryStream(pacGZ), | |||||
CompressionMode.Decompress, false)) | |||||
{ | { | ||||
throw new IOException("can not decompress pac"); | |||||
n = input.Read(buffer, 0, buffer.Length); | |||||
if (n == 0) | |||||
{ | |||||
throw new IOException("can not decompress pac"); | |||||
} | |||||
return System.Text.Encoding.UTF8.GetString(buffer, 0, n); | |||||
} | } | ||||
return System.Text.Encoding.UTF8.GetString(buffer, 0, n); | |||||
} | } | ||||
} | } | ||||
private void receiveCallback(IAsyncResult ar) | private void receiveCallback(IAsyncResult ar) | ||||
@@ -23,8 +23,15 @@ namespace shadowsocks_csharp.Controller | |||||
public string Path; | public string Path; | ||||
} | } | ||||
public class ErrorEventArgs : EventArgs | |||||
{ | |||||
public string Error; | |||||
} | |||||
public event EventHandler ConfigChanged; | public event EventHandler ConfigChanged; | ||||
public event EventHandler EnableStatusChanged; | public event EventHandler EnableStatusChanged; | ||||
public event EventHandler<ErrorEventArgs> LocalFailToStart; | |||||
// when user clicked Edit PAC, and PAC file has already created | // when user clicked Edit PAC, and PAC file has already created | ||||
public event EventHandler<PathEventArgs> PACFileReadyToOpen; | public event EventHandler<PathEventArgs> PACFileReadyToOpen; | ||||
@@ -35,9 +42,16 @@ namespace shadowsocks_csharp.Controller | |||||
polipoRunner = new PolipoRunner(); | polipoRunner = new PolipoRunner(); | ||||
polipoRunner.Start(config); | polipoRunner.Start(config); | ||||
local = new Local(config); | local = new Local(config); | ||||
local.Start(); | |||||
pacServer = new PACServer(); | |||||
pacServer.Start(); | |||||
try | |||||
{ | |||||
local.Start(); | |||||
pacServer = new PACServer(); | |||||
pacServer.Start(); | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
Console.WriteLine(e.ToString()); | |||||
} | |||||
updateSystemProxy(); | updateSystemProxy(); | ||||
} | } | ||||
@@ -93,6 +107,11 @@ namespace shadowsocks_csharp.Controller | |||||
public void TouchPACFile() | public void TouchPACFile() | ||||
{ | { | ||||
string pacFilename = pacServer.TouchPACFile(); | |||||
if (PACFileReadyToOpen != null) | |||||
{ | |||||
PACFileReadyToOpen(this, new PathEventArgs() { Path = pacFilename }); | |||||
} | |||||
} | } | ||||
private void updateSystemProxy() | private void updateSystemProxy() | ||||
@@ -271,7 +271,7 @@ namespace shadowsocks_csharp.Encrypt | |||||
PolarSSL.blowfish_free(_encryptCtx); | PolarSSL.blowfish_free(_encryptCtx); | ||||
break; | break; | ||||
case CIPHER_RC4: | case CIPHER_RC4: | ||||
PolarSSL.arc4_free(_encryptCtx); | |||||
//PolarSSL.arc4_free(_encryptCtx); | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
@@ -286,7 +286,7 @@ namespace shadowsocks_csharp.Encrypt | |||||
PolarSSL.blowfish_free(_decryptCtx); | PolarSSL.blowfish_free(_decryptCtx); | ||||
break; | break; | ||||
case CIPHER_RC4: | case CIPHER_RC4: | ||||
PolarSSL.arc4_free(_decryptCtx); | |||||
//PolarSSL.arc4_free(_decryptCtx); | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
@@ -44,7 +44,10 @@ namespace shadowsocks_csharp.Model | |||||
} | } | ||||
catch (Exception e) | catch (Exception e) | ||||
{ | { | ||||
Console.WriteLine(e); | |||||
if (!(e is FileNotFoundException)) | |||||
{ | |||||
Console.WriteLine(e); | |||||
} | |||||
return new Config | return new Config | ||||
{ | { | ||||
server = "127.0.0.1", | server = "127.0.0.1", | ||||
@@ -20,13 +20,20 @@ namespace shadowsocks_csharp | |||||
[STAThread] | [STAThread] | ||||
static void Main() | static void Main() | ||||
{ | { | ||||
string tempPath = Path.GetTempPath(); | |||||
string dllPath = tempPath + "/polarssl.dll"; | |||||
try | try | ||||
{ | { | ||||
string tempPath = Path.GetTempPath(); | |||||
string dllPath = tempPath + "/polarssl.dll"; | |||||
FileManager.UncompressFile(dllPath, Resources.polarssl_dll); | FileManager.UncompressFile(dllPath, Resources.polarssl_dll); | ||||
LoadLibrary(dllPath); | |||||
} | |||||
catch (IOException e) | |||||
{ | |||||
Console.WriteLine(e.ToString()); | |||||
} | |||||
LoadLibrary(dllPath); | |||||
try | |||||
{ | |||||
FileStream fs = new FileStream("shadowsocks.log", FileMode.Append); | FileStream fs = new FileStream("shadowsocks.log", FileMode.Append); | ||||
TextWriter tmp = Console.Out; | TextWriter tmp = Console.Out; | ||||
StreamWriter sw = new StreamWriter(fs); | StreamWriter sw = new StreamWriter(fs); | ||||
@@ -52,6 +52,7 @@ | |||||
this.aboutItem = new System.Windows.Forms.MenuItem(); | this.aboutItem = new System.Windows.Forms.MenuItem(); | ||||
this.menuItem3 = new System.Windows.Forms.MenuItem(); | this.menuItem3 = new System.Windows.Forms.MenuItem(); | ||||
this.quitItem = new System.Windows.Forms.MenuItem(); | this.quitItem = new System.Windows.Forms.MenuItem(); | ||||
this.editPACFileItem = new System.Windows.Forms.MenuItem(); | |||||
this.tableLayoutPanel1.SuspendLayout(); | this.tableLayoutPanel1.SuspendLayout(); | ||||
this.panel1.SuspendLayout(); | this.panel1.SuspendLayout(); | ||||
this.SuspendLayout(); | this.SuspendLayout(); | ||||
@@ -249,6 +250,7 @@ | |||||
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { | this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { | ||||
this.enableItem, | this.enableItem, | ||||
this.configItem, | this.configItem, | ||||
this.editPACFileItem, | |||||
this.aboutItem, | this.aboutItem, | ||||
this.menuItem3, | this.menuItem3, | ||||
this.quitItem}); | this.quitItem}); | ||||
@@ -267,21 +269,27 @@ | |||||
// | // | ||||
// aboutItem | // aboutItem | ||||
// | // | ||||
this.aboutItem.Index = 2; | |||||
this.aboutItem.Index = 3; | |||||
this.aboutItem.Text = "About"; | this.aboutItem.Text = "About"; | ||||
this.aboutItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); | this.aboutItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); | ||||
// | // | ||||
// menuItem3 | // menuItem3 | ||||
// | // | ||||
this.menuItem3.Index = 3; | |||||
this.menuItem3.Index = 4; | |||||
this.menuItem3.Text = "-"; | this.menuItem3.Text = "-"; | ||||
// | // | ||||
// quitItem | // quitItem | ||||
// | // | ||||
this.quitItem.Index = 4; | |||||
this.quitItem.Index = 5; | |||||
this.quitItem.Text = "Quit"; | this.quitItem.Text = "Quit"; | ||||
this.quitItem.Click += new System.EventHandler(this.Quit_Click); | this.quitItem.Click += new System.EventHandler(this.Quit_Click); | ||||
// | // | ||||
// editPACFileItem | |||||
// | |||||
this.editPACFileItem.Index = 2; | |||||
this.editPACFileItem.Text = "Edit PAC File..."; | |||||
this.editPACFileItem.Click += new System.EventHandler(this.editPACFileItem_Click); | |||||
// | |||||
// ConfigForm | // ConfigForm | ||||
// | // | ||||
this.AcceptButton = this.button1; | this.AcceptButton = this.button1; | ||||
@@ -332,6 +340,7 @@ | |||||
private System.Windows.Forms.MenuItem menuItem3; | private System.Windows.Forms.MenuItem menuItem3; | ||||
private System.Windows.Forms.MenuItem quitItem; | private System.Windows.Forms.MenuItem quitItem; | ||||
private System.Windows.Forms.MenuItem configItem; | private System.Windows.Forms.MenuItem configItem; | ||||
private System.Windows.Forms.MenuItem editPACFileItem; | |||||
} | } | ||||
} | } | ||||
@@ -22,6 +22,7 @@ namespace shadowsocks_csharp.View | |||||
this.controller = controller; | this.controller = controller; | ||||
controller.EnableStatusChanged += controller_EnableStatusChanged; | controller.EnableStatusChanged += controller_EnableStatusChanged; | ||||
controller.ConfigChanged += controller_ConfigChanged; | controller.ConfigChanged += controller_ConfigChanged; | ||||
controller.PACFileReadyToOpen += controller_PACFileReadyToOpen; | |||||
updateUI(); | updateUI(); | ||||
} | } | ||||
@@ -35,6 +36,14 @@ namespace shadowsocks_csharp.View | |||||
{ | { | ||||
updateUI(); | updateUI(); | ||||
} | } | ||||
void controller_PACFileReadyToOpen(object sender, ShadowsocksController.PathEventArgs e) | |||||
{ | |||||
string argument = @"/select, " + e.Path; | |||||
System.Diagnostics.Process.Start("explorer.exe", argument); | |||||
} | |||||
private void showWindow() | private void showWindow() | ||||
{ | { | ||||
@@ -131,5 +140,10 @@ namespace shadowsocks_csharp.View | |||||
controller.ToggleEnable(enableItem.Checked); | controller.ToggleEnable(enableItem.Checked); | ||||
} | } | ||||
private void editPACFileItem_Click(object sender, EventArgs e) | |||||
{ | |||||
controller.TouchPACFile(); | |||||
} | |||||
} | } | ||||
} | } |