2024-09-18 00:41:14 +02:00
|
|
|
import * as plugins from './plugins.js';
|
2024-09-18 15:30:47 +02:00
|
|
|
import { createSign } from 'crypto';
|
2024-09-18 00:41:14 +02:00
|
|
|
|
|
|
|
class TapNodeTools {
|
|
|
|
private smartshellInstance: plugins.smartshell.Smartshell;
|
|
|
|
|
2024-09-18 15:30:47 +02:00
|
|
|
constructor() {}
|
2024-09-18 00:41:14 +02:00
|
|
|
|
2024-09-18 15:30:47 +02:00
|
|
|
public async runCommand(commandArg: string): Promise<any> {
|
2024-09-18 00:41:14 +02:00
|
|
|
if (!this.smartshellInstance) {
|
|
|
|
this.smartshellInstance = new plugins.smartshell.Smartshell({
|
|
|
|
executor: 'bash',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const result = await this.smartshellInstance.exec(commandArg);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-18 15:30:47 +02:00
|
|
|
public async createHttpsCert(
|
|
|
|
commonName: string = 'localhost'
|
|
|
|
): Promise<{ key: string; cert: string }> {
|
|
|
|
// Generate RSA key pair
|
|
|
|
const { publicKey, privateKey } = plugins.crypto.generateKeyPairSync('rsa', {
|
2024-09-18 00:41:14 +02:00
|
|
|
modulusLength: 2048,
|
|
|
|
publicExponent: 65537,
|
|
|
|
});
|
2024-09-18 15:30:47 +02:00
|
|
|
|
|
|
|
// Create a self-signed certificate
|
|
|
|
const cert = this.generateSelfSignedCert(publicKey, privateKey, commonName);
|
|
|
|
|
|
|
|
// Export the private key and return the cert and key
|
|
|
|
const keyContent = privateKey.export({
|
2024-09-18 00:41:14 +02:00
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
});
|
2024-09-18 15:30:47 +02:00
|
|
|
|
2024-09-18 00:41:14 +02:00
|
|
|
return {
|
|
|
|
key: keyContent as string,
|
|
|
|
cert: cert,
|
2024-09-18 15:30:47 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private generateSelfSignedCert(publicKey, privateKey, commonName: string): string {
|
|
|
|
const sign = createSign('SHA256');
|
|
|
|
const certData = {
|
|
|
|
subject: `/CN=${commonName}`,
|
|
|
|
publicKey: publicKey.export({ type: 'spki', format: 'pem' }),
|
|
|
|
};
|
|
|
|
|
|
|
|
sign.update(JSON.stringify(certData));
|
|
|
|
sign.end();
|
|
|
|
|
|
|
|
const signature = sign.sign(privateKey, 'base64');
|
|
|
|
|
|
|
|
return (
|
|
|
|
'-----BEGIN CERTIFICATE-----\n' +
|
|
|
|
Buffer.from(certData.publicKey).toString('base64') +
|
|
|
|
'\n' +
|
|
|
|
signature +
|
|
|
|
'\n-----END CERTIFICATE-----\n'
|
|
|
|
);
|
2024-09-18 00:41:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-18 15:30:47 +02:00
|
|
|
export const tapNodeTools = new TapNodeTools();
|