Skip to content

Commit e96a73e

Browse files
committed
feat: ssl
1 parent 015adb7 commit e96a73e

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

modules/sslCheck.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const https = require('https');
2+
const tls = require('tls');
3+
4+
function checkSSLCertificate(hostname, port = 443, callback) {
5+
let certRetrieved = false;
6+
7+
// Use tls.connect for more reliable certificate retrieval
8+
const socket = tls.connect({
9+
host: hostname,
10+
port: port,
11+
rejectUnauthorized: false, // We want to check even if cert is invalid
12+
servername: hostname, // Important for SNI
13+
}, () => {
14+
try {
15+
const cert = socket.getPeerCertificate(true);
16+
17+
if (cert && cert.valid_to) {
18+
certRetrieved = true;
19+
const expirationDate = new Date(cert.valid_to);
20+
const now = new Date();
21+
const daysUntilExpiry = Math.ceil((expirationDate - now) / (1000 * 60 * 60 * 24));
22+
23+
callback(null, {
24+
expirationDate: expirationDate.toISOString(),
25+
daysUntilExpiry: daysUntilExpiry,
26+
isValid: expirationDate > now,
27+
issuer: cert.issuer ? (cert.issuer.CN || JSON.stringify(cert.issuer)) : null,
28+
subject: cert.subject ? (cert.subject.CN || hostname) : hostname,
29+
});
30+
} else {
31+
callback(null, null);
32+
}
33+
} catch (err) {
34+
callback(null, null);
35+
} finally {
36+
if (!socket.destroyed) {
37+
socket.end();
38+
}
39+
}
40+
});
41+
42+
socket.on('error', (error) => {
43+
if (!certRetrieved) {
44+
// If we can't get the certificate, return null (might be HTTP or connection issue)
45+
callback(null, null);
46+
}
47+
});
48+
49+
socket.on('timeout', () => {
50+
if (!certRetrieved) {
51+
socket.destroy();
52+
callback(null, null);
53+
}
54+
});
55+
56+
socket.setTimeout(5000);
57+
}
58+
59+
module.exports = checkSSLCertificate;
60+

0 commit comments

Comments
 (0)