Skip to content

Commit

Permalink
Optional support for redirect triggered by dnslink
Browse files Browse the repository at this point in the history
- implements #44
- disabled by default
- still work in progress, performance is poor due to the lack
  of any dns cache
  • Loading branch information
lidel committed Jan 31, 2016
1 parent 7d4c0a2 commit 037ea98
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
18 changes: 17 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
const prefs = require('sdk/simple-prefs').prefs

const notifications = require('sdk/notifications')
const Request = require('sdk/request').Request
const {Request} = require('sdk/request')
const {XMLHttpRequest} = require('sdk/net/xhr')
const URL = require('sdk/url').URL

function apiUrl (path) {
Expand Down Expand Up @@ -47,7 +48,22 @@ function getSwarmPeers (callback) {
query('swarm/peers?stream-channels=true', callback)
}

function isDnslinkPresent (fqdn) {
let xhr = new XMLHttpRequest()
xhr.overrideMimeType('application/json')
xhr.open('GET', apiUrl('dns/' + fqdn), false)
xhr.send(null)
if (xhr.status === 200) {
let json = JSON.parse(xhr.responseText)
return !!json.Path
} else {
return false
}
}

exports.apiUrl = apiUrl
exports.query = query
exports.pin = pin
exports.getVersion = getVersion
exports.getSwarmPeers = getSwarmPeers
exports.isDnslinkPresent = isDnslinkPresent
17 changes: 17 additions & 0 deletions lib/dns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

const prefs = require('sdk/simple-prefs').prefs
// const URL = require('sdk/url').URL
const api = require('./api.js')

function isIpfsEnabled (fqdn) {
if (fqdn === '127.0.0.1' || fqdn === prefs.customGatewayHost) { // TODO: be smarter?
return false
}
// TODO: add cache
let ipfsSupport = api.isDnslinkPresent(fqdn)
// console.log('dns.isIpfsEnabled(' + fqdn + ')=' + ipfsSupport)
return ipfsSupport
}

exports.isIpfsEnabled = isIpfsEnabled
30 changes: 21 additions & 9 deletions lib/redirects.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
'use strict'

const prefs = require('sdk/simple-prefs').prefs
const gw = require('./gateways')
const dns = require('./dns')

const {Cc, Ci} = require('chrome')

const ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService)
const observice = Cc['@mozilla.org/observer-service;1'].getService(Ci.nsIObserverService)

function redirectFor (uri) {
let redirectUri = null
if (gw.redirectEnabled) {
let uriSpec = uri.spec
if (uriSpec.match(gw.publicHosts) && uriSpec.match(gw.IPFS_RESOURCE)) {
// redirect IPFS paths from known public gateways
redirectUri = ioservice.newURI(uriSpec.replace(gw.publicHosts, gw.customUri.spec), null, null)
} else if (prefs.dns && dns.isIpfsEnabled(uri.host)) {
// redirect sites with dnslink record in DNS
redirectUri = ioservice.newURI(gw.customUri.spec + 'ipns/' + uri.host + uri.path + uri.ref, null, null)
}
}
// if (redirectUri) console.log('redirectFor(' + uri.spec + ')=' + redirectUri.spec)
return redirectUri
}

const ipfsRequestObserver = {
observe: function (subject, topic, data) { // eslint-disable-line no-unused-vars
if (topic === 'http-on-modify-request') {
let channel = subject.QueryInterface(Ci.nsIHttpChannel)
let httpUrl = channel.URI.spec
if (httpUrl.match(gw.publicHosts) && httpUrl.match(gw.IPFS_RESOURCE)) {
channel.setRequestHeader('x-ipfs-firefox-addon', 'true', false)
if (gw.redirectEnabled) {
// console.info('Detected HTTP request to the public gateway: ' + channel.URI.spec)
let uri = ioservice.newURI(httpUrl.replace(gw.publicHosts, gw.customUri.spec), null, null)
// console.info('Redirecting to custom gateway: ' + uri.spec)
channel.redirectTo(uri)
}
let redirect = redirectFor(channel.URI)
if (redirect) {
channel.redirectTo(redirect)
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions locale/en-US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ automatic_up_notification_text=Redirection of IPFS requests is enabled
automatic_down_notification_title=IPFS is Down
automatic_down_notification_text=Redirection of IPFS requests is disabled

dns_title=Enable DNS Lookup
dns_description=(EXPERIMENTAL) Check if domain contains DNS TXT record with dnslink to IPFS path and redirect to it

linkify_title=Extended IPFS Link Support
linkify_description=(EXPERIMENTAL) Rewrite links to /ip(f|n)s/* paths on every page to point to IPFS gateway. Make plaintext IPFS links clickable.

Expand Down
3 changes: 3 additions & 0 deletions locale/pl-PL.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ automatic_up_notification_text=Włączono przekierowanie
automatic_down_notification_title=IPFS jest niedostępne
automatic_down_notification_text=Wyłączono przekierowanie

dns_title=Odpytywanie DNS
dns_description=(EKSPERYMENT) Sprawdza, czy domena posiada wpis TXT w DNS z dnslink do zasobu IPFS i przekierowuje jeśli jest obecny

linkify_title=Rozszerzone wsparcie linków IPFS
linkify_description=(EKSPERYMENT) Przepisuje linki do zasobów /ip(f|n)s/* na każdej stronie tak, aby wskazywały na Bramę. Dodatkowo sprawia, że czysto-tekstowe adresy IPFS są klikalne.

Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
"type": "integer",
"value": 5001
},
{
"name": "dns",
"title": "Enable DNS Lookup",
"type": "bool",
"value": false
},
{
"name": "linkify",
"title": "Extended IPFS Link Support",
Expand Down

0 comments on commit 037ea98

Please sign in to comment.