@axlotl-lab/navigrator
Version:
A powerful local domain manager for development environments. Navigrator helps you manage local domains and SSL certificates with a simple web interface.
136 lines (135 loc) • 5.5 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;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CAGenerator = void 0;
const child_process_1 = require("child_process");
const fs = __importStar(require("fs/promises"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const util_1 = require("util");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
class CAGenerator {
constructor(certsDir) {
this.certsDir = certsDir || path.join(os.homedir(), '.navigrator', 'certs');
this.caDir = path.join(this.certsDir, 'ca');
this.caKeyPath = path.join(this.caDir, 'rootCA.key');
this.caCertPath = path.join(this.caDir, 'rootCA.crt');
}
/**
* Check if OpenSSL is installed on the system
*/
checkOpenSSLInstalled() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield execAsync('openssl version');
return true;
}
catch (error) {
return false;
}
});
}
/**
* Initialize the certificate directory
*/
initialize() {
return __awaiter(this, void 0, void 0, function* () {
try {
// Create required directories
yield fs.mkdir(this.certsDir, { recursive: true });
yield fs.mkdir(this.caDir, { recursive: true });
}
catch (error) {
console.error('Error creating certificate directories:', error);
throw new Error(`Failed to create certificate directories: ${error === null || error === void 0 ? void 0 : error.message}`);
}
});
}
/**
* Check if the CA certificate exists
*/
checkCAExists() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield fs.access(this.caKeyPath);
yield fs.access(this.caCertPath);
return true;
}
catch (error) {
return false;
}
});
}
/**
* Generate a local CA to sign certificates
*/
generateCA() {
return __awaiter(this, void 0, void 0, function* () {
try {
// Check if CA already exists
const caExists = yield this.checkCAExists();
if (caExists) {
return { keyPath: this.caKeyPath, certPath: this.caCertPath };
}
// Create private key for the CA
yield execAsync(`openssl genrsa -out "${this.caKeyPath}" 4096`);
// Create certificate for the CA (valid for 10 years)
yield execAsync(`openssl req -x509 -new -nodes -key "${this.caKeyPath}" -sha256 -days 3650 ` +
`-out "${this.caCertPath}" -subj "/CN=Navigrator Local CA/O=Axlotl Lab/OU=Development"`);
return { keyPath: this.caKeyPath, certPath: this.caCertPath };
}
catch (error) {
console.error('Error generating CA certificate:', error);
throw new Error(`Failed to generate CA certificate: ${error === null || error === void 0 ? void 0 : error.message}`);
}
});
}
/**
* Get the paths to the CA files
*/
getCAPaths() {
return { keyPath: this.caKeyPath, certPath: this.caCertPath };
}
}
exports.CAGenerator = CAGenerator;