-
Notifications
You must be signed in to change notification settings - Fork 0
/
npn-proxy.js
67 lines (53 loc) · 1.56 KB
/
npn-proxy.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
var tls = require('tls');
var net = require('net');
var fs = require('fs');
var config = require('./npn-server.config');
var publicNpnProtocols = [];
Object.keys(config.protocols).forEach(function(key) {
if(!config.protocols[key].hidden)
publicNpnProtocols.push(key);
});
console.log('public protocols:', publicNpnProtocols);
var options = {
key: fs.readFileSync(config.key),
cert: fs.readFileSync(config.cert),
NPNProtocols: publicNpnProtocols
};
var server = tls.createServer(options, function(stream) {
stream.pause();
console.log('npn-client connected', stream.npnProtocol,
'from', stream.remoteAddress);
var protocol = stream.npnProtocol || config.default;
var dst = config.protocols[protocol];
if(!dst){
console.log(protocol, 'not supported closing connection');
stream.end();
return;
}
console.log('protocol:', dst.host, dst.port);
function pipe() {
stream.pipe(this);
stream.resume();
this.pipe(stream);
};
if(options = dst.wrap_ssl) {
var client = tls.connect(dst.port, dst.host, options, pipe);
} else {
var client = net.connect(dst.port, dst.host, pipe);
}
client.on('error', function (e) {
console.log('proxied connection:', e.code);
stream.end();
});
});
server.listen(config.port, function() {
console.log('npn-server bound to:', config.port);
// Drop privileges
try {
if(config.group) process.setgid(config.group);
if(config.user) process.setuid(config.user);
} catch (err) {
console.log('Failed to setuid/setgid, quiting', err);
process.exit(1);
}
});