|
| 1 | +'use strict' |
| 2 | +/* eslint-env browser, webextensions */ |
| 3 | + |
| 4 | +const browser = require('webextension-polyfill') |
| 5 | +const { safeURL } = require('./options') |
| 6 | + |
| 7 | +const debug = require('debug') |
| 8 | +const log = debug('ipfs-companion:http-proxy') |
| 9 | +log.error = debug('ipfs-companion:http-proxy:error') |
| 10 | + |
| 11 | +// Preface: |
| 12 | +// |
| 13 | +// When go-ipfs runs on localhost, it exposes two types of gateway: |
| 14 | +// 127.0.0.1:8080 - old school path gateway |
| 15 | +// localhost:8080 - subdomain gateway supporting Origins like $cid.ipfs.localhost |
| 16 | +// More: https://docs-beta.ipfs.io/how-to/address-ipfs-on-web/#subdomain-gateway |
| 17 | +// |
| 18 | +// In a web browser contexts we care about Origin per content root (CID) |
| 19 | +// because entire web security model uses it as a basis for sandboxing and |
| 20 | +// access controls: |
| 21 | +// https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy |
| 22 | + |
| 23 | +// registerSubdomainProxy is necessary wourkaround for supporting subdomains |
| 24 | +// under 'localhost' (*.ipfs.localhost) because some operating systems do not |
| 25 | +// resolve them to local IP and return NX error not found instead |
| 26 | +// |
| 27 | +// State in Q2 2020: |
| 28 | +// - Chromium hardcodes `localhost` name to point at local IP and proxy is not |
| 29 | +// really necessary. The code is here (inactivE) in case we need it in the future. |
| 30 | +// - Firefox requires proxy to avoid DNS lookup, but there is an open issue |
| 31 | +// that will remove that need at some point: |
| 32 | +// https://bugzilla.mozilla.org/show_bug.cgi?id=1220810 |
| 33 | +async function registerSubdomainProxy (getState, runtime, notify) { |
| 34 | + // At the moment only firefox requires proxy registration |
| 35 | + if (!runtime.isFirefox) return |
| 36 | + |
| 37 | + try { |
| 38 | + const { active, useSubdomains, gwURLString } = getState() |
| 39 | + const enable = active && useSubdomains |
| 40 | + |
| 41 | + // HTTP Proxy feature is exposed on the gateway port |
| 42 | + // Just ensure we use localhost IP to remove any dependency on DNS |
| 43 | + const { hostname, port } = safeURL(gwURLString, { useLocalhostName: false }) |
| 44 | + |
| 45 | + // Firefox uses own APIs for selective proxying |
| 46 | + if (runtime.isFirefox) { |
| 47 | + return await registerSubdomainProxyFirefox(enable, hostname, port) |
| 48 | + } |
| 49 | + |
| 50 | + // At this point we would asume Chromium, but its not needed atm |
| 51 | + // Uncomment below if ever needed (+ add 'proxy' permission to manifest.json) |
| 52 | + // return await registerSubdomainProxyChromium(enable, hostname, port) |
| 53 | + } catch (err) { |
| 54 | + // registerSubdomainProxy is just a failsafe, not necessary in most cases, |
| 55 | + // so we should not break init when it fails. |
| 56 | + // For now we just log error and exit as NOOP |
| 57 | + log.error('registerSubdomainProxy failed', err) |
| 58 | + // Show pop-up only the first time, during init() when notify is passed |
| 59 | + try { |
| 60 | + if (notify) notify('notify_addonIssueTitle', 'notify_addonIssueMsg') |
| 61 | + } catch (_) { |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// storing listener for later |
| 67 | +var onRequestProxyListener |
| 68 | + |
| 69 | +// registerSubdomainProxyFirefox sets proxy using API available in Firefox |
| 70 | +// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/proxy/onRequest |
| 71 | +async function registerSubdomainProxyFirefox (enable, hostname, port) { |
| 72 | + const { onRequest } = browser.proxy |
| 73 | + |
| 74 | + // always remove the old listener (host and port could change) |
| 75 | + const oldListener = onRequestProxyListener |
| 76 | + if (oldListener && onRequest.hasListener(oldListener)) { |
| 77 | + onRequest.removeListener(oldListener) |
| 78 | + } |
| 79 | + |
| 80 | + if (enable) { |
| 81 | + // create new listener with the latest host:port note: the listener is |
| 82 | + // handling requests made to all localhost ports (limitation of the API, |
| 83 | + // port is ignored) that is why we manually check port inside of the listener |
| 84 | + onRequestProxyListener = (request) => { |
| 85 | + if (new URL(request.url).port === port) { |
| 86 | + return { type: 'http', host: hostname, port } |
| 87 | + } |
| 88 | + return { type: 'direct' } |
| 89 | + } |
| 90 | + |
| 91 | + // register the listener |
| 92 | + onRequest.addListener(onRequestProxyListener, { |
| 93 | + urls: ['http://*.localhost/*'], |
| 94 | + incognito: false |
| 95 | + }) |
| 96 | + log(`enabled ${hostname}:${port} as HTTP proxy for *.localhost`) |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // at this point we effectively disabled proxy |
| 101 | + log('disabled HTTP proxy for *.localhost') |
| 102 | +} |
| 103 | + |
| 104 | +/* |
| 105 | + * Chromium 80 does not need proxy, so below is not used. |
| 106 | + * Uncomment below if ever needed (+ add 'proxy' permission to manifest.json) |
| 107 | +
|
| 108 | +// Helpers for converting callback chrome.* API to promises |
| 109 | +const cb = (resolve, reject) => (result) => { |
| 110 | + const err = chrome.runtime.lastError |
| 111 | + if (err) return reject(err) |
| 112 | + return resolve(result) |
| 113 | +} |
| 114 | +const get = async (opts) => new Promise((resolve, reject) => chrome.proxy.settings.get(opts, cb(resolve, reject))) |
| 115 | +const set = async (opts) => new Promise((resolve, reject) => chrome.proxy.settings.set(opts, cb(resolve, reject))) |
| 116 | +const clear = async (opts) => new Promise((resolve, reject) => chrome.proxy.settings.clear(opts, cb(resolve, reject))) |
| 117 | +
|
| 118 | +// registerSubdomainProxyChromium sets proxy using API available in Chromium |
| 119 | +// https://developer.chrome.com/extensions/proxy |
| 120 | +async function registerSubdomainProxyChromium (enable, hostname, port) { |
| 121 | + const scope = 'regular_only' |
| 122 | +
|
| 123 | + // read current proxy settings |
| 124 | + const settings = await get({ incognito: false }) |
| 125 | +
|
| 126 | + // set or update, if enabled |
| 127 | + if (enable) { |
| 128 | + // PAC script enables selective routing to PROXY at host+port |
| 129 | + // here, PROXY is the same as HTTP API endpoint |
| 130 | + const pacConfig = { |
| 131 | + mode: 'pac_script', |
| 132 | + pacScript: { |
| 133 | + data: 'function FindProxyForURL(url, host) {\n' + |
| 134 | + ` if (shExpMatch(host, '*.localhost:${port}'))\n` + |
| 135 | + ` return 'PROXY ${hostname}:${port}';\n` + |
| 136 | + " return 'DIRECT';\n" + |
| 137 | + '}' |
| 138 | + } |
| 139 | + } |
| 140 | + await set({ value: pacConfig, scope }) |
| 141 | + log(`enabled ${hostname}:${port} as HTTP proxy for *.localhost`) |
| 142 | + // log('updated chrome.proxy.settings', await get({ incognito: false })) |
| 143 | + return |
| 144 | + } |
| 145 | +
|
| 146 | + // else: remove any existing proxy settings |
| 147 | + if (settings && settings.levelOfControl === 'controlled_by_this_extension') { |
| 148 | + // remove any proxy settings ipfs-companion set up before |
| 149 | + await clear({ scope }) |
| 150 | + log('disabled HTTP proxy for *.localhost') |
| 151 | + } |
| 152 | +} |
| 153 | +*/ |
| 154 | + |
| 155 | +module.exports.registerSubdomainProxy = registerSubdomainProxy |
0 commit comments