@@ -47,21 +47,21 @@ private void http_DownloadStringCompleted(object sender, DownloadStringCompleted
47
47
48
48
public static bool MergeAndWritePACFile ( string gfwListResult )
49
49
{
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 ) )
52
52
{
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 )
55
55
{
56
- foreach ( var rule in sr . NonWhiteSpaceLines ( ) )
57
- {
58
- if ( rule . BeginWithAny ( IgnoredLineBegins ) )
59
- continue ;
60
- lines . Add ( rule ) ;
61
- }
56
+ return false ;
62
57
}
63
58
}
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
+ {
65
65
string abpContent ;
66
66
if ( File . Exists ( PACServer . USER_ABP_FILE ) )
67
67
{
@@ -71,17 +71,20 @@ public static bool MergeAndWritePACFile(string gfwListResult)
71
71
{
72
72
abpContent = Utils . UnGzip ( Resources . abp_js ) ;
73
73
}
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 ) )
76
77
{
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 ) ;
82
80
}
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 ;
85
88
}
86
89
87
90
public void UpdatePACFromGFWList ( Configuration config )
@@ -92,10 +95,15 @@ public void UpdatePACFromGFWList(Configuration config)
92
95
http . DownloadStringAsync ( new Uri ( GFWLIST_URL ) ) ;
93
96
}
94
97
95
- public static List < string > ParseResult ( string response )
98
+ public static List < string > ParseBase64ToValidList ( string response )
96
99
{
97
100
byte [ ] bytes = Convert . FromBase64String ( response ) ;
98
101
string content = Encoding . ASCII . GetString ( bytes ) ;
102
+ return ParseToValidList ( content ) ;
103
+ }
104
+
105
+ private static List < string > ParseToValidList ( string content )
106
+ {
99
107
List < string > valid_lines = new List < string > ( ) ;
100
108
using ( var sr = new StringReader ( content ) )
101
109
{
0 commit comments