From e523cbe3fa4588a318ae1bf62f8f3b9eceef1126 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Mon, 14 Jan 2013 18:14:05 +0800 Subject: [PATCH] add Config class --- shadowsocks-csharp/Config.cs | 56 ++++++++++++++++++++ shadowsocks-csharp/Encryptor.cs | 7 +-- shadowsocks-csharp/Local.cs | 8 ++- shadowsocks-csharp/Program.cs | 4 +- shadowsocks-csharp/shadowsocks-csharp.csproj | 13 ++++- 5 files changed, 82 insertions(+), 6 deletions(-) create mode 100755 shadowsocks-csharp/Config.cs diff --git a/shadowsocks-csharp/Config.cs b/shadowsocks-csharp/Config.cs new file mode 100755 index 00000000..3800e832 --- /dev/null +++ b/shadowsocks-csharp/Config.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Runtime.Serialization.Json; +using System.Text; +using System.IO; + +namespace shadowsocks_csharp +{ + [Serializable] + public class Config + { + public string server; + public int server_port; + public int local_port; + public string password; + + public static Config Load() + { + DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Config)); + try + { + using (FileStream fs = File.OpenRead(@"config.json")) + { + Config config = ser.ReadObject(fs) as Config; + return config; + } + } + catch (IOException e) + { + return new Config + { + server = "127.0.0.1", + server_port = 8388, + local_port = 1080, + password = "foobar!" + }; + } + } + + public static void Save(Config config) + { + DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Config)); + try + { + using (FileStream fs = File.Open(@"config.json",FileMode.Create)) + { + ser.WriteObject(fs, config); + } + } + catch (IOException e) + { + Console.Error.WriteLine(e); + } + } + } +} diff --git a/shadowsocks-csharp/Encryptor.cs b/shadowsocks-csharp/Encryptor.cs index c40d9212..d998cda8 100755 --- a/shadowsocks-csharp/Encryptor.cs +++ b/shadowsocks-csharp/Encryptor.cs @@ -10,11 +10,12 @@ namespace shadowsocks_csharp public byte[] encryptTable = new byte[256]; public byte[] decryptTable = new byte[256]; - private Int64 compare(byte x, byte y, UInt64 a, int i) { - return (Int64)(a % (UInt64)(x + i)) - (Int64)(a % (UInt64)(y + i)); + private long compare(byte x, byte y, ulong a, int i) + { + return (long)(a % (ulong)(x + i)) - (long)(a % (ulong)(y + i)); } - private byte[] mergeSort(byte[] array, UInt64 a, int j) + private byte[] mergeSort(byte[] array, ulong a, int j) { if (array.Length == 1) return array; diff --git a/shadowsocks-csharp/Local.cs b/shadowsocks-csharp/Local.cs index cfe95ae9..66ad8184 100755 --- a/shadowsocks-csharp/Local.cs +++ b/shadowsocks-csharp/Local.cs @@ -13,6 +13,7 @@ namespace shadowsocks_csharp { private int port; private Encryptor encryptor; + Socket listener; public Local(int port) { this.port = port; @@ -23,7 +24,7 @@ namespace shadowsocks_csharp { // Create a TCP/IP socket. - Socket listener = new Socket(AddressFamily.InterNetwork, + listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint localEndPoint = new IPEndPoint(0, port); @@ -40,6 +41,11 @@ namespace shadowsocks_csharp } + public void Stop() + { + listener.Close(); + } + public void AcceptCallback(IAsyncResult ar) { diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index c3f34158..417442a6 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -12,10 +12,12 @@ namespace shadowsocks_csharp [STAThread] static void Main() { - new Local(1081).Start(); + Local local = new Local(1081); + local.Start(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); + local.Stop(); } } } diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index e20d1ebf..349a69cb 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -10,7 +10,7 @@ Properties shadowsocks_csharp shadowsocks-csharp - v2.0 + v3.5 512 @@ -35,13 +35,23 @@ + + 3.5 + + + 3.0 + + + 3.5 + + Form @@ -54,6 +64,7 @@ Form1.cs + Designer ResXFileCodeGenerator