diff --git a/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs b/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs index d59fcec..702170d 100644 --- a/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs +++ b/shadowsocks-csharp/Controller/Service/GfwListUpdater.cs @@ -36,42 +36,8 @@ 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)) - { - string original = FileManager.NonExclusiveReadAllText(PACServer.PAC_FILE, Encoding.UTF8); - if (original == abpContent) - { - UpdateCompleted(this, new ResultEventArgs(false)); - return; - } - } - File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8); - UpdateCompleted?.Invoke(this, new ResultEventArgs(true)); + bool pacFileChanged = MergeAndWritePACFile(e.Result); + UpdateCompleted?.Invoke(this, new ResultEventArgs(pacFileChanged)); } catch (Exception ex) { @@ -79,6 +45,45 @@ namespace Shadowsocks.Controller } } + 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)) + { + foreach (var rule in sr.NonWhiteSpaceLines()) + { + if (rule.BeginWithAny(IgnoredLineBegins)) + continue; + lines.Add(rule); + } + } + } + lines.AddRange(ParseResult(gfwListResult)); + 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 false; + } + } + File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8); + return true; + } + public void UpdatePACFromGFWList(Configuration config) { WebClient http = new WebClient(); diff --git a/shadowsocks-csharp/Controller/ShadowsocksController.cs b/shadowsocks-csharp/Controller/ShadowsocksController.cs index 89d4351..8f2fd01 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); + GFWListUpdater.MergeAndWritePACFile(FileManager.NonExclusiveReadAllText(Utils.GetTempPath("gfwlist.txt"))); } - 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; - } - } - File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8); } public void CopyPacUrl()