Skip to content

Commit

Permalink
proxy-blocker updates (mdn#422)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rebloor authored and wbamberg committed Sep 4, 2019
1 parent 12f5920 commit 72dfcf1
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 87 deletions.
8 changes: 3 additions & 5 deletions examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
21 changes: 9 additions & 12 deletions proxy-blocker/README.md
Original file line number Diff line number Diff line change
@@ -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.
83 changes: 38 additions & 45 deletions proxy-blocker/background/proxy-handler.js
Original file line number Diff line number Diff line change
@@ -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: ["<all_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}`);
});



4 changes: 2 additions & 2 deletions proxy-blocker/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -27,6 +27,6 @@
"browser_style": true
},

"permissions": ["proxy", "storage"]
"permissions": ["proxy", "storage", "<all_urls>"]

}
23 changes: 0 additions & 23 deletions proxy-blocker/proxy/proxy-script.js

This file was deleted.

25 changes: 25 additions & 0 deletions themes/temp/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit 72dfcf1

Please sign in to comment.