Browse Source

Reuse the PAC file merge procedure

tags/4.0.8
celeron533 6 years ago
parent
commit
8cd64d4022
2 changed files with 40 additions and 67 deletions
  1. +39
    -34
      shadowsocks-csharp/Controller/Service/GfwListUpdater.cs
  2. +1
    -33
      shadowsocks-csharp/Controller/ShadowsocksController.cs

+ 39
- 34
shadowsocks-csharp/Controller/Service/GfwListUpdater.cs View File

@@ -36,47 +36,52 @@ namespace Shadowsocks.Controller
try try
{ {
File.WriteAllText(Utils.GetTempPath("gfwlist.txt"), e.Result, Encoding.UTF8); File.WriteAllText(Utils.GetTempPath("gfwlist.txt"), e.Result, Encoding.UTF8);
List<string> lines = new List<string>();
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<string> lines = new List<string>();
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) public void UpdatePACFromGFWList(Configuration config)


+ 1
- 33
shadowsocks-csharp/Controller/ShadowsocksController.cs View File

@@ -612,46 +612,14 @@ namespace Shadowsocks.Controller
private static readonly IEnumerable<char> IgnoredLineBegins = new[] { '!', '[' }; private static readonly IEnumerable<char> IgnoredLineBegins = new[] { '!', '[' };
private void pacServer_UserRuleFileChanged(object sender, EventArgs e) 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"))) if (!File.Exists(Utils.GetTempPath("gfwlist.txt")))
{ {
UpdatePACFromGFWList(); UpdatePACFromGFWList();
return;
}
List<string> lines = new List<string>();
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 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() public void CopyPacUrl()


Loading…
Cancel
Save