Files
I2P_in_Private_Browsing_Mod…/proxy.js

362 lines
10 KiB
JavaScript
Raw Normal View History

var titlepref = chrome.i18n.getMessage("titlePreface");
var titleprefpriv = chrome.i18n.getMessage("titlePrefacePrivate");
var webpref = chrome.i18n.getMessage("webPreface");
var webprefpriv = chrome.i18n.getMessage("webPrefacePrivate");
var routerpref = chrome.i18n.getMessage("routerPreface");
var routerprefpriv = chrome.i18n.getMessage("routerPrefacePrivate");
var mailpref = chrome.i18n.getMessage("mailPreface");
var mailprefpriv = chrome.i18n.getMessage("mailPrefacePrivate");
var torrentpref = chrome.i18n.getMessage("torrentPreface");
var torrentprefpriv = chrome.i18n.getMessage("torrentPrefacePrivate");
var tunnelpref = chrome.i18n.getMessage("i2ptunnelPreface");
var tunnelprefpriv = chrome.i18n.getMessage("i2ptunnelPrefacePrivate");
2019-10-28 01:11:16 -04:00
browser.privacy.network.peerConnectionEnabled.set({
value: false
});
console.log("Preliminarily disabled WebRTC.");
2019-06-17 19:17:11 -04:00
chrome.privacy.network.networkPredictionEnabled.set({
2019-10-06 15:14:53 -04:00
value: false
});
chrome.privacy.network.webRTCIPHandlingPolicy.set({
2019-10-06 15:14:53 -04:00
value: "disable_non_proxied_udp"
});
function shouldProxyRequest(requestInfo) {
2019-10-06 15:14:53 -04:00
return requestInfo.parentFrameId != -1;
}
var handleContextProxyRequest = async function(requestDetails) {
2019-10-06 15:14:53 -04:00
console.log("(proxy)Searching for proxy by context");
try {
var handleProxyRequest = function(context) {
proxy = {
failoverTimeout: 0,
proxyDns: false
2019-10-06 15:18:10 -04:00
};
2019-11-27 00:31:10 -05:00
if (context == "firefox-default" || context == "firefox-private") {
proxy = null;
return proxy;
}
if (context != undefined) {
if (context.name == titlepref) {
proxy = {
type: getScheme(),
host: getHost(),
port: getPort()
};
console.log("(proxy)", context.name);
console.log("Using", proxy.type);
console.log("proxy ", proxy.host + ":" + proxy.port);
return proxy;
} else if (context.name == routerpref) {
2019-11-10 22:24:10 -05:00
if (routerHost(requestDetails.url)) {
return proxy;
} else if (!routerHost(requestDetails.url)) {
proxy = {
type: "http",
host: "localhost",
2019-11-10 22:24:10 -05:00
port: "65535"
};
}
2019-11-10 22:24:10 -05:00
proxy = {
type: getScheme(),
host: getHost(),
port: getPort()
};
console.log("(proxy)", context.name);
console.log("Using", proxy.type);
console.log("proxy ", proxy.host + ":" + proxy.port);
2019-11-10 22:24:10 -05:00
return proxy;
} else if (context.name == webpref) {
2019-11-10 22:24:10 -05:00
if (localHost(requestDetails.url)) {
if (!routerHost(requestDetails.url)) {
proxy = {
type: "http",
host: "localhost",
port: "65535"
};
}
}
console.log("(proxy)", context.name);
console.log("Using", proxy.type);
console.log("proxy ", proxy.host + ":" + proxy.port);
return proxy;
}
}
2019-11-10 22:24:10 -05:00
if (!routerHost(requestDetails.url)) {
if (localHost(requestDetails.url)) {
2019-11-11 16:34:41 -05:00
console.log(
"(proxy) non-routerconsole localhost url, will not interfere",
2019-11-11 16:34:41 -05:00
requestDetails.url
);
/*proxy = {
type: "http",
host: "localhost",
port: "65535"
};*/
}
2019-11-10 22:24:10 -05:00
} else if (i2pHost(requestDetails.url)) {
2019-10-06 15:14:53 -04:00
proxy = {
type: getScheme(),
host: getHost(),
2019-10-06 15:18:10 -04:00
port: getPort()
};
2019-10-06 15:14:53 -04:00
}
2019-10-06 15:18:10 -04:00
return proxy;
};
2019-10-06 15:14:53 -04:00
var contextGet = async function(tabInfo) {
try {
2019-10-06 15:18:10 -04:00
console.log("(proxy)Tab info from Function", tabInfo);
context = await browser.contextualIdentities.get(tabInfo.cookieStoreId);
return context;
} catch (error) {
return; //"firefox-default";
}
};
2019-10-06 15:14:53 -04:00
var tabGet = async function(tabId) {
try {
2019-10-06 15:18:10 -04:00
console.log("(proxy)Tab ID from Request", tabId);
let tabInfo = await browser.tabs.get(tabId);
return tabInfo;
} catch (error) {
console.log("(proxy)Tab error", error);
2019-10-06 15:14:53 -04:00
}
2019-10-06 15:18:10 -04:00
};
2019-10-06 15:14:53 -04:00
if (requestDetails.tabId > 0) {
2019-11-11 21:39:35 -05:00
console.log("manifest", requestDetails);
2019-11-11 21:38:38 -05:00
if (proxyHost(requestDetails.url)) {
proxy = {
type: getScheme(),
host: getHost(),
port: getPort()
};
2019-11-11 21:39:35 -05:00
return proxy;
} else if (i2pHost(requestDetails.url)) {
console.log("(Proxy)I2P URL detected, ");
var tab = tabGet(requestDetails.tabId);
requestDetails.tabId = tab;
var context = tab.then(contextGet);
var proxy = await context.then(handleProxyRequest);
console.log("(proxy)Returning I2P Proxy", proxy);
return proxy;
} else {
var tab = tabGet(requestDetails.tabId);
var context = tab.then(contextGet);
var proxy = await context.then(handleProxyRequest);
console.log("(proxy)Returning I2P Proxy", proxy);
return proxy;
}
proxy = {};
console.log("(proxy)Returning unset Proxy", proxy);
2019-11-10 22:24:10 -05:00
return proxy;
2019-10-06 15:14:53 -04:00
}
2019-10-06 15:18:10 -04:00
} catch (error) {
2019-10-06 15:14:53 -04:00
console.log("(proxy)Not using I2P Proxy.", error);
}
2019-10-06 15:18:10 -04:00
};
2019-06-17 19:17:11 -04:00
2019-10-06 15:18:10 -04:00
var proxy_scheme = "HTTP";
var proxy_host = "127.0.0.1";
var proxy_port = "4444";
var control_host = "127.0.0.1";
var control_port = "4444";
var disable_history = false;
function SetupSettings() {
console.log("Initialising Settings");
//
function checkSchemeStoredSettings(storedSettings) {
if (storedSettings.proxy_scheme != undefined) {
proxy_scheme = storedSettings.proxy_scheme;
} else {
proxy_scheme = "http";
storedSettings.proxy_scheme = proxy_scheme;
}
console.log("Initialising Proxy Scheme", storedSettings.proxy_scheme);
setupProxy();
}
var gettingSchemeStoredSettings = browser.storage.local.get("proxy_scheme");
gettingSchemeStoredSettings.then(checkSchemeStoredSettings, onError);
//
function checkHostStoredSettings(storedSettings) {
if (storedSettings.proxy_host != undefined) {
proxy_host = storedSettings.proxy_host;
} else {
proxy_host = "127.0.0.1";
storedSettings.proxy_host = proxy_host;
}
console.log("Initialising Host", storedSettings.proxy_host);
setupProxy();
}
var gettingHostStoredSettings = browser.storage.local.get("proxy_host");
gettingHostStoredSettings.then(checkHostStoredSettings, onError);
//
function checkPortStoredSettings(storedSettings) {
if (storedSettings.proxy_port != undefined) {
proxy_port = storedSettings.proxy_port;
} else {
proxy_port = "4444";
storedSettings.proxy_port = proxy_port;
}
console.log("Initialising Port", storedSettings.proxy_port);
setupProxy();
}
var gettingPortStoredSettings = browser.storage.local.get("proxy_port");
gettingPortStoredSettings.then(checkPortStoredSettings, onError);
//
function checkControlHostStoredSettings(storedSettings) {
if (storedSettings.control_host != undefined) {
control_host = storedSettings.control_host;
} else {
control_host = "127.0.0.1";
storedSettings.control_host = control_host;
}
console.log("Initialising Control Host", storedSettings.control_host);
setupProxy();
}
2019-12-24 16:51:38 -05:00
var gettingControlHostStoredSettings = browser.storage.local.get(
"control_host"
);
gettingControlHostStoredSettings.then(
checkControlHostStoredSettings,
onError
);
//
function checkControlPortStoredSettings(storedSettings) {
if (storedSettings.control_port != undefined) {
contro_port = storedSettings.control_port;
} else {
control_port = "7657";
storedSettings.control_port = control_port;
}
console.log("Initialising Control Port", storedSettings.control_port);
setupProxy();
}
2019-12-24 16:51:38 -05:00
var gettingControlPortStoredSettings = browser.storage.local.get(
"control_port"
);
gettingControlPortStoredSettings.then(
checkControlPortStoredSettings,
onError
);
//
function checkHistoryStoredSettings(storedSettings) {
if (storedSettings.disable_history != undefined) {
disable_history = storedSettings.disable_history;
} else {
disable_history = false;
storedSettings.disable_history = disable_history;
}
console.log(
"Initialising Disabled History",
storedSettings.disable_history
);
setupProxy();
}
2019-12-24 16:51:38 -05:00
var gettingHistoryStoredSettings = browser.storage.local.get(
"disable_history"
);
gettingHistoryStoredSettings.then(checkHistoryStoredSettings, onError);
}
2019-06-17 19:17:11 -04:00
function getScheme() {
2019-10-06 15:14:53 -04:00
if (proxy_scheme == undefined) {
2019-10-06 15:18:10 -04:00
proxy_scheme = "http";
2019-10-06 15:14:53 -04:00
}
if (proxy_scheme == "HTTP") {
2019-10-06 15:18:10 -04:00
proxy_scheme = "http";
2019-10-06 15:14:53 -04:00
}
if (proxy_scheme == "SOCKS") {
2019-10-06 15:18:10 -04:00
proxy_scheme = "socks";
2019-10-06 15:14:53 -04:00
}
if (proxy_scheme != "http" && proxy_scheme != "socks") {
2019-10-06 15:18:10 -04:00
proxy_scheme = "http";
2019-10-06 15:14:53 -04:00
}
//console.log("Got i2p proxy scheme:", proxy_scheme);
return proxy_scheme;
2019-06-17 19:17:11 -04:00
}
function getHost() {
2019-10-06 15:14:53 -04:00
if (proxy_host == undefined) {
2019-10-06 15:18:10 -04:00
proxy_host = "127.0.0.1";
2019-10-06 15:14:53 -04:00
}
return proxy_host;
2019-06-17 19:17:11 -04:00
}
function getPort() {
2019-10-06 15:14:53 -04:00
if (proxy_port == undefined) {
2019-10-06 15:18:10 -04:00
var scheme = getScheme();
2019-10-06 15:14:53 -04:00
if (scheme == "socks") {
2019-10-06 15:18:10 -04:00
proxy_port = "4446";
2019-10-06 15:14:53 -04:00
} else {
2019-10-06 15:18:10 -04:00
proxy_port = "4444";
2019-06-17 19:17:11 -04:00
}
2019-10-06 15:14:53 -04:00
}
return proxy_port;
2019-06-17 19:17:11 -04:00
}
function getControlHost() {
2019-10-06 15:14:53 -04:00
if (control_host == undefined) {
2019-10-06 15:18:10 -04:00
return "127.0.0.1";
2019-10-06 15:14:53 -04:00
}
return control_host;
2019-06-17 19:17:11 -04:00
}
function getControlPort() {
2019-10-06 15:14:53 -04:00
if (control_port == undefined) {
return "7657";
2019-10-06 15:14:53 -04:00
}
return control_port;
}
function setupProxy() {
2019-10-28 01:11:16 -04:00
/**/
console.log("Setting up Firefox WebExtension proxy");
browser.proxy.onRequest.addListener(handleContextProxyRequest, {
urls: ["<all_urls>"]
});
console.log("i2p settings created for WebExtension Proxy");
/**/
}
function update() {
2019-10-06 15:18:10 -04:00
console.log("restoring proxy scheme:", proxy_scheme);
console.log("restoring proxy host:", proxy_host);
console.log("restoring proxy port:", proxy_port);
console.log("restoring control host:", control_host);
console.log("restoring control port:", control_port);
2019-06-17 19:17:11 -04:00
}
function updateFromStorage() {
console.log("updating settings from storage");
var gettingInfo = browser.runtime.getPlatformInfo();
gettingInfo.then(got => {
if (got.os != "android") {
browser.windows.getAll().then(wins => wins.forEach(themeWindow));
chrome.storage.local.get(function(got) {
SetupSettings();
update();
setupProxy();
});
} else {
chrome.storage.local.get(function(got) {
SetupSettings();
update();
setupProxy();
});
}
});
2019-05-02 17:31:04 -04:00
}
2019-11-24 04:28:14 -05:00
browser.storage.onChanged.addListener(updateFromStorage);
SetupSettings();
2019-11-27 00:31:10 -05:00
setupProxy();