rex-server
Version:
Rex Server is a Node.js-based reverse proxy server available as an npm package. It allows you to handle HTTP and HTTPS traffic, route requests to upstream servers, and manage worker processes efficiently. With its CLI interface, Rex makes it easy to confi
96 lines (95 loc) • 4.05 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 });
exports.getSSLConfig = getSSLConfig;
exports.getHostnameFromSSL = getHostnameFromSSL;
const fs_1 = require("fs");
const crypto_1 = __importStar(require("crypto"));
const _lib_1 = require("../lib/index");
/**
* Retrieves the SSL configuration by reading the certificate and key files.
*
* This function takes an object with paths to the SSL certificate and key files,
* reads the files synchronously, and returns an SSL configuration object containing:
* - The certificate content
* - The private key content
* - SSL options to disable SSLv2 and SSLv3 for improved security.
*
* @param {Object} sslConfig - An object containing the paths to the SSL certificate and private key files.
* @param {string} sslConfig.cert - The path to the SSL certificate file.
* @param {string} sslConfig.key - The path to the SSL private key file.
*
* @returns {Object} An object containing:
* - `cert` (Buffer): The content of the SSL certificate file.
* - `key` (Buffer): The content of the SSL private key file.
* - `secureOptions` (number): Security options to disable SSLv2 and SSLv3.
*
* @throws {Error} If there is an issue reading the certificate or key files.
*
* @example
* const sslConfig = getSSLConfig({ cert: '/path/to/cert.pem', key: '/path/to/key.pem' });
* console.log(sslConfig.cert); // Outputs the SSL certificate content.
* console.log(sslConfig.key); // Outputs the SSL private key content.
*/
function getSSLConfig(sslConfig) {
const cert = (0, fs_1.readFileSync)(sslConfig.cert);
const key = (0, fs_1.readFileSync)(sslConfig.key);
return {
cert,
key,
secureOptions: crypto_1.default.constants.SSL_OP_NO_SSLv2 | crypto_1.default.constants.SSL_OP_NO_SSLv3,
};
}
/**
* Extracts the hostname from an SSL certificate file.
*
* @param certPath - The file path to the SSL certificate.
* @returns The hostname extracted from the certificate. If extraction fails, returns 'localhost'.
*
* @throws Will log an error and return 'localhost' if an error occurs during extraction.
*/
function getHostnameFromSSL(certPath) {
try {
const fullCert = (0, fs_1.readFileSync)(certPath, 'utf-8');
const websiteCert = fullCert.split("-----END CERTIFICATE-----")[0].trim() + "\n-----END CERTIFICATE-----";
const cert = new crypto_1.X509Certificate(websiteCert);
const hostname = cert.subject.split("CN=")[1];
return hostname || "localhost";
}
catch (error) {
_lib_1.logger.error(`AN_ERROR_OCCURED_WHILE_EXTRACTING_HOST_FROM_CERT ${(0, _lib_1.formatObjects)(error)}`);
return 'localhost';
}
}