-
Notifications
You must be signed in to change notification settings - Fork 0
/
modifyHost.js
43 lines (34 loc) · 878 Bytes
/
modifyHost.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
const { execSync } = require('child_process');
const hostify = require('hostify').operations;
const PORT = 3001;
const host = 'example.com';
const hostFilter = {
filterHostFn: val => val.includes(host),
};
function getHosts() {
return hostify.list(hostFilter);
}
try {
execSync(`echo "
rdr pass inet proto tcp from any to any port 80 -> 127.0.0.1 port ${PORT}
rdr pass inet proto tcp from any to any port 443 -> 127.0.0.1 port ${PORT}
" | sudo pfctl -ef -`);
} catch (e) {
// supress err
}
// add hostfile entry if non exist
if (getHosts().length <= 0) {
hostify.add({
entries: [
{ ip: '127.0.0.1', host, comment: 'TEMP DEV' },
],
});
}
// cleanup hostfile on exit
process.on('SIGINT', () => {
hostify.delete(hostFilter);
// flush rules
execSync('sudo pfctl -F all -f /etc/pf.conf');
process.exit(2);
});
process.stdin.resume();