Created
June 15, 2017 15:15
-
-
Save max-lt/31306ce38a0975093b2c57054c0c42cb to your computer and use it in GitHub Desktop.
Node tls info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const tls = require('tls'); | |
const url = require('url'); | |
const getSslDetails = (host) => new Promise((resolve, reject) => { | |
const sock = tls.connect({rejectUnauthorized: false, port: 443, host}, () => { | |
const certs = sock.getPeerCertificate(); | |
delete certs.raw; | |
resolve({ | |
authorized: sock.authorized, | |
error: sock.authorized ? undefined : sock.authorizationError, | |
certificates: certs | |
}); | |
}); | |
sock.on('error', reject); | |
}); | |
function send(res, status, content) { | |
if (content) { | |
res.writeHead(status, {'Content-Type': 'application/json'}); | |
res.end(JSON.stringify(content)); | |
} | |
else { | |
res.writeHead(status); | |
res.end(); | |
} | |
} | |
http.createServer((req, res) => { | |
const _url = url.parse(req.url, true); | |
if (req.method !== 'GET' || !_url.query.domain) | |
return send(res, 400); | |
const domain = _url.query.domain; | |
getSslDetails(domain) | |
.then((certs) => send(res, 200, Object.assign(certs, {domain}))) | |
.catch((error) => { | |
switch (error.code) { | |
case 'ENOTFOUND': | |
return send(res, 404, {error}); | |
default: | |
console.error(error); | |
return send(res, 500, {error}); | |
} | |
}) | |
}).listen(4567); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment