-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdr
executable file
·88 lines (83 loc) · 2.67 KB
/
rdr
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
#!/usr/bin/env node
'use strict'
var fs = require('fs')
var readline = require('readline')
var bash = require('child_process').execSync
require('./scripts/map-args')()
var CMN = require('./scripts/common')
var on = require('./scripts/on')
var off = require('./scripts/off')
var configure = require('./scripts/configure')
var reset = require('./scripts/configure/reset')
/**
* Reports everything that's being forwarded.
* @function
* @return {}
*/
function list() {
var anchors = CMN.safeJSON(fs.readFileSync(CMN.FILE.ANCHORS_DB, 'utf8'), [])
var hosts = CMN.safeJSON(fs.readFileSync(CMN.FILE.HOSTS_DB, 'utf8'), [])
if (!hosts.length && !anchors.length) {
CMN.stdout('No redirect rules have been setup.')
return
}
if (!hosts.length) {
CMN.stdout('No domains redirected.')
} else {
CMN.stdout('Domain redirects:')
hosts.forEach(function (host) {
CMN.stdout(host.src + ' -> ' + host.dst)
})
}
CMN.stdout.blankline()
if (!anchors.length) {
CMN.stdout('No ports redirected.')
} else {
CMN.stdout('Port redirects:')
anchors.forEach(function (anchor) {
anchor = anchor.replace('rdr pass inet proto tcp from any to ', '')
CMN.stdout(anchor)
})
}
}
function restore() {
if (CMN.isRoot()) {
var backup = fs.readFileSync(CMN.FILE.BACKUP, 'utf8') || '(Empty)'
var EXPLANATION = ''+
'\n This operation will overwrite /etc/hosts with the content of' +
'\n ~/etchosts.bak. Examine the file (printed immediately above)'+
'\n before proceeding.'
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
process.stdout.write('\n' + backup + '\n')
CMN.stdout(EXPLANATION).write()
rl.question(' Do you want to proceed (Y/n)? ', function (ans) {
if (ans === 'Y') {
fs.writeFileSync('/etc/hosts', backup)
reset()
rl.close()
} else {
CMN.stoudout('Restore aborted.')
rl.close()
}
})
}
}
// RUN =========================================================================
bash('sh ' + CMN.FILE.PREFIX + '/scripts/mkdirs.sh')
if (global.$1 === '.') configure()
else if (global.$1 === 'on') on()
else if (global.$1 === 'off') off()
else if (global.$1 === 'list') list()
else if (global.$1 === 'reset') reset()
else if (global.$1 === 'restore') restore()
else if (global.$1 === '--help') CMN.stdout(CMN.STRING.HELP)
else if (global.$1 === '--version') process.stderr.write(CMN.STRING.VERSION)
else if (global.$1 && global.$3) {
configure([global.$1 +' '+ global.$2 +' '+ global.$3])
}
else CMN.stdout(CMN.STRING.HELP)
CMN.stdout.write()
// =============================================================================