Browse Source

fixes for new, yet another proxy API implementation in firefox

Andrew Dolgov 3 months ago
parent
commit
9804235dff
4 changed files with 33 additions and 60 deletions
  1. 3 9
      background.js
  2. 29 13
      common.js
  3. 1 1
      manifest.json
  4. 0 37
      proxy.js

+ 3 - 9
background.js

@@ -102,7 +102,9 @@ function ping() {
                set_proxy(settings, false);
 				}
 			})
-			.catch(() => {
+			.catch((err) => {
+				console.log('ping error', url, err);
+
 				set_badge('er1', 'red');
 				set_title("Proxy connection failed");
 
@@ -127,14 +129,6 @@ chrome.browserAction.onClicked.addListener(() => {
 	ping();
 });
 
-if (!chrome.proxy.settings) {
-	chrome.runtime.onMessage.addListener((message, sender) => {
-		if (sender.url == browser.extension.getURL('proxy.js')) {
-			console.log('<<<', message);
-		}
-	});
-}
-
 setTimeout(function() {
 	ping();
 }, 1000);

+ 29 - 13
common.js

@@ -37,17 +37,13 @@ function set_proxy(settings, reachable) {
 		console.log("proxy set as unreachable, removing...");
 
 		if (chrome.proxy.settings) {
-
 			chrome.proxy.settings.set(
 				{value: {mode: "direct"}, scope: 'regular'}, () => { console.log("proxy removed"); });
-
-		} else if (browser.proxy.register) {
-			console.warn("TODO", "reset to direct mode on firefox not implemented yet");
 		}
 
 		return;
 	} else {
-		console.log("setting proxy...");
+		console.log("reachable, setting proxy...");
 	}
 
 	if (typeof browser == 'undefined') {
@@ -68,17 +64,37 @@ function set_proxy(settings, reachable) {
 	} else {
 		console.log('ff mode');
 
-		browser.proxy.onProxyError.addListener((error) => {
+		/*browser.proxy.onError.addListener((error) => {
 			console.error('proxy error:', error);
-		});
+		});*/
+
+		browser.proxy.onRequest.addListener((requestInfo) => {
+			const url = new URL(requestInfo.url);
+			const host = url.hostname;
+
+			const [proxy_host, proxy_port] = settings.url.split(/:/);
+			const whitelisted_urls = settings.whitelist.split(/\n/);
+
+			if (host == proxy_host)
+				return {type: 'direct'};
+
+			for (var i = 0; i < whitelisted_urls.length; i++) {
+				var check_url = whitelisted_urls[i];
+
+				if (check_url[0] == ".") check_url = check_url.substr(1);
+
+				if (check_url) {
+					// not strictly subdomain matching i suppose...
+					if (host.indexOf(check_url) !== -1) {
+						return { type: 'https', host: proxy_host, port: proxy_port };
+					}
+				}
+			}
 
-		browser.proxy.register('proxy.js').then(() => {
-			console.log('ff proxy registered');
+			return {type: 'direct'};
 
-			const proxy_url = settings.url;
-			const urls = settings.whitelist.split("\n");
+		}, {urls: ["<all_urls>"]});
 
-			browser.runtime.sendMessage({urls: urls, proxy: proxy_url}, {toProxyScript: true});
-		});
+		console.log('ff: proxy listener set');
 	}
 }

+ 1 - 1
manifest.json

@@ -3,7 +3,7 @@
 		"gecko": { "id": "[email protected]", "update_url": "https://fakecake.org/xpi/updates.php" }
 	},
   "name": "Selective HTTPS proxy",
-  "version": "0.7.2",
+  "version": "0.8",
   "manifest_version": 2,
   "description": "Routes specified domains via HTTPS proxy",
   "permissions": [ "proxy", "unlimitedStorage",

+ 0 - 37
proxy.js

@@ -1,37 +0,0 @@
-var urls = [];
-var proxy = "";
-
-browser.runtime.onMessage.addListener(function(message) {
-	if (message.urls) {
-		urls = message.urls;
-	}
-
-	if (message.proxy) {
-		proxy = message.proxy;
-	}
-});
-
-function debug(msg) {
-	browser.runtime.sendMessage(msg);
-}
-
-// TODO: use proper dnsDomainIs() syntax when functions are available in Firefox
-// ref. https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/proxy
-// ref. https://bugzilla.mozilla.org/show_bug.cgi?id=1353510
-
-function FindProxyForURL(url, host) {
-
-	for (var i = 0; i < urls.length; i++) {
-		var check_url = urls[i];
-
-		if (check_url[0] == ".") check_url = check_url.substr(1);
-
-		if (check_url) {
-			if (host.indexOf(check_url) !== -1) {
-				return 'HTTPS ' + proxy;
-			}
-		}
-	}
-
-	return 'DIRECT';
-}