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
This commit is contained in:
rebloor
2019-09-05 06:28:46 +12:00
committed by wbamberg
parent 12f59209c9
commit 72dfcf1bad
6 changed files with 78 additions and 88 deletions

View File

@@ -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"
],

View File

@@ -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.

View File

@@ -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"]
}
// Set the default list on installation.
browser.runtime.onInstalled.addListener(details => {
browser.storage.local.set({
blockedHosts: blockedHosts
});
});
// Register the proxy script
browser.proxy.register(proxyScriptURL);
// Get the stored list
browser.storage.local.get(data => {
if (data.blockedHosts) {
blockedHosts = data.blockedHosts;
}
});
// Listen for changes in the blocked list
browser.storage.onChanged.addListener(changeData => {
blockedHosts = changeData.blockedHosts.newValue;
});
// Managed the proxy
// Listen for a request to open a webpage
browser.proxy.onRequest.addListener(handleProxyRequest, {urls: ["<all_urls>"]});
// 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"};
}
// Log any errors from the proxy script
browser.proxy.onProxyError.addListener(error => {
browser.proxy.onError.addListener(error => {
console.error(`Proxy error: ${error.message}`);
});
// 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});
});
// 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");
});
}
function handleMessage(message, sender) {
// only handle messages from the proxy script
if (sender.url != browser.extension.getURL(proxyScriptURL)) {
return;
}
if (message === "init") {
handleInit(message);
} else {
// after the init message the only other messages are status messages
console.log(message);
}
}
browser.runtime.onMessage.addListener(handleMessage);

View File

@@ -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", "<all_urls>"]
}

View File

@@ -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;
}

25
themes/temp/manifest.json Normal file
View File

@@ -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"
}
}
}