Skip to content

Commit

Permalink
fix: HTTP recovery should respect redirect state
Browse files Browse the repository at this point in the history
This ensures that HTTP recovery will use public gateway instead
of local one when redirect to local node is disabled in Preferences.
  • Loading branch information
lidel committed Mar 2, 2021
1 parent 6e7a3a8 commit e457302
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion add-on/src/lib/dnslink.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = function createDnslinkResolver (getState) {
// to load the correct path from IPFS
// - https://github.com/ipfs/ipfs-companion/issues/298
const ipnsPath = dnslinkResolver.convertToIpnsPath(url)
const gateway = state.localGwAvailable ? state.gwURLString : state.pubGwURLString
const gateway = state.redirect && state.localGwAvailable ? state.gwURLString : state.pubGwURLString
return pathAtHttpGateway(ipnsPath, gateway)
}
},
Expand Down
27 changes: 22 additions & 5 deletions test/functional/lib/dnslink.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'
const { describe, it, before, after } = require('mocha')
const { describe, it, before, beforeEach, after } = require('mocha')
const { expect } = require('chai')
const { URL } = require('url')
const sinon = require('sinon')
Expand Down Expand Up @@ -58,10 +58,15 @@ describe('dnslinkResolver (dnslinkPolicy=detectIpfsPathHeader)', function () {
global.URL = URL
})

const getState = () => Object.assign(initState(testOptions), {
ipfsNodeType: 'external',
dnslinkPolicy: 'detectIpfsPathHeader',
peerCount: 1
let getState
beforeEach(() => {
// ensure each case uses clean state
getState = () => Object.assign(initState(testOptions), {
ipfsNodeType: 'external',
dnslinkPolicy: 'detectIpfsPathHeader',
redirect: true,
peerCount: 1
})
})
const getExternalNodeState = () => Object.assign(getState(), { ipfsNodeType: 'external' })
const getEmbeddedNodeState = () => Object.assign(getState(), { ipfsNodeType: 'embedded' })
Expand All @@ -86,6 +91,18 @@ describe('dnslinkResolver (dnslinkPolicy=detectIpfsPathHeader)', function () {
expect(dnslinkResolver.dnslinkAtGateway(url.toString()))
.to.equal('http://localhost:8080/ipns/dnslinksite4.io/foo/barl?a=b#c=d')
})
it('[external node] should return redirect to public gateway if dnslink is present in cache but redirect to local gw is off', function () {
const oldState = getState
getState = () => Object.assign(oldState(), { redirect: false })
const url = new URL('https://dnslinksite4.io/foo/barl?a=b#c=d')
const dnslinkResolver = createDnslinkResolver(getExternalNodeState)
dnslinkResolver.setDnslink(url.hostname, '/ipfs/bafybeigxjv2o4jse2lajbd5c7xxl5rluhyqg5yupln42252e5tcao7hbge')
expectNoDnsTxtRecordLookup(url.hostname, dnslinkResolver)
// note: locahost will redirect to subdomain if its go-ipfs >0.5,
// so companion does not need to handle that
expect(dnslinkResolver.dnslinkAtGateway(url.toString()))
.to.equal('https://gateway.foobar.io/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')
const dnslinkResolver = createDnslinkResolver(getEmbeddedNodeState)
Expand Down

0 comments on commit e457302

Please sign in to comment.