-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
42 lines (34 loc) · 1.01 KB
/
main.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
const https = require('https');
const fs = require('fs');
const crypto = require('crypto');
const options = {
key: fs.readFileSync('server-key.pem'),
cert: fs.readFileSync('server-cert.pem'),
requestCert: true,
rejectUnauthorized: false,
};
const trustedClients = new Set();
const server = https.createServer(options, (req, res) => {
const clientCert = req.socket.getPeerCertificate();
if (!clientCert || !clientCert.raw) {
res.writeHead(403);
res.end('Client certificate required.\n');
return;
}
const fingerprint = crypto
.createHash('sha256')
.update(clientCert.raw)
.digest('hex');
console.log(`Certificate hash of client: ${fingerprint}`);
if (trustedClients.has(fingerprint)) {
res.writeHead(200);
res.end('Welcome back, trusted client!\n');
} else {
trustedClients.add(fingerprint);
res.writeHead(200);
res.end('Hello, new client! You are now trusted.\n');
}
});
server.listen(3000, () => {
console.log('Server listening on https://localhost:3000');
});