Reuse the PAC file merge procedure
This commit is contained in:
@@ -36,42 +36,8 @@ namespace Shadowsocks.Controller
|
||||
try
|
||||
{
|
||||
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))
|
||||
{
|
||||
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<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(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();
|
||||
|
||||
@@ -612,46 +612,14 @@ namespace Shadowsocks.Controller
|
||||
private static readonly IEnumerable<char> 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<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
|
||||
{
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user