-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocker.js
96 lines (75 loc) · 3.36 KB
/
blocker.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
const fs = require('fs');
const filePath = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
/*
WINDOWS:
- File path: C:\Windows\System32\drivers\etc\hosts
- Backslashes need to be escaped
- const filePath = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
*/
/*
MAC/LINUX:
- File path: /etc/hosts
- const filePath = '/etc/hosts';
*/
const redirectPath = '127.0.0.1';
let websites = ['www.reddit.com', 'www.facebook.com', 'www.linkedin.com', 'www.twitch.tv', 'www.instagram.com', 'www.netflix.com'];
let originalContent;
//DELAY-TIME PROMPT
let delay;
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("For how many hours: ", function(hours) {
delay = hours * 60 * 60 * 1000;
rl.close();
let endTime = new Date(Date.now() + delay);
console.log('Blocked until: ' + "\x1b[35m" + endTime.toLocaleTimeString([], { hour12: false }) + "\x1b[0m" + '\n');
block();
setTimeout(unblock, delay);
});
//FUNCTIONS
function block() {
console.log('Blocking websites...');
console.log('==================================');
fs.readFile(filePath, (err, data) => {
if (err) return console.log(err);
let fileContents = data.toString();
originalContent = fileContents;
for(let i = 0; i < websites.length; i++) {
let blockWebsite = redirectPath + "\t" + websites[i] + "\r\n";
if (fileContents.indexOf(blockWebsite) < 0) {
console.log(blockWebsite + ' is NOT included');
console.log('----------------------------------');
fs.appendFile(filePath, blockWebsite, (err) => {
if (err) return console.log('Error: ', err);
console.log('File Updated Successfully');
});
} else {
console.log(blockWebsite + ' is included');
console.log('----------------------------------');
}
}
});
};
function unblock() {
console.log('==================================');
console.log('Unblocking websites...');
fs.writeFile(filePath, originalContent, (err) => {
if (err) return console.log('Error!', err);
});
}
/* TROUBLESHOOTING RESOURCES for PROBLEMS:
Script doesn't work on Firefox and some other modern browsers, because of DoH (DNS over HTTPS):
https://stackoverflow.com/questions/37452361/why-is-my-hosts-file-entry-being-ignored-by-the-browser
General:
https://newbedev.com/hosts-file-ignored-how-to-troubleshoot
Flushing the OS DNS cache didn't seem to work for all sites.
https://en.wikiversity.org/wiki/Computer_Networks/Ipconfig/DNS_Cache_Options#Activity_4_-_Purge_the_DNS_Resolver_Cache
Firefox holds its own DNS cache in memory, so you have to wait for it to clear after closing the tabs (about 3min).
- Disabling it in the 'about:config' by setting 'network.dns.offline-local' to 'false' and restarting Firefox, for immmediate and long-term effect didn't seem to work:
- last comment here: https://stackoverflow.com/questions/37452361/why-is-my-hosts-file-entry-being-ignored-by-the-browser
- https://superuser.com/questions/1433325/does-firefox-ignore-the-hosts-file-how-to-make-firefox-honor-the-hosts-file
- clearing the Firefox cache at 'about:networking#dns' didn't seem to work: https://support.mozilla.org/en-US/questions/1258756
*/