summaryrefslogtreecommitdiff
path: root/data/extensions/torproxy@icecat.gnu/background.js
diff options
context:
space:
mode:
authorRuben Rodriguez <ruben@trisquel.info>2022-09-08 20:18:54 -0400
committerRuben Rodriguez <ruben@trisquel.info>2022-09-08 20:18:54 -0400
commit5da28b0f8771834ae208d61431d632875e9f8e7d (patch)
tree688ecaff26197bad8abde617b4947b11d617309e /data/extensions/torproxy@icecat.gnu/background.js
parent4a87716686104266a9cccc2d83cc249e312f3673 (diff)
Updated extensions:
* Upgraded Privacy Redirect to 1.1.49 and configured to use the 10 most reliable invidious instances * Removed ViewTube * Added torproxy@icecat.gnu based on 'Proxy toggle' extension * Added jShelter 0.11.1 * Upgraded LibreJS to 7.21.0 * Upgraded HTTPS Everywhere to 2021.7.13 * Upgraded SubmitMe to 1.9
Diffstat (limited to 'data/extensions/torproxy@icecat.gnu/background.js')
-rw-r--r--data/extensions/torproxy@icecat.gnu/background.js149
1 files changed, 149 insertions, 0 deletions
diff --git a/data/extensions/torproxy@icecat.gnu/background.js b/data/extensions/torproxy@icecat.gnu/background.js
new file mode 100644
index 0000000..8a27219
--- /dev/null
+++ b/data/extensions/torproxy@icecat.gnu/background.js
@@ -0,0 +1,149 @@
+const DEBUG = 0;
+const DIRECT_PROXY = {
+ type: 'direct'
+};
+const DEFAULT_PROXY_SETTINGS = {
+ type: 'socks',
+ host: '127.0.0.1',
+ port: 9050,
+ username: '',
+ password: '',
+ proxyDNS: true
+};
+let proxies = [ DIRECT_PROXY, DIRECT_PROXY ];
+var skipLocal = 1;
+var pendingRequests = [];
+var currentProxy = 0;
+
+function buttonClicked() {
+ currentProxy = (currentProxy ? 0 : 1);
+ browser.storage.local.set({ currentProxy: currentProxy });
+ updateState();
+}
+
+function updateState() {
+ if(currentProxy==0)
+ {
+ if (DEBUG)
+ console.log('Update state (proxy off)');
+ browser.browserAction.setIcon({path: "icons/p1.svg"});
+ browser.browserAction.setTitle({title: "Tor Proxy (disabled)"});
+ }
+ else
+ {
+ if (DEBUG)
+ console.log('Update state (proxy on)');
+ browser.browserAction.setIcon({path: "icons/p2.svg"});
+ browser.browserAction.setTitle({title: "Tor Proxy (enabled)"});
+ }
+}
+
+function settingsChanged(settings) {
+ if ("proxySettings" in settings)
+ proxies[1] = settings.proxySettings.newValue;
+ if ("skipLocal" in settings)
+ skipLocal = settings.skipLocal.newValue;
+}
+
+function completed(requestDetails) {
+ if (DEBUG) {
+ console.log("completed request: " + requestDetails.requestId);
+ }
+ var index = pendingRequests.indexOf(requestDetails.requestId);
+ if (index > -1) {
+ pendingRequests.splice(index, 1);
+ }
+}
+
+function provideCredentialsSync(requestDetails) {
+ if (!requestDetails.isProxy)
+ return;
+ if (!currentProxy == 1)
+ return;
+ if (pendingRequests.indexOf(requestDetails.requestId) != -1) {
+ //if we've seen the request before, assume bad credentials and give up
+ console.log("Bad proxy credentials for request: " + requestDetails.requestId);
+ return {cancel:true};
+ }
+ var credentials = {
+ username: proxies[1].username,
+ password: proxies[1].password
+ }
+ pendingRequests.push(requestDetails.requestId);
+ if (DEBUG) {
+ console.log(`Providing proxy credentials for request: ${requestDetails.requestId} username: ${credentials.username}`);
+ }
+ return {authCredentials: credentials};
+}
+
+function isLocalIPv4(host)
+{
+ var octets = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(host);
+ if(!octets)
+ return false;
+ if(octets[1]>255||octets[2]>255||octets[3]>255||octets[4]>255)
+ return false;
+ if(octets[1]==10||octets[1]==127) //class A or local loopback
+ return true;
+ if(octets[1]==172&&octets[2]>=16&&octets[2]<=31) //class B
+ return true;
+ if(octets[1]==192&&octets[2]==168) //class C
+ return true;
+ return false;
+}
+
+function isLocal(host)
+{
+ if(host.indexOf('.') == -1)
+ return true;
+ if(host.endsWith(".local"))
+ return true;
+ if(host=="::1")
+ return true;
+ return(isLocalIPv4(host));
+}
+
+function handleProxyRequest(requestInfo) {
+ const url = new URL(requestInfo.url);
+ var host = url.hostname;
+ var proxyNum = currentProxy;
+ if (skipLocal) {
+ if(isLocal(host)) {
+ if (DEBUG)
+ console.log(`Local host detected: ${host}`);
+ proxyNum = 0;
+ }
+ }
+ if (DEBUG) {
+ console.log(`Proxying: ${url.hostname}`);
+ console.log(proxies[proxyNum]);
+ }
+ return(proxies[proxyNum]);
+}
+
+browser.storage.local.get({ currentProxy: 0, skipLocal: true, proxySettings: DEFAULT_PROXY_SETTINGS }, items=>{
+ currentProxy = items.currentProxy;
+ skipLocal = items.skipLocal;
+ proxies[1] = items.proxySettings;
+ updateState();
+});
+
+browser.storage.onChanged.addListener(settingsChanged);
+browser.browserAction.onClicked.addListener(buttonClicked);
+browser.proxy.onRequest.addListener(handleProxyRequest, {urls: ["<all_urls>"]});
+browser.proxy.onError.addListener(error => {
+ console.error(`Proxy error: ${error.message}`);
+});
+browser.webRequest.onAuthRequired.addListener(
+ provideCredentialsSync,
+ {urls: ["<all_urls>"]},
+ ["blocking"]
+);
+browser.webRequest.onCompleted.addListener(
+ completed,
+ {urls: ["<all_urls>"]}
+);
+browser.webRequest.onErrorOccurred.addListener(
+ completed,
+ {urls: ["<all_urls>"]}
+);