From 2a5e67e3f5158fc45657dee97369ab5d3a2b11d4 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 27 Apr 2021 08:56:07 -0400 Subject: [PATCH] Use CDN URLs as fall back URLs Related feedback: - https://github.com/uBlockOrigin/uBlock-issues/issues/1566#issuecomment-826473517 Additionally, add more CDN URLs to default filter lists. --- assets/assets.json | 20 ++++++++++++++++++++ src/js/assets.js | 31 ++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/assets/assets.json b/assets/assets.json index 0a2b9485b..7ec081a73 100644 --- a/assets/assets.json +++ b/assets/assets.json @@ -22,6 +22,11 @@ "contentURL": [ "https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/badlists.txt", "assets/ublock/badlists.txt" + ], + "cdnURLs": [ + "https://gitcdn.xyz/repo/uBlockOrigin/uAssets/master/filters/badlists.txt", + "https://cdn.jsdelivr.net/gh/uBlockOrigin/uAssets@master/filters/badlists.txt", + "https://cdn.statically.io/gh/uBlockOrigin/uAssets/master/filters/badlists.txt" ] }, "ublock-filters": { @@ -47,6 +52,11 @@ "https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/badware.txt", "assets/ublock/badware.txt" ], + "cdnURLs": [ + "https://gitcdn.xyz/repo/uBlockOrigin/uAssets/master/filters/badware.txt", + "https://cdn.jsdelivr.net/gh/uBlockOrigin/uAssets@master/filters/badware.txt", + "https://cdn.statically.io/gh/uBlockOrigin/uAssets/master/filters/badware.txt" + ], "supportURL": "https://github.com/gorhill/uBlock/wiki/Badware-risks", "instructionURL": "https://github.com/gorhill/uBlock/wiki/Badware-risks" }, @@ -57,6 +67,11 @@ "contentURL": [ "https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/privacy.txt", "assets/ublock/privacy.txt" + ], + "cdnURLs": [ + "https://gitcdn.xyz/repo/uBlockOrigin/uAssets/master/filters/privacy.txt", + "https://cdn.jsdelivr.net/gh/uBlockOrigin/uAssets@master/filters/privacy.txt", + "https://cdn.statically.io/gh/uBlockOrigin/uAssets/master/filters/privacy.txt" ] }, "ublock-abuse": { @@ -66,6 +81,11 @@ "contentURL": [ "https://raw.githubusercontent.com/uBlockOrigin/uAssets/master/filters/resource-abuse.txt", "assets/ublock/resource-abuse.txt" + ], + "cdnURLs": [ + "https://gitcdn.xyz/repo/uBlockOrigin/uAssets/master/filters/resource-abuse.txt", + "https://cdn.jsdelivr.net/gh/uBlockOrigin/uAssets@master/filters/resource-abuse.txt", + "https://cdn.statically.io/gh/uBlockOrigin/uAssets/master/filters/resource-abuse.txt" ] }, "ublock-unbreak": { diff --git a/src/js/assets.js b/src/js/assets.js index 310c6ef77..f5a284c57 100644 --- a/src/js/assets.js +++ b/src/js/assets.js @@ -730,14 +730,20 @@ api.get = async function(assetKey, options = {}) { const assetRegistry = await getAssetSourceRegistry(); assetDetails = assetRegistry[assetKey] || {}; - let contentURLs = []; + const contentURLs = []; if ( typeof assetDetails.contentURL === 'string' ) { - contentURLs = [ assetDetails.contentURL ]; + contentURLs.push(assetDetails.contentURL); } else if ( Array.isArray(assetDetails.contentURL) ) { - contentURLs = assetDetails.contentURL.slice(0); + contentURLs.push(...assetDetails.contentURL); } else if ( reIsExternalPath.test(assetKey) ) { assetDetails.content = 'filters'; - contentURLs = [ assetKey ]; + contentURLs.push(assetKey); + } + + // https://github.com/uBlockOrigin/uBlock-issues/issues/1566#issuecomment-826473517 + // Use CDN URLs as fall back URLs. + if ( Array.isArray(assetDetails.cdnURLs) ) { + contentURLs.push(...assetDetails.cdnURLs); } for ( const contentURL of contentURLs ) { @@ -776,24 +782,31 @@ const getRemote = async function(assetKey) { return details; }; - let contentURLs = []; + const contentURLs = []; if ( typeof assetDetails.contentURL === 'string' ) { - contentURLs = [ assetDetails.contentURL ]; + contentURLs.push(assetDetails.contentURL); } else if ( Array.isArray(assetDetails.contentURL) ) { - contentURLs = assetDetails.contentURL.slice(0); + contentURLs.push(...assetDetails.contentURL); } // If asked to be gentle on remote servers, favour using dedicated CDN // servers. If more than one CDN server is present, randomly shuffle the // set of servers so as to spread the bandwidth burden. - if ( remoteServerFriendly && Array.isArray(assetDetails.cdnURLs) ) { + // + // https://github.com/uBlockOrigin/uBlock-issues/issues/1566#issuecomment-826473517 + // In case of manual update, use CDNs URLs as fall back URLs. + if ( Array.isArray(assetDetails.cdnURLs) ) { const cdnURLs = assetDetails.cdnURLs.slice(); for ( let i = 0, n = cdnURLs.length; i < n; i++ ) { const j = Math.floor(Math.random() * n); if ( j === i ) { continue; } [ cdnURLs[j], cdnURLs[i] ] = [ cdnURLs[i], cdnURLs[j] ]; } - contentURLs.unshift(...cdnURLs); + if ( remoteServerFriendly ) { + contentURLs.unshift(...cdnURLs); + } else { + contentURLs.push(...cdnURLs); + } } for ( const contentURL of contentURLs ) {