Save hostnameCache in localStorage

main
Elbert Alias 7 years ago
parent cd0b1b6e60
commit e46e4e7f1e

@ -30,9 +30,9 @@ function getOption(name, defaultValue = null) {
browser.storage.local.get(name) browser.storage.local.get(name)
.then(callback) .then(callback)
.catch(error => { .catch(error => {
wappalyzer.log(error, 'driver', 'error') wappalyzer.log(error, 'driver', 'error')
reject(); reject();
}); });
}); });
} }
@ -41,7 +41,7 @@ function getOption(name, defaultValue = null) {
* Set a value in localStorage * Set a value in localStorage
*/ */
function setOption(name, value) { function setOption(name, value) {
var option = {}; const option = {};
option[name] = value; option[name] = value;
@ -68,18 +68,12 @@ function post(url, body) {
method: 'POST', method: 'POST',
body: JSON.stringify(body) body: JSON.stringify(body)
}) })
.then(response => { .then(response => wappalyzer.log(`POST ${url}: ${response.status}`, 'driver'))
wappalyzer.log('POST ' + url + ': ' + response.status, 'driver'); .catch(error => wappalyzer.log(`POST ${url}: ${error}`, 'driver', 'error'));
})
.catch(error => {
wappalyzer.log('POST ' + url + ': ' + error, 'driver', 'error');
});
} }
fetch('../apps.json') fetch('../apps.json')
.then(response => { .then(response => response.json())
return response.json();
})
.then(json => { .then(json => {
wappalyzer.apps = json.apps; wappalyzer.apps = json.apps;
wappalyzer.categories = json.categories; wappalyzer.categories = json.categories;
@ -90,29 +84,25 @@ fetch('../apps.json')
.map(categoryId => parseInt(categoryId, 10)) .map(categoryId => parseInt(categoryId, 10))
.sort((a, b) => wappalyzer.categories[a].priority - wappalyzer.categories[b].priority); .sort((a, b) => wappalyzer.categories[a].priority - wappalyzer.categories[b].priority);
}) })
.catch(error => { .catch(error => wappalyzer.log(`GET apps.json: ${error}`, 'driver', 'error'));
wappalyzer.log('GET apps.json: ' + error, 'driver', 'error');
});
// Version check // Version check
var version = browser.runtime.getManifest().version; let version = browser.runtime.getManifest().version;
getOption('version') getOption('version')
.then(previousVersion => { .then(previousVersion => {
if ( previousVersion === null ) { if (previousVersion === null) {
openTab({ openTab({
url: wappalyzer.config.websiteURL + 'installed' url: wappalyzer.config.websiteURL + 'installed'
}); });
} else if ( version !== previousVersion ) { } else if (version !== previousVersion) {
getOption('upgradeMessage', true) getOption('upgradeMessage', true)
.then(upgradeMessage => { .then(upgradeMessage => {
if ( upgradeMessage ) { if (upgradeMessage) {
/*
openTab({ openTab({
url: wappalyzer.config.websiteURL + 'upgraded?v' + version, url: wappalyzer.config.websiteURL + 'upgraded?v' + version,
background: true background: true
}); });
*/
} }
}); });
} }
@ -123,6 +113,8 @@ getOption('version')
getOption('dynamicIcon', true); getOption('dynamicIcon', true);
getOption('pinnedCategory'); getOption('pinnedCategory');
getOption('hostnameCache', {}).then(hostnameCache => wappalyzer.hostnameCache = hostnameCache);
// Run content script on all tabs // Run content script on all tabs
browser.tabs.query({ url: [ 'http://*/*', 'https://*/*' ] }) browser.tabs.query({ url: [ 'http://*/*', 'https://*/*' ] })
.then(tabs => { .then(tabs => {
@ -138,23 +130,23 @@ browser.tabs.query({ url: [ 'http://*/*', 'https://*/*' ] })
browser.webRequest.onCompleted.addListener(request => { browser.webRequest.onCompleted.addListener(request => {
const headers = {}; const headers = {};
if ( request.responseHeaders ) { if (request.responseHeaders) {
const url = wappalyzer.parseUrl(request.url); const url = wappalyzer.parseUrl(request.url);
browser.tabs.query({ url: [ url.href ] }) browser.tabs.query({ url: [ url.href ] })
.then(tabs => { .then(tabs => {
const tab = tabs[0] || null; const tab = tabs[0] || null;
if ( tab ) { if (tab) {
request.responseHeaders.forEach(header => { request.responseHeaders.forEach(header => {
const name = header.name.toLowerCase(); const name = header.name.toLowerCase();
headers[name] = headers[name] || []; headers[name] = headers[name] || [];
headers[name].push(( header.value || header.binaryValue || '' ).toString()); headers[name].push((header.value || header.binaryValue || '').toString());
}); });
if ( headers['content-type'] && /\/x?html/.test(headers['content-type'][0]) ) { if (headers['content-type'] && /\/x?html/.test(headers['content-type'][0])) {
wappalyzer.analyze(url, { headers }, { tab }); wappalyzer.analyze(url, { headers }, { tab });
} }
} }
@ -164,14 +156,14 @@ browser.webRequest.onCompleted.addListener(request => {
}, { urls: [ 'http://*/*', 'https://*/*' ], types: [ 'main_frame' ] }, [ 'responseHeaders' ]); }, { urls: [ 'http://*/*', 'https://*/*' ], types: [ 'main_frame' ] }, [ 'responseHeaders' ]);
// Listen for messages // Listen for messages
( chrome || browser ).runtime.onMessage.addListener((message, sender, sendResponse) => { (chrome || browser).runtime.onMessage.addListener((message, sender, sendResponse) => {
if ( typeof message.id != 'undefined' ) { if (typeof message.id != 'undefined') {
if ( message.id !== 'log' ) { if (message.id !== 'log') {
wappalyzer.log('Message' + ( message.source ? ' from ' + message.source : '' ) + ': ' + message.id, 'driver'); wappalyzer.log('Message' + (message.source ? ' from ' + message.source : '') + ': ' + message.id, 'driver');
} }
var url = wappalyzer.parseUrl(sender.tab ? sender.tab.url : ''); let url = wappalyzer.parseUrl(sender.tab ? sender.tab.url : '');
var response; let response;
switch ( message.id ) { switch ( message.id ) {
case 'log': case 'log':
@ -186,6 +178,8 @@ browser.webRequest.onCompleted.addListener(request => {
case 'analyze': case 'analyze':
wappalyzer.analyze(url, message.subject, { tab: sender.tab }); wappalyzer.analyze(url, message.subject, { tab: sender.tab });
setOption('hostnameCache', wappalyzer.hostnameCache);
break; break;
case 'ad_log': case 'ad_log':
wappalyzer.cacheDetectedAds(message.subject); wappalyzer.cacheDetectedAds(message.subject);
@ -225,16 +219,16 @@ wappalyzer.driver.document = document;
* Log messages to console * Log messages to console
*/ */
wappalyzer.driver.log = (message, source, type) => { wappalyzer.driver.log = (message, source, type) => {
console.log('[wappalyzer ' + type + ']', '[' + source + ']', message); console.log(`[wappalyzer ${type}]`, `[${source}]`, message);
}; };
/** /**
* Display apps * Display apps
*/ */
wappalyzer.driver.displayApps = (detected, meta, context) => { wappalyzer.driver.displayApps = (detected, meta, context) => {
var tab = context.tab; let tab = context.tab;
if ( tab === undefined ) { if (tab === undefined) {
return; return;
} }
@ -252,14 +246,14 @@ wappalyzer.driver.displayApps = (detected, meta, context) => {
let app = detected[appName]; let app = detected[appName];
app.props.cats.forEach(category => { app.props.cats.forEach(category => {
if ( category === match && !found ) { if (category === match && !found) {
let icon = app.props.icon || 'default.svg'; let icon = app.props.icon || 'default.svg';
if ( !options.dynamicIcon ) { if (!options.dynamicIcon) {
icon = 'default.svg'; icon = 'default.svg';
} }
if ( /\.svg$/i.test(icon) ) { if (/\.svg$/i.test(icon)) {
icon = 'converted/' + icon.replace(/\.svg$/, '.png'); icon = 'converted/' + icon.replace(/\.svg$/, '.png');
} }
@ -278,7 +272,7 @@ wappalyzer.driver.displayApps = (detected, meta, context) => {
}); });
}); });
if ( typeof chrome !== 'undefined' ) { if (typeof chrome !== 'undefined') {
// Browser polyfill doesn't seem to work here // Browser polyfill doesn't seem to work here
chrome.pageAction.show(tab.id); chrome.pageAction.show(tab.id);
} else { } else {
@ -290,14 +284,14 @@ wappalyzer.driver.displayApps = (detected, meta, context) => {
* Fetch and cache robots.txt for host * Fetch and cache robots.txt for host
*/ */
wappalyzer.driver.getRobotsTxt = (host, secure = false) => { wappalyzer.driver.getRobotsTxt = (host, secure = false) => {
if ( robotsTxtQueue.hasOwnProperty(host) ) { if (robotsTxtQueue.hasOwnProperty(host)) {
return robotsTxtQueue[host]; return robotsTxtQueue[host];
} }
robotsTxtQueue[host] = new Promise(resolve => { robotsTxtQueue[host] = new Promise(resolve => {
getOption('tracking', true) getOption('tracking', true)
.then(tracking => { .then(tracking => {
if ( !tracking ) { if (!tracking) {
return resolve([]); return resolve([]);
} }
@ -311,7 +305,7 @@ wappalyzer.driver.getRobotsTxt = (host, secure = false) => {
const timeout = setTimeout(() => resolve([]), 3000); const timeout = setTimeout(() => resolve([]), 3000);
fetch('http' + ( secure ? 's' : '' ) + '://' + host + '/robots.txt', { redirect: 'follow' }) fetch('http' + (secure ? 's' : '') + '://' + host + '/robots.txt', { redirect: 'follow' })
.then(response => { .then(response => {
clearTimeout(timeout); clearTimeout(timeout);
@ -339,12 +333,12 @@ wappalyzer.driver.getRobotsTxt = (host, secure = false) => {
wappalyzer.driver.ping = (hostnameCache = {}, adCache = []) => { wappalyzer.driver.ping = (hostnameCache = {}, adCache = []) => {
getOption('tracking', true) getOption('tracking', true)
.then(tracking => { .then(tracking => {
if ( tracking ) { if (tracking) {
if ( Object.keys(hostnameCache).length ) { if (Object.keys(hostnameCache).length) {
post('https://api.wappalyzer.com/ping/v1/', hostnameCache); post('https://api.wappalyzer.com/ping/v1/', hostnameCache);
} }
if ( adCache.length ) { if (adCache.length) {
post('https://ad.wappalyzer.com/log/wp/', adCache); post('https://ad.wappalyzer.com/log/wp/', adCache);
} }