diff --git a/add-on/_locales/en/messages.json b/add-on/_locales/en/messages.json index 7ff048cab..faa83773b 100644 --- a/add-on/_locales/en/messages.json +++ b/add-on/_locales/en/messages.json @@ -263,6 +263,14 @@ "message": "Redirect requests for IPFS resources to the Custom gateway", "description": "An option description on the Preferences screen (option_useCustomGateway_description)" }, + "option_noRedirectHostnames_title": { + "message": "Redirect Opt-Outs", + "description": "An option title on the Preferences screen (option_noRedirectHostnames_title)" + }, + "option_noRedirectHostnames_description": { + "message": "List of websites that should not be redirected to the Custom Gateway (includes subresources from other domains). One hostname per line.", + "description": "An option description on the Preferences screen (option_noRedirectHostnames_description)" + }, "option_publicGatewayUrl_title": { "message": "Default Public Gateway", "description": "An option title on the Preferences screen (option_publicGatewayUrl_title)" diff --git a/add-on/src/lib/options.js b/add-on/src/lib/options.js index 0b5f85139..1eaa4d257 100644 --- a/add-on/src/lib/options.js +++ b/add-on/src/lib/options.js @@ -1,5 +1,7 @@ 'use strict' +const isFQDN = require('is-fqdn') + exports.optionDefaults = Object.freeze({ active: true, // global ON/OFF switch, overrides everything else ipfsNodeType: 'external', // or 'embedded' @@ -65,6 +67,25 @@ function normalizeGatewayURL (url) { exports.normalizeGatewayURL = normalizeGatewayURL exports.safeURL = (url) => new URL(normalizeGatewayURL(url)) +// convert JS array to multiline textarea +function hostArrayToText (array) { + array = array.map(host => host.trim().toLowerCase()) + array = [...new Set(array)] // dedup + array = array.filter(Boolean).filter(isFQDN) + array.sort() + return array.join('\n') +} +// convert JS array to multiline textarea +function hostTextToArray (text) { + let array = text.split('\n').map(host => host.trim().toLowerCase()) + array = [...new Set(array)] // dedup + array = array.filter(Boolean).filter(isFQDN) + array.sort() + return array +} +exports.hostArrayToText = hostArrayToText +exports.hostTextToArray = hostTextToArray + exports.migrateOptions = async (storage) => { // <= v2.4.4 // DNSLINK: convert old on/off 'dnslink' flag to text-based 'dnslinkPolicy' diff --git a/add-on/src/options/forms/gateways-form.js b/add-on/src/options/forms/gateways-form.js index dddc87a41..df993b9cd 100644 --- a/add-on/src/options/forms/gateways-form.js +++ b/add-on/src/options/forms/gateways-form.js @@ -3,7 +3,7 @@ const browser = require('webextension-polyfill') const html = require('choo/html') -const { normalizeGatewayURL } = require('../../lib/options') +const { normalizeGatewayURL, hostTextToArray, hostArrayToText } = require('../../lib/options') // Warn about mixed content issues when changing the gateway // https://github.com/ipfs-shipyard/ipfs-companion/issues/648 @@ -13,19 +13,40 @@ function gatewaysForm ({ ipfsNodeType, customGatewayUrl, useCustomGateway, + noRedirectHostnames, publicGatewayUrl, onOptionChange }) { const onCustomGatewayUrlChange = onOptionChange('customGatewayUrl', normalizeGatewayURL) const onUseCustomGatewayChange = onOptionChange('useCustomGateway') const onPublicGatewayUrlChange = onOptionChange('publicGatewayUrl', normalizeGatewayURL) + const onNoRedirectHostnamesChange = onOptionChange('noRedirectHostnames', hostTextToArray) const mixedContentWarning = !secureContextUrl.test(customGatewayUrl) + const supportRedirectToCustomGateway = ipfsNodeType === 'external' return html`
` diff --git a/add-on/src/options/forms/ipfs-node-form.js b/add-on/src/options/forms/ipfs-node-form.js index 3a86b36f6..3ae72e8ce 100644 --- a/add-on/src/options/forms/ipfs-node-form.js +++ b/add-on/src/options/forms/ipfs-node-form.js @@ -46,7 +46,7 @@ function ipfsNodeForm ({ ipfsNodeType, ipfsNodeConfig, onOptionChange }) {