Merge branch 'gfwlist' of https://github.com/GangZhuo/shadowsocks-csharp into GangZhuo-gfwlist

This commit is contained in:
clowwindy
2015-01-08 19:58:36 +08:00
10 changed files with 315 additions and 8 deletions
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace Shadowsocks.Controller
{
public class GfwListUpdater
{
private const string GFWLIST_URL = "https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt";
public IWebProxy proxy = null;
public class GfwListDownloadCompletedArgs : EventArgs
{
public string Content;
}
public event EventHandler<GfwListDownloadCompletedArgs> DownloadCompleted;
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)
{
DownloadCompleted(this, new GfwListDownloadCompletedArgs
{
Content = response
});
}
}
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 (line.StartsWith("!") || line.StartsWith("["))
continue;
valid_lines.Add(line);
}
return valid_lines.ToArray();
}
}
}
}
+118 -1
View File
@@ -8,6 +8,7 @@ using System.IO.Compression;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
namespace Shadowsocks.Controller
{
@@ -22,6 +23,10 @@ namespace Shadowsocks.Controller
public event EventHandler PACFileChanged;
public event EventHandler UpdatePACFromGFWListCompleted;
public event ErrorEventHandler UpdatePACFromGFWListError;
public void Start(Configuration configuration)
{
try
@@ -137,7 +142,7 @@ namespace Shadowsocks.Controller
using (GZipStream input = new GZipStream(new MemoryStream(pacGZ),
CompressionMode.Decompress, false))
{
while((n = input.Read(buffer, 0, buffer.Length)) > 0)
while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
{
sb.Write(buffer, 0, n);
}
@@ -242,5 +247,117 @@ 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("\"");
}
}
}
@@ -38,6 +38,10 @@ namespace Shadowsocks.Controller
// when user clicked Edit PAC, and PAC file has already created
public event EventHandler<PathEventArgs> PACFileReadyToOpen;
public event EventHandler UpdatePACFromGFWListCompleted;
public event ErrorEventHandler UpdatePACFromGFWListError;
public event ErrorEventHandler Errored;
public ShadowsocksController()
@@ -128,6 +132,11 @@ namespace Shadowsocks.Controller
{
polipoRunner.Stop();
}
if (pacServer != null)
{
pacServer.Stop();
pacServer = null;
}
if (_config.enabled)
{
SystemProxy.Disable();
@@ -151,6 +160,18 @@ namespace Shadowsocks.Controller
return "ss://" + base64;
}
public void UpdatePACFromGFWList()
{
if (pacServer != null)
{
pacServer.UpdatePACFromGFWList();
}
else if (UpdatePACFromGFWListError != null)
{
UpdatePACFromGFWListError(this, new ErrorEventArgs(new Exception("The PACServer is not run.")));
}
}
protected void Reload()
{
// some logic in configuration updated the config when saving, we need to read it again
@@ -164,6 +185,8 @@ namespace Shadowsocks.Controller
{
pacServer = new PACServer();
pacServer.PACFileChanged += pacServer_PACFileChanged;
pacServer.UpdatePACFromGFWListCompleted += pacServer_UpdatePACFromGFWListCompleted;
pacServer.UpdatePACFromGFWListError += pacServer_UpdatePACFromGFWListError;
}
pacServer.Stop();
@@ -242,6 +265,18 @@ namespace Shadowsocks.Controller
UpdateSystemProxy();
}
private void pacServer_UpdatePACFromGFWListCompleted(object sender, EventArgs e)
{
if (UpdatePACFromGFWListCompleted != null)
UpdatePACFromGFWListCompleted(this, e);
}
private void pacServer_UpdatePACFromGFWListError(object sender, ErrorEventArgs e)
{
if (UpdatePACFromGFWListError != null)
UpdatePACFromGFWListError(this, e);
}
private void StartReleasingMemory()
{
_ramThread = new Thread(new ThreadStart(ReleaseMemory));