-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbackground.js
84 lines (73 loc) · 2.5 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// (c) Lauritz Holtmann
//
const WEEK_IN_MS = 7 * 24 * 60 * 60 * 1000;
chrome.webNavigation.onCompleted.addListener((details) => {
if (details?.tab?.url?.startsWith("chrome://")) return undefined;
chrome.scripting.executeScript({
target: { tabId: details.tabId },
files: ['content.js']
});
}, { url: [{ urlMatches: 'https?://*/*' }] });
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'checkDomains') {
checkAndStoreDomains(message.domains, message.pageUrl);
}
});
function checkAndStoreDomains(pageDomains, pageUrl) {
chrome.storage.local.get(['domains'], ({ domains }) => {
chrome.storage.sync.get(
{ cacheDuration: WEEK_IN_MS },
({ cacheDuration }) => {
let storedDomains = domains || {};
const now = Date.now();
// Clean up old entries
for (let domain in storedDomains) {
if (storedDomains[domain].timestamp < now - cacheDuration) {
delete storedDomains[domain];
}
}
pageDomains.forEach(domain => {
if (!storedDomains.hasOwnProperty(domain.domain)) {
if (!isIpAddress(domain.domain)) {
resolveDomain(domain.domain, (resolvable) => {
storedDomains[domain.domain] = {
timestamp: now,
pageUrl: domain.pageUrl,
sinkElement: domain.sinkElement,
dead: !resolvable
};
chrome.storage.local.set({ domains: storedDomains });
if (!resolvable) {
createNotification(`Domain not resolvable: ${domain.domain}\n\nFound on: ${domain.pageUrl}\n\nElement: ${domain.sinkElement}`);
}
});
}
}
});
});
});
}
function isIpAddress(domain) {
return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(domain);
}
function resolveDomain(domain, callback) {
if (domain.trim() != '') {
fetch(`https://dns.google/resolve?name=${domain}`)
.then(response => response.json())
.then(data => {
callback(data.Answer && data.Answer.length > 0);
})
.catch(() => {
callback(false);
});
}
}
function createNotification(message) {
chrome.notifications.create({
type: 'basic',
title: '🚨 Dead Domain Discovery 🚨',
iconUrl: 'icons/icon48.png',
message: message
});
}