-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.js
151 lines (130 loc) · 5.8 KB
/
main.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
const gatherIceCandidates = () => {
return new Promise(async resolve => {
let candidates = [];
const rtc = new RTCPeerConnection();
rtc.createDataChannel("");
const offer = await rtc.createOffer();
await rtc.setLocalDescription(offer);
const id = setTimeout(() => {
rtc.onicecandidate = null;
resolve(candidates);
}, 12000);
rtc.onicecandidate = (event) => {
if (!event.candidate) {
resolve(candidates);
return clearTimeout(id);
}
candidates.push({
foundation: event.candidate.foundation,
protocol: event.candidate.protocol,
address: event.candidate.address,
});
};
});
}
window.addEventListener('load', () => {
const loading = document.querySelector("#loading");
const loadingStatus = document.querySelector("#loadingStatus");
const setLoadingStatus = (statusText = '', complete = false, error = false) => {
if (!complete) {
loading.classList.remove('hidden');
} else {
loading.classList.add('hidden');
}
loadingStatus.innerText = statusText;
}
const candidatesTable = document.querySelector("#candidatesTable");
let tableData = {candidates: []};
const updateTable = () => {
const tBody = candidatesTable.querySelector("tbody");
tBody.innerHTML = '';
if (tableData.candidates.length > 0) {
candidatesTable.classList.remove('hidden');
}
for (let candidate of tableData.candidates) {
const sepRow = document.createElement("tr");
sepRow.innerHTML = `<td colspan="3" style="height: 2px; background: #e2e8f0; padding: 0;"></td>`;
tBody.appendChild(sepRow);
const row = document.createElement("tr");
row.innerHTML = `
<td>${candidate.foundation}</td>
<td class="whitespace-nowrap overflow-hidden text-ellipsis" title="${candidate.address}">
${candidate.address}
</td>
<td>${candidate.protocol.toUpperCase()}</td>
`;
tBody.appendChild(row);
const detailRow = document.createElement("tr");
if (candidate.result === "unknown") {
detailRow.innerHTML = `
<td colspan="3">
❌ Lookup failed. Perhaps this is not a local IP?
</td>`;
} else if (!candidate.result) {
detailRow.innerHTML = `
<td colspan="3">
⌛ Hang on...
</td>`;
} else {
detailRow.innerHTML = `
<td colspan="3">
🧬 Address <span class="font-medium">${candidate.result.address}</span>,
protocol <span class="font-medium">${candidate.result.protocol.toUpperCase()}</span>,
type <span class="font-medium">${candidate.result.type}</span>
${candidate.result.address.startsWith("172") ? `
<br />
<div class="mt-[.5rem] text-xs font-medium bg-red-50 rounded py-[.25rem] px-[.5rem] w-max">
🚨 This may be a local subnet for WSL, but no guarantee
</div>
`: ''}
</td>`;
}
tBody.appendChild(detailRow);
}
}
const lookupStats = document.querySelector("#lookupStats");
const actionButton = document.querySelector("#actionButton");
actionButton.addEventListener('click', async () => {
try {
actionButton.style.display = 'none';
setLoadingStatus("Gather ICE candidates...");
tableData.candidates = await gatherIceCandidates();
updateTable();
setLoadingStatus("Lookup foundation keys...");
const worker = new Worker('worker.js');
await new Promise(resolve => setTimeout(resolve, 2000));
worker.addEventListener('message', ({data}) => {
if (data.method !== "lookupResult") {
return;
}
for (let i = 0; i < tableData.candidates.length; i++) {
if (tableData.candidates[i].foundation === data.foundation) {
tableData.candidates[i].result = data.result;
}
}
})
for (let i = 0; i < tableData.candidates.length; i++) {
worker.postMessage({method: "lookup", foundation: tableData.candidates[i].foundation});
}
await new Promise(resolve => setTimeout(resolve, 2500));
setTimeout(() => worker.postMessage({method: "loadLookup"}), 500);
await new Promise((resolve, reject) => {
const handleMessage = ({data}) => {
console.log('main recv', data);
if (data.method === "loadLookupComplete") {
lookupStats.classList.remove('hidden');
lookupStats.innerHTML = `✅ Used ${new Intl.NumberFormat().format(data.count)} keys for lookups`;
resolve();
}
worker.removeEventListener('message', handleMessage);
}
worker.addEventListener('message', handleMessage);
});
await new Promise(resolve => setTimeout(resolve, 500));
updateTable();
setLoadingStatus(undefined, true, undefined);
} catch (err) {
setLoadingStatus(err.toString());
}
})
});