Files
I2P_in_Private_Browsing_Mod…/proxy.js

424 lines
12 KiB
JavaScript
Raw Normal View History

2021-09-30 16:46:20 -04:00
var titlepref = chrome.i18n.getMessage('titlePreface');
var webpref = chrome.i18n.getMessage('webPreface');
var routerpref = chrome.i18n.getMessage('routerPreface');
var mailpref = chrome.i18n.getMessage('mailPreface');
var torrentpref = chrome.i18n.getMessage('torrentPreface');
var tunnelpref = chrome.i18n.getMessage('i2ptunnelPreface');
var ircpref = chrome.i18n.getMessage('ircPreface');
var extensionpref = chrome.i18n.getMessage('extensionPreface');
var muwirepref = chrome.i18n.getMessage('muwirePreface');
var botepref = chrome.i18n.getMessage('botePreface');
var blogpref = chrome.i18n.getMessage('blogPreface');
var blogprefpriv = chrome.i18n.getMessage('blogPrefacePrivate');
2019-10-28 01:11:16 -04:00
browser.privacy.network.peerConnectionEnabled.set({
2021-09-30 16:46:20 -04:00
value: true
2019-10-28 01:11:16 -04:00
});
2019-06-17 19:17:11 -04:00
chrome.privacy.network.networkPredictionEnabled.set({
2021-09-30 16:46:20 -04:00
value: false
});
chrome.privacy.network.webRTCIPHandlingPolicy.set({
2021-09-30 16:46:20 -04:00
value: 'disable_non_proxied_udp'
});
2021-09-30 16:46:20 -04:00
console.log('Disabled unproxied UDP.');
function shouldProxyRequest(requestInfo) {
2019-10-06 15:14:53 -04:00
return requestInfo.parentFrameId != -1;
}
2021-09-30 16:46:20 -04:00
var handleContextProxyRequest = async function(requestDetails) {
function ircProxy() {
2021-09-30 16:46:20 -04:00
if (!requestDetails.url.includes('7669')) {
proxy = {
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
};
return proxy;
}
2021-09-30 16:46:20 -04:00
if (requestDetails.url.includes(':7669')) {
proxy = null;
return proxy;
}
}
function blogProxy() {
2021-09-30 16:46:20 -04:00
if (!requestDetails.url.includes('8084')) {
proxy = {
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
};
return proxy;
}
2021-09-30 16:46:20 -04:00
if (requestDetails.url.includes(':8084')) {
proxy = null;
return proxy;
}
}
function btProxy() {
2020-12-09 17:49:36 -05:00
proxy = routerProxy();
2021-09-30 16:46:20 -04:00
if (requestDetails.url.includes(':7662')) {
proxy = null;
return proxy;
}
2021-09-30 16:46:20 -04:00
console.log('(bt proxy)', proxy);
return proxy;
}
function mainProxy() {
2021-09-30 16:46:20 -04:00
console.log('(proxy) mainproxy 0');
proxy = {
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
};
2020-12-09 21:58:58 -05:00
let url = new URL(requestDetails.url);
2020-12-11 16:30:22 -05:00
if (
requestDetails.url.startsWith(
2021-09-30 16:46:20 -04:00
'http://' + getHost() + ':' + getConsolePort() + '/i2psnark/'
2020-12-11 16:30:22 -05:00
)
) {
2020-12-09 21:58:58 -05:00
//+url.host)) {
2021-09-30 16:46:20 -04:00
console.log('(proxy) mainproxy 2', url);
proxy = null;
}
return proxy;
}
function routerProxy() {
if (routerHost(requestDetails.url)) {
proxy = null;
return proxy;
} else if (!routerHost(requestDetails.url)) {
proxy = {
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
};
return proxy;
}
}
2019-10-06 15:14:53 -04:00
try {
2021-09-30 16:46:20 -04:00
var handleProxyRequest = function(context) {
2019-10-06 15:14:53 -04:00
proxy = {
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
2019-10-06 15:18:10 -04:00
};
2021-09-30 16:46:20 -04:00
if (context == 'firefox-default' || context == 'firefox-private') {
2019-11-27 00:31:10 -05:00
proxy = null;
return proxy;
}
2020-12-09 17:49:36 -05:00
if (context != undefined) {
2021-09-30 16:46:20 -04:00
console.log('(proxy), context', context);
proxy = routerProxy();
2020-12-06 12:01:27 -05:00
if (context.name == ircpref) {
proxy = ircProxy();
return proxy;
} else if (context.name == blogpref) {
proxy = blogProxy();
return proxy;
2020-12-09 17:49:36 -05:00
} else if (context.name == titlepref) {
proxy = mainProxy();
return proxy;
2020-12-09 17:49:36 -05:00
} else if (context.name == routerpref) {
proxy = routerProxy();
return proxy;
2020-12-09 17:49:36 -05:00
} else if (context.name == torrentpref) {
proxy = btProxy();
return proxy;
2021-07-07 21:28:06 -04:00
} else if (context.name == mailpref) {
return proxy;
2021-07-07 21:28:06 -04:00
} else if (context.name == tunnelpref) {
return proxy;
2021-07-07 21:28:06 -04:00
} else if (context.name == muwirepref) {
return proxy;
2021-07-07 21:28:06 -04:00
} else if (context.name == botepref) {
return proxy;
}
} else {
if (!routerHost(requestDetails.url)) {
if (localHost(requestDetails.url)) {
2021-09-30 16:46:20 -04:00
if (requestDetails.url.includes(':7669')) {
proxy = null;
2021-09-30 16:46:20 -04:00
} else if (requestDetails.url.includes(':7662')) {
proxy = null;
} else {
console.log(
2021-09-30 16:46:20 -04:00
'(proxy) non-routerconsole localhost url, will not interfere',
requestDetails.url
);
}
}
}
if (i2pHost(requestDetails.url)) {
2020-11-10 21:41:13 -05:00
proxy = {
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
2020-11-10 21:41:13 -05:00
};
2021-07-07 21:28:06 -04:00
} else {
proxy = null;
}
2021-09-30 16:46:20 -04:00
if (requestDetails.url.includes('rpc')) {
console.log('(proxy for rpc url)', rpc);
}
//var tab = tabGet(requestDetails.tabId);
//tab.then(handleTabRequest,)
return proxy;
2019-10-06 15:14:53 -04:00
}
2019-10-06 15:18:10 -04:00
};
2021-09-30 16:46:20 -04:00
var contextGet = async function(tabInfo) {
2019-10-06 15:14:53 -04:00
try {
2019-10-06 15:18:10 -04:00
context = await browser.contextualIdentities.get(tabInfo.cookieStoreId);
return context;
} catch (error) {
2021-09-30 16:46:20 -04:00
console.warn(error);
return 'firefox-default';
}
};
2021-09-30 16:46:20 -04:00
var tabGet = async function(tabId) {
2019-10-06 15:14:53 -04:00
try {
2019-10-06 15:18:10 -04:00
let tabInfo = await browser.tabs.get(tabId);
return tabInfo;
} catch (error) {
2021-09-30 16:46:20 -04:00
console.log('(proxy)Tab error', error);
2019-10-06 15:14:53 -04:00
}
2019-10-06 15:18:10 -04:00
};
2020-01-11 23:14:58 -05:00
if (proxyHost(requestDetails.url)) {
proxy = {
2020-03-05 23:25:08 -05:00
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
2020-03-05 23:25:08 -05:00
};
return proxy;
}
2021-09-30 16:46:20 -04:00
if (requestDetails.originUrl == browser.runtime.getURL('security.html')) {
2020-03-05 23:25:08 -05:00
proxy = {
2020-01-11 23:14:58 -05:00
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
2020-01-11 23:14:58 -05:00
};
return proxy;
}
2020-01-04 11:47:25 -05:00
if (
2021-09-30 16:46:20 -04:00
requestDetails.cookieStoreId == 'firefox-default' ||
requestDetails.cookieStoreId == 'firefox-private'
2020-01-04 11:47:25 -05:00
) {
2021-09-30 16:46:20 -04:00
if (browser.windows != undefined) {
return browser.proxy.settings.get({});
}
}
2019-10-06 15:14:53 -04:00
if (requestDetails.tabId > 0) {
2021-09-30 16:46:20 -04:00
if (requestDetails.url.includes('MuWire')) {
return;
}
2019-11-11 21:38:38 -05:00
if (proxyHost(requestDetails.url)) {
proxy = {
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
2019-11-11 21:38:38 -05:00
};
2019-11-11 21:39:35 -05:00
return proxy;
} else if (i2pHost(requestDetails.url)) {
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;
2021-05-24 15:40:35 -04:00
} else if (extensionHost(requestDetails)) {
return;
} else {
var tab = tabGet(requestDetails.tabId);
var context = tab.then(contextGet);
var proxy = await context.then(handleProxyRequest);
2020-01-13 15:20:32 -05:00
//console.log("(proxy)Returning I2P Proxy", proxy);
return proxy;
}
/*proxy = {};
console.log("(proxy)Returning unset Proxy", proxy);
return proxy;*/
2021-01-31 01:07:14 -05:00
} else {
proxy = {
type: getScheme(),
host: getHost(),
2021-09-30 16:46:20 -04:00
port: getPort()
2021-01-31 01:07:14 -05:00
};
2021-09-30 16:46:20 -04:00
//console.log('(proxy for rpc url)', rpc);
2021-01-31 01:07:14 -05:00
return proxy;
2019-10-06 15:14:53 -04:00
}
2019-10-06 15:18:10 -04:00
} catch (error) {
2021-09-30 16:46:20 -04:00
console.log('(proxy)Not using I2P Proxy.', error);
2019-10-06 15:14:53 -04:00
}
2021-09-30 16:46:20 -04:00
}
2019-06-17 19:17:11 -04:00
function SetupSettings() {
2021-09-30 16:46:20 -04:00
console.log('Initialising Settings');
function onSetupError() {
2021-09-30 16:46:20 -04:00
console.log('Settings initialization error');
}
//
function checkSchemeStoredSettings(storedSettings) {
if (storedSettings.proxy_scheme == undefined)
2021-09-30 16:46:20 -04:00
storedSettings.proxy_scheme = 'http';
else proxy_scheme = storedSettings.proxy_scheme;
2021-09-30 16:46:20 -04:00
console.log('Initialising Proxy Scheme', storedSettings.proxy_scheme);
setupProxy();
}
2021-09-30 16:46:20 -04:00
var gettingSchemeStoredSettings = browser.storage.local.get('proxy_scheme');
gettingSchemeStoredSettings.then(checkSchemeStoredSettings, onSetupError);
//
function checkHostStoredSettings(storedSettings) {
if (storedSettings.proxy_host == undefined)
2021-09-30 16:46:20 -04:00
storedSettings.proxy_host = '127.0.0.1';
else proxy_host = storedSettings.proxy_host;
2021-09-30 16:46:20 -04:00
console.log('Initialising Host', storedSettings.proxy_host);
setupProxy();
}
2021-09-30 16:46:20 -04:00
var gettingHostStoredSettings = browser.storage.local.get('proxy_host');
gettingHostStoredSettings.then(checkHostStoredSettings, onSetupError);
//
function checkPortStoredSettings(storedSettings) {
if (storedSettings.proxy_port == undefined)
2021-09-30 16:46:20 -04:00
storedSettings.proxy_port = '4444';
else proxy_port = storedSettings.proxy_port;
2021-09-30 16:46:20 -04:00
console.log('Initialising Port', storedSettings.proxy_port);
setupProxy();
}
2021-09-30 16:46:20 -04:00
var gettingPortStoredSettings = browser.storage.local.get('proxy_port');
gettingPortStoredSettings.then(checkPortStoredSettings, onSetupError);
//
function checkControlHostStoredSettings(storedSettings) {
if (storedSettings.control_host == undefined)
2021-09-30 16:46:20 -04:00
storedSettings.control_host = '127.0.0.1';
else control_host = storedSettings.control_host;
2021-09-30 16:46:20 -04:00
console.log('Initialising Control Host', storedSettings.control_host);
setupProxy();
}
2019-12-24 16:51:38 -05:00
var gettingControlHostStoredSettings = browser.storage.local.get(
2021-09-30 16:46:20 -04:00
'control_host'
2019-12-24 16:51:38 -05:00
);
gettingControlHostStoredSettings.then(
checkControlHostStoredSettings,
onSetupError
);
//
function checkControlPortStoredSettings(storedSettings) {
if (storedSettings.control_port == undefined)
2021-09-30 16:46:20 -04:00
storedSettings.control_port = '7657';
else control_port = storedSettings.control_port;
2021-09-30 16:46:20 -04:00
console.log('Initialising Control Port', storedSettings.control_port);
setupProxy();
}
2019-12-24 16:51:38 -05:00
var gettingControlPortStoredSettings = browser.storage.local.get(
2021-09-30 16:46:20 -04:00
'control_port'
2019-12-24 16:51:38 -05:00
);
gettingControlPortStoredSettings.then(
checkControlPortStoredSettings,
onSetupError
);
//
function checkHistoryStoredSettings(storedSettings) {
if (storedSettings.disable_history == undefined)
storedSettings.disable_history = false;
else disable_history = storedSettings.disable_history;
console.log(
2021-09-30 16:46:20 -04:00
'Initialising Disabled History',
storedSettings.disable_history
);
setupProxy();
}
2019-12-24 16:51:38 -05:00
var gettingHistoryStoredSettings = browser.storage.local.get(
2021-09-30 16:46:20 -04:00
'disable_history'
2019-12-24 16:51:38 -05:00
);
gettingHistoryStoredSettings.then(checkHistoryStoredSettings, onSetupError);
}
2019-06-17 19:17:11 -04:00
function getScheme() {
2021-09-30 16:46:20 -04:00
if (proxy_scheme == 'HTTP') return 'http';
if (proxy_scheme == 'SOCKS') return 'socks';
if (proxy_scheme == 'http') return 'http';
if (proxy_scheme == 'socks') return 'socks';
else return 'http';
2019-06-17 19:17:11 -04:00
}
function getHost() {
2019-10-06 15:14:53 -04:00
if (proxy_host == undefined) {
2021-09-30 16:46:20 -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();
2021-09-30 16:46:20 -04:00
if (scheme == 'socks') proxy_port = '4446';
else proxy_port = '4444';
2019-10-06 15:14:53 -04:00
}
return proxy_port;
2019-06-17 19:17:11 -04:00
}
2020-11-10 21:41:13 -05:00
function getConsolePort() {
if (control_port == undefined) {
var scheme = getScheme();
2021-09-30 16:46:20 -04:00
if (scheme == 'socks') proxy_port = '7657';
else control_port = '7657';
2020-11-10 21:41:13 -05:00
}
2020-12-11 16:30:22 -05:00
return control_port;
2020-11-10 21:41:13 -05:00
}
function setupProxy() {
2021-09-30 16:46:20 -04:00
console.log('Setting up Firefox WebExtension proxy');
2019-10-28 01:11:16 -04:00
browser.proxy.onRequest.addListener(handleContextProxyRequest, {
2021-09-30 16:46:20 -04:00
urls: ['<all_urls>']
2019-10-28 01:11:16 -04:00
});
2021-09-30 16:46:20 -04:00
console.log('i2p settings created for WebExtension Proxy');
}
function update() {
2021-09-30 16:46:20 -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() {
2021-09-30 16:46:20 -04:00
console.log('updating settings from storage');
chrome.storage.local.get(function() {
SetupSettings();
update();
setupProxy();
});
2019-05-02 17:31:04 -04:00
}
2020-01-13 15:20:32 -05:00
updateFromStorage();
browser.storage.onChanged.addListener(updateFromStorage);
SetupSettings();
2019-11-27 00:31:10 -05:00
setupProxy();
2020-01-13 15:20:32 -05:00
var gettingListenerInfo = browser.runtime.getPlatformInfo();
2020-11-10 21:48:08 -05:00
gettingListenerInfo.then((got) => {
2021-09-30 16:46:20 -04:00
if (browser.windows != undefined) {
browser.windows.onCreated.addListener(() => {
chrome.storage.local.get(function() {
setupProxy();
});
2020-01-13 15:20:32 -05:00
});
2021-09-30 16:46:20 -04:00
}
2020-01-13 15:20:32 -05:00
});