@@ -11,13 +11,13 @@ namespace Shadowsocks.Controller | |||
class Local | |||
{ | |||
private Server _server; | |||
private Configuration _config; | |||
private bool _shareOverLAN; | |||
//private Encryptor encryptor; | |||
Socket _listener; | |||
public Local(Configuration config) | |||
{ | |||
this._server = config.GetCurrentServer(); | |||
this._config = config; | |||
_shareOverLAN = config.shareOverLan; | |||
//this.encryptor = new Encryptor(config.method, config.password); | |||
} | |||
@@ -32,11 +32,11 @@ namespace Shadowsocks.Controller | |||
IPEndPoint localEndPoint = null; | |||
if (_shareOverLAN) | |||
{ | |||
localEndPoint = new IPEndPoint(IPAddress.Any, _server.local_port); | |||
localEndPoint = new IPEndPoint(IPAddress.Any, _config.localPort); | |||
} | |||
else | |||
{ | |||
localEndPoint = new IPEndPoint(IPAddress.Loopback, _server.local_port); | |||
localEndPoint = new IPEndPoint(IPAddress.Loopback, _config.localPort); | |||
} | |||
// Bind the socket to the local endpoint and listen for incoming connections. | |||
@@ -74,8 +74,9 @@ namespace Shadowsocks.Controller | |||
Handler handler = new Handler(); | |||
handler.connection = conn; | |||
handler.encryptor = EncryptorFactory.GetEncryptor(_server.method, _server.password); | |||
handler.config = _server; | |||
Server server = _config.GetCurrentServer(); | |||
handler.encryptor = EncryptorFactory.GetEncryptor(server.method, server.password); | |||
handler.server = server; | |||
handler.Start(); | |||
} | |||
@@ -104,7 +105,7 @@ namespace Shadowsocks.Controller | |||
{ | |||
//public Encryptor encryptor; | |||
public IEncryptor encryptor; | |||
public Server config; | |||
public Server server; | |||
// Client socket. | |||
public Socket remote; | |||
public Socket connection; | |||
@@ -134,13 +135,13 @@ namespace Shadowsocks.Controller | |||
{ | |||
// TODO async resolving | |||
IPAddress ipAddress; | |||
bool parsed = IPAddress.TryParse(config.server, out ipAddress); | |||
bool parsed = IPAddress.TryParse(server.server, out ipAddress); | |||
if (!parsed) | |||
{ | |||
IPHostEntry ipHostInfo = Dns.GetHostEntry(config.server); | |||
IPHostEntry ipHostInfo = Dns.GetHostEntry(server.server); | |||
ipAddress = ipHostInfo.AddressList[0]; | |||
} | |||
IPEndPoint remoteEP = new IPEndPoint(ipAddress, config.server_port); | |||
IPEndPoint remoteEP = new IPEndPoint(ipAddress, server.server_port); | |||
remote = new Socket(ipAddress.AddressFamily, | |||
@@ -45,8 +45,8 @@ namespace Shadowsocks.Controller | |||
Console.WriteLine(e.ToString()); | |||
} | |||
} | |||
string polipoConfig = Resources.polipo_config; | |||
polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", server.local_port.ToString()); | |||
string polipoConfig = Resources.polipo_config; | |||
polipoConfig = polipoConfig.Replace("__SOCKS_PORT__", configuration.localPort.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)); | |||
@@ -74,7 +74,7 @@ namespace Shadowsocks.Controller | |||
return Configuration.Load(); | |||
} | |||
public void SaveServers(List<Server> servers) | |||
public void SaveServers(List<Server> servers, int localPort) | |||
{ | |||
_config.configs = servers; | |||
SaveConfig(_config); | |||
@@ -16,6 +16,7 @@ namespace Shadowsocks.Model | |||
public bool enabled; | |||
public bool shareOverLan; | |||
public bool isDefault; | |||
public int localPort; | |||
private static string CONFIG_FILE = "gui-config.json"; | |||
@@ -33,7 +34,6 @@ namespace Shadowsocks.Model | |||
public static void CheckServer(Server server) | |||
{ | |||
CheckPort(server.local_port); | |||
CheckPort(server.server_port); | |||
CheckPassword(server.password); | |||
CheckServer(server.server); | |||
@@ -46,6 +46,10 @@ namespace Shadowsocks.Model | |||
string configContent = File.ReadAllText(CONFIG_FILE); | |||
Configuration config = SimpleJson.SimpleJson.DeserializeObject<Configuration>(configContent, new JsonSerializerStrategy()); | |||
config.isDefault = false; | |||
if (config.localPort == 0) | |||
{ | |||
config.localPort = 1080; | |||
} | |||
return config; | |||
} | |||
catch (Exception e) | |||
@@ -58,6 +62,7 @@ namespace Shadowsocks.Model | |||
{ | |||
index = 0, | |||
isDefault = true, | |||
localPort = 1080, | |||
configs = new List<Server>() | |||
{ | |||
GetDefaultServer() | |||
@@ -105,7 +110,7 @@ namespace Shadowsocks.Model | |||
} | |||
} | |||
private static void CheckPort(int port) | |||
public static void CheckPort(int port) | |||
{ | |||
if (port <= 0 || port > 65535) | |||
{ | |||
@@ -14,7 +14,6 @@ namespace Shadowsocks.Model | |||
{ | |||
public string server; | |||
public int server_port; | |||
public int local_port; | |||
public string password; | |||
public string method; | |||
public string remarks; | |||
@@ -39,7 +38,6 @@ namespace Shadowsocks.Model | |||
{ | |||
this.server = ""; | |||
this.server_port = 8388; | |||
this.local_port = 1080; | |||
this.method = "aes-256-cfb"; | |||
this.password = ""; | |||
this.remarks = ""; | |||
@@ -79,12 +79,14 @@ namespace Shadowsocks.View | |||
server = IPTextBox.Text, | |||
server_port = int.Parse(ServerPortTextBox.Text), | |||
password = PasswordTextBox.Text, | |||
local_port = int.Parse(ProxyPortTextBox.Text), | |||
method = EncryptionSelect.Text, | |||
remarks = RemarksTextBox.Text | |||
}; | |||
int localPort = int.Parse(ProxyPortTextBox.Text); | |||
Configuration.CheckServer(server); | |||
Configuration.CheckPort(localPort); | |||
_modifiedConfiguration.configs[_oldSelectedIndex] = server; | |||
_modifiedConfiguration.localPort = localPort; | |||
return true; | |||
} | |||
@@ -108,7 +110,7 @@ namespace Shadowsocks.View | |||
IPTextBox.Text = server.server; | |||
ServerPortTextBox.Text = server.server_port.ToString(); | |||
PasswordTextBox.Text = server.password; | |||
ProxyPortTextBox.Text = server.local_port.ToString(); | |||
ProxyPortTextBox.Text = _modifiedConfiguration.localPort.ToString(); | |||
EncryptionSelect.Text = server.method ?? "aes-256-cfb"; | |||
RemarksTextBox.Text = server.remarks; | |||
ServerGroupBox.Visible = true; | |||
@@ -202,7 +204,7 @@ namespace Shadowsocks.View | |||
MessageBox.Show(I18N.GetString("Please add at least one server")); | |||
return; | |||
} | |||
controller.SaveServers(_modifiedConfiguration.configs); | |||
controller.SaveServers(_modifiedConfiguration.configs, _modifiedConfiguration.localPort); | |||
this.Close(); | |||
} | |||