-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.mjs
68 lines (55 loc) · 2.17 KB
/
module.mjs
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
import process from "node:process";
parseInt(process.version.match(/(?:v?)([\d]+)(?:\.)/)[1]) < 21 && process.removeAllListeners("warning");
import services from "./services.mjs";
const IPv4_regex =
/^(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]?)$/;
const gip = async ({ services: customServices = [], ensure = 3 } = {}) => {
for (const [index, s] of customServices.entries()) {
if (!/^https?:\/\//.test(s)) {
customServices[index] = `https://${s.replace(/^\W+/g, "")}`;
}
}
const allServices = [...new Set([...services, ...customServices])];
if (ensure > allServices.length) throw new Error(`Maximum ensure count is ${allServices.length}`);
// Ensure count must be at least 1
ensure = Math.max(1, ensure);
const controller = new AbortController(); // Create an AbortController instance
const signal = controller.signal; // Get the signal to pass to fetch
// Map to store IP addresses and their counts
const ipCounts = new Map();
const promises = allServices.map((url) =>
fetch(url, { signal })
.then((res) => res.text())
.then((ip) => {
const trimmedIP = ip.trim();
if (IPv4_regex.test(trimmedIP)) {
// Increment count for this IP
const currentCount = (ipCounts.get(trimmedIP) || 0) + 1;
ipCounts.set(trimmedIP, currentCount);
// Check if this IP has reached the ensure count
if (currentCount === ensure) {
controller.abort(); // Stop further requests
return trimmedIP;
}
}
return null;
})
.catch((err) => {
if (err.name !== "AbortError") {
console.error("Fetch error:", err);
}
return null;
})
);
// Wait for all promises to settle
await Promise.allSettled(promises);
// Find the IP that matches the ensure count
for (const [ip, count] of ipCounts.entries()) {
if (count === ensure) {
return ip;
}
}
// If no IP meets the ensure count
throw new Error(`Not enough IP addresses found to meet the ensure count of ${ensure}`);
};
export default gip;