From 34ae93af27c996628c9bd059ce50bc7500da4d11 Mon Sep 17 00:00:00 2001 From: supernova <76390419+driedpampas@users.noreply.github.com> Date: Fri, 29 Sep 2023 12:35:08 +0300 Subject: [PATCH] fix: bg code not executing correctly on firefox partially fix clictune bypass --- .github/workflows/codeql-analysis.yml | 4 +- .github/workflows/main.yml | 2 +- scripts/build_js/injection_script_template.js | 6 +- src/bypasses/clictune.js | 2 +- src/js/background.js | 307 +++++++++--------- 5 files changed, 159 insertions(+), 162 deletions(-) diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index e8d8645f7..18b61fbb1 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -38,11 +38,11 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v2 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b7d215376..1189dba82 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,7 +2,7 @@ name: Package on: push: - branches: [main, manifest-v3] + branches: [main, manifest-v3, fixes] workflow_dispatch: diff --git a/scripts/build_js/injection_script_template.js b/scripts/build_js/injection_script_template.js index 7f006b610..e31c826bf 100644 --- a/scripts/build_js/injection_script_template.js +++ b/scripts/build_js/injection_script_template.js @@ -6,12 +6,14 @@ function matchingBypass(bypasses) { for (const [key] of Object.entries(bypasses)) { if (key.charAt(0) === '/' && key.charAt(key.length - 1) === '/') { let pattern = new RegExp(key.substring(1, key.length - 1)); - return pattern.test(location.href) ? key : null; + if (pattern.test(location.href)) { + return key; + } } else if (key === location.host) { return key; } - return null; } + return null; } if (matchingBypass(bypasses)) { diff --git a/src/bypasses/clictune.js b/src/bypasses/clictune.js index 3098007df..cbf61bc75 100644 --- a/src/bypasses/clictune.js +++ b/src/bypasses/clictune.js @@ -16,4 +16,4 @@ export default class Clictune extends BypassDefinition { } } -export const matches = ['www.dlink2.com'] +export const matches = ['www.dlink2.net', 'www.dlink2.com', 'www.clictune.com'] diff --git a/src/js/background.js b/src/js/background.js index 58d62ab07..1152b711b 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -12,189 +12,184 @@ if (isFirefox) { browser.tabs.create({ url: 'html/consent.html', }); - } else { - // If consent has been already granted, execute background script - executeBackgroundScript(); + return; } }); + return; } }); -} else { - // For non-Firefox browsers, execute background script - executeBackgroundScript(); } -function executeBackgroundScript() { - const brws = typeof browser !== 'undefined' ? browser : chrome; - const fetchDomains = ['crowd.fastforward.team', 'redirect-api.work.ink']; //only allow requests to these domains +const brws = typeof browser !== 'undefined' ? browser : chrome; +const fetchDomains = ['crowd.fastforward.team', 'redirect-api.work.ink']; //only allow requests to these domains - async function getOptions() { - return new Promise((resolve) => { - brws.storage.local.get('options').then((result) => { - resolve(result.options); - }); +async function getOptions() { + return new Promise((resolve) => { + brws.storage.local.get('options').then((result) => { + resolve(result.options); }); - } + }); +} - function ffclipboardClear() { - brws.storage.local.set({ ff_clipboard: '{}' }); - } +function ffclipboardClear() { + brws.storage.local.set({ ff_clipboard: '{}' }); +} - function clearCrowdIgnoredURLs() { - brws.storage.local.set({ crowd_ignore: '{}' }); - } +function clearCrowdIgnoredURLs() { + brws.storage.local.set({ crowd_ignore: '{}' }); +} - function firstrun(details) { - if (details.reason == 'install' || details.reason == 'update') { - brws.tabs.create({ url: 'https://fastforward.team/firstrun' }); - ffclipboardClear(); - brws.storage.local.set({ tempDisableCrowd: 'false' }); - brws.storage.local.set({ version: brws.runtime.getManifest().version }); - brws.runtime.openOptionsPage(); //required for loading default options, to do: implement a better way - brws.declarativeNetRequest.updateDynamicRules({ - addRules: constants.beforeNavigateRules, - removeRuleIds: constants.beforeNavigateRules.map((rule) => rule.id), - }); - } +function firstrun(details) { + if (details.reason == 'install' || details.reason == 'update') { + brws.tabs.create({ url: 'https://fastforward.team/firstrun' }); + ffclipboardClear(); + brws.storage.local.set({ tempDisableCrowd: 'false' }); + brws.storage.local.set({ version: brws.runtime.getManifest().version }); + brws.runtime.openOptionsPage(); //required for loading default options, to do: implement a better way + brws.declarativeNetRequest.updateDynamicRules({ + addRules: constants.beforeNavigateRules, + removeRuleIds: constants.beforeNavigateRules.map((rule) => rule.id), + }); } +} - function preflight(details) { - let url = new URL(details.url); - if (url.hostname !== 'fastforward.team') { - return; - } - //navigate - if (url.pathname === '/bypassed') { - let ext_url = new URL(brws.runtime.getURL('')); - url.hostname = ext_url.hostname; - url.protocol = ext_url.protocol; - url.pathname = '/html' + url.pathname; - if (url.searchParams.get('crowd') === 'true') { - url.pathname = - url.pathname.split('/').slice(0, -1).join('/') + - '/crowd-bypassed.html'; - } else { - url.pathname = - url.pathname.split('/').slice(0, -1).join('/') + - '/before-navigate.html'; - } - - brws.tabs.update(details.tabId, { - url: url.href, - }); - } +function preflight(details) { + let url = new URL(details.url); + if (url.hostname !== 'fastforward.team') { + return; } + //navigate + if (url.pathname === '/bypassed') { + let ext_url = new URL(brws.runtime.getURL('')); + url.hostname = ext_url.hostname; + url.protocol = ext_url.protocol; + url.pathname = '/html' + url.pathname; + if (url.searchParams.get('crowd') === 'true') { + url.pathname = + url.pathname.split('/').slice(0, -1).join('/') + + '/crowd-bypassed.html'; + } else { + url.pathname = + url.pathname.split('/').slice(0, -1).join('/') + + '/before-navigate.html'; + } - function reEnableCrowdBypassStartup() { - brws.storage.local.get(['tempDisableCrowd']).then((result) => { - if (result.tempDisableCrowd === 'true') { - brws.storage.local.get(['options']).then((result) => { - let opt = result.options; - opt.optionCrowdBypass = true; - brws.storage.local.set({ options: opt }); - }); - } - brws.storage.local.set({ tempDisableCrowd: 'false' }); + brws.tabs.update(details.tabId, { + url: url.href, }); } +} - brws.alarms.onAlarm.addListener((alarm) => { - brws.storage.local.get(['tempDisableCrowd']).then((result) => { - if ( - alarm.name === 'enableCrowdBypass' && - result.tempDisableCrowd === 'true' - ) { - brws.storage.local.get(['options']).then((result) => { - let opt = result.options; - opt.optionCrowdBypass = true; - brws.storage.local.set({ options: opt }); - brws.storage.local.set({ tempDisableCrowd: 'false' }); - }); - } - }); +function reEnableCrowdBypassStartup() { + brws.storage.local.get(['tempDisableCrowd']).then((result) => { + if (result.tempDisableCrowd === 'true') { + brws.storage.local.get(['options']).then((result) => { + let opt = result.options; + opt.optionCrowdBypass = true; + brws.storage.local.set({ options: opt }); + }); + } + brws.storage.local.set({ tempDisableCrowd: 'false' }); }); +} - brws.runtime.onInstalled.addListener(firstrun); - brws.runtime.onStartup.addListener(() => { - ffclipboardClear(); - clearCrowdIgnoredURLs(); - reEnableCrowdBypassStartup(); - brws.storage.local.set({ version: brws.runtime.getManifest().version }); +brws.alarms.onAlarm.addListener((alarm) => { + brws.storage.local.get(['tempDisableCrowd']).then((result) => { + if ( + alarm.name === 'enableCrowdBypass' && + result.tempDisableCrowd === 'true' + ) { + brws.storage.local.get(['options']).then((result) => { + let opt = result.options; + opt.optionCrowdBypass = true; + brws.storage.local.set({ options: opt }); + brws.storage.local.set({ tempDisableCrowd: 'false' }); + }); + } }); +}); + +brws.runtime.onInstalled.addListener(firstrun); +brws.runtime.onStartup.addListener(() => { + ffclipboardClear(); + clearCrowdIgnoredURLs(); + reEnableCrowdBypassStartup(); + brws.storage.local.set({ version: brws.runtime.getManifest().version }); +}); + +brws.runtime.onMessage.addListener((request, _, sendResponse) => { + (async () => { + let options = await getOptions(); + if (options.optionCrowdBypass === false) { + return; + } + let url; + request.type === 'crowdQuery' + ? (url = 'https://crowd.fastforward.team/crowd/query_v1') + : (url = 'https://crowd.fastforward.team/crowd/contribute_v1'); - brws.runtime.onMessage.addListener((request, _, sendResponse) => { - (async () => { - let options = await getOptions(); - if (options.optionCrowdBypass === false) { - return; - } - let url; - request.type === 'crowdQuery' - ? (url = 'https://crowd.fastforward.team/crowd/query_v1') - : (url = 'https://crowd.fastforward.team/crowd/contribute_v1'); - - let params = new URLSearchParams(); + let params = new URLSearchParams(); - if (request.type !== 'followAndContribute') { - for (let key in request.detail) { - params.append(key, request.detail[key]); - } - } else { - for (let key in request.detail) { - if (key === 'target') { - let dest = new URL(request.detail[key]); - if (!fetchDomains.includes(dest.hostname)) { - return; - } - let res = await fetch(dest.href, { - method: 'GET', - redirect: 'follow', - }); - params.append(key, res.url); - } else { - params.append(key, request.detail[key]); + if (request.type !== 'followAndContribute') { + for (let key in request.detail) { + params.append(key, request.detail[key]); + } + } else { + for (let key in request.detail) { + if (key === 'target') { + let dest = new URL(request.detail[key]); + if (!fetchDomains.includes(dest.hostname)) { + return; } + let res = await fetch(dest.href, { + method: 'GET', + redirect: 'follow', + }); + params.append(key, res.url); + } else { + params.append(key, request.detail[key]); } } + } + + let response = await fetch(url, { + method: 'POST', + body: params.toString(), + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + }); + if (request.type === 'crowdQuery') + response.text().then((res) => { + sendResponse(res); + }); + })(); + return true; +}); - let response = await fetch(url, { - method: 'POST', - body: params.toString(), - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, +brws.storage.onChanged.addListener(() => { + getOptions().then((options) => { + if (typeof options === 'undefined') { + return; + } + if (options.optionBlockIpLoggers === false) { + brws.declarativeNetRequest.updateEnabledRulesets({ + disableRulesetIds: ['ipLoggerRuleset'], + }); + } else { + brws.declarativeNetRequest.updateEnabledRulesets({ + enableRulesetIds: ['ipLoggerRuleset'], + }); + } + if (options.optionTrackerBypass === false) { + brws.declarativeNetRequest.updateEnabledRulesets({ + disableRulesetIds: ['trackerRuleset'], + }); + } else { + brws.declarativeNetRequest.updateEnabledRulesets({ + enableRulesetIds: ['trackerRuleset'], }); - if (request.type === 'crowdQuery') - response.text().then((res) => { - sendResponse(res); - }); - })(); - return true; + } }); +}); - brws.storage.onChanged.addListener(() => { - getOptions().then((options) => { - if (typeof options === 'undefined') { - return; - } - if (options.optionBlockIpLoggers === false) { - brws.declarativeNetRequest.updateEnabledRulesets({ - disableRulesetIds: ['ipLoggerRuleset'], - }); - } else { - brws.declarativeNetRequest.updateEnabledRulesets({ - enableRulesetIds: ['ipLoggerRuleset'], - }); - } - if (options.optionTrackerBypass === false) { - brws.declarativeNetRequest.updateEnabledRulesets({ - disableRulesetIds: ['trackerRuleset'], - }); - } else { - brws.declarativeNetRequest.updateEnabledRulesets({ - enableRulesetIds: ['trackerRuleset'], - }); - } - }); - }); -}