2022-10-25 10:53:32 -04:00
|
|
|
import fs from "fs";
|
2022-11-14 18:44:01 -05:00
|
|
|
import * as AdBlockClient from "adblock-rs";
|
2022-10-25 10:53:32 -04:00
|
|
|
|
2021-07-19 15:49:43 -07:00
|
|
|
const RULE_TYPES = ["block", "allowOnly"];
|
|
|
|
|
2021-07-27 09:41:21 -07:00
|
|
|
const ALWAYS_ALLOW = ["https://pywb.proxy/", "http://pywb.proxy/"];
|
|
|
|
|
2021-08-17 20:54:18 -07:00
|
|
|
const BlockState = {
|
|
|
|
ALLOW: null,
|
|
|
|
BLOCK_PAGE_NAV: "page",
|
|
|
|
BLOCK_IFRAME_NAV: "iframe",
|
2022-10-25 10:53:32 -04:00
|
|
|
BLOCK_OTHER: "resource",
|
2022-11-14 18:44:01 -05:00
|
|
|
BLOCK_AD: "advertisement",
|
|
|
|
BLOCK_COOKIE_POPUP: "cookie pop-up"
|
2021-08-17 20:54:18 -07:00
|
|
|
};
|
|
|
|
|
2021-07-19 15:49:43 -07:00
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
class BlockRule
|
|
|
|
{
|
|
|
|
constructor(data) {
|
|
|
|
if (typeof(data) === "string") {
|
|
|
|
this.url = new RegExp(data);
|
|
|
|
this.type = "block";
|
|
|
|
} else {
|
|
|
|
this.url = data.url ? new RegExp(data.url) : null;
|
|
|
|
this.frameTextMatch = data.frameTextMatch ? new RegExp(data.frameTextMatch) : null;
|
|
|
|
this.inFrameUrl = data.inFrameUrl ? new RegExp(data.inFrameUrl) : null;
|
|
|
|
this.type = data.type || "block";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!RULE_TYPES.includes(this.type)) {
|
|
|
|
throw new Error("Rule \"type\" must be: " + RULE_TYPES.join(", "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return `\
|
|
|
|
* Rule for URL Regex: ${this.url}
|
|
|
|
Type: ${this.type}
|
|
|
|
In Frame Regex: ${this.inFrameUrl ? this.inFrameUrl : "any"}
|
|
|
|
Resource Type: ${this.frameTextMatch ? "frame" : "any"}
|
|
|
|
${this.frameTextMatch ? "Frame Text Regex: " + this.frameTextMatch : ""}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ===========================================================================
|
2022-10-24 15:30:10 +02:00
|
|
|
export class BlockRules
|
2021-07-19 15:49:43 -07:00
|
|
|
{
|
2022-11-14 18:44:01 -05:00
|
|
|
constructor(blockRules, blockPutUrl, blockErrMsg, blockAds, adBlockErrMsg, blockCookiePopups, debugLog) {
|
2021-07-19 15:49:43 -07:00
|
|
|
this.rules = [];
|
|
|
|
this.blockPutUrl = blockPutUrl;
|
|
|
|
this.blockErrMsg = blockErrMsg;
|
2022-11-14 18:44:01 -05:00
|
|
|
this.blockAds = blockAds;
|
|
|
|
this.adBlockErrMsg = adBlockErrMsg;
|
|
|
|
this.blockCookiePopups = blockCookiePopups;
|
2021-07-27 09:41:21 -07:00
|
|
|
this.debugLog = debugLog;
|
2021-08-17 20:54:18 -07:00
|
|
|
|
2022-11-14 18:44:01 -05:00
|
|
|
this.adhosts = JSON.parse(fs.readFileSync(new URL("../ad-hosts.json", import.meta.url)));
|
|
|
|
if (this.blockCookiePopups) {
|
|
|
|
const easylistRules = fs.readFileSync(new URL("../easylist-cookies.txt", import.meta.url), { encoding: "utf-8" }).split("\n");
|
|
|
|
const filterSet = new AdBlockClient.FilterSet(true);
|
|
|
|
filterSet.addFilters(easylistRules);
|
|
|
|
this.cookieBlockClient = new AdBlockClient.Engine(filterSet, true);
|
|
|
|
}
|
|
|
|
|
2021-08-17 20:54:18 -07:00
|
|
|
this.blockedUrlSet = new Set();
|
2021-07-19 15:49:43 -07:00
|
|
|
|
|
|
|
for (const ruleData of blockRules) {
|
|
|
|
this.rules.push(new BlockRule(ruleData));
|
|
|
|
}
|
|
|
|
|
2021-07-20 15:56:30 -07:00
|
|
|
if (this.rules.length) {
|
2021-07-27 09:41:21 -07:00
|
|
|
this.debugLog("URL Block Rules:\n");
|
2021-07-20 15:56:30 -07:00
|
|
|
for (const rule of this.rules) {
|
2021-07-27 09:41:21 -07:00
|
|
|
this.debugLog(rule.toString());
|
2021-07-20 15:56:30 -07:00
|
|
|
}
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async initPage(page) {
|
2021-08-17 20:54:18 -07:00
|
|
|
if (page._btrix_interceptionAdded) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
page._btrix_interceptionAdded = true;
|
|
|
|
|
2021-07-19 15:49:43 -07:00
|
|
|
await page.setRequestInterception(true);
|
|
|
|
|
0.4.1 Release! (#70)
* optimization: don't intercept requests if no blockRules set
* page load: set waitUntil to use networkidle2 instead of networkidle0 as reasonable default for most pages
* add --behaviorTimeout to set max running time for behaviors (defaults to 90 seconds)
* refactor profile loadProfile/saveProfile to util/browser.js
- support augmenting existing profile when creating a new profile
* screencasting: convert newContext to window instead of page by default, instead of just warning about it
* shared multiplatform image support:
- determine browser exe from list of options, getBrowserExe() returns current exe
- supports running with 'google-chrome' under amd64, and 'chromium-browser' under arm64
- update to multiplatform oldwebtoday/chrome:91 as browser image
- enable multiplatform build with latest build-push-action@v2
* seeds: add trim() to seed URLs
* logging: reduce initial debug logging, enable only if '--logging debug' is set. log if profile, text-extraction enabled, and post-processing stages automatically
* profile creation: add --windowSize flag, set default to 1600x900, default to loading Application tab, tweak UI styles
* extractLinks: support passing in custom property to get link, and also loading as an attribute via getAttribute. Fixes #25
* update CHANGES and README with new features
* bump version to 0.4.1
2021-07-22 14:24:51 -07:00
|
|
|
page.on("request", async (request) => {
|
|
|
|
try {
|
|
|
|
await this.handleRequest(request);
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
});
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async handleRequest(request) {
|
|
|
|
const url = request.url();
|
|
|
|
|
2021-08-17 20:54:18 -07:00
|
|
|
let blockState;
|
2021-07-19 15:49:43 -07:00
|
|
|
|
2021-08-17 20:54:18 -07:00
|
|
|
try {
|
2022-11-14 18:44:01 -05:00
|
|
|
if (this.blockAds) {
|
|
|
|
blockState = await this.shouldBlockAd(request, url);
|
|
|
|
if (blockState === BlockState.BLOCK_AD) {
|
|
|
|
return await request.abort("blockedbyclient");
|
|
|
|
}
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
2022-11-14 18:44:01 -05:00
|
|
|
if (this.blockCookiePopups) {
|
|
|
|
blockState = await this.shouldBlockCookiePopup(request, url);
|
|
|
|
if (blockState === BlockState.BLOCK_COOKIE_POPUP) {
|
|
|
|
return await request.abort("blockedbyclient");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.rules.length) {
|
|
|
|
blockState = await this.shouldBlock(request, url);
|
|
|
|
if (blockState !== BlockState.ALLOW) {
|
|
|
|
return await request.abort("blockedbyclient");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await request.continue();
|
2021-08-17 20:54:18 -07:00
|
|
|
} catch (e) {
|
|
|
|
this.debugLog(`Block: (${blockState}) Failed On: ${url} Reason: ${e}`);
|
|
|
|
}
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
2021-08-17 20:54:18 -07:00
|
|
|
async shouldBlock(request, url) {
|
|
|
|
if (!url.startsWith("http:") && !url.startsWith("https:")) {
|
|
|
|
return BlockState.ALLOW;
|
|
|
|
}
|
2021-07-19 15:49:43 -07:00
|
|
|
|
2021-07-27 09:41:21 -07:00
|
|
|
const isNavReq = request.isNavigationRequest();
|
|
|
|
|
|
|
|
const frame = request.frame();
|
|
|
|
|
2021-08-17 20:54:18 -07:00
|
|
|
let frameUrl = "";
|
|
|
|
let blockState;
|
2021-07-27 09:41:21 -07:00
|
|
|
|
|
|
|
if (isNavReq) {
|
|
|
|
const parentFrame = frame.parentFrame();
|
|
|
|
if (parentFrame) {
|
|
|
|
frameUrl = parentFrame.url();
|
2021-08-17 20:54:18 -07:00
|
|
|
blockState = BlockState.BLOCK_IFRAME_NAV;
|
2021-07-27 09:41:21 -07:00
|
|
|
} else {
|
|
|
|
frameUrl = frame.url();
|
2021-08-17 20:54:18 -07:00
|
|
|
blockState = BlockState.BLOCK_PAGE_NAV;
|
2021-07-27 09:41:21 -07:00
|
|
|
}
|
|
|
|
} else {
|
2021-08-17 20:54:18 -07:00
|
|
|
frameUrl = frame ? frame.url() : "";
|
|
|
|
blockState = BlockState.BLOCK_OTHER;
|
2021-07-27 09:41:21 -07:00
|
|
|
}
|
2021-07-19 15:49:43 -07:00
|
|
|
|
|
|
|
// ignore initial page
|
|
|
|
if (frameUrl === "about:blank") {
|
2021-08-17 20:54:18 -07:00
|
|
|
return BlockState.ALLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
// always allow special pywb proxy script
|
|
|
|
for (const allowUrl of ALWAYS_ALLOW) {
|
|
|
|
if (url.startsWith(allowUrl)) {
|
|
|
|
return BlockState.ALLOW;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const rule of this.rules) {
|
|
|
|
const {done, block} = await this.ruleCheck(rule, request, url, frameUrl, isNavReq);
|
|
|
|
|
|
|
|
if (block) {
|
|
|
|
if (blockState === BlockState.BLOCK_PAGE_NAV) {
|
|
|
|
console.warn(`Warning: Block rule match for page request "${url}" ignored, set --exclude to block full pages`);
|
|
|
|
return BlockState.ALLOW;
|
|
|
|
}
|
|
|
|
this.debugLog(`URL Blocked/Aborted: ${url} in frame ${frameUrl}`);
|
|
|
|
await this.recordBlockMsg(url);
|
|
|
|
return blockState;
|
|
|
|
}
|
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
2021-08-17 20:54:18 -07:00
|
|
|
return BlockState.ALLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
async ruleCheck(rule, request, reqUrl, frameUrl, isNavReq) {
|
|
|
|
const {url, inFrameUrl, frameTextMatch} = rule;
|
|
|
|
|
|
|
|
const type = rule.type || "block";
|
|
|
|
const allowOnly = (type === "allowOnly");
|
|
|
|
|
2021-07-19 15:49:43 -07:00
|
|
|
// not a frame match, skip rule
|
|
|
|
if (inFrameUrl && !frameUrl.match(inFrameUrl)) {
|
2021-08-17 20:54:18 -07:00
|
|
|
return {block: false, done: false};
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const urlMatched = (url && reqUrl.match(url));
|
|
|
|
|
|
|
|
// if frame text-based rule: if url matched and a frame request
|
|
|
|
// frame text-based match: only applies to nav requests, never block otherwise
|
|
|
|
if (frameTextMatch) {
|
2021-07-27 09:41:21 -07:00
|
|
|
if (!urlMatched || !isNavReq) {
|
2021-08-17 20:54:18 -07:00
|
|
|
return {block: false, done: false};
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const block = await this.isTextMatch(request, reqUrl, frameTextMatch) ? !allowOnly : allowOnly;
|
2021-07-27 09:41:21 -07:00
|
|
|
this.debugLog(`iframe ${url} conditionally ${block ? "BLOCKED" : "ALLOWED"}, parent frame ${frameUrl}`);
|
2021-08-17 20:54:18 -07:00
|
|
|
return {block, done: true};
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// for non frame text rule, simply match by URL
|
|
|
|
const block = urlMatched ? !allowOnly : allowOnly;
|
2021-08-17 20:54:18 -07:00
|
|
|
return {block, done: false};
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async isTextMatch(request, reqUrl, frameTextMatch) {
|
|
|
|
try {
|
|
|
|
const res = await fetch(reqUrl);
|
|
|
|
const text = await res.text();
|
|
|
|
|
|
|
|
return !!text.match(frameTextMatch);
|
|
|
|
|
|
|
|
} catch (e) {
|
2021-07-27 09:41:21 -07:00
|
|
|
this.debugLog(e);
|
2021-07-19 15:49:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 18:44:01 -05:00
|
|
|
async shouldBlockAd(request, url) {
|
|
|
|
const fragments = url.split("/");
|
|
|
|
const domain = fragments.length > 2 ? fragments[2] : null;
|
|
|
|
if (this.adhosts.includes(domain)) {
|
|
|
|
this.debugLog(`URL blocked for being an ad: ${url}`);
|
|
|
|
await this.recordBlockMsg(url, true);
|
|
|
|
return BlockState.BLOCK_AD;
|
|
|
|
}
|
|
|
|
return BlockState.ALLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
async shouldBlockCookiePopup(request, url) {
|
|
|
|
const checkResult = this.cookieBlockClient.check(url, "", "");
|
|
|
|
if (checkResult != false) {
|
|
|
|
this.debugLog(`URL blocked for being a cookie pop-up: ${url}`);
|
|
|
|
return BlockState.BLOCK_COOKIE_POPUP;
|
|
|
|
}
|
|
|
|
return BlockState.ALLOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
async recordBlockMsg(url, ad=false) {
|
2021-08-17 20:54:18 -07:00
|
|
|
if (this.blockedUrlSet.has(url)) {
|
2021-07-19 15:49:43 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-17 20:54:18 -07:00
|
|
|
this.blockedUrlSet.add(url);
|
|
|
|
|
|
|
|
if (!this.blockErrMsg || !this.blockPutUrl) {
|
2021-07-19 15:49:43 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-14 18:44:01 -05:00
|
|
|
let body = this.blockErrMsg;
|
|
|
|
if (ad) {
|
|
|
|
body = this.adBlockErrMessage;
|
|
|
|
}
|
2021-07-19 15:49:43 -07:00
|
|
|
const putUrl = new URL(this.blockPutUrl);
|
|
|
|
putUrl.searchParams.set("url", url);
|
|
|
|
await fetch(putUrl.href, {method: "PUT", headers: {"Content-Type": "text/html"}, body});
|
|
|
|
}
|
|
|
|
}
|