Improved Adguard rule parser

This commit is contained in:
M66B 2023-12-28 22:59:21 +01:00
parent d7a81f2277
commit f28a086090
1 changed files with 16 additions and 10 deletions

View File

@ -36,6 +36,7 @@ import java.util.regex.Pattern;
public class Adguard { public class Adguard {
// https://github.com/AdguardTeam/AdguardFilters // https://github.com/AdguardTeam/AdguardFilters
// https://github.com/AdguardTeam/FiltersRegistry/blob/master/filters/filter_17_TrackParam/filter.txt // https://github.com/AdguardTeam/FiltersRegistry/blob/master/filters/filter_17_TrackParam/filter.txt
// https://github.com/AdguardTeam/TestCases/tree/master/public/Filters/removeparam-rules
private static final List<String> ADGUARD_IGNORE = Collections.unmodifiableList(Arrays.asList( private static final List<String> ADGUARD_IGNORE = Collections.unmodifiableList(Arrays.asList(
"cookie", "font", "image", "media", "script", "subdocument", "stylesheet", "xmlhttprequest" "cookie", "font", "image", "media", "script", "subdocument", "stylesheet", "xmlhttprequest"
@ -153,7 +154,10 @@ public class Adguard {
break; break;
} }
} else { } else {
if (!name.equals("document") && if (!"document".equals(name) &&
!"image".equals(name) &&
!"important".equals(name) &&
!"script".equals(name) &&
!(name.startsWith("~") && !name.equals("~document"))) { !(name.startsWith("~") && !name.equals("~document"))) {
if (!ADGUARD_IGNORE.contains(name)) if (!ADGUARD_IGNORE.contains(name))
Log.w("Adguard ignoring=" + name); Log.w("Adguard ignoring=" + name);
@ -269,12 +273,11 @@ public class Adguard {
return true; return true;
} }
if (remove.startsWith("~")) {
// $removeparam=~param removes all query parameters with the name different from param. // $removeparam=~param removes all query parameters with the name different from param.
// $removeparam=~/regexp/ removes all query parameters that do not match the regexp regular expression. // $removeparam=~/regexp/ removes all query parameters that do not match the regexp regular expression.
Log.w("Adguard not supported remove=" + remove); boolean not = remove.startsWith("~");
return false; if (not)
} remove = remove.substring(1);
if (remove.startsWith("/")) { if (remove.startsWith("/")) {
// $removeparam=/regexp/[options] // $removeparam=/regexp/[options]
@ -290,15 +293,18 @@ public class Adguard {
String rest = remove.substring(end + 1); String rest = remove.substring(end + 1);
Log.i("Adguard regex=" + regex + " rest=" + rest); Log.i("Adguard regex=" + regex + " rest=" + rest);
if (!TextUtils.isEmpty(rest)) int flags = 0;
if ("i".equals(rest))
flags = Pattern.CASE_INSENSITIVE;
else if (!TextUtils.isEmpty(rest))
Log.w("Adguard unexpected remove=" + remove); Log.w("Adguard unexpected remove=" + remove);
String all = key + "=" + value; String all = key + "=" + value;
if (Pattern.compile(regex).matcher(all).find()) { if (Pattern.compile(regex, flags).matcher(all).find() ^ not) {
Log.i("Adguard omit regex=" + regex); Log.i("Adguard omit regex=" + regex);
return true; return true;
} }
} else if (remove.equals(key)) { } else if (remove.equals(key) ^ not) {
// $removeparam=param // $removeparam=param
Log.i("Adguard omit key=" + key); Log.i("Adguard omit key=" + key);
return true; return true;