Browse Source

use gui-config.json from shadowsocks-gui

tags/3.2
clowwindy 10 years ago
parent
commit
0eb0e8c9e6
9 changed files with 190 additions and 144 deletions
  1. +3
    -3
      shadowsocks-csharp/Controller/Local.cs
  2. +1
    -1
      shadowsocks-csharp/Controller/PolipoRunner.cs
  3. +15
    -9
      shadowsocks-csharp/Controller/ShadowsocksController.cs
  4. +0
    -117
      shadowsocks-csharp/Model/Config.cs
  5. +133
    -0
      shadowsocks-csharp/Model/Configuration.cs
  6. +20
    -0
      shadowsocks-csharp/Model/Server.cs
  7. +14
    -11
      shadowsocks-csharp/View/ConfigForm.cs
  8. +3
    -2
      shadowsocks-csharp/shadowsocks-csharp.csproj
  9. +1
    -1
      shadowsocks-csharp/simple-json

+ 3
- 3
shadowsocks-csharp/Controller/Local.cs View File

@@ -11,10 +11,10 @@ namespace shadowsocks_csharp.Controller
class Local
{
private Config config;
private Server config;
//private Encryptor encryptor;
Socket listener;
public Local(Config config)
public Local(Server config)
{
this.config = config;
//this.encryptor = new Encryptor(config.method, config.password);
@@ -102,7 +102,7 @@ namespace shadowsocks_csharp.Controller
{
//public Encryptor encryptor;
public IEncryptor encryptor;
public Config config;
public Server config;
// Client socket.
public Socket remote;
public Socket connection;


+ 1
- 1
shadowsocks-csharp/Controller/PolipoRunner.cs View File

@@ -13,7 +13,7 @@ namespace shadowsocks_csharp.Controller
{
private Process process;
public void Start(Config config)
public void Start(Server config)
{
if (process == null)
{


+ 15
- 9
shadowsocks-csharp/Controller/ShadowsocksController.cs View File

@@ -14,7 +14,7 @@ namespace shadowsocks_csharp.Controller
private Local local;
private PACServer pacServer;
private Config config;
private Configuration config;
private PolipoRunner polipoRunner;
private bool stopped = false;
@@ -38,10 +38,10 @@ namespace shadowsocks_csharp.Controller
public ShadowsocksController()
{
config = Config.Load();
config = Configuration.Load();
polipoRunner = new PolipoRunner();
polipoRunner.Start(config);
local = new Local(config);
polipoRunner.Start(config.GetCurrentServer());
local = new Local(config.GetCurrentServer());
try
{
local.Start();
@@ -57,16 +57,16 @@ namespace shadowsocks_csharp.Controller
updateSystemProxy();
}
public void SaveConfig(Config newConfig)
public void SaveConfig(Configuration newConfig)
{
Config.Save(newConfig);
Configuration.Save(newConfig);
config = newConfig;
local.Stop();
polipoRunner.Stop();
polipoRunner.Start(config);
polipoRunner.Start(config.GetCurrentServer());
local = new Local(config);
local = new Local(config.GetCurrentServer());
local.Start();
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;
}
public void ToggleEnable(bool enabled)
{
config.enabled = enabled;


+ 0
- 117
shadowsocks-csharp/Model/Config.cs View File

@@ -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);
}
}
}
}

+ 133
- 0
shadowsocks-csharp/Model/Configuration.cs View File

@@ -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");
}
}
}
}

+ 20
- 0
shadowsocks-csharp/Model/Server.cs View File

@@ -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;
}
}

+ 14
- 11
shadowsocks-csharp/View/ConfigForm.cs View File

@@ -53,20 +53,20 @@ namespace shadowsocks_csharp.View
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)
{
if (!controller.GetConfig().isDefault)
if (!controller.GetConfiguration().isDefault)
{
this.Opacity = 0;
BeginInvoke(new MethodInvoker(delegate
@@ -90,15 +90,18 @@ namespace shadowsocks_csharp.View
{
try
{
Config config = new Config
Server server = new Server
{
server = textBox1.Text,
server_port = int.Parse(textBox2.Text),
password = textBox3.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);
this.Hide();
}


+ 3
- 2
shadowsocks-csharp/shadowsocks-csharp.csproj View File

@@ -60,7 +60,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;SIMPLE_JSON_NO_LINQ_EXPRESSION</DefineConstants>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
@@ -89,7 +89,8 @@
<Compile Include="Encrypt\TableEncryptor.cs" />
<Compile Include="Encrypt\IEncryptor.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">
<SubType>Form</SubType>
</Compile>


+ 1
- 1
shadowsocks-csharp/simple-json

@@ -1 +1 @@
Subproject commit 8ef093ac7b18a9f48fb7a8e41085501c7e35e46c
Subproject commit 663c62a77ab3318f04ddb9449b2b882364709625

Loading…
Cancel
Save