-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
70 lines (66 loc) · 2.36 KB
/
Copy pathbackground.js
File metadata and controls
70 lines (66 loc) · 2.36 KB
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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'analyzeHeaders' && message.url) {
fetchHeaders(message.url).then(sendResponse).catch(() => sendResponse({ ok: false }));
return true;
}
if (message.type === 'analyzeDNS' && message.url) {
fetchDNS(message.url).then(sendResponse).catch(() => sendResponse({ ok: false }));
return true;
}
if (message.type === 'analyzeSSL' && message.url) {
fetchSSL(message.url).then(sendResponse).catch(() => sendResponse({ ok: false }));
return true;
}
if (message.type === 'analyzeWHOIS' && message.url) {
fetchWHOIS(message.url).then(sendResponse).catch(() => sendResponse({ ok: false }));
return true;
}
});
async function fetchHeaders(url) {
const response = await fetch(url, { method: 'HEAD', mode: 'cors' });
const headersToCheck = [
'content-security-policy',
'x-frame-options',
'strict-transport-security',
'x-content-type-options',
'referrer-policy',
'permissions-policy',
];
const headers = headersToCheck.map(name => ({
name,
value: response.headers.get(name) || '❌ Not Present',
}));
return { ok: true, headers };
}
async function fetchDNS(url) {
const hostname = new URL(url).hostname;
const response = await fetch(
`https://dns.google/resolve?name=${encodeURIComponent(hostname)}&type=A`
);
const data = await response.json();
return { ok: true, data };
}
async function fetchSSL(url) {
const hostname = new URL(url).hostname;
const response = await fetch(
`https://crt.sh/?q=${encodeURIComponent(hostname)}&output=json`
);
const raw = await response.json();
const certs = Array.isArray(raw) ? raw.slice(0, 200) : [];
const now = Date.now();
const valid = certs.filter(c => new Date(c.not_after).getTime() > now);
const pool = valid.length > 0 ? valid : certs;
const sorted = pool.sort((a, b) => new Date(b.not_after) - new Date(a.not_after));
return { ok: true, cert: sorted[0] || null };
}
async function fetchWHOIS(url) {
const hostname = new URL(url).hostname;
// Strip subdomains to get the registrable domain (works for most TLDs)
const parts = hostname.split('.');
const domain = parts.slice(-2).join('.');
const response = await fetch(
`https://rdap.org/domain/${encodeURIComponent(domain)}`
);
const data = await response.json();
return { ok: true, data };
}