diff --git a/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs b/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs index d59fcec3..702170d4 100644 --- a/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs +++ b/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs @@ -36,47 +36,52 @@ namespace Shadowsocks.Controller try { File.WriteAllText(Utils.GetTempPath("gfwlist.txt"), e.Result, Encoding.UTF8); - List lines = new List(); - if (File.Exists(PACServer.USER_RULE_FILE)) - { - string local = FileManager.NonExclusiveReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8); - using (var sr = new StringReader(local)) - { - foreach (var rule in sr.NonWhiteSpaceLines()) - { - if (rule.BeginWithAny(IgnoredLineBegins)) - continue; - lines.Add(rule); - } - } - } - lines.AddRange(ParseResult(e.Result)); - string abpContent; - if (File.Exists(PACServer.USER_ABP_FILE)) - { - abpContent = FileManager.NonExclusiveReadAllText(PACServer.USER_ABP_FILE, Encoding.UTF8); - } - else - { - abpContent = Utils.UnGzip(Resources.abp_js); - } - abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented)); - if (File.Exists(PACServer.PAC_FILE)) + bool pacFileChanged = MergeAndWritePACFile(e.Result); + UpdateCompleted?.Invoke(this, new ResultEventArgs(pacFileChanged)); + } + catch (Exception ex) + { + Error?.Invoke(this, new ErrorEventArgs(ex)); + } + } + + public static bool MergeAndWritePACFile(string gfwListResult) + { + List lines = new List(); + if (File.Exists(PACServer.USER_RULE_FILE)) + { + string local = FileManager.NonExclusiveReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8); + using (var sr = new StringReader(local)) { - string original = FileManager.NonExclusiveReadAllText(PACServer.PAC_FILE, Encoding.UTF8); - if (original == abpContent) + foreach (var rule in sr.NonWhiteSpaceLines()) { - UpdateCompleted(this, new ResultEventArgs(false)); - return; + if (rule.BeginWithAny(IgnoredLineBegins)) + continue; + lines.Add(rule); } } - File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8); - UpdateCompleted?.Invoke(this, new ResultEventArgs(true)); } - catch (Exception ex) + lines.AddRange(ParseResult(gfwListResult)); + string abpContent; + if (File.Exists(PACServer.USER_ABP_FILE)) { - Error?.Invoke(this, new ErrorEventArgs(ex)); + abpContent = FileManager.NonExclusiveReadAllText(PACServer.USER_ABP_FILE, Encoding.UTF8); + } + else + { + abpContent = Utils.UnGzip(Resources.abp_js); + } + abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented)); + if (File.Exists(PACServer.PAC_FILE)) + { + string original = FileManager.NonExclusiveReadAllText(PACServer.PAC_FILE, Encoding.UTF8); + if (original == abpContent) + { + return false; + } } + File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8); + return true; } public void UpdatePACFromGFWList(Configuration config) diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 89d4351c..8f2fd018 100644 --- a/shadowsocks-csharp/Controller/ShadowsocksController.cs +++ b/shadowsocks-csharp/Controller/ShadowsocksController.cs @@ -612,46 +612,14 @@ namespace Shadowsocks.Controller private static readonly IEnumerable IgnoredLineBegins = new[] { '!', '[' }; private void pacServer_UserRuleFileChanged(object sender, EventArgs e) { - // TODO: this is a dirty hack. (from code GListUpdater.http_DownloadStringCompleted()) if (!File.Exists(Utils.GetTempPath("gfwlist.txt"))) { UpdatePACFromGFWList(); - return; - } - List lines = new List(); - if (File.Exists(PACServer.USER_RULE_FILE)) - { - string local = FileManager.NonExclusiveReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8); - using (var sr = new StringReader(local)) - { - foreach (var rule in sr.NonWhiteSpaceLines()) - { - if (rule.BeginWithAny(IgnoredLineBegins)) - continue; - lines.Add(rule); - } - } - } - lines.AddRange(GFWListUpdater.ParseResult(FileManager.NonExclusiveReadAllText(Utils.GetTempPath("gfwlist.txt")))); - string abpContent; - if (File.Exists(PACServer.USER_ABP_FILE)) - { - abpContent = FileManager.NonExclusiveReadAllText(PACServer.USER_ABP_FILE, Encoding.UTF8); } else { - abpContent = Utils.UnGzip(Resources.abp_js); - } - abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented)); - if (File.Exists(PACServer.PAC_FILE)) - { - string original = FileManager.NonExclusiveReadAllText(PACServer.PAC_FILE, Encoding.UTF8); - if (original == abpContent) - { - return; - } + GFWListUpdater.MergeAndWritePACFile(FileManager.NonExclusiveReadAllText(Utils.GetTempPath("gfwlist.txt"))); } - File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8); } public void CopyPacUrl()