|
| 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 | +async function registerSubdomainProxy (getState, runtime) { |
| 27 | + const { useSubdomainProxy: enable, gwURLString } = getState() |
| 28 | + |
| 29 | + // HTTP Proxy feature is exposed on the gateway port |
| 30 | + // Just ensure we use localhost IP to remove any dependency on DNS |
| 31 | + const proxy = safeURL(gwURLString, { useLocalhostName: false }) |
| 32 | + |
| 33 | + // Firefox uses own APIs for selective proxying |
| 34 | + if (runtime.isFirefox) { |
| 35 | + return registerSubdomainProxyFirefox(enable, proxy.hostname, proxy.port) |
| 36 | + } |
| 37 | + |
| 38 | + // at this point we asume Chromium |
| 39 | + return registerSubdomainProxyChromium(enable, proxy.host) |
| 40 | +} |
| 41 | + |
| 42 | +// storing listener for later |
| 43 | +var onRequestProxyListener |
| 44 | + |
| 45 | +// registerSubdomainProxyFirefox sets proxy using API available in Firefox |
| 46 | +// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/proxy/onRequest |
| 47 | +async function registerSubdomainProxyFirefox (enable, host, port) { |
| 48 | + const { onRequest } = browser.proxy |
| 49 | + |
| 50 | + // always remove the old listener (host and port could change) |
| 51 | + const oldListener = onRequestProxyListener |
| 52 | + if (oldListener && onRequest.hasListener(oldListener)) { |
| 53 | + onRequest.removeListener(oldListener) |
| 54 | + } |
| 55 | + |
| 56 | + if (enable) { |
| 57 | + // create new listener with the latest host:port |
| 58 | + onRequestProxyListener = (request) => ({ type: 'http', host, port }) |
| 59 | + |
| 60 | + // register the listener |
| 61 | + onRequest.addListener(onRequestProxyListener, { |
| 62 | + urls: ['http://*.localhost/*'], |
| 63 | + incognito: false |
| 64 | + }) |
| 65 | + log(`enabled ${host}:${port} as HTTP proxy for *.localhost`) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + // at this point we effectively disabled proxy |
| 70 | + log('disabled HTTP proxy for *.localhost') |
| 71 | +} |
| 72 | + |
| 73 | +// Helpers for converting callback chrome.* API to promises |
| 74 | +const cb = (resolve, reject) => (result) => { |
| 75 | + const err = chrome.runtime.lastError |
| 76 | + if (err) return reject(err) |
| 77 | + return resolve(result) |
| 78 | +} |
| 79 | +const get = async (opts) => new Promise((resolve, reject) => chrome.proxy.settings.get(opts, cb(resolve, reject))) |
| 80 | +const set = async (opts) => new Promise((resolve, reject) => chrome.proxy.settings.set(opts, cb(resolve, reject))) |
| 81 | +const clear = async (opts) => new Promise((resolve, reject) => chrome.proxy.settings.clear(opts, cb(resolve, reject))) |
| 82 | + |
| 83 | +// registerSubdomainProxyChromium sets proxy using API available in Chromium |
| 84 | +// https://developer.chrome.com/extensions/proxy |
| 85 | +async function registerSubdomainProxyChromium (enable, proxyHost) { |
| 86 | + const scope = 'regular_only' |
| 87 | + |
| 88 | + // read current proxy settings |
| 89 | + const settings = await get({ incognito: false }) |
| 90 | + |
| 91 | + // set or update, if enabled |
| 92 | + if (enable) { |
| 93 | + // PAC script enables selective routing to PROXY at host+port |
| 94 | + // here, PROXY is the same as HTTP API endpoint |
| 95 | + const pacConfig = { |
| 96 | + mode: 'pac_script', |
| 97 | + pacScript: { |
| 98 | + data: 'function FindProxyForURL(url, host) {\n' + |
| 99 | + " if (shExpMatch(host, '*.localhost'))\n" + |
| 100 | + ` return 'PROXY ${proxyHost}';\n` + |
| 101 | + " return 'DIRECT';\n" + |
| 102 | + '}' |
| 103 | + } |
| 104 | + } |
| 105 | + await set({ value: pacConfig, scope }) |
| 106 | + log(`enabled ${proxyHost} as HTTP proxy for *.localhost`) |
| 107 | + // log('updated chrome.proxy.settings', await get({ incognito: false })) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + // else: remove any existing proxy settings |
| 112 | + if (settings && settings.levelOfControl === 'controlled_by_this_extension') { |
| 113 | + // remove any proxy settings ipfs-companion set up before |
| 114 | + await clear({ scope }) |
| 115 | + log('disabled HTTP proxy for *.localhost') |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +module.exports.registerSubdomainProxy = registerSubdomainProxy |
0 commit comments