Browse Source

Update the PAC javascript to support user rules in a better way

PAC will check the user rules first to find whether the URL should be connected via PROXY or DIRECT. If it does not match, then go to check GFWList rules.
tags/4.1.5
celeron533 5 years ago
parent
commit
9ee988d9f1
2 changed files with 29 additions and 21 deletions
  1. +29
    -21
      shadowsocks-csharp/Controller/Service/GfwListUpdater.cs
  2. BIN
      shadowsocks-csharp/Data/abp.js.gz

+ 29
- 21
shadowsocks-csharp/Controller/Service/GfwListUpdater.cs View File

@@ -47,21 +47,21 @@ namespace Shadowsocks.Controller
public static bool MergeAndWritePACFile(string gfwListResult)
{
List<string> lines = new List<string>();
if (File.Exists(PACServer.USER_RULE_FILE))
string abpContent = MergePACFile(gfwListResult);
if (File.Exists(PACServer.PAC_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())
{
if (rule.BeginWithAny(IgnoredLineBegins))
continue;
lines.Add(rule);
}
return false;
}
}
lines.AddRange(ParseResult(gfwListResult));
File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8);
return true;
}
private static string MergePACFile(string gfwListResult)
{
string abpContent;
if (File.Exists(PACServer.USER_ABP_FILE))
{
@@ -71,17 +71,20 @@ namespace Shadowsocks.Controller
{
abpContent = Utils.UnGzip(Resources.abp_js);
}
abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented));
if (File.Exists(PACServer.PAC_FILE))
List<string> userruleLines = new List<string>();
if (File.Exists(PACServer.USER_RULE_FILE))
{
string original = FileManager.NonExclusiveReadAllText(PACServer.PAC_FILE, Encoding.UTF8);
if (original == abpContent)
{
return false;
}
string userrulesString = FileManager.NonExclusiveReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8);
userruleLines = ParseToValidList(userrulesString);
}
File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8);
return true;
List<string> gfwLines = new List<string>();
gfwLines = ParseBase64ToValidList(gfwListResult);
abpContent = abpContent.Replace("__USERRULES__", JsonConvert.SerializeObject(userruleLines, Formatting.Indented))
.Replace("__RULES__", JsonConvert.SerializeObject(gfwLines, Formatting.Indented));
return abpContent;
}
public void UpdatePACFromGFWList(Configuration config)
@@ -92,10 +95,15 @@ namespace Shadowsocks.Controller
http.DownloadStringAsync(new Uri(GFWLIST_URL));
}
public static List<string> ParseResult(string response)
public static List<string> ParseBase64ToValidList(string response)
{
byte[] bytes = Convert.FromBase64String(response);
string content = Encoding.ASCII.GetString(bytes);
return ParseToValidList(content);
}
private static List<string> ParseToValidList(string content)
{
List<string> valid_lines = new List<string>();
using (var sr = new StringReader(content))
{


BIN
shadowsocks-csharp/Data/abp.js.gz View File


Loading…
Cancel
Save