-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
57 lines (49 loc) · 1.79 KB
/
app.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
var certroot = process.env.PKI_ROOT || '/home/pki';
var listen = process.env.LISTEN_PORT || 8080;
var assert = require('assert');
var exec = require('child_process').exec;
var express = require('express');
var ns = require('dns');
var xapp = express();
function sendFile(ca, host, ext, res) {
exec("/bin/cat " + certroot + "/" + ca + "/" + host + "." + ext, function (err, sto, ste) {
console.log("Sending", ca + "/" + host + "." + ext);
res.send(sto);
});
}
function reverseLookup(what, ip, ca, host, res) {
ns.reverse(ip, function (err, domains) {
if (err != null) {
res.send('client has no PTR record');
console.log('unexpected access from', ip);
} else {
domains.forEach(function (domain) {
ns.lookup(domain, function (derr, address, family) {
if (ip == address) {
sendFile(ca, host, what, res);
} else {
console.log("Unexpected access from", ip, "requesting", ca + "/" + host, "certificate");
res.send("");
}
});
});
}
});
}
xapp.get(/^\/certificate\/([^\/]+)\/([^\/]+)\//, function (req, res, next) {
var ca = req.params[0];
var cert = req.params[1];
var host = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if (host.indexOf(',') > 0) { host = host.split(',')[0]; }
reverseLookup("crt", host, ca, cert, res);
});
xapp.get(/^\/key\/([^\/]+)\/([^\/]+)\//, function (req, res, next) {
var ca = req.params[0];
var cert = req.params[1];
var host = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if (host.indexOf(',') > 0) { host = host.split(',')[0]; }
reverseLookup("key", host, ca, cert, res);
});
xapp.listen(listen, 'localhost');
console.log('listening on http://localhost:' + listen);
console.log('consider using https to distribute your keys');