Firefox extension proxy

471 Views Asked by At

I am trying to create a Firefox extension to block search terms on school computers. I'd like to prohibit a list of keywords, but the blocking doesn't seem to be working.

I found an example through a plugin gallery here: https://github.com/mdn/webextensions-examples/blob/master/proxy-blocker/background/proxy-handler.js

This plugin listens to blocked hosts, and then basically returns localhost. I'd like to do the same, but when search terms are added in. I used the code in the example above as a starting point.

Here is the code I have so far:

// Initialize the list of blocked hosts
let blockedHosts = ["www.duckduckgo.com", "www.google.com"];
let blockedTerms = ["games", "minecraft", "legos"];

// Set the default list on installation.
browser.runtime.onInstalled.addListener(details => {
  browser.storage.local.set({
    blockedHosts: blockedHosts
  });
});

// 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>"]});

function handleProxyRequest(requestInfo) {
  let urlToCheck = new URL(requestInfo.documentUrl)
  let searchString = urlToCheck.search;
  const url = new URL(requestInfo.url);
  let found;

  blockedTerms.map((term) =>{
    if(searchString.search(term) != -1){
      found = true
    }
  })

  if ( blockedHosts.indexOf(url.hostname) != -1 & found) {
    return {type: "https", 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.onError.addListener(error => {
  console.error(`Proxy error: ${error.message}`);
});

The URL that the browser creates is https://duckduckgo.com/?t=ffab&q=games&ia=web for example. I can determine that the term "games" was found, and that it was found in a duck duck go search, but the proxy wont work and the browser wont stop the user from going to the page.

Any help would be appreciated!

1

There are 1 best solutions below

4
erosman On

To start with, in a school environment, I suppose they have to use school net connection. It would be a lot easier to block at the main internet connection instead of creating and installing an addon on each computer (that might be altered or bypassed with another browser).

However, to answer your question, the following would be one (simpler) way of doing that using webRequest.onBeforeRequest:

// add a listener for web requests
browser.webRequest.onBeforeRequest.addListener(process, {
    urls: ['*://*/*']
  },
  ['blocking']
);

function process(e) {
  // e.url is the target url
  // no need for storage as the filter-list is hard-coded
  const blockedHosts = ['www.duckduckgo.com', 'www.google.com'];
  const blockedTerms = ['games', 'minecraft', 'legos'];

  const hostRegExp = new RegExp(`^https?://(${blockedHosts.join('|')})/`, 'i');
  const termRegExp = new RegExp(`(${blockedTerms.join('|')})`, 'i');

  // if matches above criteria, redirect to 127.0.0.1
  if (hostRegExp.test(e.url) && termRegExp.test(e.url)) {
    return {redirectUrl: 'https://127.0.0.1:65535/'};
  }
}