-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
140 lines (123 loc) · 3.52 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const path = require('path');
const fs = require('fs');
const loadConfig = require('./lib/load-config');
const RuleParser = require('./lib/rule-parser');
const { setupTCPServer, setupUDPServer } = require('./lib/server');
const loadInput = () => {
const inputShortMap = {
'c': 'config-file',
'i': 'init',
'h': 'help'
};
const defaultValue = {
'config-file': './config.json',
'init': './config.json',
'help': true
};
const input = {};
let lastInput = null;
for (let arg of process.argv.slice(2)) {
if (arg.indexOf('-') === 0) {
if (arg.indexOf('--') === 0) {
lastInput = arg.substr(2);
}
else {
lastInput = inputShortMap[arg.substr(1)];
}
input[lastInput] = defaultValue[lastInput];
}
else if (lastInput) {
input[lastInput] = arg;
lastInput = null;
}
}
// if no config file specified, load from environment variable
if (!input['config-file']) {
input['config-file'] = process.env.DNSPROXY_CONFIG || path.resolve('./config.json');
}
return input;
};
const init = (input) => {
console.log(`Loading config file '${input['config-file']}'...`);
const config = loadConfig(input['config-file']);
const { servers, settings } = config;
const defaultServer = servers.default || servers[Object.keys(servers)[0]];
const rules = new RuleParser();
rules.initDefaultServer(defaultServer);
rules.initParsers(config['extend-parsers'], input['config-file']);
rules.initRules(config.rules, servers, input['config-file']);
let udpServer;
let tcpServer;
if (!settings.udp && !settings.tcp) {
console.log('Both TCP and UDP servers are not enabled');
return;
}
if (settings.udp) {
const { host, port, timeout } = settings;
udpServer = setupUDPServer(host, port, timeout, rules);
}
if (settings.tcp) {
const { host, port, timeout } = settings;
tcpServer = setupTCPServer(host, port, timeout, rules);
}
const closeListener = (data) => {
const keyCode = data[0];
switch (keyCode) {
case 0x03:
case 0x1a:
process.exit();
break;
case 0x12:
process.stdin.removeListener('data', closeListener);
console.log('Closing proxy servers...');
udpServer && udpServer.close();
tcpServer && tcpServer.close();
setTimeout(init, 0, input);
break;
}
};
if (process.stdin) {
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.on('data', closeListener);
console.log('Press ^R to reload, ^C or ^Z to exit');
}
else {
console.log(`Cannot listen to stdin, if you're using DNSProxy in startup script, try killing pid ${process.pid} to terminate`);
}
};
const printHelp = () => {
console.log(`Usage:
dnsproxy [options]
Options:
-c, --config-file <path> Specify the configuration file
(default: env['DNSPROXY_CONFIG'] || ./config.json)
-i, --init <path> Create a configuration template file
(default: ./config.json)
-h, --help Show help
`);
};
const main = () => {
const input = loadInput();
if (input.help) {
printHelp();
return;
}
if (input.init) {
const dir = path.resolve(input.init);
if (fs.existsSync(dir)) {
console.log(`File ${dir} is already exist.`);
return;
}
const read = fs.createReadStream(path.resolve(__dirname, './config.sample.json'));
const write = fs.createWriteStream(dir);
write.on('close', () => {
console.log(`Config template file ${dir} is created.`);
});
read.pipe(write);
return;
}
init(input);
};
console.log(`DNSProxy v${process.env.npm_package_version || require('./package.json').version}\n`);
main();