2017-09-02 04:11:33 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2023-12-04 10:10:34 -07:00
|
|
|
uBlock Origin - a comprehensive, efficient content blocker
|
2018-12-13 10:30:54 -07:00
|
|
|
Copyright (C) 2017-present Raymond Hill
|
2017-09-02 04:11:33 -06:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2021-07-25 08:55:35 -06:00
|
|
|
import {
|
|
|
|
domainFromHostname,
|
|
|
|
hostnameFromNetworkURL,
|
|
|
|
} from './uri-utils.js';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2024-09-13 09:20:37 -06:00
|
|
|
const dnsAPI = browser.dns || {
|
|
|
|
resolve() {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-12 09:19:57 -06:00
|
|
|
const isPromise = o => o instanceof Promise;
|
2024-09-13 08:00:41 -06:00
|
|
|
const isResolvedObject = o => o instanceof Object &&
|
|
|
|
o instanceof Promise === false;
|
2024-09-12 09:19:57 -06:00
|
|
|
const reIPv4 = /^\d+\.\d+\.\d+\.\d+$/
|
2024-10-02 12:51:26 -06:00
|
|
|
const skipDNS = proxyInfo =>
|
2024-10-07 09:56:22 -06:00
|
|
|
proxyInfo && (proxyInfo.proxyDNS || proxyInfo.type?.charCodeAt(0) === 0x68 /* h */);
|
2020-03-23 11:31:43 -06:00
|
|
|
|
2024-09-13 08:00:41 -06:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
// Related issues:
|
|
|
|
// - https://github.com/gorhill/uBlock/issues/1327
|
|
|
|
// - https://github.com/uBlockOrigin/uBlock-issues/issues/128
|
|
|
|
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
|
2017-09-02 04:11:33 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
// Extend base class to normalize as per platform.
|
2017-09-02 04:11:33 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
vAPI.Net = class extends vAPI.Net {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.pendingRequests = [];
|
2024-09-12 09:19:57 -06:00
|
|
|
this.dnsList = []; // ring buffer
|
|
|
|
this.dnsWritePtr = 0; // next write pointer in ring buffer
|
2024-09-22 08:02:45 -06:00
|
|
|
this.dnsMaxCount = 512; // max size of ring buffer
|
2024-09-12 09:19:57 -06:00
|
|
|
this.dnsDict = new Map(); // hn to index in ring buffer
|
2024-09-22 08:02:45 -06:00
|
|
|
this.dnsCacheTTL = 600; // TTL in seconds
|
2024-09-12 09:19:57 -06:00
|
|
|
this.canUncloakCnames = true;
|
|
|
|
this.cnameUncloakEnabled = true;
|
2022-11-29 10:00:38 -07:00
|
|
|
this.cnameIgnoreList = null;
|
|
|
|
this.cnameIgnore1stParty = true;
|
|
|
|
this.cnameIgnoreExceptions = true;
|
|
|
|
this.cnameIgnoreRootDocument = true;
|
|
|
|
this.cnameReplayFullURL = false;
|
2024-09-19 06:43:54 -06:00
|
|
|
this.dnsResolveEnabled = true;
|
2022-11-29 10:00:38 -07:00
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
setOptions(options) {
|
|
|
|
super.setOptions(options);
|
|
|
|
if ( 'cnameUncloakEnabled' in options ) {
|
2024-09-12 09:19:57 -06:00
|
|
|
this.cnameUncloakEnabled =
|
2022-11-29 10:00:38 -07:00
|
|
|
options.cnameUncloakEnabled !== false;
|
2019-11-19 10:05:33 -07:00
|
|
|
}
|
2022-11-29 10:00:38 -07:00
|
|
|
if ( 'cnameIgnoreList' in options ) {
|
|
|
|
this.cnameIgnoreList =
|
|
|
|
this.regexFromStrList(options.cnameIgnoreList);
|
|
|
|
}
|
|
|
|
if ( 'cnameIgnore1stParty' in options ) {
|
|
|
|
this.cnameIgnore1stParty =
|
|
|
|
options.cnameIgnore1stParty !== false;
|
|
|
|
}
|
|
|
|
if ( 'cnameIgnoreExceptions' in options ) {
|
|
|
|
this.cnameIgnoreExceptions =
|
|
|
|
options.cnameIgnoreExceptions !== false;
|
|
|
|
}
|
|
|
|
if ( 'cnameIgnoreRootDocument' in options ) {
|
|
|
|
this.cnameIgnoreRootDocument =
|
|
|
|
options.cnameIgnoreRootDocument !== false;
|
|
|
|
}
|
|
|
|
if ( 'cnameReplayFullURL' in options ) {
|
|
|
|
this.cnameReplayFullURL = options.cnameReplayFullURL === true;
|
|
|
|
}
|
2024-09-22 08:02:45 -06:00
|
|
|
if ( 'dnsCacheTTL' in options ) {
|
|
|
|
this.dnsCacheTTL = options.dnsCacheTTL;
|
|
|
|
}
|
2024-09-19 06:43:54 -06:00
|
|
|
if ( 'dnsResolveEnabled' in options ) {
|
|
|
|
this.dnsResolveEnabled = options.dnsResolveEnabled === true;
|
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
this.dnsList.fill(null);
|
|
|
|
this.dnsDict.clear();
|
2022-11-29 10:00:38 -07:00
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
normalizeDetails(details) {
|
2024-09-17 08:26:40 -06:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/3379
|
2024-10-02 12:51:26 -06:00
|
|
|
if ( skipDNS(details.proxyInfo) && details.ip === '0.0.0.0' ) {
|
2024-09-17 08:26:40 -06:00
|
|
|
details.ip = null;
|
|
|
|
}
|
2022-11-29 10:00:38 -07:00
|
|
|
const type = details.type;
|
|
|
|
if ( type === 'imageset' ) {
|
|
|
|
details.type = 'image';
|
|
|
|
return;
|
|
|
|
}
|
2024-10-02 11:39:36 -06:00
|
|
|
if ( type !== 'object' ) { return; }
|
|
|
|
// Try to extract type from response headers if present.
|
|
|
|
if ( details.responseHeaders === undefined ) { return; }
|
|
|
|
const ctype = this.headerValue(details.responseHeaders, 'content-type');
|
2022-11-29 10:00:38 -07:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/345
|
|
|
|
// Re-categorize an embedded object as a `sub_frame` if its
|
|
|
|
// content type is that of a HTML document.
|
2024-10-02 11:39:36 -06:00
|
|
|
if ( ctype === 'text/html' ) {
|
|
|
|
details.type = 'sub_frame';
|
2018-12-16 08:51:25 -07:00
|
|
|
}
|
2022-11-29 10:00:38 -07:00
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
denormalizeTypes(types) {
|
|
|
|
if ( types.length === 0 ) {
|
|
|
|
return Array.from(this.validTypes);
|
|
|
|
}
|
|
|
|
const out = new Set();
|
|
|
|
for ( const type of types ) {
|
|
|
|
if ( this.validTypes.has(type) ) {
|
|
|
|
out.add(type);
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
2022-11-29 10:00:38 -07:00
|
|
|
if ( type === 'image' && this.validTypes.has('imageset') ) {
|
|
|
|
out.add('imageset');
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
2022-11-29 10:00:38 -07:00
|
|
|
if ( type === 'sub_frame' ) {
|
|
|
|
out.add('object');
|
2019-12-31 14:36:51 -07:00
|
|
|
}
|
|
|
|
}
|
2022-11-29 10:00:38 -07:00
|
|
|
return Array.from(out);
|
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
canonicalNameFromHostname(hn) {
|
2024-09-12 09:19:57 -06:00
|
|
|
if ( hn === '' ) { return; }
|
2024-09-22 08:02:45 -06:00
|
|
|
const dnsEntry = this.dnsFromCache(hn, true);
|
2024-09-13 08:00:41 -06:00
|
|
|
if ( isResolvedObject(dnsEntry) === false ) { return; }
|
|
|
|
return dnsEntry.cname;
|
2022-11-29 10:00:38 -07:00
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
regexFromStrList(list) {
|
2024-09-12 09:19:57 -06:00
|
|
|
if ( typeof list !== 'string' || list.length === 0 || list === 'unset' ) {
|
2022-11-29 10:00:38 -07:00
|
|
|
return null;
|
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
if ( list === '*' ) { return /^./; }
|
2022-11-29 10:00:38 -07:00
|
|
|
return new RegExp(
|
2024-09-10 12:58:40 -06:00
|
|
|
'(?:^|\\.)(?:' +
|
2022-11-29 10:00:38 -07:00
|
|
|
list.trim()
|
|
|
|
.split(/\s+/)
|
|
|
|
.map(a => a.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
|
|
.join('|') +
|
|
|
|
')$'
|
|
|
|
);
|
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
onBeforeSuspendableRequest(details) {
|
2024-09-12 09:19:57 -06:00
|
|
|
const hn = hostnameFromNetworkURL(details.url);
|
|
|
|
const dnsEntry = this.dnsFromCache(hn);
|
2024-09-13 08:00:41 -06:00
|
|
|
if ( isResolvedObject(dnsEntry) && dnsEntry.ip ) {
|
2024-09-12 09:19:57 -06:00
|
|
|
details.ip = dnsEntry.ip;
|
|
|
|
}
|
2022-11-29 10:00:38 -07:00
|
|
|
const r = super.onBeforeSuspendableRequest(details);
|
|
|
|
if ( r !== undefined ) {
|
2019-11-19 10:05:33 -07:00
|
|
|
if (
|
2022-11-29 10:00:38 -07:00
|
|
|
r.cancel === true ||
|
|
|
|
r.redirectUrl !== undefined ||
|
|
|
|
this.cnameIgnoreExceptions
|
2019-11-19 10:05:33 -07:00
|
|
|
) {
|
2022-11-29 10:00:38 -07:00
|
|
|
return r;
|
2019-11-19 10:05:33 -07:00
|
|
|
}
|
|
|
|
}
|
2024-09-13 08:00:41 -06:00
|
|
|
if ( isResolvedObject(dnsEntry) ) {
|
|
|
|
return this.onAfterDNSResolution(hn, details, dnsEntry);
|
2022-11-29 10:00:38 -07:00
|
|
|
}
|
2024-10-02 12:51:26 -06:00
|
|
|
if ( skipDNS(details.proxyInfo) ) { return; }
|
2024-09-12 09:19:57 -06:00
|
|
|
if ( this.dnsShouldResolve(hn) === false ) { return; }
|
|
|
|
const promise = dnsEntry || this.dnsResolve(hn, details);
|
|
|
|
return promise.then(( ) => this.onAfterDNSResolution(hn, details));
|
|
|
|
}
|
|
|
|
|
|
|
|
onAfterDNSResolution(hn, details, dnsEntry) {
|
|
|
|
if ( dnsEntry === undefined ) {
|
|
|
|
dnsEntry = this.dnsFromCache(hn);
|
2024-09-13 08:00:41 -06:00
|
|
|
if ( isResolvedObject(dnsEntry) === false ) { return; }
|
2024-09-12 09:19:57 -06:00
|
|
|
}
|
|
|
|
let proceed = false;
|
|
|
|
if ( dnsEntry.cname && this.cnameUncloakEnabled ) {
|
|
|
|
const newURL = this.uncloakURL(hn, dnsEntry, details);
|
|
|
|
if ( newURL ) {
|
|
|
|
details.aliasURL = details.url;
|
|
|
|
details.url = newURL;
|
|
|
|
proceed = true;
|
2019-11-19 10:05:33 -07:00
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
}
|
|
|
|
if ( dnsEntry.ip && details.ip !== dnsEntry.ip ) {
|
|
|
|
details.ip = dnsEntry.ip
|
|
|
|
proceed = true;
|
|
|
|
}
|
|
|
|
if ( proceed === false ) { return; }
|
|
|
|
// Must call method on base class
|
|
|
|
return super.onBeforeSuspendableRequest(details);
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsToCache(hn, record, details) {
|
2024-09-22 08:02:45 -06:00
|
|
|
const dnsEntry = { hn, until: Date.now() + this.dnsCacheTTL * 1000 };
|
2024-09-12 09:19:57 -06:00
|
|
|
if ( record ) {
|
|
|
|
const cname = this.cnameFromRecord(hn, record, details);
|
|
|
|
if ( cname ) { dnsEntry.cname = cname; }
|
|
|
|
const ip = this.ipFromRecord(record);
|
|
|
|
if ( ip ) { dnsEntry.ip = ip; }
|
|
|
|
}
|
2024-09-13 08:00:41 -06:00
|
|
|
this.dnsSetCache(-1, hn, dnsEntry);
|
2024-09-12 09:19:57 -06:00
|
|
|
return dnsEntry;
|
|
|
|
}
|
|
|
|
|
2024-09-22 08:02:45 -06:00
|
|
|
dnsFromCache(hn, passive = false) {
|
2024-09-12 09:19:57 -06:00
|
|
|
const i = this.dnsDict.get(hn);
|
|
|
|
if ( i === undefined ) { return; }
|
2024-09-13 08:00:41 -06:00
|
|
|
if ( isPromise(i) ) { return i; }
|
2024-09-12 09:19:57 -06:00
|
|
|
const dnsEntry = this.dnsList[i];
|
2024-09-13 08:00:41 -06:00
|
|
|
if ( dnsEntry !== null && dnsEntry.hn === hn ) {
|
2024-09-22 08:02:45 -06:00
|
|
|
if ( passive || dnsEntry.until >= Date.now() ) {
|
2024-09-13 08:00:41 -06:00
|
|
|
return dnsEntry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.dnsSetCache(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsSetCache(i, hn, after) {
|
|
|
|
if ( i < 0 ) {
|
|
|
|
const j = this.dnsDict.get(hn);
|
|
|
|
if ( typeof j === 'number' ) {
|
|
|
|
this.dnsList[j] = after;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
i = this.dnsWritePtr++;
|
|
|
|
this.dnsWritePtr %= this.dnsMaxCount;
|
|
|
|
}
|
|
|
|
const before = this.dnsList[i];
|
|
|
|
if ( before ) {
|
|
|
|
this.dnsDict.delete(before.hn);
|
|
|
|
}
|
|
|
|
if ( after ) {
|
|
|
|
this.dnsDict.set(hn, i);
|
|
|
|
this.dnsList[i] = after;
|
|
|
|
} else {
|
|
|
|
if ( hn ) { this.dnsDict.delete(hn); }
|
|
|
|
this.dnsList[i] = null;
|
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dnsShouldResolve(hn) {
|
2024-09-19 06:43:54 -06:00
|
|
|
if ( this.dnsResolveEnabled === false ) { return false; }
|
2024-09-12 09:19:57 -06:00
|
|
|
if ( hn === '' ) { return false; }
|
|
|
|
const c0 = hn.charCodeAt(0);
|
|
|
|
if ( c0 === 0x5B /* [ */ ) { return false; }
|
|
|
|
if ( c0 > 0x39 /* 9 */ ) { return true; }
|
|
|
|
return reIPv4.test(hn) === false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsResolve(hn, details) {
|
|
|
|
const promise = dnsAPI.resolve(hn, [ 'canonical_name' ]).then(
|
|
|
|
rec => this.dnsToCache(hn, rec, details),
|
|
|
|
( ) => this.dnsToCache(hn)
|
2022-11-29 10:00:38 -07:00
|
|
|
);
|
2024-09-13 08:00:41 -06:00
|
|
|
this.dnsDict.set(hn, promise);
|
|
|
|
return promise;
|
2022-11-29 10:00:38 -07:00
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
|
|
|
|
cnameFromRecord(hn, record, details) {
|
|
|
|
const cn = record.canonicalName;
|
|
|
|
if ( cn === undefined ) { return; }
|
|
|
|
if ( cn === hn ) { return; }
|
|
|
|
if ( this.cnameIgnore1stParty ) {
|
|
|
|
if ( domainFromHostname(cn) === domainFromHostname(hn) ) { return; }
|
|
|
|
}
|
|
|
|
if ( this.cnameIgnoreList !== null ) {
|
|
|
|
if ( this.cnameIgnoreList.test(cn) === false ) { return; }
|
|
|
|
}
|
|
|
|
if ( this.cnameIgnoreRootDocument ) {
|
|
|
|
const origin = hostnameFromNetworkURL(details.documentUrl || details.url);
|
|
|
|
if ( hn === origin ) { return; }
|
|
|
|
}
|
|
|
|
return cn;
|
|
|
|
}
|
|
|
|
|
|
|
|
uncloakURL(hn, dnsEntry, details) {
|
|
|
|
const hnBeg = details.url.indexOf(hn);
|
|
|
|
if ( hnBeg === -1 ) { return; }
|
|
|
|
const oldURL = details.url;
|
|
|
|
const newURL = oldURL.slice(0, hnBeg) + dnsEntry.cname;
|
|
|
|
const hnEnd = hnBeg + hn.length;
|
|
|
|
if ( this.cnameReplayFullURL ) {
|
|
|
|
return newURL + oldURL.slice(hnEnd);
|
|
|
|
}
|
|
|
|
const pathBeg = oldURL.indexOf('/', hnEnd);
|
|
|
|
if ( pathBeg !== -1 ) {
|
|
|
|
return newURL + oldURL.slice(hnEnd, pathBeg + 1);
|
|
|
|
}
|
|
|
|
return newURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipFromRecord(record) {
|
|
|
|
const { addresses } = record;
|
|
|
|
if ( Array.isArray(addresses) === false ) { return; }
|
|
|
|
if ( addresses.length === 0 ) { return; }
|
2024-09-20 05:20:55 -06:00
|
|
|
return addresses.join('\n');
|
2024-09-12 09:19:57 -06:00
|
|
|
}
|
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
suspendOneRequest(details) {
|
|
|
|
const pending = {
|
|
|
|
details: Object.assign({}, details),
|
|
|
|
resolve: undefined,
|
|
|
|
promise: undefined
|
|
|
|
};
|
|
|
|
pending.promise = new Promise(resolve => {
|
|
|
|
pending.resolve = resolve;
|
|
|
|
});
|
|
|
|
this.pendingRequests.push(pending);
|
|
|
|
return pending.promise;
|
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
unsuspendAllRequests(discard = false) {
|
|
|
|
const pendingRequests = this.pendingRequests;
|
|
|
|
this.pendingRequests = [];
|
|
|
|
for ( const entry of pendingRequests ) {
|
|
|
|
entry.resolve(
|
|
|
|
discard !== true
|
|
|
|
? this.onBeforeSuspendableRequest(entry.details)
|
|
|
|
: undefined
|
2019-11-21 10:04:19 -07:00
|
|
|
);
|
2019-11-19 10:05:33 -07:00
|
|
|
}
|
2022-11-29 10:00:38 -07:00
|
|
|
}
|
2024-09-12 09:19:57 -06:00
|
|
|
|
2022-11-29 10:00:38 -07:00
|
|
|
static canSuspend() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2018-12-23 15:59:31 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|
2022-12-12 12:02:57 -07:00
|
|
|
|
2023-05-09 10:44:14 -06:00
|
|
|
vAPI.scriptletsInjector = ((doc, details) => {
|
2022-12-12 12:02:57 -07:00
|
|
|
let script, url;
|
|
|
|
try {
|
2023-05-09 10:44:14 -06:00
|
|
|
const blob = new self.Blob(
|
|
|
|
[ details.scriptlets ],
|
|
|
|
{ type: 'text/javascript; charset=utf-8' }
|
|
|
|
);
|
2022-12-12 12:02:57 -07:00
|
|
|
url = self.URL.createObjectURL(blob);
|
|
|
|
script = doc.createElement('script');
|
|
|
|
script.async = false;
|
|
|
|
script.src = url;
|
2023-05-23 19:49:42 -06:00
|
|
|
(doc.head || doc.documentElement || doc).append(script);
|
2023-05-09 10:44:14 -06:00
|
|
|
self.uBO_scriptletsInjected = details.filters;
|
2022-12-12 12:02:57 -07:00
|
|
|
} catch (ex) {
|
|
|
|
}
|
|
|
|
if ( url ) {
|
|
|
|
if ( script ) { script.remove(); }
|
|
|
|
self.URL.revokeObjectURL(url);
|
|
|
|
}
|
|
|
|
}).toString();
|
|
|
|
|
|
|
|
/******************************************************************************/
|