diff --git a/.gitignore b/.gitignore index 44124a8c7..986f5358e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ ipfs-firefox-addon.xpi node_modules +firefox diff --git a/lib/gateways.js b/lib/gateways.js index eea54a9a6..abcde6ac7 100644 --- a/lib/gateways.js +++ b/lib/gateways.js @@ -10,8 +10,9 @@ var ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOServi var gui = require('./gui.js'); var proto = require('./protocols.js'); -const PUBLIC_GATEWAY_URI = ioservice.newURI('http://gateway.ipfs.io', null, null); -const PUBLIC_GATEWAY_REGEX = new RegExp('^' + PUBLIC_GATEWAY_URI.spec.replace(/[^\w\s]/g, '\\$&') + '(' + proto.ipfsScheme + '|' + proto.ipnsScheme + ')\\/'); +const PUBLIC_GATEWAY_URI = ioservice.newURI('https://ipfs.io', null, null); +const PUBLIC_GATEWAY_REGEX = /^https?:\/\/(gateway\.|)ipfs\.io\//; +const PUBLIC_IPFS_RESOURCE = /^https?:\/\/(gateway\.|)ipfs\.io\/ip(fs|ns)\//; var CUSTOM_GATEWAY_URI; @@ -20,11 +21,11 @@ var ipfsRequestObserver = { if (topic == 'http-on-modify-request') { let channel = subject.QueryInterface(Ci.nsIHttpChannel); let httpUrl = channel.URI.spec; - if (httpUrl.match(PUBLIC_GATEWAY_REGEX)) { + if (httpUrl.match(PUBLIC_IPFS_RESOURCE)) { channel.setRequestHeader('x-ipfs-firefox-addon', 'true', false); if (prefs.useCustomGateway) { //console.info('Detected HTTP request to the public gateway: ' + channel.URI.spec); - let uri = ioservice.newURI(httpUrl.replace(PUBLIC_GATEWAY_URI.spec, CUSTOM_GATEWAY_URI.spec), null, null); + let uri = ioservice.newURI(httpUrl.replace(PUBLIC_GATEWAY_REGEX, CUSTOM_GATEWAY_URI.spec), null, null); //console.info('Redirecting to custom gateway: ' + uri.spec); channel.redirectTo(uri); } diff --git a/package.json b/package.json index 0cb10e461..7d8faf759 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "id": "ipfs-firefox-addon@lidel.org", "description": "Access IPFS resources via custom HTTP2IPFS gateway", "author": "Marcin Rataj", - "version": "1.0.2", + "version": "1.0.3", "license": "CC0", "homepage": "https://github.com/lidel/ipfs-firefox-addon", "icon": "data/icon-on-64.png", @@ -19,7 +19,7 @@ }, { "name": "customGatewayHost", - "title": "Custom IPFS Gateway Host", + "title": "Custom IPFS Gateway Address", "type": "string", "value": "127.0.0.1" }, diff --git a/test/test-gateways.js b/test/test-gateways.js index 52e5e210f..ed18bc42f 100644 --- a/test/test-gateways.js +++ b/test/test-gateways.js @@ -7,11 +7,51 @@ const ipfsPath = 'ipfs/QmTkzDwWqPbnAh5YiV5VwcTLnGdwSNsNTn2aDxdXBFca7D/'; const ipnsPath = 'ipns/ipfs.git.sexy/'; const button = gui.toggleButton; -exports['test enabled /ipfs/ redirect'] = function(assert, done) { +exports['test enabled /ipfs/ redirect (old http gw)'] = function(assert, done) { // open Public Gateway URL and check if it gets redirected to gw.customUri() gw.enableHttpGatewayRedirect(button); + // HTTP tabs.open({ - url: gw.publicUri().spec + ipfsPath, + url: 'http://gateway.ipfs.io/' + ipfsPath, + onReady: function onReady(tab) { + assert.equal(tab.url, gw.customUri().spec + ipfsPath, 'expected redirect'); + tab.close(done); + } + }); +}; + +exports['test enabled /ipfs/ redirect (old https gw)'] = function(assert, done) { + // open Public Gateway URL and check if it gets redirected to gw.customUri() + gw.enableHttpGatewayRedirect(button); + // HTTPS + tabs.open({ + url: 'https://gateway.ipfs.io/' + ipfsPath, + onReady: function onReady(tab) { + assert.equal(tab.url, gw.customUri().spec + ipfsPath, 'expected redirect'); + tab.close(done); + } + }); +}; + +exports['test enabled /ipfs/ redirect (http gw)'] = function(assert, done) { + // open Public Gateway URL and check if it gets redirected to gw.customUri() + gw.enableHttpGatewayRedirect(button); + // HTTP + tabs.open({ + url: 'http://ipfs.io/' + ipfsPath, + onReady: function onReady(tab) { + assert.equal(tab.url, gw.customUri().spec + ipfsPath, 'expected redirect'); + tab.close(done); + } + }); +}; + +exports['test enabled /ipfs/ redirect (https gw)'] = function(assert, done) { + // open Public Gateway URL and check if it gets redirected to gw.customUri() + gw.enableHttpGatewayRedirect(button); + // HTTPS + tabs.open({ + url: 'https://ipfs.io/' + ipfsPath, onReady: function onReady(tab) { assert.equal(tab.url, gw.customUri().spec + ipfsPath, 'expected redirect'); tab.close(done);