@xyz/whois
Version:
A powerful TypeScript/JavaScript tool for comprehensive domain analysis, featuring detailed WHOIS data with registration dates, registrars, and domain status. Offers SSL certificate extraction (with PEM support), DNS records, and server details. Includes
115 lines (114 loc) ⢠5.31 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../index");
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
/**
* Example demonstrating how to work with certificate data
* including PEM certificates and human-readable details
*/
async function main() {
try {
// Replace with your domain
const domain = 'example.com';
console.log(`Fetching SSL certificate information for ${domain}...`);
const domainInfo = await (0, index_1.fetchDomainInfo)(domain);
if (!(domainInfo === null || domainInfo === void 0 ? void 0 : domainInfo.sslData)) {
console.error('No SSL data returned');
return;
}
// Display human-readable certificate details
console.log('\nš Certificate Details (human-readable):');
if (domainInfo.sslData.details) {
console.log(` - Subject: ${domainInfo.sslData.details.subject}`);
console.log(` - Issuer: ${domainInfo.sslData.details.issuer}`);
console.log(` - Valid from: ${domainInfo.sslData.details.validFrom.toLocaleString()}`);
console.log(` - Valid until: ${domainInfo.sslData.details.validTo.toLocaleString()}`);
console.log(` - Days until expiration: ${Math.floor((domainInfo.sslData.details.validTo.getTime() - Date.now()) / (1000 * 60 * 60 * 24))}`);
}
else {
console.log(' Human-readable details not available');
}
// Create a directory for saving certificates
const certDir = path.join(__dirname, 'certificates');
try {
await fs.mkdir(certDir, { recursive: true });
console.log(`\nSaving certificates to directory: ${certDir}`);
}
catch (err) {
console.error(`Error creating directory: ${err instanceof Error ? err.message : String(err)}`);
return;
}
// Save certificates if available
const certStatus = [];
if (domainInfo.sslData.certificate) {
const serverCertPath = path.join(certDir, `${domain}-server.pem`);
await fs.writeFile(serverCertPath, domainInfo.sslData.certificate);
certStatus.push(`ā
Server certificate saved to: ${serverCertPath}`);
}
else {
certStatus.push('ā Server certificate not available');
}
if (domainInfo.sslData.intermediateCertificate) {
const intermediateCertPath = path.join(certDir, `${domain}-intermediate.pem`);
await fs.writeFile(intermediateCertPath, domainInfo.sslData.intermediateCertificate);
certStatus.push(`ā
Intermediate certificate saved to: ${intermediateCertPath}`);
}
else {
certStatus.push('ā Intermediate certificate not available');
}
if (domainInfo.sslData.rootCertificate) {
const rootCertPath = path.join(certDir, `${domain}-root.pem`);
await fs.writeFile(rootCertPath, domainInfo.sslData.rootCertificate);
certStatus.push(`ā
Root certificate saved to: ${rootCertPath}`);
}
else {
certStatus.push('ā Root certificate not available');
}
// Display certificate status
console.log('\nš Certificate Files:');
certStatus.forEach((status) => console.log(` ${status}`));
console.log('\nš” Tip: You can use these certificates for:');
console.log(' - SSL pinning in mobile or web applications');
console.log(' - Certificate validation in custom TLS implementations');
console.log(' - Analysis or verification of the certificate chain');
console.log(' - Implementing mutual TLS authentication');
}
catch (error) {
console.error('Error:', error instanceof Error ? error.message : String(error));
}
}
main().catch(console.error);