-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshecan.js
67 lines (58 loc) · 1.67 KB
/
shecan.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
#!/usr/bin/env node
import os from "os";
import fs from "fs";
import ora from "ora";
const nameservers = ["nameserver 178.22.122.100", "nameserver 185.51.200.2"];
let spinner = ora({
text: "Detecting OS...",
spinner: "dots",
}).start();
const platform = os.platform();
let resolvConfPath;
switch (platform) {
case "linux":
resolvConfPath = "/etc/resolv.conf";
break;
case "darwin":
spinner.info("You're on macOS. Please change your DNS settings manually.");
spinner.stop();
process.exit(0);
break;
case "win32":
spinner.info(
"You're on Windows. Please change your DNS settings manually."
);
spinner.stop();
process.exit(0);
break;
default:
spinner.info(
"Unsupported platform. Please change your DNS settings manually."
);
spinner.stop();
process.exit(0);
break;
}
spinner.text = "Adding shecan to " + resolvConfPath + "...";
try {
let resolvConf = fs.readFileSync(resolvConfPath, "utf8");
spinner.text = "Successfully read existing " + resolvConfPath + ".";
let needToUpdateResolvConf = false;
for (const ns of nameservers) {
if (!resolvConf.includes(ns)) {
needToUpdateResolvConf = true;
break;
}
}
if (needToUpdateResolvConf) {
spinner.text = "Updating " + resolvConfPath + "...";
resolvConf = nameservers.join("\n") + "\n" + resolvConf;
fs.writeFileSync(resolvConfPath, resolvConf);
spinner.succeed("Successfully added shecan to " + resolvConfPath + ".");
} else {
spinner.info(resolvConfPath + " is already up to date. No changes made.");
}
} catch (error) {
spinner.fail("Failed to update " + resolvConfPath + ": " + error.message);
}
spinner.stop();