From dd0051658766c6c0206b1562701ee72803d4d8fb Mon Sep 17 00:00:00 2001 From: Gang Zhuo Date: Thu, 8 Jan 2015 03:30:55 +0800 Subject: [PATCH] refine, remove duplicate domains --- .../Controller/GfwListUpdater.cs | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/shadowsocks-csharp/Controller/GfwListUpdater.cs b/shadowsocks-csharp/Controller/GfwListUpdater.cs index 97f0cb2e..ef4fe4eb 100644 --- a/shadowsocks-csharp/Controller/GfwListUpdater.cs +++ b/shadowsocks-csharp/Controller/GfwListUpdater.cs @@ -203,23 +203,32 @@ namespace Shadowsocks.Controller string line = lines[i]; if (line.IndexOf(".*") >= 0) continue; - else if (line.IndexOf("*") >= 0) + if (line.StartsWith("http://")) + line = line.Substring(7); + else if (line.StartsWith("https://")) + line = line.Substring(8); + if (line.IndexOf("*") >= 0) line = line.Replace("*", "/"); if (line.StartsWith("||")) - line = line.Substring(2); + while (line.StartsWith("||")) + line = line.Substring(2); else if (line.StartsWith("|")) - line = line.Substring(1); + line = line.TrimStart('|'); else if (line.StartsWith(".")) - line = line.Substring(1); + line = line.TrimStart('.'); if (line.StartsWith("!")) continue; else if (line.StartsWith("[")) continue; else if (line.StartsWith("@")) continue; /*ignore white list*/ - domains.Add(line); + int pos = line.IndexOfAny(new char[] { '/'}); + if (pos >= 0) + line = line.Substring(0, pos); + if (line.Length > 0) + domains.Add(line); } - return domains.ToArray(); + return RemoveDuplicate(domains.ToArray()); } /* refer https://github.com/clowwindy/gfwlist2pac/blob/master/gfwlist2pac/main.py */ @@ -250,7 +259,22 @@ namespace Shadowsocks.Controller new_domains.Add(last_root_domain); } - return new_domains.ToArray(); + return RemoveDuplicate(new_domains.ToArray()); + } + + private string[] RemoveDuplicate(string[] src) + { + List list = new List(src.Length); + Dictionary dic = new Dictionary(src.Length); + foreach(string s in src) + { + if (!dic.ContainsKey(s)) + { + dic.Add(s, s); + list.Add(s); + } + } + return list.ToArray(); } private string[] GetTlds()