2018-10-21 11:48:15 +02:00
|
|
|
const urlUtil = require('url')
|
|
|
|
const extension = require('extensionizer')
|
|
|
|
const resolveEnsToIpfsContentId = require('./resolver.js')
|
|
|
|
|
|
|
|
const supportedTopLevelDomains = ['eth']
|
|
|
|
|
|
|
|
module.exports = setupEnsIpfsResolver
|
|
|
|
|
2018-11-17 01:54:55 +01:00
|
|
|
function setupEnsIpfsResolver ({ provider }) {
|
2018-10-21 11:48:15 +02:00
|
|
|
|
|
|
|
// install listener
|
|
|
|
const urlPatterns = supportedTopLevelDomains.map(tld => `*://*.${tld}/*`)
|
2019-11-15 19:23:46 +01:00
|
|
|
extension.webRequest.onErrorOccurred.addListener(webRequestDidFail, { urls: urlPatterns, types: ['main_frame']})
|
2018-10-21 11:48:15 +02:00
|
|
|
|
|
|
|
// return api object
|
|
|
|
return {
|
|
|
|
// uninstall listener
|
|
|
|
remove () {
|
|
|
|
extension.webRequest.onErrorOccurred.removeListener(webRequestDidFail)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
async function webRequestDidFail (details) {
|
|
|
|
const { tabId, url } = details
|
|
|
|
// ignore requests that are not associated with tabs
|
2019-11-20 01:03:20 +01:00
|
|
|
if (tabId === -1) {
|
|
|
|
return
|
|
|
|
}
|
2018-10-21 11:48:15 +02:00
|
|
|
// parse ens name
|
|
|
|
const urlData = urlUtil.parse(url)
|
|
|
|
const { hostname: name, path, search } = urlData
|
|
|
|
const domainParts = name.split('.')
|
|
|
|
const topLevelDomain = domainParts[domainParts.length - 1]
|
|
|
|
// if unsupported TLD, abort
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!supportedTopLevelDomains.includes(topLevelDomain)) {
|
|
|
|
return
|
|
|
|
}
|
2018-10-21 11:48:15 +02:00
|
|
|
// otherwise attempt resolve
|
|
|
|
attemptResolve({ tabId, name, path, search })
|
|
|
|
}
|
|
|
|
|
2018-11-17 01:54:55 +01:00
|
|
|
async function attemptResolve ({ tabId, name, path, search }) {
|
2018-10-21 11:48:15 +02:00
|
|
|
extension.tabs.update(tabId, { url: `loading.html` })
|
2019-05-04 18:57:19 +02:00
|
|
|
let url = `https://manager.ens.domains/name/${name}`
|
2018-10-21 11:48:15 +02:00
|
|
|
try {
|
2019-04-04 17:15:57 +02:00
|
|
|
const {type, hash} = await resolveEnsToIpfsContentId({ provider, name })
|
|
|
|
if (type === 'ipfs-ns') {
|
2019-05-04 18:57:19 +02:00
|
|
|
const resolvedUrl = `https://gateway.ipfs.io/ipfs/${hash}${path}${search || ''}`
|
2019-04-04 17:15:57 +02:00
|
|
|
try {
|
|
|
|
// check if ipfs gateway has result
|
2019-05-04 18:57:19 +02:00
|
|
|
const response = await fetch(resolvedUrl, { method: 'HEAD' })
|
2019-11-20 01:03:20 +01:00
|
|
|
if (response.status === 200) {
|
|
|
|
url = resolvedUrl
|
|
|
|
}
|
2019-04-04 17:15:57 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.warn(err)
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
2019-04-04 17:15:57 +02:00
|
|
|
} else if (type === 'swarm-ns') {
|
|
|
|
url = `https://swarm-gateways.net/bzz:/${hash}${path}${search || ''}`
|
2019-07-23 21:29:20 +02:00
|
|
|
} else if (type === 'onion' || type === 'onion3') {
|
|
|
|
url = `http://${hash}.onion${path}${search || ''}`
|
2019-10-31 19:37:06 +01:00
|
|
|
} else if (type === 'zeronet') {
|
|
|
|
url = `http://127.0.0.1:43110/${hash}${path}${search || ''}`
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err)
|
2019-05-04 18:57:19 +02:00
|
|
|
} finally {
|
|
|
|
extension.tabs.update(tabId, { url })
|
2018-10-21 11:48:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|