Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: switch to brave node if the default port is offline #968

Merged
merged 1 commit into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion add-on/src/lib/ipfs-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,24 @@ function _isWebuiTab (url) {
return bundled || ipns
}

function _isInternalTab (url, extensionOrigin) {
return url.startsWith(extensionOrigin)
}

async function _reloadIpfsClientDependents (browser, instance, opts) {
// online || offline
if (browser.tabs && browser.tabs.query) {
const tabs = await browser.tabs.query({})
if (tabs) {
const extensionOrigin = browser.runtime.getURL('/')
tabs.forEach((tab) => {
// detect bundled webui in any of open tabs
if (_isWebuiTab(tab.url)) {
log(`reloading webui at ${tab.url}`)
browser.tabs.reload(tab.id)
} else if (_isInternalTab(tab.url, extensionOrigin)) {
log(`reloading internal extension page at ${tab.url}`)
browser.tabs.reload(tab.id)
log('reloading bundled webui')
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const { createRuntimeChecks } = require('./runtime-checks')
const { createContextMenus, findValueForContext, contextMenuCopyAddressAtPublicGw, contextMenuCopyRawCid, contextMenuCopyCanonicalAddress, contextMenuViewOnGateway, contextMenuCopyPermalink, contextMenuCopyCidAddress } = require('./context-menus')
const createIpfsProxy = require('./ipfs-proxy')
const { registerSubdomainProxy } = require('./http-proxy')
const { showPendingLandingPages } = require('./on-installed')
const { runPendingOnInstallTasks } = require('./on-installed')

// init happens on addon load in background/background.js
module.exports = async function init () {
Expand Down Expand Up @@ -89,7 +89,7 @@ module.exports = async function init () {
await registerSubdomainProxy(getState, runtime, notify)
log('init done')
setApiStatusUpdateInterval(options.ipfsApiPollMs)
await showPendingLandingPages()
await runPendingOnInstallTasks()
} catch (error) {
log.error('Unable to initialize addon due to error', error)
if (notify) notify('notify_addonIssueTitle', 'notify_addonIssueMsg')
Expand Down
40 changes: 32 additions & 8 deletions add-on/src/lib/on-installed.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,48 @@ exports.updatePage = 'https://github.com/ipfs-shipyard/ipfs-companion/releases/t
exports.onInstalled = async (details) => {
// details.temporary === run via `npm run firefox`
if (details.reason === 'install' || details.temporary) {
await browser.storage.local.set({ showLandingPage: 'onInstallWelcome' })
await browser.storage.local.set({ onInstallTasks: 'onFirstInstall' })
} else if (details.reason === 'update' || details.temporary) {
await browser.storage.local.set({ showLandingPage: 'onVersionUpdate' })
await browser.storage.local.set({ onInstallTasks: 'onVersionUpdate' })
}
}

exports.showPendingLandingPages = async () => {
const { showLandingPage, displayReleaseNotes } = await browser.storage.local.get(['showLandingPage', 'displayReleaseNotes'])
switch (showLandingPage) {
case 'onInstallWelcome':
await browser.storage.local.remove('showLandingPage')
exports.runPendingOnInstallTasks = async () => {
const { onInstallTasks, displayReleaseNotes } = await browser.storage.local.get(['onInstallTasks', 'displayReleaseNotes'])
await browser.storage.local.remove('onInstallTasks')
switch (onInstallTasks) {
case 'onFirstInstall':
await useNativeNodeIfFeasible(browser)
return browser.tabs.create({
url: exports.welcomePage
})
case 'onVersionUpdate':
await browser.storage.local.remove('showLandingPage')
if (!displayReleaseNotes) return
await browser.storage.local.set({ dismissedUpdate: version })
return browser.tabs.create({ url: exports.updatePage + version })
}
}

async function useNativeNodeIfFeasible (browser) {
// lazy-loaded dependencies due to debug package
// depending on the value of localStorage.debug, which is set later
const debug = require('debug')
const log = debug('ipfs-companion:on-installed')
log.error = debug('ipfs-companion:on-installed:error')
const { brave, braveNodeType } = require('./ipfs-client/brave')
const { ipfsNodeType, ipfsApiUrl } = await browser.storage.local.get(['ipfsNodeType', 'ipfsApiUrl'])

// Brave >= v1.19 (https://brave.com/ipfs-support/)
if (typeof brave !== 'undefined' && ipfsNodeType !== braveNodeType) {
try {
log(`brave detected, but node type is ${ipfsNodeType}. testing external endpoint at ${ipfsApiUrl}`)
const response = await (await fetch(`${ipfsApiUrl}/api/v0/id`, { method: 'post' })).json()
if (typeof response.ID === 'undefined') throw new Error(`unable to read PeerID from API at ${ipfsApiUrl}`)
log(`endpoint is online, PeerID is ${response.ID}, nothing to do`)
} catch (e) {
log.error(`endpoint ${ipfsApiUrl} does not work`, e)
log('switching node type to one provided by brave')
await browser.storage.local.set({ ipfsNodeType: braveNodeType })
}
}
}