@@ -11,10 +11,10 @@ namespace shadowsocks_csharp.Controller | |||||
class Local | class Local | ||||
{ | { | ||||
private Config config; | |||||
private Server config; | |||||
//private Encryptor encryptor; | //private Encryptor encryptor; | ||||
Socket listener; | Socket listener; | ||||
public Local(Config config) | |||||
public Local(Server config) | |||||
{ | { | ||||
this.config = config; | this.config = config; | ||||
//this.encryptor = new Encryptor(config.method, config.password); | //this.encryptor = new Encryptor(config.method, config.password); | ||||
@@ -102,7 +102,7 @@ namespace shadowsocks_csharp.Controller | |||||
{ | { | ||||
//public Encryptor encryptor; | //public Encryptor encryptor; | ||||
public IEncryptor encryptor; | public IEncryptor encryptor; | ||||
public Config config; | |||||
public Server config; | |||||
// Client socket. | // Client socket. | ||||
public Socket remote; | public Socket remote; | ||||
public Socket connection; | public Socket connection; | ||||
@@ -13,7 +13,7 @@ namespace shadowsocks_csharp.Controller | |||||
{ | { | ||||
private Process process; | private Process process; | ||||
public void Start(Config config) | |||||
public void Start(Server config) | |||||
{ | { | ||||
if (process == null) | if (process == null) | ||||
{ | { | ||||
@@ -14,7 +14,7 @@ namespace shadowsocks_csharp.Controller | |||||
private Local local; | private Local local; | ||||
private PACServer pacServer; | private PACServer pacServer; | ||||
private Config config; | |||||
private Configuration config; | |||||
private PolipoRunner polipoRunner; | private PolipoRunner polipoRunner; | ||||
private bool stopped = false; | private bool stopped = false; | ||||
@@ -38,10 +38,10 @@ namespace shadowsocks_csharp.Controller | |||||
public ShadowsocksController() | public ShadowsocksController() | ||||
{ | { | ||||
config = Config.Load(); | |||||
config = Configuration.Load(); | |||||
polipoRunner = new PolipoRunner(); | polipoRunner = new PolipoRunner(); | ||||
polipoRunner.Start(config); | |||||
local = new Local(config); | |||||
polipoRunner.Start(config.GetCurrentServer()); | |||||
local = new Local(config.GetCurrentServer()); | |||||
try | try | ||||
{ | { | ||||
local.Start(); | local.Start(); | ||||
@@ -57,16 +57,16 @@ namespace shadowsocks_csharp.Controller | |||||
updateSystemProxy(); | updateSystemProxy(); | ||||
} | } | ||||
public void SaveConfig(Config newConfig) | |||||
public void SaveConfig(Configuration newConfig) | |||||
{ | { | ||||
Config.Save(newConfig); | |||||
Configuration.Save(newConfig); | |||||
config = newConfig; | config = newConfig; | ||||
local.Stop(); | local.Stop(); | ||||
polipoRunner.Stop(); | polipoRunner.Stop(); | ||||
polipoRunner.Start(config); | |||||
polipoRunner.Start(config.GetCurrentServer()); | |||||
local = new Local(config); | |||||
local = new Local(config.GetCurrentServer()); | |||||
local.Start(); | local.Start(); | ||||
if (ConfigChanged != null) | if (ConfigChanged != null) | ||||
@@ -75,11 +75,17 @@ namespace shadowsocks_csharp.Controller | |||||
} | } | ||||
} | } | ||||
public Config GetConfig() | |||||
public Server GetCurrentServer() | |||||
{ | |||||
return config.GetCurrentServer(); | |||||
} | |||||
public Configuration GetConfiguration() | |||||
{ | { | ||||
return config; | return config; | ||||
} | } | ||||
public void ToggleEnable(bool enabled) | public void ToggleEnable(bool enabled) | ||||
{ | { | ||||
config.enabled = enabled; | config.enabled = enabled; | ||||
@@ -1,117 +0,0 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using System.IO; | |||||
using System.Diagnostics; | |||||
using SimpleJson; | |||||
namespace shadowsocks_csharp.Model | |||||
{ | |||||
[Serializable] | |||||
public class Config | |||||
{ | |||||
public bool enabled; | |||||
public string server; | |||||
public int server_port; | |||||
public int local_port; | |||||
public string password; | |||||
public string method; | |||||
public bool isDefault; | |||||
private static void assert(bool condition) | |||||
{ | |||||
if(!condition) | |||||
{ | |||||
throw new Exception("assertion failure"); | |||||
} | |||||
} | |||||
public static Config Load() | |||||
{ | |||||
try | |||||
{ | |||||
using (StreamReader sr = new StreamReader(File.OpenRead(@"config.json"))) | |||||
{ | |||||
Config config = SimpleJson.SimpleJson.DeserializeObject<Config>(sr.ReadToEnd()); | |||||
assert(!string.IsNullOrEmpty(config.server)); | |||||
assert(!string.IsNullOrEmpty(config.password)); | |||||
assert(config.local_port > 0); | |||||
assert(config.server_port > 0); | |||||
config.isDefault = false; | |||||
return config; | |||||
} | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
if (!(e is FileNotFoundException)) | |||||
{ | |||||
Console.WriteLine(e); | |||||
} | |||||
return new Config | |||||
{ | |||||
server = "127.0.0.1", | |||||
server_port = 8388, | |||||
local_port = 1080, | |||||
password = "barfoo!", | |||||
method = "table", | |||||
enabled = true, | |||||
isDefault = true | |||||
}; | |||||
} | |||||
} | |||||
private static void checkPort(int port) | |||||
{ | |||||
if (port <= 0 || port > 65535) | |||||
{ | |||||
throw new ArgumentException("port out of range"); | |||||
} | |||||
} | |||||
private static void checkPassword(string password) | |||||
{ | |||||
if (string.IsNullOrEmpty(password)) | |||||
{ | |||||
throw new ArgumentException("password can not be blank"); | |||||
} | |||||
} | |||||
private static void checkServer(string server) | |||||
{ | |||||
if (string.IsNullOrEmpty(server)) | |||||
{ | |||||
throw new ArgumentException("server can not be blank"); | |||||
} | |||||
} | |||||
public static void Save(Config config) | |||||
{ | |||||
checkPort(config.local_port); | |||||
checkPort(config.server_port); | |||||
checkPassword(config.password); | |||||
checkServer(config.server); | |||||
try | |||||
{ | |||||
using (StreamWriter sw = new StreamWriter(File.Open(@"config.json", FileMode.Create))) | |||||
{ | |||||
string jsonString = SimpleJson.SimpleJson.SerializeObject(new | |||||
{ | |||||
server = config.server, | |||||
server_port = config.server_port, | |||||
local_port = config.local_port, | |||||
password = config.password, | |||||
method = config.method, | |||||
enabled = config.enabled | |||||
}); | |||||
sw.Write(jsonString); | |||||
sw.Flush(); | |||||
} | |||||
} | |||||
catch (IOException e) | |||||
{ | |||||
Console.Error.WriteLine(e); | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,133 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.IO; | |||||
using System.Text; | |||||
namespace shadowsocks_csharp.Model | |||||
{ | |||||
[Serializable] | |||||
public class Configuration | |||||
{ | |||||
public Configuration() | |||||
{ | |||||
} | |||||
public List<Server> configs; | |||||
public int index; | |||||
public bool enabled; | |||||
public bool isDefault; | |||||
private static string CONFIG_FILE = "gui-config.json"; | |||||
public Server GetCurrentServer() | |||||
{ | |||||
if (index >= 0 && index < configs.Count) | |||||
{ | |||||
return configs[index]; | |||||
} | |||||
else | |||||
{ | |||||
return getDefaultServer(); | |||||
} | |||||
} | |||||
public static void CheckServer(Server server) | |||||
{ | |||||
checkPort(server.local_port); | |||||
checkPort(server.server_port); | |||||
checkPassword(server.password); | |||||
checkServer(server.server); | |||||
} | |||||
public static Configuration Load() | |||||
{ | |||||
try | |||||
{ | |||||
var json = "[0,1,2]"; | |||||
var result = SimpleJson.SimpleJson.DeserializeObject<List<int>>(json); | |||||
string configContent = File.ReadAllText(CONFIG_FILE); | |||||
Configuration config = SimpleJson.SimpleJson.DeserializeObject<Configuration>(configContent); | |||||
config.isDefault = false; | |||||
return config; | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
if (!(e is FileNotFoundException)) | |||||
{ | |||||
Console.WriteLine(e); | |||||
} | |||||
return new Configuration | |||||
{ | |||||
index = 0, | |||||
configs = new List<Server>() | |||||
{ | |||||
getDefaultServer() | |||||
} | |||||
}; | |||||
} | |||||
} | |||||
public static void Save(Configuration config) | |||||
{ | |||||
try | |||||
{ | |||||
config.isDefault = false; | |||||
using (StreamWriter sw = new StreamWriter(File.Open(CONFIG_FILE, FileMode.Create))) | |||||
{ | |||||
string jsonString = SimpleJson.SimpleJson.SerializeObject(config); | |||||
sw.Write(jsonString); | |||||
sw.Flush(); | |||||
} | |||||
} | |||||
catch (IOException e) | |||||
{ | |||||
Console.Error.WriteLine(e); | |||||
} | |||||
} | |||||
private static Server getDefaultServer() | |||||
{ | |||||
return new Server() | |||||
{ | |||||
server = "", | |||||
server_port = 8388, | |||||
local_port = 1080, | |||||
method = "aes-256-cfb", | |||||
password = "" | |||||
}; | |||||
} | |||||
private static void assert(bool condition) | |||||
{ | |||||
if (!condition) | |||||
{ | |||||
throw new Exception("assertion failure"); | |||||
} | |||||
} | |||||
private static void checkPort(int port) | |||||
{ | |||||
if (port <= 0 || port > 65535) | |||||
{ | |||||
throw new ArgumentException("port out of range"); | |||||
} | |||||
} | |||||
private static void checkPassword(string password) | |||||
{ | |||||
if (string.IsNullOrEmpty(password)) | |||||
{ | |||||
throw new ArgumentException("password can not be blank"); | |||||
} | |||||
} | |||||
private static void checkServer(string server) | |||||
{ | |||||
if (string.IsNullOrEmpty(server)) | |||||
{ | |||||
throw new ArgumentException("server can not be blank"); | |||||
} | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,20 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using System.IO; | |||||
using System.Diagnostics; | |||||
using SimpleJson; | |||||
namespace shadowsocks_csharp.Model | |||||
{ | |||||
[Serializable] | |||||
public class Server | |||||
{ | |||||
public string server; | |||||
public int server_port; | |||||
public int local_port; | |||||
public string password; | |||||
public string method; | |||||
} | |||||
} |
@@ -53,20 +53,20 @@ namespace shadowsocks_csharp.View | |||||
private void updateUI() | private void updateUI() | ||||
{ | { | ||||
Config config = controller.GetConfig(); | |||||
Server server = controller.GetCurrentServer(); | |||||
textBox1.Text = config.server; | |||||
textBox2.Text = config.server_port.ToString(); | |||||
textBox3.Text = config.password; | |||||
textBox4.Text = config.local_port.ToString(); | |||||
comboBox1.Text = config.method == null ? "aes-256-cfb" : config.method; | |||||
textBox1.Text = server.server; | |||||
textBox2.Text = server.server_port.ToString(); | |||||
textBox3.Text = server.password; | |||||
textBox4.Text = server.local_port.ToString(); | |||||
comboBox1.Text = server.method == null ? "aes-256-cfb" : server.method; | |||||
enableItem.Checked = config.enabled; | |||||
enableItem.Checked = controller.GetConfiguration().enabled; | |||||
} | } | ||||
private void Form1_Load(object sender, EventArgs e) | private void Form1_Load(object sender, EventArgs e) | ||||
{ | { | ||||
if (!controller.GetConfig().isDefault) | |||||
if (!controller.GetConfiguration().isDefault) | |||||
{ | { | ||||
this.Opacity = 0; | this.Opacity = 0; | ||||
BeginInvoke(new MethodInvoker(delegate | BeginInvoke(new MethodInvoker(delegate | ||||
@@ -90,15 +90,18 @@ namespace shadowsocks_csharp.View | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
Config config = new Config | |||||
Server server = new Server | |||||
{ | { | ||||
server = textBox1.Text, | server = textBox1.Text, | ||||
server_port = int.Parse(textBox2.Text), | server_port = int.Parse(textBox2.Text), | ||||
password = textBox3.Text, | password = textBox3.Text, | ||||
local_port = int.Parse(textBox4.Text), | local_port = int.Parse(textBox4.Text), | ||||
method = comboBox1.Text, | |||||
isDefault = false | |||||
method = comboBox1.Text | |||||
}; | }; | ||||
Configuration config = controller.GetConfiguration(); | |||||
config.configs.Clear(); | |||||
config.configs.Add(server); | |||||
config.index = 0; | |||||
controller.SaveConfig(config); | controller.SaveConfig(config); | ||||
this.Hide(); | this.Hide(); | ||||
} | } | ||||
@@ -60,7 +60,7 @@ | |||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> | <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> | ||||
<DebugSymbols>true</DebugSymbols> | <DebugSymbols>true</DebugSymbols> | ||||
<OutputPath>bin\x86\Debug\</OutputPath> | <OutputPath>bin\x86\Debug\</OutputPath> | ||||
<DefineConstants>TRACE;DEBUG;SIMPLE_JSON_NO_LINQ_EXPRESSION</DefineConstants> | |||||
<DefineConstants>TRACE;DEBUG</DefineConstants> | |||||
<DebugType>full</DebugType> | <DebugType>full</DebugType> | ||||
<PlatformTarget>x86</PlatformTarget> | <PlatformTarget>x86</PlatformTarget> | ||||
<ErrorReport>prompt</ErrorReport> | <ErrorReport>prompt</ErrorReport> | ||||
@@ -89,7 +89,8 @@ | |||||
<Compile Include="Encrypt\TableEncryptor.cs" /> | <Compile Include="Encrypt\TableEncryptor.cs" /> | ||||
<Compile Include="Encrypt\IEncryptor.cs" /> | <Compile Include="Encrypt\IEncryptor.cs" /> | ||||
<Compile Include="Controller\PACServer.cs" /> | <Compile Include="Controller\PACServer.cs" /> | ||||
<Compile Include="Model\Config.cs" /> | |||||
<Compile Include="Model\Server.cs" /> | |||||
<Compile Include="Model\Configuration.cs" /> | |||||
<Compile Include="View\ConfigForm.cs"> | <Compile Include="View\ConfigForm.cs"> | ||||
<SubType>Form</SubType> | <SubType>Form</SubType> | ||||
</Compile> | </Compile> | ||||
@@ -1 +1 @@ | |||||
Subproject commit 8ef093ac7b18a9f48fb7a8e41085501c7e35e46c | |||||
Subproject commit 663c62a77ab3318f04ddb9449b2b882364709625 |