-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
161 lines (144 loc) · 5.44 KB
/
app.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { isInSubnet } from "https://cdn.jsdelivr.net/npm/[email protected]/+esm";
import { getIpData } from './data-store.js';
import { copyText } from './copy-text.js';
function createCopyButton(text) {
const icon = document.createElement('i');
icon.classList.add('fa-regular', 'fa-copy');
const button = document.createElement('button');
button.classList.add('copy-button');
button.title = "Copy"
button.appendChild(icon);
button.onclick = () => copyText(text, button, icon);
return button;
}
function createCell(text, copyable) {
const cell = document.createElement("td");
const wrapper = document.createElement('div');
const span = document.createElement('span');
const content = Array.isArray(text) ? text.join(", ") : text;
span.appendChild(document.createTextNode(content));
wrapper.appendChild(span);
if (copyable) {
wrapper.appendChild(createCopyButton(content));
wrapper.classList.add('spread-items');
}
cell.appendChild(wrapper);
return cell;
}
function createRow(match) {
const row = document.createElement("tr");
const address = createCell(match.ipAddress, true);
address.scope = "row";
row.appendChild(address);
row.appendChild(createCell(match.ip_prefix ?? match.ipv6_prefix, true));
row.appendChild(createCell(match.region));
row.appendChild(createCell(match.service));
row.appendChild(createCell(match.network_border_group));
return row;
}
async function lookupDnsForHostname(name, type) {
const typeNumber = type === 'A' ? 1 : 28;
const response = await fetch('https://cloudflare-dns.com/dns-query?' + new URLSearchParams({ type, name }), {
method: 'GET',
headers: {
'Accept': 'application/dns-json',
},
signal: AbortSignal.timeout(3000),
});
const json = await response.json();
return json.Answer?.filter((answer) => answer.type === typeNumber).map(({ data }) => data);
}
function getMatchesHelper(key, objField, ipAddress) {
const matches = ipData[key]
.filter(({ [objField]: prefix }) => isInSubnet(ipAddress, prefix))
.map((match) => ({ ...match, ipAddress }));
if (!matches.length) {
return [];
}
if (matches.length == 1) {
return matches;
}
// Remove duplicate entries listed as just being 'AMAZON'
return matches.filter(({ service }) => service !== 'AMAZON');
}
function getMatchesv4(ipAddress) {
return getMatchesHelper('prefixes', 'ip_prefix', ipAddress);
}
function getMatchesv6(ipAddress) {
return getMatchesHelper('ipv6_prefixes', 'ipv6_prefix', ipAddress);
}
async function handleLookup() {
const input = document.getElementById("lookup");
const text = input.value;
if (text && new URL(window.location).searchParams?.get("lookup") !== text) {
const newUrl = new URL(window.location);
newUrl.search = new URLSearchParams({ lookup: text }).toString()
window.history.pushState({ path: newUrl.toString() }, '', newUrl.toString());
}
const ipv4ishRegex = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
const ipv6ishRegex = /^[a-f\d:]+$/i;
const matches = [];
if (ipv4ishRegex.test(text)) {
matches.push(...getMatchesv4(text));
} else if (ipv6ishRegex.test(text)) {
matches.push(...getMatchesv6(text));
} else {
const v4Promise = lookupDnsForHostname(text, 'A');
const v6Promise = lookupDnsForHostname(text, 'AAAA');
const [v4s, v6s] = await Promise.all([v4Promise, v6Promise]);
matches.push(...(v4s?.flatMap((address) => getMatchesv4(address)) ?? []));
matches.push(...(v6s?.flatMap((address) => getMatchesv6(address)) ?? []));
}
return matches;
}
function handleSubmit() {
const table = document.getElementById("table");
const errorContainer = document.getElementById("error-container");
const notFound = document.getElementById("not-found");
const loading = document.getElementById("loading");
const heading = document.getElementById("table-heading");
const input = document.getElementById("lookup").value;
loading.style.display = "block";
table.style.display = "none";
table.style.visibility = "visible";
errorContainer.style.display = "none";
notFound.style.display = "none";
notFound.replaceChildren([]);
handleLookup()
.then((matches) => {
if (matches?.length) {
const rows = matches.map((match) => createRow(match));
const tableBody = document.getElementById("table-body");
heading.innerText = input;
tableBody.replaceChildren(...rows);
loading.style.display = "none";
table.style.display = "table";
} else {
notFound.appendChild(document.createTextNode('It looks like '));
const codeNode = document.createElement("code");
codeNode.innerText = input;
notFound.appendChild(codeNode);
notFound.appendChild(document.createTextNode(' may not be within AWS-owned IP space.'));
loading.style.display = "none";
notFound.style.display = "block";
}
})
.catch((error) => {
errorContainer.innerText = "Unable to load data\n" + error + "\n" + error.stack;
loading.style.display = "none";
errorContainer.style.display = "block";
});
}
const ipData = await getIpData();
document.getElementById("form").onsubmit = () => handleSubmit();
function loadFromUrl() {
const urlInput = new URL(window.location).searchParams?.get("lookup");
if (urlInput) {
document.getElementById("lookup").value = urlInput;
handleSubmit();
} else {
document.getElementById("lookup").focus();
}
}
loadFromUrl();
window.addEventListener('popstate', () => loadFromUrl());