From 72dfcf1badc55c4c4e0d718ae0d7e1ea3a505546 Mon Sep 17 00:00:00 2001 From: rebloor Date: Thu, 5 Sep 2019 06:28:46 +1200 Subject: [PATCH] proxy-blocker updates (#422) * proxy-blocker updates These changes replace the use of the deprecated `proxy.register` (PAC file) with `proxy.onRequest` to handle the proxying of requests. * Changes as per feedback * Fixed typo * Addressed feedback relating to issue with variable initialization after enable/disable * Typo fix * The changes as suggested by @wbamberg --- examples.json | 8 +-- proxy-blocker/README.md | 21 +++--- proxy-blocker/background/proxy-handler.js | 83 +++++++++++------------ proxy-blocker/manifest.json | 4 +- proxy-blocker/proxy/proxy-script.js | 23 ------- themes/temp/manifest.json | 25 +++++++ 6 files changed, 77 insertions(+), 87 deletions(-) delete mode 100644 proxy-blocker/proxy/proxy-script.js create mode 100644 themes/temp/manifest.json diff --git a/examples.json b/examples.json index 4f3da26f..b3864c55 100644 --- a/examples.json +++ b/examples.json @@ -410,13 +410,11 @@ "name": "private-browsing-theme" }, { - "description": "Uses the proxy API to block requests to specific hosts.", + "description": "Uses the proxy API to block requests to hosts specified on a list.", "javascript_apis": [ "extension.getURL", - "proxy.onProxyError", - "proxy.register", - "runtime.onMessage", - "runtime.sendMessage", + "proxy.onRequest", + "proxy.onError", "storage.local", "storage.onChanged" ], diff --git a/proxy-blocker/README.md b/proxy-blocker/README.md index 926db7f5..c050a1d7 100644 --- a/proxy-blocker/README.md +++ b/proxy-blocker/README.md @@ -1,22 +1,19 @@ -# proxy-filter ## What it does -This add-on registers a [PAC script](https://developer.mozilla.org/en-US/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_%28PAC%29_file) using the proxy API. +This extension uses the proxy API listener `onRequest` to listen for requests to visit a web page, compare the webpage's domain with a blocked host list, and proxy domains on the blocked list to 127.0.0.1. -The PAC script is initialized with a list of hostnames: it blocks requests to any hosts that are in the list. +The list of blocked domains is held in local storage and given the initial value `["example.com", "example.org"]` when the extension installs. The list can be modified through the extension"s options page. -The list is given the following default values: `["example.com", "example.org"]`, but the user can add and remove hosts using the add-on's options page. +Note that the hostname-matching is simple: hostnames must match an entry in the list if they are to be blocked. So with the default settings, "example.org" is blocked but "www.example.org" is permitted. -Note that the hostname-matching is very simple: hostnames must exactly match an entry in the list if they are to be blocked. So with the default settings, "example.org" would be blocked but "www.example.org" would be permitted. - -To try it out: +To try out this extension: * install it -* try visiting `http://example.com`, and see it is blocked -* visit `about:addons`, open the add-on's preferences, and try changing the hostnames in the text box -* try visiting some different pages, to see the effect of your changes. +* visit `http://example.com` and see it is blocked +* visit `about:addons`, open the add-on's preferences, and change the hostnames in the text box +* visit some pages to see the effect of your changes. ## What it shows -* How to implement a simple PAC script, and register it using the proxy API. -* How to exchange messages between a PAC script and a background script. +* How to implement `browser.proxy.onRequest` and proxy requests. +* How to store and retrieve lists from local storage. diff --git a/proxy-blocker/background/proxy-handler.js b/proxy-blocker/background/proxy-handler.js index 4a0ff0bf..b2a2d57d 100644 --- a/proxy-blocker/background/proxy-handler.js +++ b/proxy-blocker/background/proxy-handler.js @@ -1,55 +1,48 @@ -// Location of the proxy script, relative to manifest.json -const proxyScriptURL = "proxy/proxy-script.js"; +// Initialize the list of blocked hosts +let blockedHosts = ["example.com", "example.org"]; -// Default settings. If there is nothing in storage, use these values. -const defaultSettings = { - blockedHosts: ["example.com", "example.org"] - } - -// Register the proxy script -browser.proxy.register(proxyScriptURL); +// Set the default list on installation. +browser.runtime.onInstalled.addListener(details => { + browser.storage.local.set({ + blockedHosts: blockedHosts + }); +}); -// Log any errors from the proxy script -browser.proxy.onProxyError.addListener(error => { - console.error(`Proxy error: ${error.message}`); +// Get the stored list +browser.storage.local.get(data => { + if (data.blockedHosts) { + blockedHosts = data.blockedHosts; + } }); -// Initialize the proxy -function handleInit() { - // update the proxy whenever stored settings change - browser.storage.onChanged.addListener((newSettings) => { - browser.runtime.sendMessage(newSettings.blockedHosts.newValue, {toProxyScript: true}); - }); +// Listen for changes in the blocked list +browser.storage.onChanged.addListener(changeData => { + blockedHosts = changeData.blockedHosts.newValue; +}); - // get the current settings, then... - browser.storage.local.get() - .then((storedSettings) => { - // if there are stored settings, update the proxy with them... - if (storedSettings.blockedHosts) { - browser.runtime.sendMessage(storedSettings.blockedHosts, {toProxyScript: true}); - // ...otherwise, initialize storage with the default values - } else { - browser.storage.local.set(defaultSettings); - } - - }) - .catch(()=> { - console.log("Error retrieving stored settings"); - }); -} +// Managed the proxy -function handleMessage(message, sender) { - // only handle messages from the proxy script - if (sender.url != browser.extension.getURL(proxyScriptURL)) { - return; - } +// Listen for a request to open a webpage +browser.proxy.onRequest.addListener(handleProxyRequest, {urls: [""]}); - if (message === "init") { - handleInit(message); - } else { - // after the init message the only other messages are status messages - console.log(message); +// On the request to open a webpage +function handleProxyRequest(requestInfo) { +// Read the web address of the page to be visited + const url = new URL(requestInfo.url); +// Determine whether the domain in the web address is on the blocked hosts list + if (blockedHosts.indexOf(url.hostname) != -1) { +// Write details of the proxied host to the console and return the proxy address + console.log(`Proxying: ${url.hostname}`); + return {type: "http", host: "127.0.0.1", port: 65535}; } +// Return instructions to open the requested webpage + return {type: "direct"}; } -browser.runtime.onMessage.addListener(handleMessage); +// Log any errors from the proxy script +browser.proxy.onError.addListener(error => { + console.error(`Proxy error: ${error.message}`); +}); + + + diff --git a/proxy-blocker/manifest.json b/proxy-blocker/manifest.json index da368524..6e7263fd 100644 --- a/proxy-blocker/manifest.json +++ b/proxy-blocker/manifest.json @@ -3,7 +3,7 @@ "manifest_version": 2, "name": "Proxy-blocker", "description": "Uses the proxy API to block requests to specific hosts.", - "version": "1.0", + "version": "2.0", "icons": { "48": "icons/block.svg", @@ -27,6 +27,6 @@ "browser_style": true }, - "permissions": ["proxy", "storage"] + "permissions": ["proxy", "storage", ""] } diff --git a/proxy-blocker/proxy/proxy-script.js b/proxy-blocker/proxy/proxy-script.js deleted file mode 100644 index 4a133fb5..00000000 --- a/proxy-blocker/proxy/proxy-script.js +++ /dev/null @@ -1,23 +0,0 @@ -/* exported FindProxyForURL */ - -var blockedHosts = []; -const allow = "DIRECT"; -const deny = "PROXY 127.0.0.1:65535"; - -// tell the background script that we are ready -browser.runtime.sendMessage("init"); - -// listen for updates to the blocked host list -browser.runtime.onMessage.addListener((message) => { - blockedHosts = message; -}); - -// required PAC function that will be called to determine -// if a proxy should be used. -function FindProxyForURL(url, host) { - if (blockedHosts.indexOf(host) != -1) { - browser.runtime.sendMessage(`Proxy-blocker: blocked ${url}`); - return deny; - } - return allow; -} diff --git a/themes/temp/manifest.json b/themes/temp/manifest.json new file mode 100644 index 00000000..44ba9470 --- /dev/null +++ b/themes/temp/manifest.json @@ -0,0 +1,25 @@ +{ + + "description": "Theme with a single image placed centrally and then tiled. Also, illustrates the use of frame_inactive to change the header background color when the browser window isn't in focus. See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples#themes", + "manifest_version": 2, + "name": "temp", + "version": "1.1", + "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/themes/weta_tiled", + + "theme": { + "colors": { + "frame": "blue", + "frame_inactive": "#c6c6c6", + "toolbar_text": "black", + "tab_background_text":"black", + "tab_text":"black", + "sidebar_border":"black", + "toolbar_field_border":"black", + "tab_background_separator":"black", + "toolbar_top_separator":"black", + "toolbar_vertical_separator":"black", + "toolbar_bottom_separator":"black", + "toolbar_field_separator":"black" + } + } +} \ No newline at end of file