Skip to content

Commit

Permalink
feat(mv3): adding dynamicNetRequest rule reconciliation logic + Firef…
Browse files Browse the repository at this point in the history
…ox Builds (#1186)

* feat(mv3): ✨ MV3 Manifest Migration

* fix(mv3): 🗑️ No longer needed

* fix(mv3): 🔧 Corresponding MV3 Changes

* feat(mv3): 📦 Adding deps

* feat(telemetry): Refactor Metrics Tracking from background service_worker (#1172)

* feat(telemetry): ♻️ Init Telemetry away from background service_worker.

* feat(telemetry): ♻️ Track metrics from page context instead of service_worker context

* feat(mv3): 🩹 Patch @protobufjs/inquire to not have eval

* fix(mv3): 👽 Fixing contextMenus API changes (#1177)

* fix(mv3): 👽 Fixing contextMenus API changes

* fix(mv3): 🩹 Fixing the browser.action api

* fix(mv3): webpack configs (#1178)

* fix(mv3): 👽 Fixing contextMenus API changes

* fix(mv3): 🩹 Fixing the browser.action api

* fix(mv3): 🔧 Fixing webpack config

* fix(mv3): 🩹 Patching debug package and making background sw work.

* feat(mv3): ✨ XHR to Fetch Migration (#1179)

* fix(mv3): 👽 Fixing contextMenus API changes

* fix(mv3): 🩹 Fixing the browser.action api

* fix(mv3): 🔧 Fixing webpack config

* fix(mv3): 🩹 Patching debug package and making background sw work.

* feat(mv3): ✨ XMLHttpRequest => fetch

* fix(mv3): 🚧 Related changes to ipfs-path

* fix(mv3): 🚧 Other Related changes

* fix(mv3): 🚧 Changes to companion

* fix(mv3): ✅ Fixing tests to account for async code.

* Fix(mv3): Popup Was Broken (#1180)

* fix(mv3): 👽 Fixing contextMenus API changes

* fix(mv3): 🩹 Fixing the browser.action api

* fix(mv3): 🔧 Fixing webpack config

* fix(mv3): 🩹 Patching debug package and making background sw work.

* feat(mv3): ✨ XMLHttpRequest => fetch

* fix(mv3): 🚧 Related changes to ipfs-path

* fix(mv3): 🚧 Other Related changes

* fix(mv3): 🚧 Changes to companion

* fix(mv3): ✅ Fixing tests to account for async code.

* feat(mv3): ♻️ Implementing a non-windowed companion instance

* fix(mv3): 🗑️ Removing calls to background page.

* fix: 🗑️ Unneeded debug statement

* fix(mv3): 🛂 Limiting permissions to chrome-extension

* Update add-on/src/lib/ipfs-companion.js

Co-authored-by: Russell Dempsey <[email protected]>

* fix(types): 🏷️ Refactoring existing type declaration

* fix(types): 🏷️ Moving to new types path

* feat(types): ✨ Adding typescript support for transpilation

* feat(mv3): ✨ Adding blocking request tester

* fix(mv3): 🩹 package.json

* fix(mv3): 🚨 Fix Lint

* fix: 🚨 fix lint

* fix(mv3): 🩹 temp fix to build background context

* fix(mv3): 👔 Detection Logic for MV3 world.

* feat(mv3): ✨ Dynamic RegexSubstitution

* fix(types): ⬆️ Adding .mocharc.json to fix mocha for TS.

* fix: 🚨 Lint Fix

* fix(mv3): ♻️ refactor background.service_worker

* feat(mv3): ✨ Passing state to BlockOrObserve

* fix(recovery): 🐛 conditional for recovery

* fix: 🗑️ unneeded @ts-ignore

* fix: 💡 Adding comments

* fix: fixing string method.

* fix: removing extra space.

* fix: removing @ts-nocheck

* no longer needed

* fix(mv3): ♻️ Refactor

* feat(mv3): ✨ Adding rule-recon logic

* saving state

* fix(mv3): 🔧 Manifest

* fix(mv3): 🔧 Fixing firefox webpack config

* fix(mv3): 🩹 Patching debug to use in memory store instead of browser.storage.local

* fix: 🚨 fixing lint and moving from record type to map type.

* fix: 📝 Adding docstrings.

* fix(mv3): 💩 web-ext making things harder than it needs to be.

* fix(mv3): ⏪ no more debug patching

* fix(mv3): 💩 improved recon logic

* fix: 📝 adding comments regarding debug.

* fix: 🚨 Fix lint

* fix(mv3): 🛂 manifest perms

* fix: 🗑️ unnecessary blank line

* feat(mv3): 🧪 Adding initial tests

* feat(mv3): 🤡 Adding Mock DeclarativeNetRequest Implementation

* nits

* fix: adding more test examples

* fix: self-documenting code.

* fix: unneeded comment

* Update test/functional/lib/redirect-handler/blockOrObserve.test.ts

---------

Co-authored-by: Russell Dempsey <[email protected]>
  • Loading branch information
whizzzkid and SgtPooki authored May 10, 2023
1 parent 52a9aa8 commit 0d52545
Show file tree
Hide file tree
Showing 12 changed files with 394 additions and 137 deletions.
6 changes: 4 additions & 2 deletions add-on/manifest.chromium.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"minimum_chrome_version": "101",
"minimum_chrome_version": "111",
"background": {
"service_worker": "dist/bundles/backgroundPage.bundle.js"
},
"permissions": [
"clipboardWrite",
"contextMenus",
Expand All @@ -13,6 +16,5 @@
"declarativeNetRequest",
"declarativeNetRequestFeedback"
],
"host_permissions": ["<all_urls>"],
"incognito": "not_allowed"
}
8 changes: 2 additions & 6 deletions add-on/manifest.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"description": "__MSG_manifest_extensionDescription__",
"homepage_url": "https://github.com/ipfs-shipyard/ipfs-companion",
"author": "IPFS Community",
"background": {
"service_worker": "dist/bundles/backgroundPage.bundle.js"
},
"icons": {
"19": "icons/png/ipfs-logo-on_19.png",
"38": "icons/png/ipfs-logo-on_38.png",
Expand All @@ -28,6 +25,7 @@
"browser_style": false,
"page": "dist/options/options.html"
},
"host_permissions": ["<all_urls>"],
"web_accessible_resources": [
{
"resources": [
Expand All @@ -40,9 +38,7 @@
"dist/recovery/recovery.html",
"dist/recovery/recovery.js"
],
"matches": [
"<all_urls>"
]
"matches": ["<all_urls>"]
}
],
"content_security_policy": {
Expand Down
2 changes: 1 addition & 1 deletion add-on/manifest.firefox-beta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"update_url": "https://ipfs-shipyard.github.io/ipfs-companion/ci/firefox/update.json"
"strict_min_version": "111.0.0"
}
}
}
6 changes: 4 additions & 2 deletions add-on/manifest.firefox.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
"options_ui": {
"browser_style": false
},
"background": {
"scripts": ["dist/bundles/backgroundPage.firefox.bundle.js"]
},
"browser_specific_settings": {
"gecko": {
"id": "[email protected]",
"strict_min_version": "109.0.0"
"strict_min_version": "111.0.0"
}
},
"permissions": [
"<all_urls>",
"idle",
"tabs",
"notifications",
Expand Down
35 changes: 16 additions & 19 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import { registerSubdomainProxy } from './http-proxy.js'
import { runPendingOnInstallTasks } from './on-installed.js'
import { getExtraInfoSpec } from './redirect-handler/blockOrObserve.js'

// this won't work in webworker context. Needs to be enabled manually
// https://github.com/debug-js/debug/issues/916
const log = debug('ipfs-companion:main')
log.error = debug('ipfs-companion:main:error')

let browserActionPort // reuse instance for status updates between on/off toggles

// init happens on addon load in background/background.js
export default async function init (windowedContext = false) {
export default async function init () {
// INIT
// ===================================================================
let ipfs // ipfs-api instance
Expand Down Expand Up @@ -79,23 +81,19 @@ export default async function init (windowedContext = false) {
copier = createCopier(notify, ipfsPathValidator)
ipfsImportHandler = createIpfsImportHandler(getState, getIpfs, ipfsPathValidator, runtime, copier)
inspector = createInspector(notify, ipfsPathValidator, getState)
if (!windowedContext) {
contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, {
onAddFromContext,
onCopyCanonicalAddress: copier.copyCanonicalAddress,
onCopyRawCid: copier.copyRawCid,
onCopyAddressAtPublicGw: copier.copyAddressAtPublicGw
})
modifyRequest = createRequestModifier(getState, dnslinkResolver, ipfsPathValidator, runtime)
log('register all listeners')
registerListeners()
await registerSubdomainProxy(getState, runtime, notify)
log('init done')
setApiStatusUpdateInterval(options.ipfsApiPollMs)
await runPendingOnInstallTasks()
} else {
log('init done (windowed context)')
}
contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, {
onAddFromContext,
onCopyCanonicalAddress: copier.copyCanonicalAddress,
onCopyRawCid: copier.copyRawCid,
onCopyAddressAtPublicGw: copier.copyAddressAtPublicGw
})
modifyRequest = createRequestModifier(getState, dnslinkResolver, ipfsPathValidator, runtime)
log('register all listeners')
registerListeners()
await registerSubdomainProxy(getState, runtime, notify)
log('init done')
setApiStatusUpdateInterval(options.ipfsApiPollMs)
await runPendingOnInstallTasks()
} catch (error) {
log.error('Unable to initialize addon due to error', error)
if (notify) notify('notify_addonIssueTitle', 'notify_addonIssueMsg')
Expand All @@ -120,7 +118,6 @@ export default async function init (windowedContext = false) {
onBeforeSendInfoSpec.push('extraHeaders')
}
browser.webRequest.onBeforeSendHeaders.addListener(

onBeforeSendHeaders, { urls: ['<all_urls>'] }, getExtraInfoSpec(onBeforeSendInfoSpec))
browser.webRequest.onBeforeRequest.addListener(onBeforeRequest, { urls: ['<all_urls>'] }, getExtraInfoSpec())
browser.webRequest.onHeadersReceived.addListener(onHeadersReceived, { urls: ['<all_urls>'] }, getExtraInfoSpec(['responseHeaders']))
Expand Down
53 changes: 27 additions & 26 deletions add-on/src/lib/ipfs-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { dropSlash, ipfsUri, pathAtHttpGateway, sameGateway } from './ipfs-path.
import { safeURL } from './options.js'
import { braveNodeType } from './ipfs-client/brave.js'
import { recoveryPagePath } from './constants.js'
import { addRuleToDynamicRuleSetGenerator, supportsBlock } from './redirect-handler/blockOrObserve.js'
import { addRuleToDynamicRuleSetGenerator, isLocalHost, supportsBlock } from './redirect-handler/blockOrObserve.js'

const log = debug('ipfs-companion:request')
log.error = debug('ipfs-companion:request:error')
Expand Down Expand Up @@ -100,7 +100,7 @@ export function createRequestModifier (getState, dnslinkResolver, ipfsPathValida
ignore(request.requestId)
}
// skip all local requests
if (request.url.startsWith('http://127.0.0.1') || request.url.startsWith('http://localhost') || request.url.startsWith('http://[::1]')) {
if (isLocalHost(request.url)) {
ignore(request.requestId)
}

Expand Down Expand Up @@ -160,23 +160,19 @@ export function createRequestModifier (getState, dnslinkResolver, ipfsPathValida
// take advantage of subdomain redirect provided by go-ipfs >= 0.5
if (state.redirect && request.type === 'main_frame' && sameGateway(request.url, state.gwURL)) {
const redirectUrl = safeURL(request.url, { useLocalhostName: state.useSubdomains }).toString()
if (redirectUrl !== request.url) {
return handleRedirection({
originUrl: request.url,
redirectUrl
})
}
return handleRedirection({
originUrl: request.url,
redirectUrl
})
}

// For now normalize API to the IP to comply with go-ipfs checks
if (state.redirect && request.type === 'main_frame' && sameGateway(request.url, state.apiURL)) {
const redirectUrl = safeURL(request.url, { useLocalhostName: false }).toString()
if (redirectUrl !== request.url) {
return handleRedirection({
originUrl: request.url,
redirectUrl
})
}
return handleRedirection({
originUrl: request.url,
redirectUrl
})
}

// early sanity checks
Expand Down Expand Up @@ -476,13 +472,21 @@ export function createRequestModifier (getState, dnslinkResolver, ipfsPathValida
}
}

/**
* Handles redirection in MV2 and MV3.
*
* @param {object} input contains originUrl and redirectUrl.
* @returns
*/
function handleRedirection ({ originUrl, redirectUrl }) {
if (supportsBlock) {
return { redirectUrl }
}
if (redirectUrl !== '' && originUrl !== '' && redirectUrl !== originUrl) {
if (supportsBlock) {
return { redirectUrl }
}

// Let browser handle redirection MV3 style.
addRuleToDynamicRuleSet({ originUrl, redirectUrl })
// Let browser handle redirection MV3 style.
addRuleToDynamicRuleSet({ originUrl, redirectUrl })
}
}

// Returns a string with URL at the active gateway (local or public)
Expand Down Expand Up @@ -532,13 +536,10 @@ async function redirectToGateway (request, url, state, ipfsPathValidator, runtim
}
}

// return a redirect only if URL changed
if (redirectUrl && request.url !== redirectUrl) {
return handleRedirection({
originUrl: request.url,
redirectUrl
})
}
return handleRedirection({
originUrl: request.url,
redirectUrl
})
}

function isSafeToRedirect (request, runtime) {
Expand Down
Loading

0 comments on commit 0d52545

Please sign in to comment.