Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default local gateway URLs to 'localhost' #780

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions add-on/src/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion add-on/src/lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/functional/lib/dnslink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down