From d93adb16c4ff166b0f6e979f35c591f1f0fca130 Mon Sep 17 00:00:00 2001 From: Gang Zhuo Date: Thu, 8 Jan 2015 03:07:26 +0800 Subject: [PATCH] support build in gfwlist --- .../Controller/GfwListUpdater.cs | 77 +++++++++++++++--- shadowsocks-csharp/Data/builtin.txt.gz | Bin 0 -> 104 bytes .../Properties/Resources.Designer.cs | 10 +++ shadowsocks-csharp/Properties/Resources.resx | 3 + shadowsocks-csharp/shadowsocks-csharp.csproj | 1 + 5 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 shadowsocks-csharp/Data/builtin.txt.gz diff --git a/shadowsocks-csharp/Controller/GfwListUpdater.cs b/shadowsocks-csharp/Controller/GfwListUpdater.cs index ef200930..97f0cb2e 100644 --- a/shadowsocks-csharp/Controller/GfwListUpdater.cs +++ b/shadowsocks-csharp/Controller/GfwListUpdater.cs @@ -6,6 +6,7 @@ using System.Net; using System.IO; using System.IO.Compression; using System.Security.Cryptography; +using System.Text.RegularExpressions; using Shadowsocks.Model; using Shadowsocks.Properties; @@ -194,9 +195,10 @@ namespace Shadowsocks.Controller /* refer https://github.com/clowwindy/gfwlist2pac/blob/master/gfwlist2pac/main.py */ public string[] GetDomains() { - string[] lines = GetLines(); - List domains = new List(lines.Length); - for(int i =0;i < lines.Length;i++) + List lines = new List(GetLines()); + lines.AddRange(GetBuildIn()); + List domains = new List(lines.Count); + for (int i = 0; i < lines.Count; i++) { string line = lines[i]; if (line.IndexOf(".*") >= 0) @@ -225,7 +227,7 @@ namespace Shadowsocks.Controller { string[] domains = GetDomains(); List new_domains = new List(domains.Length); - IDictionary tld_dic = GetTldDictionary(); + TldIndex tldIndex = GetTldIndex(); foreach(string domain in domains) { @@ -233,13 +235,13 @@ namespace Shadowsocks.Controller int pos; pos = domain.LastIndexOf('.'); last_root_domain = domain.Substring(pos + 1); - if (!tld_dic.ContainsKey(last_root_domain)) + if (!tldIndex.Contains(last_root_domain)) continue; while(pos > 0) { pos = domain.LastIndexOf('.', pos - 1); last_root_domain = domain.Substring(pos + 1); - if (tld_dic.ContainsKey(last_root_domain)) + if (tldIndex.Contains(last_root_domain)) continue; else break; @@ -273,18 +275,73 @@ namespace Shadowsocks.Controller return tlds; } - private IDictionary GetTldDictionary() + private TldIndex GetTldIndex() { string[] tlds = GetTlds(); - IDictionary dic = new Dictionary(tlds.Length); + TldIndex index = new TldIndex(); foreach (string tld in tlds) { - if (!dic.ContainsKey(tld)) + index.Add(tld); + } + return index; + } + + private string[] GetBuildIn() + { + string[] buildin = null; + byte[] builtinGZ = Resources.builtin_txt; + byte[] buffer = new byte[1024]; + int n; + using (MemoryStream sb = new MemoryStream()) + { + using (GZipStream input = new GZipStream(new MemoryStream(builtinGZ), + CompressionMode.Decompress, false)) + { + while ((n = input.Read(buffer, 0, buffer.Length)) > 0) + { + sb.Write(buffer, 0, n); + } + } + buildin = System.Text.Encoding.UTF8.GetString(sb.ToArray()) + .Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); + } + return buildin; + } + + class TldIndex + { + List patterns = new List(); + IDictionary dic = new Dictionary(); + + public void Add(string tld) + { + if (string.IsNullOrEmpty(tld)) + return; + if (tld.IndexOfAny(new char[] { '*', '?' }) >= 0) + { + patterns.Add("^" + Regex.Escape(tld).Replace("\\*", ".*").Replace("\\?", ".") + "$"); + } + else if (!dic.ContainsKey(tld)) + { dic.Add(tld, tld); + } } - return dic; + + public bool Contains(string tld) + { + if (dic.ContainsKey(tld)) + return true; + foreach(string pattern in patterns) + { + if (Regex.IsMatch(tld, pattern)) + return true; + } + return false; + } + } + } } diff --git a/shadowsocks-csharp/Data/builtin.txt.gz b/shadowsocks-csharp/Data/builtin.txt.gz new file mode 100644 index 0000000000000000000000000000000000000000..c846e6448a7ec005ee03576b0d40e751c51f85f7 GIT binary patch literal 104 zcmV-u0GIzCiwFp_d973c17dY)Y; + /// 查找 System.Byte[] 类型的本地化资源。 + /// + internal static byte[] builtin_txt { + get { + object obj = ResourceManager.GetObject("builtin_txt", resourceCulture); + return ((byte[])(obj)); + } + } + /// /// 查找类似 Shadowsocks=Shadowsocks ///Enable=启用代理 diff --git a/shadowsocks-csharp/Properties/Resources.resx b/shadowsocks-csharp/Properties/Resources.resx index b5e75b3f..2b53c8e1 100755 --- a/shadowsocks-csharp/Properties/Resources.resx +++ b/shadowsocks-csharp/Properties/Resources.resx @@ -118,6 +118,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + ..\Data\builtin.txt.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + ..\data\cn.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index b192d0fd..6ed7cf4f 100755 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -143,6 +143,7 @@ Designer +