Skip to content

Commit 9ee988d

Browse files
committed
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.
1 parent fcef17a commit 9ee988d

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

shadowsocks-csharp/Controller/Service/GfwListUpdater.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@ private void http_DownloadStringCompleted(object sender, DownloadStringCompleted
4747

4848
public static bool MergeAndWritePACFile(string gfwListResult)
4949
{
50-
List<string> lines = new List<string>();
51-
if (File.Exists(PACServer.USER_RULE_FILE))
50+
string abpContent = MergePACFile(gfwListResult);
51+
if (File.Exists(PACServer.PAC_FILE))
5252
{
53-
string local = FileManager.NonExclusiveReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8);
54-
using (var sr = new StringReader(local))
53+
string original = FileManager.NonExclusiveReadAllText(PACServer.PAC_FILE, Encoding.UTF8);
54+
if (original == abpContent)
5555
{
56-
foreach (var rule in sr.NonWhiteSpaceLines())
57-
{
58-
if (rule.BeginWithAny(IgnoredLineBegins))
59-
continue;
60-
lines.Add(rule);
61-
}
56+
return false;
6257
}
6358
}
64-
lines.AddRange(ParseResult(gfwListResult));
59+
File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8);
60+
return true;
61+
}
62+
63+
private static string MergePACFile(string gfwListResult)
64+
{
6565
string abpContent;
6666
if (File.Exists(PACServer.USER_ABP_FILE))
6767
{
@@ -71,17 +71,20 @@ public static bool MergeAndWritePACFile(string gfwListResult)
7171
{
7272
abpContent = Utils.UnGzip(Resources.abp_js);
7373
}
74-
abpContent = abpContent.Replace("__RULES__", JsonConvert.SerializeObject(lines, Formatting.Indented));
75-
if (File.Exists(PACServer.PAC_FILE))
74+
75+
List<string> userruleLines = new List<string>();
76+
if (File.Exists(PACServer.USER_RULE_FILE))
7677
{
77-
string original = FileManager.NonExclusiveReadAllText(PACServer.PAC_FILE, Encoding.UTF8);
78-
if (original == abpContent)
79-
{
80-
return false;
81-
}
78+
string userrulesString = FileManager.NonExclusiveReadAllText(PACServer.USER_RULE_FILE, Encoding.UTF8);
79+
userruleLines = ParseToValidList(userrulesString);
8280
}
83-
File.WriteAllText(PACServer.PAC_FILE, abpContent, Encoding.UTF8);
84-
return true;
81+
82+
List<string> gfwLines = new List<string>();
83+
gfwLines = ParseBase64ToValidList(gfwListResult);
84+
85+
abpContent = abpContent.Replace("__USERRULES__", JsonConvert.SerializeObject(userruleLines, Formatting.Indented))
86+
.Replace("__RULES__", JsonConvert.SerializeObject(gfwLines, Formatting.Indented));
87+
return abpContent;
8588
}
8689

8790
public void UpdatePACFromGFWList(Configuration config)
@@ -92,10 +95,15 @@ public void UpdatePACFromGFWList(Configuration config)
9295
http.DownloadStringAsync(new Uri(GFWLIST_URL));
9396
}
9497

95-
public static List<string> ParseResult(string response)
98+
public static List<string> ParseBase64ToValidList(string response)
9699
{
97100
byte[] bytes = Convert.FromBase64String(response);
98101
string content = Encoding.ASCII.GetString(bytes);
102+
return ParseToValidList(content);
103+
}
104+
105+
private static List<string> ParseToValidList(string content)
106+
{
99107
List<string> valid_lines = new List<string>();
100108
using (var sr = new StringReader(content))
101109
{

shadowsocks-csharp/Data/abp.js.gz

146 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)