Files
I2P_in_Private_Browsing_Mod…/handler.js

113 lines
3.4 KiB
JavaScript
Raw Normal View History

function routerHost(url) {
2020-11-11 01:44:09 -05:00
console.log('(urlcheck) HANDLER URL CHECK');
2020-06-22 21:49:14 -04:00
let hostname = '';
let path = '';
function pathcheck(str) {
2020-11-11 01:44:09 -05:00
console.log('(urlcheck) HANDLER PATH CHECK', str);
if (str != undefined) {
2020-06-22 21:49:14 -04:00
let final = str.split('/')[0];
if (final === 'i2ptunnelmgr' || final === 'i2ptunnel') {
console.log('(urlcheck) Tunnel application path', final);
return 'i2ptunnelmgr';
} else if (
final === 'i2psnark' ||
final === 'torrents' ||
final.startsWith('transmission') ||
2020-11-11 01:44:09 -05:00
final.startsWith('tracker') ||
str.includes(':7662')
) {
2020-06-22 21:49:14 -04:00
console.log('(urlcheck) Torrent application path', final);
return 'i2psnark';
} else if (final === 'webmail' || final === 'susimail') {
console.log('(urlcheck) Mail application path', final);
return 'webmail';
} else if (final.startsWith('MuWire')) {
if (!url.includes('.png')) {
2020-11-10 21:48:08 -05:00
console.log('(urlcheck) MuWire application path', final);
return 'muwire';
2020-03-24 19:46:37 -04:00
}
} else if (
2020-06-22 21:49:14 -04:00
final === 'home' ||
final === 'console' ||
final === 'dns' ||
final === 'sitemap' ||
2020-06-22 21:49:14 -04:00
final.startsWith('config')
) {
2020-06-22 21:49:14 -04:00
console.log('(urlcheck) Console application path', final);
return 'routerconsole';
}
}
return true;
}
2020-06-22 21:49:14 -04:00
if (url.indexOf('://') > -1) {
hostname = url.split('/')[2];
let prefix = url.substr(0, url.indexOf('://') + 3);
path = url.replace(prefix + hostname + '/', '');
} else if (identifyProtocolHandler(url)) {
let newurl = identifyProtocolHandler(url);
return routerHost(newurl);
} else {
2020-06-22 21:49:14 -04:00
hostname = url.split('/')[0];
path = url.replace(hostname + '/', '');
}
2020-06-22 21:49:14 -04:00
if (hostname === control_host + ':' + control_port) {
2020-01-14 23:31:41 -05:00
//console.log("(hostcheck) router console found on configured ports");
return pathcheck(path);
}
2020-06-22 21:49:14 -04:00
if (hostname === 'localhost' + ':' + control_port) {
//console.log("(hostcheck) router console found on configured ports");
return pathcheck(path);
}
2020-06-22 21:49:14 -04:00
if (hostname === '127.0.0.1' + ':' + control_port) {
return pathcheck(path);
}
return false;
}
2019-11-23 18:41:18 -05:00
function identifyProtocolHandler(url) {
//console.log("looking for handler-able requests")
if (routerHost(url)) {
2020-06-22 21:49:14 -04:00
if (url.includes(encodeURIComponent('ext+rc:'))) {
return url.replace(encodeURIComponent('ext+rc:'), '');
} else if (url.includes('ext+rc:')) {
return url.replace('ext+rc:', '');
2019-11-23 18:41:18 -05:00
}
2020-06-22 21:49:14 -04:00
} else if (url.includes('ext+rc:')) {
2019-11-23 18:41:18 -05:00
return url;
}
return false;
}
2019-11-23 22:17:20 -05:00
function trimHost(url) {
2020-06-22 21:49:14 -04:00
let hostname = '';
let prefix = '';
if (url.indexOf('://') > -1) {
prefix = url.substr(0, url.indexOf('://') + 3);
hostname = url.split('/')[2];
2019-11-23 22:17:20 -05:00
} else {
2020-06-22 21:49:14 -04:00
hostname = url.split('/')[0];
2019-11-23 22:17:20 -05:00
}
2020-06-22 21:49:14 -04:00
let path = url.replace(prefix + hostname, '');
console.log('(handler) path', prefix + hostname, path);
2019-11-24 04:13:12 -05:00
return path;
2019-11-23 22:17:20 -05:00
}
var handlerSetup = function(requestDetails) {
2019-11-23 18:41:18 -05:00
//console.log("checking protocol handler listener")
let rwurl = identifyProtocolHandler(requestDetails.url);
2019-11-23 18:41:18 -05:00
if (rwurl != false) {
2020-06-22 21:49:14 -04:00
console.log('(handler) rewrite URL requested', rwurl);
2019-11-23 18:41:18 -05:00
requestDetails.redirectUrl = rwurl;
2019-11-23 22:17:20 -05:00
requestDetails.url = trimHost(rwurl);
requestDetails.originUrl = trimHost(rwurl);
2019-11-23 18:41:18 -05:00
}
return requestDetails;
};
browser.webRequest.onBeforeRequest.addListener(
handlerSetup,
2020-06-22 21:49:14 -04:00
{ urls: ['<all_urls>'] },
['blocking']
2019-11-23 18:41:18 -05:00
);