add new menu to update pac file via gfwlist

This commit is contained in:
Gang Zhuo
2015-01-07 23:41:45 -05:00
parent dd00516587
commit c24c1d44b7
9 changed files with 234 additions and 236 deletions
+49 -151
View File
@@ -16,186 +16,84 @@ namespace Shadowsocks.Controller
{
private const string GFWLIST_URL = "https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt";
private const int EXPIRE_HOURS = 6;
public IWebProxy proxy = null;
public bool useSystemProxy = true;
public class GfwListChangedArgs : EventArgs
public class GfwListDownloadCompletedArgs : EventArgs
{
public string[] GfwList { get; set; }
public string Content;
}
public event EventHandler<GfwListChangedArgs> GfwListChanged;
public event EventHandler<GfwListDownloadCompletedArgs> DownloadCompleted;
private bool running = false;
private bool closed = false;
private int jobId = 0;
DateTime lastUpdateTimeUtc;
string lastUpdateMd5;
public event ErrorEventHandler Error;
private object locker = new object();
public GfwListUpdater()
public void Download()
{
WebClient http = new WebClient();
http.Proxy = proxy;
http.DownloadStringCompleted += http_DownloadStringCompleted;
http.DownloadStringAsync(new Uri(GFWLIST_URL));
}
~GfwListUpdater()
protected void ReportError(Exception e)
{
Stop();
}
public void Start()
{
lock (locker)
if (Error != null)
{
if (running)
return;
running = true;
closed = false;
jobId++;
new Thread(new ParameterizedThreadStart(UpdateJob)).Start(jobId);
Error(this, new ErrorEventArgs(e));
}
}
public void Stop()
{
lock(locker)
{
closed = true;
running = false;
jobId++;
}
}
public void ScheduleUpdateTime(int delaySeconds)
{
lock(locker)
{
lastUpdateTimeUtc = DateTime.UtcNow.AddHours(-1 * EXPIRE_HOURS).AddSeconds(delaySeconds);
}
}
private string DownloadGfwListFile()
private void http_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
try
{
WebClient http = new WebClient();
http.Proxy = useSystemProxy ? WebRequest.GetSystemWebProxy() : proxy;
return http.DownloadString(new Uri(GFWLIST_URL));
string response = e.Result;
if (DownloadCompleted != null)
{
DownloadCompleted(this, new GfwListDownloadCompletedArgs
{
Content = response
});
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
ReportError(ex);
}
return null;
}
private bool IsExpire()
public class Parser
{
lock (locker)
private string _Content;
public string Content
{
TimeSpan ts = DateTime.UtcNow - lastUpdateTimeUtc;
bool expire = ((int)ts.TotalHours) >= EXPIRE_HOURS;
if (expire)
lastUpdateTimeUtc = DateTime.UtcNow;
return expire;
get { return _Content; }
}
}
private bool IsJobStop(int currentJobId)
{
lock (locker)
{
if (!running || closed || currentJobId != this.jobId)
return true;
}
return false;
}
private bool IsGfwListChanged(string content)
{
byte[] inputBytes = Encoding.UTF8.GetBytes(content);
byte[] md5Bytes = MD5.Create().ComputeHash(inputBytes);
string md5 = "";
for (int i = 0; i < md5Bytes.Length; i++)
md5 += md5Bytes[i].ToString("x").PadLeft(2, '0');
if (md5 == lastUpdateMd5)
return false;
lastUpdateMd5 = md5;
return true;
}
private void ParseGfwList(string response)
{
if (!IsGfwListChanged(response))
return;
if (GfwListChanged != null)
{
try
{
Parser parser = new Parser(response);
GfwListChangedArgs args = new GfwListChangedArgs
{
GfwList = parser.GetReducedDomains()
};
GfwListChanged(this, args);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
private void UpdateJob(object state)
{
int currentJobId = (int)state;
int retryTimes = 3;
while (!IsJobStop(currentJobId))
{
if (IsExpire())
{
string response = DownloadGfwListFile();
if (response != null)
{
ParseGfwList(response);
}
else if (retryTimes > 0)
{
ScheduleUpdateTime(30); /*Delay 30 seconds to retry*/
retryTimes--;
}
else
{
retryTimes = 3; /* reset retry times, and wait next update time. */
}
}
Thread.Sleep(1000);
}
}
class Parser
{
public string Content { get; private set; }
public Parser(string response)
{
byte[] bytes = Convert.FromBase64String(response);
this.Content = Encoding.ASCII.GetString(bytes);
this._Content = Encoding.ASCII.GetString(bytes);
}
public string[] GetLines()
public string[] GetValidLines()
{
return Content.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
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();
}
/* refer https://github.com/clowwindy/gfwlist2pac/blob/master/gfwlist2pac/main.py */
public string[] GetDomains()
{
List<string> lines = new List<string>(GetLines());
List<string> lines = new List<string>(GetValidLines());
lines.AddRange(GetBuildIn());
List<string> domains = new List<string>(lines.Count);
for (int i = 0; i < lines.Count; i++)
@@ -222,7 +120,7 @@ namespace Shadowsocks.Controller
continue;
else if (line.StartsWith("@"))
continue; /*ignore white list*/
int pos = line.IndexOfAny(new char[] { '/'});
int pos = line.IndexOfAny(new char[] { '/' });
if (pos >= 0)
line = line.Substring(0, pos);
if (line.Length > 0)
@@ -238,7 +136,7 @@ namespace Shadowsocks.Controller
List<string> new_domains = new List<string>(domains.Length);
TldIndex tldIndex = GetTldIndex();
foreach(string domain in domains)
foreach (string domain in domains)
{
string last_root_domain = null;
int pos;
@@ -246,7 +144,7 @@ namespace Shadowsocks.Controller
last_root_domain = domain.Substring(pos + 1);
if (!tldIndex.Contains(last_root_domain))
continue;
while(pos > 0)
while (pos > 0)
{
pos = domain.LastIndexOf('.', pos - 1);
last_root_domain = domain.Substring(pos + 1);
@@ -266,7 +164,7 @@ namespace Shadowsocks.Controller
{
List<string> list = new List<string>(src.Length);
Dictionary<string, string> dic = new Dictionary<string, string>(src.Length);
foreach(string s in src)
foreach (string s in src)
{
if (!dic.ContainsKey(s))
{
@@ -281,9 +179,9 @@ namespace Shadowsocks.Controller
{
string[] tlds = null;
byte[] pacGZ = Resources.tld_txt;
byte[] buffer = new byte[1024];
byte[] buffer = new byte[1024];
int n;
using(MemoryStream sb = new MemoryStream())
using (MemoryStream sb = new MemoryStream())
{
using (GZipStream input = new GZipStream(new MemoryStream(pacGZ),
CompressionMode.Decompress, false))
@@ -332,7 +230,7 @@ namespace Shadowsocks.Controller
return buildin;
}
class TldIndex
public class TldIndex
{
List<string> patterns = new List<string>();
IDictionary<string, string> dic = new Dictionary<string, string>();
@@ -355,7 +253,7 @@ namespace Shadowsocks.Controller
{
if (dic.ContainsKey(tld))
return true;
foreach(string pattern in patterns)
foreach (string pattern in patterns)
{
if (Regex.IsMatch(tld, pattern))
return true;
@@ -365,7 +263,7 @@ namespace Shadowsocks.Controller
}
}
}
+88 -60
View File
@@ -1,6 +1,7 @@
using Shadowsocks.Model;
using Shadowsocks.Properties;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
@@ -21,10 +22,12 @@ namespace Shadowsocks.Controller
Socket _listener;
FileSystemWatcher watcher;
GfwListUpdater gfwlistUpdater;
public event EventHandler PACFileChanged;
public event EventHandler UpdatePACFromGFWListCompleted;
public event ErrorEventHandler UpdatePACFromGFWListError;
public void Start(Configuration configuration)
{
try
@@ -51,7 +54,6 @@ namespace Shadowsocks.Controller
_listener);
WatchPacFile();
StartGfwListUpdater();
}
catch (SocketException)
{
@@ -62,11 +64,6 @@ namespace Shadowsocks.Controller
public void Stop()
{
if (gfwlistUpdater != null)
{
gfwlistUpdater.Stop();
gfwlistUpdater = null;
}
if (_listener != null)
{
_listener.Close();
@@ -146,7 +143,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);
}
@@ -252,72 +249,103 @@ Connection: Close
return proxy;
}
private void StartGfwListUpdater()
public void UpdatePACFromGFWList()
{
if (gfwlistUpdater != null)
{
gfwlistUpdater.Stop();
gfwlistUpdater = null;
}
gfwlistUpdater = new GfwListUpdater();
gfwlistUpdater.GfwListChanged += gfwlistUpdater_GfwListChanged;
IPEndPoint localEndPoint = (IPEndPoint)_listener.LocalEndPoint;
gfwlistUpdater.proxy = new WebProxy(localEndPoint.Address.ToString(), 8123);
gfwlistUpdater.useSystemProxy = false;
/* Delay 30 seconds, wait proxy start up. */
gfwlistUpdater.ScheduleUpdateTime(30);
gfwlistUpdater.Start();
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 gfwlistUpdater_GfwListChanged(object sender, GfwListUpdater.GfwListChangedArgs e)
private void gfwlist_DownloadCompleted(object sender, GfwListUpdater.GfwListDownloadCompletedArgs e)
{
if (e.GfwList == null || e.GfwList.Length == 0) return;
string pacfile = TouchPACFile();
string pacContent = File.ReadAllText(pacfile);
string oldDomains;
if (ClearPacContent(ref pacContent, out oldDomains))
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);
if (UpdatePACFromGFWListCompleted != null)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("{");
for (int i = 0; i < e.GfwList.Length; i++)
UpdatePACFromGFWListCompleted(this, new EventArgs());
}
}
private void gfwlist_Error(object sender, ErrorEventArgs e)
{
if (UpdatePACFromGFWListError != null)
{
UpdatePACFromGFWListError(this, e);
}
}
private string GetAbpContent()
{
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))
{
if (i == e.GfwList.Length - 1)
sb.AppendFormat("\t\"{0}\": {1}\r\n", e.GfwList[i], 1);
else
sb.AppendFormat("\t\"{0}\": {1},\r\n", e.GfwList[i], 1);
while ((n = input.Read(buffer, 0, buffer.Length)) > 0)
{
sb.Write(buffer, 0, n);
}
}
sb.Append("}");
string newDomains = sb.ToString();
if (!string.Equals(oldDomains, newDomains))
{
pacContent = pacContent.Replace("__LAST_MODIFIED__", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
pacContent = pacContent.Replace("__DOMAINS__", newDomains);
File.WriteAllText(pacfile, pacContent);
Console.WriteLine("gfwlist updated - " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
else
{
Console.WriteLine("Broken pac file.");
return System.Text.Encoding.UTF8.GetString(sb.ToArray());
}
}
private bool ClearPacContent(ref string pacContent, out string oldDomains)
private static void SerializeRules(string[] rules, StringBuilder builder)
{
Regex regex = new Regex("(/\\*.*?\\*/\\s*)?var\\s+domains\\s*=\\s*(\\{(\\s*\"[^\"]*\"\\s*:\\s*\\d+\\s*,)*\\s*(\\s*\"[^\"]*\"\\s*:\\s*\\d+\\s*)\\})", RegexOptions.Singleline);
Match m = regex.Match(pacContent);
if (m.Success)
builder.Append("[\n");
bool first = true;
foreach (string rule in rules)
{
oldDomains = m.Result("$2");
pacContent = regex.Replace(pacContent, "/* Last Modified: __LAST_MODIFIED__ */\r\nvar domains = __DOMAINS__");
return true;
if (!first)
builder.Append(",\n");
SerializeString(rule, builder);
first = false;
}
oldDomains = null;
return 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()
@@ -156,6 +160,14 @@ namespace Shadowsocks.Controller
return "ss://" + base64;
}
public void UpdatePACFromGFWList()
{
if (pacServer != null)
{
pacServer.UpdatePACFromGFWList();
}
}
protected void Reload()
{
// some logic in configuration updated the config when saving, we need to read it again
@@ -169,6 +181,8 @@ namespace Shadowsocks.Controller
{
pacServer = new PACServer();
pacServer.PACFileChanged += pacServer_PACFileChanged;
pacServer.UpdatePACFromGFWListCompleted += pacServer_UpdatePACFromGFWListCompleted;
pacServer.UpdatePACFromGFWListError += pacServer_UpdatePACFromGFWListError;
}
pacServer.Stop();
@@ -247,6 +261,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));