-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
66 lines (56 loc) · 1.77 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
async function fetchData(ip) {
try {
const response = await fetch(`http://${ip}/api/system/info`, { timeout: 1000 });
if (response.ok) {
const data = await response.json();
const result = { ip };
if (data.hashRate_1h !== undefined) {
result.hashRate = data.hashRate_1h;
} else if (data.hashRate !== undefined) {
result.hashRate = data.hashRate;
}
if (data.temp !== undefined) {
result.temp = data.temp;
}
if (data.power !== undefined) {
result.power = data.power;
}
if (data.bestDiff !== undefined) {
result.bestDiff = data.bestDiff;
}
if (Object.keys(result).length > 1) { // More than just IP
chrome.runtime.sendMessage({
type: 'partialResult',
data: result
});
return result;
}
}
} catch (error) {
// Error handling is silent
}
return null;
}
async function scanNetwork(baseAddress) {
const results = [];
const promises = [];
for (let i = 1; i <= 255; i++) {
const ip = `${baseAddress}${i}`;
promises.push(fetchData(ip));
}
const responses = await Promise.all(promises);
responses.forEach((result) => {
if (result) {
results.push(result);
}
});
return results;
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "scanNetwork") {
scanNetwork(request.baseAddress).then((results) => {
sendResponse({ results });
});
return true; // Will respond asynchronously
}
});