lint code

This commit is contained in:
clowwindy
2015-01-08 21:57:51 +08:00
parent 6192e09970
commit a0a5cafe3d
10 changed files with 87 additions and 212 deletions
+41 -59
View File
@@ -3,88 +3,70 @@ using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using Shadowsocks.Properties;
using System.IO.Compression;
using System.Text.RegularExpressions;
using SimpleJson;
using Shadowsocks.Util;
namespace Shadowsocks.Controller
{
public class GfwListUpdater
public class GFWListUpdater
{
private const string GFWLIST_URL = "https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt";
public IWebProxy proxy = null;
private static string PAC_FILE = PACServer.PAC_FILE;
public class GfwListDownloadCompletedArgs : EventArgs
{
public string Content;
}
public event EventHandler<GfwListDownloadCompletedArgs> DownloadCompleted;
public event EventHandler UpdateCompleted;
public event ErrorEventHandler Error;
public void Download()
{
WebClient http = new WebClient();
http.Proxy = proxy;
http.DownloadStringCompleted += http_DownloadStringCompleted;
http.DownloadStringAsync(new Uri(GFWLIST_URL));
}
protected void ReportError(Exception e)
{
if (Error != null)
{
Error(this, new ErrorEventArgs(e));
}
}
private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
string response = e.Result;
if (DownloadCompleted != null)
string[] lines = ParseResult(e.Result);
JsonArray rules = new JsonArray();
rules.AddRange(lines);
string abpContent = Utils.UnGzip(Resources.abp_js);
abpContent = abpContent.Replace("__RULES__", rules.ToString());
File.WriteAllText(PAC_FILE, abpContent, Encoding.UTF8);
if (UpdateCompleted != null)
{
DownloadCompleted(this, new GfwListDownloadCompletedArgs
{
Content = response
});
UpdateCompleted(this, new EventArgs());
}
}
catch (Exception ex)
{
ReportError(ex);
}
}
public class Parser
{
private string _Content;
public string Content
{
get { return _Content; }
}
public Parser(string response)
{
byte[] bytes = Convert.FromBase64String(response);
this._Content = Encoding.ASCII.GetString(bytes);
}
public string[] GetValidLines()
{
string[] lines = Content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
List<string> valid_lines = new List<string>(lines.Length);
foreach (string line in lines)
if (Error != null)
{
if (line.StartsWith("!") || line.StartsWith("["))
continue;
valid_lines.Add(line);
Error(this, new ErrorEventArgs(ex));
}
return valid_lines.ToArray();
}
}
public void UpdatePACFromGFWList()
{
WebClient http = new WebClient();
http.DownloadStringCompleted += http_DownloadStringCompleted;
http.DownloadStringAsync(new Uri(GFWLIST_URL));
}
public string[] ParseResult(string response)
{
byte[] bytes = Convert.FromBase64String(response);
string content = Encoding.ASCII.GetString(bytes);
string[] lines = content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
List<string> valid_lines = new List<string>(lines.Length);
foreach (string line in lines)
{
if (line.StartsWith("!") || line.StartsWith("["))
continue;
valid_lines.Add(line);
}
return valid_lines.ToArray();
}
}
}
+4 -132
View File
@@ -1,5 +1,6 @@
using Shadowsocks.Model;
using Shadowsocks.Properties;
using Shadowsocks.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
@@ -8,14 +9,13 @@ using System.IO.Compression;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
namespace Shadowsocks.Controller
{
class PACServer
{
private static int PORT = 8093;
private static string PAC_FILE = "pac.txt";
public static string PAC_FILE = "pac.txt";
private static Configuration config;
Socket _listener;
@@ -23,10 +23,6 @@ namespace Shadowsocks.Controller
public event EventHandler PACFileChanged;
public event EventHandler UpdatePACFromGFWListCompleted;
public event ErrorEventHandler UpdatePACFromGFWListError;
public void Start(Configuration configuration)
{
try
@@ -135,19 +131,7 @@ namespace Shadowsocks.Controller
}
else
{
byte[] pacGZ = Resources.proxy_pac_txt;
byte[] buffer = new byte[1024]; // builtin pac gzip size: maximum 100K
MemoryStream sb = new MemoryStream();
int n;
using (GZipStream input = new GZipStream(new MemoryStream(pacGZ),
CompressionMode.Decompress, false))
{
while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
{
sb.Write(buffer, 0, n);
}
return System.Text.Encoding.UTF8.GetString(sb.ToArray());
}
return Utils.UnGzip(Resources.proxy_pac_txt);
}
}
@@ -180,7 +164,7 @@ Connection: Close
", System.Text.Encoding.UTF8.GetBytes(pac).Length) + pac;
byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
conn.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), conn);
Util.Util.ReleaseMemory();
Util.Utils.ReleaseMemory();
}
else
{
@@ -247,117 +231,5 @@ Connection: Close
//}
return proxy;
}
public void UpdatePACFromGFWList()
{
GfwListUpdater gfwlist = new GfwListUpdater();
gfwlist.DownloadCompleted += gfwlist_DownloadCompleted;
gfwlist.Error += gfwlist_Error;
gfwlist.proxy = new WebProxy(IPAddress.Loopback.ToString(), 8123); /* use polipo proxy*/
gfwlist.Download();
}
private void gfwlist_DownloadCompleted(object sender, GfwListUpdater.GfwListDownloadCompletedArgs e)
{
GfwListUpdater.Parser parser = new GfwListUpdater.Parser(e.Content);
string[] lines = parser.GetValidLines();
StringBuilder rules = new StringBuilder(lines.Length * 16);
SerializeRules(lines, rules);
string abpContent = GetAbpContent();
abpContent = abpContent.Replace("__RULES__", rules.ToString());
File.WriteAllText(PAC_FILE, abpContent, Encoding.UTF8);
if (UpdatePACFromGFWListCompleted != null)
{
UpdatePACFromGFWListCompleted(this, new EventArgs());
}
}
private void gfwlist_Error(object sender, ErrorEventArgs e)
{
if (UpdatePACFromGFWListError != null)
{
UpdatePACFromGFWListError(this, e);
}
}
private string GetAbpContent()
{
string content;
if (File.Exists(PAC_FILE))
{
content = File.ReadAllText(PAC_FILE, Encoding.UTF8);
Regex regex = new Regex("var\\s+rules\\s*=\\s*(\\[(\\s*\"[^\"]*\"\\s*,)*(\\s*\"[^\"]*\")\\s*\\])", RegexOptions.Singleline);
Match m = regex.Match(content);
if (m.Success)
{
content = regex.Replace(content, "var rules = __RULES__");
return content;
}
}
byte[] abpGZ = Resources.abp_js;
byte[] buffer = new byte[1024]; // builtin pac gzip size: maximum 100K
int n;
using (MemoryStream sb = new MemoryStream())
{
using (GZipStream input = new GZipStream(new MemoryStream(abpGZ),
CompressionMode.Decompress, false))
{
while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
{
sb.Write(buffer, 0, n);
}
}
content = System.Text.Encoding.UTF8.GetString(sb.ToArray());
}
return content;
}
private static void SerializeRules(string[] rules, StringBuilder builder)
{
builder.Append("[\n");
bool first = true;
foreach (string rule in rules)
{
if (!first)
builder.Append(",\n");
SerializeString(rule, builder);
first = false;
}
builder.Append("\n]");
}
private static void SerializeString(string aString, StringBuilder builder)
{
builder.Append("\t\"");
char[] charArray = aString.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
char c = charArray[i];
if (c == '"')
builder.Append("\\\"");
else if (c == '\\')
builder.Append("\\\\");
else if (c == '\b')
builder.Append("\\b");
else if (c == '\f')
builder.Append("\\f");
else if (c == '\n')
builder.Append("\\n");
else if (c == '\r')
builder.Append("\\r");
else if (c == '\t')
builder.Append("\\t");
else
builder.Append(c);
}
builder.Append("\"");
}
}
}
@@ -21,6 +21,7 @@ namespace Shadowsocks.Controller
private PACServer pacServer;
private Configuration _config;
private PolipoRunner polipoRunner;
private GFWListUpdater gfwListUpdater;
private bool stopped = false;
private bool _systemProxyIsDirty = false;
@@ -157,13 +158,9 @@ namespace Shadowsocks.Controller
public void UpdatePACFromGFWList()
{
if (pacServer != null)
if (gfwListUpdater != null)
{
pacServer.UpdatePACFromGFWList();
}
else if (UpdatePACFromGFWListError != null)
{
UpdatePACFromGFWListError(this, new ErrorEventArgs(new Exception("The PACServer is not run.")));
gfwListUpdater.UpdatePACFromGFWList();
}
}
@@ -180,8 +177,12 @@ namespace Shadowsocks.Controller
{
pacServer = new PACServer();
pacServer.PACFileChanged += pacServer_PACFileChanged;
pacServer.UpdatePACFromGFWListCompleted += pacServer_UpdatePACFromGFWListCompleted;
pacServer.UpdatePACFromGFWListError += pacServer_UpdatePACFromGFWListError;
}
if (gfwListUpdater == null)
{
gfwListUpdater = new GFWListUpdater();
gfwListUpdater.UpdateCompleted += pacServer_PACUpdateCompleted;
gfwListUpdater.Error += pacServer_PACUpdateError;
}
pacServer.Stop();
@@ -226,7 +227,7 @@ namespace Shadowsocks.Controller
}
UpdateSystemProxy();
Util.Util.ReleaseMemory();
Util.Utils.ReleaseMemory();
}
@@ -260,13 +261,13 @@ namespace Shadowsocks.Controller
UpdateSystemProxy();
}
private void pacServer_UpdatePACFromGFWListCompleted(object sender, EventArgs e)
private void pacServer_PACUpdateCompleted(object sender, EventArgs e)
{
if (UpdatePACFromGFWListCompleted != null)
UpdatePACFromGFWListCompleted(this, e);
}
private void pacServer_UpdatePACFromGFWListError(object sender, ErrorEventArgs e)
private void pacServer_PACUpdateError(object sender, ErrorEventArgs e)
{
if (UpdatePACFromGFWListError != null)
UpdatePACFromGFWListError(this, e);
@@ -283,7 +284,7 @@ namespace Shadowsocks.Controller
{
while (true)
{
Util.Util.ReleaseMemory();
Util.Utils.ReleaseMemory();
Thread.Sleep(30 * 1000);
}
}