-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathserver.js
213 lines (178 loc) · 6.04 KB
/
server.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var fs = require('fs')
, path = require('path')
, net = require('net')
, d = require('domain').create()
, debug = require('./debug')('APP')
// ----
function getLocalIP(callback) {
var socket = net.createConnection(53, '8.8.8.8');
socket.on('connect', function() {
callback(undefined, socket.address().address);
socket.destroy();
});
socket.on('error', function(e) {
callback(e, 'error');
});
}
var template = "/tmpl/server.tmpl";
function getConfig(ctx, callback) {
try {
var t = fs.readFileSync(path.dirname(__filename)+template, 'utf8');
var s = tmpl(t, ctx);
var j = JSON.parse(s);
callback(undefined, j);
} catch(x) {
callback(x);
}
}
// ---- begin inline underscore template function
// By default, Underscore uses ERB-style template delimiters, change the
// following template settings to use alternative delimiters.
var templateSettings = {
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%=([\s\S]+?)%>/g,
escape : /<%-([\s\S]+?)%>/g
};
// When customizing `templateSettings`, if you don't want to define an
// interpolation, evaluation or escaping regex, we need one that is
// guaranteed not to match.
var noMatch = /(.)^/;
// Certain characters need to be escaped so that they can be put into a
// string literal.
var escapes = {
"'": "'",
'\\': '\\',
'\r': 'r',
'\n': 'n',
'\t': 't',
'\u2028': 'u2028',
'\u2029': 'u2029'
};
var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g;
// JavaScript micro-templating, similar to John Resig's implementation.
// Underscore templating handles arbitrary delimiters, preserves whitespace,
// and correctly escapes quotes within interpolated code.
var tmpl = function(text, data, settings) {
settings = settings || templateSettings;
// Combine delimiters into one regular expression via alternation.
var matcher = new RegExp([
(settings.escape || noMatch).source,
(settings.interpolate || noMatch).source,
(settings.evaluate || noMatch).source
].join('|') + '|$', 'g');
// Compile the template source, escaping string literals appropriately.
var index = 0;
var source = "__p+='";
text.replace(matcher, function(match, escape, interpolate, evaluate, offset) {
source += text.slice(index, offset)
.replace(escaper, function(match) { return '\\' + escapes[match]; });
if (escape) {
source += "'+\n((__t=(" + escape + "))==null?'':_.escape(__t))+\n'";
}
if (interpolate) {
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'";
}
if (evaluate) {
source += "';\n" + evaluate + "\n__p+='";
}
index = offset + match.length;
return match;
});
source += "';\n";
// If a variable is not specified, place data values in local scope.
if (!settings.variable) source = 'with(obj||{}){\n' + source + '}\n';
source = "var __t,__p='',__j=Array.prototype.join," +
"print=function(){__p+=__j.call(arguments,'');};\n" +
source + "return __p;\n";
try {
var render = new Function(settings.variable || 'obj', source);
} catch (e) {
e.source = source;
throw e;
}
if (data) return render(data);
var template = function(data) {
return render.call(this, data);
};
// Provide the compiled function source as a convenience for precompilation.
template.source = 'function(' + (settings.variable || 'obj') + '){\n' + source + '}';
return template;
};
// ---- end inline underscore template function
// ---- exports
exports.tmpl = tmpl;
// ---- main entry
/*
// node http leaks socket, bug 3536
process.on('uncaughtException', function(e){
debug('UNCAUGHTEXCEPTION', e);
});
*/
// command
// ** on local as a LOCAL (worker ip: 1.2.3.4)
// start local app for shadowsocks / socks5 / vpn
// npm -g start --lodns=udp://192.168.1.1:53 --worker=shadow://[email protected]:5678
// npm -g start --lodns=udp://192.168.1.1:53 --worker=socks5://192.168.1.3:7070
// npm -g start --lodns=udp://192.168.1.1:53 --worker=vpn://192.168.5.1
// ** on remote as a WORKER (self ip: 1.2.3.4)
// npm -g start --app=worker --shadow=shadow://[email protected]:5678
// npm -g start --app=worker --socks5=socks5://0.0.0.0:7890
// npm -g start --app=worker --vpn=vpn://192.168.5.2
if (!module.parent) {
var app = process.env.npm_config_app || 'local';
var lodns = process.env.npm_config_lodns; // || 'udp://192.168.1.1:53';
if ((lodns == undefined) && (app == 'local')) {
console.log('It\'s not a bug, but local DNS is needed.');
console.log(' example: sudo npm start --lodns=udp://192.168.1.1:53');
console.log('Will run in remote DNS only, it\'s very slow.');
// console.log('');
// process.exit(0);
}
getLocalIP(function (error, localip) {
if (error) return console.log('Not Online? error:', error);
var ip = localip || '127.0.0.1';
var ctx = {
local: {
ip: ip,
lodns: lodns || 'udp://8.8.8.8:53',
worker: process.env.npm_config_worker || 'shadow://cool@'+ip+':1027'
},
worker: {
socks5: process.env.npm_config_socks5 || 'socks5://cool@'+ip+':1026',
shadow: process.env.npm_config_shadow || 'shadow://cool@'+ip+':1027'
}
};
getConfig(ctx, function(error, conf){
if (error) return console.log('Config fail. error:', error);
debug('starting %s on %s', app, localip);
// the config parser
function config(){
// debug("%j",conf);
var args = Array.prototype.slice.call(arguments);
var val = conf;
for (var i=0; i<args.length; i++) {
var key = args[i];
val = val[key];
if (!val) {
debug("!!!! config('"+args.slice(0,i+1).join()+"'):undefined");
break;
}
}
return val;
}
var cfg = config(app);
// start them one by one
d.on('error', function(e){
debug('!!!! ERROR %s %j', e.message, e.stack);
// debug('!!!! ERROR %s', e.message);
});
d.run(function(){
for(var mod in cfg){
var conf = cfg[mod];
// debug("start %s:%s %j", app, mod, conf);
require('./'+mod).start(conf);
}
});
});
});
}