@@ -0,0 +1,55 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using System.IO.Compression; | |||
using System.Text; | |||
namespace shadowsocks_csharp.Controller | |||
{ | |||
public class FileManager | |||
{ | |||
public static bool ByteArrayToFile(string fileName, byte[] content) | |||
{ | |||
try | |||
{ | |||
System.IO.FileStream _FileStream = | |||
new System.IO.FileStream(fileName, System.IO.FileMode.Create, | |||
System.IO.FileAccess.Write); | |||
_FileStream.Write(content, 0, content.Length); | |||
_FileStream.Close(); | |||
return true; | |||
} | |||
catch (Exception _Exception) | |||
{ | |||
Console.WriteLine("Exception caught in process: {0}", | |||
_Exception.ToString()); | |||
} | |||
return false; | |||
} | |||
public static void UncompressFile(string fileName, byte[] content) | |||
{ | |||
FileStream destinationFile = File.Create(fileName); | |||
// Because the uncompressed size of the file is unknown, | |||
// we are using an arbitrary buffer size. | |||
byte[] buffer = new byte[4096]; | |||
int n; | |||
using (GZipStream input = new GZipStream(new MemoryStream(content), | |||
CompressionMode.Decompress, false)) | |||
{ | |||
while (true) | |||
{ | |||
n = input.Read(buffer, 0, buffer.Length); | |||
if (n == 0) | |||
{ | |||
break; | |||
} | |||
destinationFile.Write(buffer, 0, n); | |||
} | |||
} | |||
destinationFile.Close(); | |||
} | |||
} | |||
} |
@@ -4,11 +4,11 @@ using System.Text; | |||
using System.Net.Sockets; | |||
using System.Net; | |||
using shadowsocks_csharp.Encrypt; | |||
using shadowsocks_csharp.Model; | |||
namespace shadowsocks_csharp | |||
namespace shadowsocks_csharp.Controller | |||
{ | |||
class Local | |||
{ | |||
private Config config; | |||
@@ -7,7 +7,7 @@ using System.Net; | |||
using System.Net.Sockets; | |||
using System.Text; | |||
namespace shadowsocks_csharp | |||
namespace shadowsocks_csharp.Controller | |||
{ | |||
class PACServer | |||
{ | |||
@@ -1,4 +1,5 @@ | |||
using shadowsocks_csharp.Properties; | |||
using shadowsocks_csharp.Model; | |||
using shadowsocks_csharp.Properties; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Diagnostics; | |||
@@ -6,54 +7,11 @@ using System.IO; | |||
using System.IO.Compression; | |||
using System.Text; | |||
namespace shadowsocks_csharp | |||
namespace shadowsocks_csharp.Controller | |||
{ | |||
class PolipoRunner | |||
{ | |||
private Process process; | |||
private bool ByteArrayToFile(string fileName, byte[] content) | |||
{ | |||
try | |||
{ | |||
System.IO.FileStream _FileStream = | |||
new System.IO.FileStream(fileName, System.IO.FileMode.Create, | |||
System.IO.FileAccess.Write); | |||
_FileStream.Write(content, 0, content.Length); | |||
_FileStream.Close(); | |||
return true; | |||
} | |||
catch (Exception _Exception) | |||
{ | |||
Console.WriteLine("Exception caught in process: {0}", | |||
_Exception.ToString()); | |||
} | |||
return false; | |||
} | |||
public static void UncompressFile(string fileName, byte[] content) | |||
{ | |||
FileStream destinationFile = File.Create(fileName); | |||
// Because the uncompressed size of the file is unknown, | |||
// we are using an arbitrary buffer size. | |||
byte[] buffer = new byte[4096]; | |||
int n; | |||
using (GZipStream input = new GZipStream(new MemoryStream(content), | |||
CompressionMode.Decompress, false)) | |||
{ | |||
while (true) | |||
{ | |||
n = input.Read(buffer, 0, buffer.Length); | |||
if (n == 0) | |||
{ | |||
break; | |||
} | |||
destinationFile.Write(buffer, 0, n); | |||
} | |||
} | |||
destinationFile.Close(); | |||
} | |||
public void Start(Config config) | |||
{ | |||
@@ -68,8 +26,8 @@ namespace shadowsocks_csharp | |||
string temppath = Path.GetTempPath(); | |||
string polipoConfig = Resources.polipo_config; | |||
polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", config.local_port.ToString()); | |||
ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); | |||
UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe); | |||
FileManager.ByteArrayToFile(temppath + "/polipo.conf", System.Text.Encoding.UTF8.GetBytes(polipoConfig)); | |||
FileManager.UncompressFile(temppath + "/ss_polipo.exe", Resources.polipo_exe); | |||
process = new Process(); | |||
// Configure the process using the StartInfo properties. | |||
@@ -1,8 +1,9 @@ | |||
using System; | |||
using shadowsocks_csharp.Model; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace shadowsocks_csharp | |||
namespace shadowsocks_csharp.Controller | |||
{ | |||
public class ShadowsocksController | |||
{ | |||
@@ -17,8 +18,16 @@ namespace shadowsocks_csharp | |||
private PolipoRunner polipoRunner; | |||
private bool stopped = false; | |||
public class PathEventArgs : EventArgs | |||
{ | |||
public string Path; | |||
} | |||
public event EventHandler ConfigChanged; | |||
public event EventHandler EnableStatusChanged; | |||
// when user clicked Edit PAC, and PAC file has already created | |||
public event EventHandler<PathEventArgs> PACFileReadyToOpen; | |||
public ShadowsocksController() | |||
{ | |||
@@ -37,6 +46,14 @@ namespace shadowsocks_csharp | |||
{ | |||
Config.Save(newConfig); | |||
config = newConfig; | |||
local.Stop(); | |||
polipoRunner.Stop(); | |||
polipoRunner.Start(config); | |||
local = new Local(config); | |||
local.Start(); | |||
if (ConfigChanged != null) | |||
{ | |||
ConfigChanged(this, new EventArgs()); | |||
@@ -74,6 +91,10 @@ namespace shadowsocks_csharp | |||
} | |||
} | |||
public void TouchPACFile() | |||
{ | |||
} | |||
private void updateSystemProxy() | |||
{ | |||
if (config.enabled) | |||
@@ -4,9 +4,9 @@ using System.Collections.Generic; | |||
using System.Runtime.InteropServices; | |||
using System.Text; | |||
namespace shadowsocks_csharp | |||
namespace shadowsocks_csharp.Controller | |||
{ | |||
class SystemProxy | |||
public class SystemProxy | |||
{ | |||
[DllImport("wininet.dll")] | |||
@@ -5,7 +5,7 @@ using System.IO; | |||
using System.Diagnostics; | |||
using SimpleJson; | |||
namespace shadowsocks_csharp | |||
namespace shadowsocks_csharp.Model | |||
{ | |||
[Serializable] | |||
public class Config | |||
@@ -1,4 +1,6 @@ | |||
using shadowsocks_csharp.Properties; | |||
using shadowsocks_csharp.Controller; | |||
using shadowsocks_csharp.Properties; | |||
using shadowsocks_csharp.View; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
@@ -22,7 +24,8 @@ namespace shadowsocks_csharp | |||
{ | |||
string tempPath = Path.GetTempPath(); | |||
string dllPath = tempPath + "/polarssl.dll"; | |||
PolipoRunner.UncompressFile(dllPath, Resources.polarssl_dll); | |||
// TODO: PolipoRunner should not do this job | |||
FileManager.UncompressFile(dllPath, Resources.polarssl_dll); | |||
LoadLibrary(dllPath); | |||
FileStream fs = new FileStream("shadowsocks.log", FileMode.Append); | |||
@@ -1,4 +1,4 @@ | |||
namespace shadowsocks_csharp | |||
namespace shadowsocks_csharp.View | |||
{ | |||
partial class ConfigForm | |||
{ | |||
@@ -5,8 +5,10 @@ using System.Drawing; | |||
using System.Text; | |||
using System.Windows.Forms; | |||
using System.Diagnostics; | |||
using shadowsocks_csharp.Controller; | |||
using shadowsocks_csharp.Model; | |||
namespace shadowsocks_csharp | |||
namespace shadowsocks_csharp.View | |||
{ | |||
public partial class ConfigForm : Form | |||
{ | |||
@@ -81,6 +81,7 @@ | |||
<Reference Include="System.Windows.Forms" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Compile Include="Controller\FileManager.cs" /> | |||
<Compile Include="Encrypt\EncryptorBase.cs" /> | |||
<Compile Include="Encrypt\EncryptorFactory.cs" /> | |||
<Compile Include="Encrypt\PolarSSL.cs" /> | |||