mcraft-fun-mineflayer
Version:
Mineflayer viewer (connector) for mcraft.fun project and vanilla Minecraft client! Both TCP and WebSockets servers are supported.
53 lines (52 loc) • 1.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateSelfSignedCertificate = generateSelfSignedCertificate;
const crypto_1 = require("crypto");
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const os_1 = require("os");
const path_1 = require("path");
function generateSelfSignedCertificate() {
// Generate key pair
const { privateKey, publicKey } = (0, crypto_1.generateKeyPairSync)('rsa', {
modulusLength: 2048,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
});
// Create temporary files for OpenSSL
const tmpDir = (0, os_1.tmpdir)();
const keyPath = (0, path_1.join)(tmpDir, 'server.key');
const csrPath = (0, path_1.join)(tmpDir, 'server.csr');
const certPath = (0, path_1.join)(tmpDir, 'server.crt');
try {
// Write private key to temp file
(0, fs_1.writeFileSync)(keyPath, privateKey);
// Generate CSR
(0, child_process_1.execSync)(`openssl req -new -key ${keyPath} -out ${csrPath} -subj "/CN=localhost"`);
// Generate self-signed certificate
(0, child_process_1.execSync)(`openssl x509 -req -days 365 -in ${csrPath} -signkey ${keyPath} -out ${certPath}`);
// Read the generated certificate
const cert = (0, child_process_1.execSync)(`cat ${certPath}`).toString();
return {
key: privateKey,
cert
};
}
finally {
// Clean up temporary files
try {
(0, fs_1.unlinkSync)(keyPath);
(0, fs_1.unlinkSync)(csrPath);
(0, fs_1.unlinkSync)(certPath);
}
catch (e) {
// Ignore cleanup errors
}
}
}