From 1cb2032f8c9c525a740ca0ee62c5530a6e7be77f Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 2 Oct 2019 12:01:46 +0200 Subject: [PATCH] feat: local gateway on 'localhost' Known Issues: - webui loaded from gateway port is broken - go-ipfs returns 403 Forbidden if request is made to `localhost` API - normalizing API host to IP solves the issue under Firefox and Chromium - Brave continues to show CORS error needs additional work --- add-on/src/lib/options.js | 11 +++++++---- add-on/src/lib/state.js | 2 +- test/functional/lib/dnslink.test.js | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/add-on/src/lib/options.js b/add-on/src/lib/options.js index 1bb01861b..a44193eb2 100644 --- a/add-on/src/lib/options.js +++ b/add-on/src/lib/options.js @@ -73,19 +73,22 @@ exports.storeMissingOptions = async (read, defaults, storage) => { return changes } -function normalizeGatewayURL (url) { +function normalizeGatewayURL (url, { localhost = true }) { if (typeof url === 'string') { url = new URL(url) } - // https://github.com/ipfs/ipfs-companion/issues/328 - if (url.hostname.toLowerCase() === 'localhost') { + // localhost normalization (https://github.com/ipfs-shipyard/ipfs-companion/issues/328) + if (localhost && url.hostname === '127.0.0.1') { + url.hostname = 'localhost' + } + if (!localhost && url.hostname === 'localhost') { url.hostname = '127.0.0.1' } // Return string without trailing slash return url.toString().replace(/\/$/, '') } exports.normalizeGatewayURL = normalizeGatewayURL -exports.safeURL = (url) => new URL(normalizeGatewayURL(url)) +exports.safeURL = (url, opts) => new URL(normalizeGatewayURL(url, opts || {})) // convert JS array to multiline textarea function hostArrayCleanup (array) { diff --git a/add-on/src/lib/state.js b/add-on/src/lib/state.js index b674b5265..c02ad82cb 100644 --- a/add-on/src/lib/state.js +++ b/add-on/src/lib/state.js @@ -19,7 +19,7 @@ function initState (options) { delete state.publicGatewayUrl state.redirect = options.useCustomGateway delete state.useCustomGateway - state.apiURL = safeURL(options.ipfsApiUrl) + state.apiURL = safeURL(options.ipfsApiUrl, { localhost: false }) // go-ipfs returns 403 if IP is beautified to 'localhost' state.apiURLString = state.apiURL.toString() delete state.ipfsApiUrl state.gwURL = safeURL(options.customGatewayUrl) diff --git a/test/functional/lib/dnslink.test.js b/test/functional/lib/dnslink.test.js index 464fa4258..3d9ad9f19 100644 --- a/test/functional/lib/dnslink.test.js +++ b/test/functional/lib/dnslink.test.js @@ -8,7 +8,7 @@ const { initState } = require('../../../add-on/src/lib/state') const { optionDefaults } = require('../../../add-on/src/lib/options') const testOptions = Object.assign({}, optionDefaults, { - customGatewayUrl: 'http://127.0.0.1:8080', + customGatewayUrl: 'http://localhost:8080', publicGatewayUrl: 'https://gateway.foobar.io' }) @@ -82,7 +82,7 @@ describe('dnslinkResolver (dnslinkPolicy=detectIpfsPathHeader)', function () { dnslinkResolver.setDnslink(url.hostname, '/ipfs/bafybeigxjv2o4jse2lajbd5c7xxl5rluhyqg5yupln42252e5tcao7hbge') expectNoDnsTxtRecordLookup(url.hostname, dnslinkResolver) expect(dnslinkResolver.dnslinkRedirect(url.toString()).redirectUrl) - .to.equal('http://127.0.0.1:8080/ipns/dnslinksite4.io/foo/barl?a=b#c=d') + .to.equal('http://localhost:8080/ipns/dnslinksite4.io/foo/barl?a=b#c=d') }) it('[embedded node] should return redirect to public gateway if dnslink is present in cache', function () { const url = new URL('https://dnslinksite4.io/foo/barl?a=b#c=d') @@ -164,7 +164,7 @@ describe('dnslinkResolver (dnslinkPolicy=enabled)', function () { const dnslinkResolver = createDnslinkResolver(getExternalNodeState) spoofDnsTxtRecord(url.hostname, dnslinkResolver, dnslinkValue) expect(dnslinkResolver.dnslinkRedirect(url.toString()).redirectUrl) - .to.equal('http://127.0.0.1:8080/ipns/dnslinksite4.io/foo/barl?a=b#c=d') + .to.equal('http://localhost:8080/ipns/dnslinksite4.io/foo/barl?a=b#c=d') }) it('[embedded node] should return redirect to public gateway if DNS TXT record is present and path does not belong to a gateway', function () { const url = new URL('https://dnslinksite4.io/foo/barl?a=b#c=d')