-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.mjs
115 lines (94 loc) · 2.68 KB
/
index.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
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
import {
ACTION,
COLORS,
RESOLVE_EVENT,
RESOLVE_STATUS,
} from "./lib/constant.mjs";
import { DnsServer } from "./lib/server.mjs";
import { IP, IPQueue } from "./lib/ip.mjs";
import { Ping, PingQueue } from "./lib/ping.mjs";
import { Painter } from "./lib/painter.mjs";
import { interval, sleep } from "./lib/utils.mjs";
import { stdout, watchKeypress } from "./lib/stdout.mjs";
import { HostSetting } from "./lib/host.mjs";
export { COLORS };
export { stdout };
export async function resolve(options) {
const { host } = options;
const hostSetting = new HostSetting(host);
const startTime = new Date();
const ipQueue = new IPQueue();
const pingQueue = new PingQueue();
let resolveStatus = RESOLVE_STATUS.PENDING;
const server = new DnsServer(options);
const painter = new Painter(host);
await hostSetting.getContent();
server.resolve(host);
painter.print(ipQueue, resolveStatus, hostSetting);
interval(() => painter.print(ipQueue, resolveStatus, hostSetting), 100);
let isFirstIp = false;
server.on(RESOLVE_EVENT.FULFILLED, (data) => {
data.ips.forEach((addr) => {
let ip = ipQueue.get(addr);
if (ip) {
return;
}
ip = new IP({
server: data.server,
addr,
resolveTime: new Date() - startTime,
selected: !isFirstIp,
});
isFirstIp = true;
ipQueue.set(addr, ip);
const ping = new Ping(addr);
ping.onResponse((res) => {
ip.received ||= 0;
ip.received += +res.received;
ip.time = res.time;
});
pingQueue.set(addr, ping);
});
});
server.on(RESOLVE_EVENT.FINISHED, async (data) => {
resolveStatus = RESOLVE_STATUS.SUCCESS;
if (!ipQueue?.size) {
resolveStatus = RESOLVE_STATUS.FAIL;
await sleep(100);
stdout.error(
`can not resolve ${host}, please make sure host exists and is reachable\n`
);
process.exit(1);
}
});
function onExit(code) {
stdout.showCursor();
pingQueue.exit();
process.exit(typeof code === "number" ? code : 0);
}
process.on("exit", onExit);
process.on("SIGINT", onExit);
process.on("uncaughtException", (err) => {
console.error(
painter.color(
err + (err.code === "EACCES" ? "\nAre you running as root?" : ""),
COLORS.red
)
);
onExit(1);
});
watchKeypress((str, key) => {
if (key.ctrl && ["c", "d"].includes(key.name)) {
onExit(0);
}
const move = {
left: ACTION.prev,
right: ACTION.next,
}[key.name];
move && ipQueue.selectIP(move);
const selectedIP = ipQueue.getSelectedIP();
if (key.name === "return" && selectedIP) {
hostSetting.setHostIP(selectedIP);
}
});
}