Browse Source

support build in gfwlist

tags/2.3
Gang Zhuo 10 years ago
parent
commit
d93adb16c4
5 changed files with 81 additions and 10 deletions
  1. +67
    -10
      shadowsocks-csharp/Controller/GfwListUpdater.cs
  2. BIN
      shadowsocks-csharp/Data/builtin.txt.gz
  3. +10
    -0
      shadowsocks-csharp/Properties/Resources.Designer.cs
  4. +3
    -0
      shadowsocks-csharp/Properties/Resources.resx
  5. +1
    -0
      shadowsocks-csharp/shadowsocks-csharp.csproj

+ 67
- 10
shadowsocks-csharp/Controller/GfwListUpdater.cs View File

@@ -6,6 +6,7 @@ using System.Net;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text.RegularExpressions;
using Shadowsocks.Model; using Shadowsocks.Model;
using Shadowsocks.Properties; using Shadowsocks.Properties;
@@ -194,9 +195,10 @@ namespace Shadowsocks.Controller
/* refer https://github.com/clowwindy/gfwlist2pac/blob/master/gfwlist2pac/main.py */ /* refer https://github.com/clowwindy/gfwlist2pac/blob/master/gfwlist2pac/main.py */
public string[] GetDomains() public string[] GetDomains()
{ {
string[] lines = GetLines();
List<string> domains = new List<string>(lines.Length);
for(int i =0;i < lines.Length;i++)
List<string> lines = new List<string>(GetLines());
lines.AddRange(GetBuildIn());
List<string> domains = new List<string>(lines.Count);
for (int i = 0; i < lines.Count; i++)
{ {
string line = lines[i]; string line = lines[i];
if (line.IndexOf(".*") >= 0) if (line.IndexOf(".*") >= 0)
@@ -225,7 +227,7 @@ namespace Shadowsocks.Controller
{ {
string[] domains = GetDomains(); string[] domains = GetDomains();
List<string> new_domains = new List<string>(domains.Length); List<string> new_domains = new List<string>(domains.Length);
IDictionary<string, string> tld_dic = GetTldDictionary();
TldIndex tldIndex = GetTldIndex();
foreach(string domain in domains) foreach(string domain in domains)
{ {
@@ -233,13 +235,13 @@ namespace Shadowsocks.Controller
int pos; int pos;
pos = domain.LastIndexOf('.'); pos = domain.LastIndexOf('.');
last_root_domain = domain.Substring(pos + 1); last_root_domain = domain.Substring(pos + 1);
if (!tld_dic.ContainsKey(last_root_domain))
if (!tldIndex.Contains(last_root_domain))
continue; continue;
while(pos > 0) while(pos > 0)
{ {
pos = domain.LastIndexOf('.', pos - 1); pos = domain.LastIndexOf('.', pos - 1);
last_root_domain = domain.Substring(pos + 1); last_root_domain = domain.Substring(pos + 1);
if (tld_dic.ContainsKey(last_root_domain))
if (tldIndex.Contains(last_root_domain))
continue; continue;
else else
break; break;
@@ -273,18 +275,73 @@ namespace Shadowsocks.Controller
return tlds; return tlds;
} }
private IDictionary<string, string> GetTldDictionary()
private TldIndex GetTldIndex()
{ {
string[] tlds = GetTlds(); string[] tlds = GetTlds();
IDictionary<string, string> dic = new Dictionary<string, string>(tlds.Length);
TldIndex index = new TldIndex();
foreach (string tld in tlds) 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<string> patterns = new List<string>();
IDictionary<string, string> dic = new Dictionary<string, string>();
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); 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;
}
} }
} }
} }


BIN
shadowsocks-csharp/Data/builtin.txt.gz View File


+ 10
- 0
shadowsocks-csharp/Properties/Resources.Designer.cs View File

@@ -60,6 +60,16 @@ namespace Shadowsocks.Properties {
} }
} }
/// <summary>
/// 查找 System.Byte[] 类型的本地化资源。
/// </summary>
internal static byte[] builtin_txt {
get {
object obj = ResourceManager.GetObject("builtin_txt", resourceCulture);
return ((byte[])(obj));
}
}
/// <summary> /// <summary>
/// 查找类似 Shadowsocks=Shadowsocks /// 查找类似 Shadowsocks=Shadowsocks
///Enable=启用代理 ///Enable=启用代理


+ 3
- 0
shadowsocks-csharp/Properties/Resources.resx View File

@@ -118,6 +118,9 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<data name="builtin_txt" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Data\builtin.txt.gz;System.Byte[], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="cn" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="cn" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\data\cn.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value> <value>..\data\cn.txt;System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data> </data>


+ 1
- 0
shadowsocks-csharp/shadowsocks-csharp.csproj View File

@@ -143,6 +143,7 @@
<None Include="app.manifest"> <None Include="app.manifest">
<SubType>Designer</SubType> <SubType>Designer</SubType>
</None> </None>
<None Include="Data\builtin.txt.gz" />
<None Include="Data\libsscrypto.dll.gz" /> <None Include="Data\libsscrypto.dll.gz" />
<None Include="Data\polipo.exe.gz" /> <None Include="Data\polipo.exe.gz" />
<None Include="Data\proxy.pac.txt.gz" /> <None Include="Data\proxy.pac.txt.gz" />


Loading…
Cancel
Save