前言
使用 npm包 ssl-validator 来获取证书的域名、有效期等信息。
代码
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
| const sslValidator = require('ssl-validator'); const SSLData = require('./data');
(async () => { try { const res = await sslValidator.validateSSL(SSLData.CSR, { key: SSLData.KEY }); const certInfo = { domain: res.certInfo.commonName, validity: { start: res.certInfo.validity.start, startString: new Date(res.certInfo.validity.start).toLocaleString(), end: res.certInfo.validity.end, endString: new Date(res.certInfo.validity.end).toLocaleString() } }; console.log('certInfo', certInfo); } catch (e) { if(e.message.indexOf('unable to load certificate') !== -1 || e.message.indexOf('Certificate must start and end with proper formatting.') !== -1){ console.error('无效的证书'); }else if(e.message.indexOf('unable to load Private Key') !== -1 || e.message.indexOf('Key must start and end with proper formatting.') !== -1){ console.error('无效的秘钥'); }else if(e.message.indexOf('The certificate does not match the domain.') !== -1){ console.error('证书与域名不匹配'); }else if(e.message.indexOf('The provided certificate and key do not match.') !== -1){ console.error('证书与秘钥不匹配'); }else { console.error('证书、秘钥、域名格式错误或不匹配'); } }; })();
|
总结
- 如果在windowns下执行,需要先安装OpenSSL