2024-09-17 22:41:14 +00:00
|
|
|
import * as plugins from './plugins.js';
|
|
|
|
|
|
|
|
class TapNodeTools {
|
|
|
|
private smartshellInstance: plugins.smartshell.Smartshell;
|
|
|
|
|
2024-09-18 13:30:47 +00:00
|
|
|
constructor() {}
|
2024-09-17 22:41:14 +00:00
|
|
|
|
2024-09-19 08:30:14 +00:00
|
|
|
private qenv: plugins.qenv.Qenv;
|
2024-09-19 06:38:38 +00:00
|
|
|
public async getQenv(): Promise<plugins.qenv.Qenv> {
|
2024-09-19 08:30:14 +00:00
|
|
|
this.qenv = this.qenv || new plugins.qenv.Qenv('./', '.nogit/');
|
|
|
|
return this.qenv;
|
|
|
|
}
|
|
|
|
public async getEnvVarOnDemand(envVarNameArg: string): Promise<string> {
|
|
|
|
const qenv = await this.getQenv();
|
|
|
|
return qenv.getEnvVarOnDemand(envVarNameArg);
|
2024-09-19 06:38:38 +00:00
|
|
|
}
|
|
|
|
|
2024-09-18 13:30:47 +00:00
|
|
|
public async runCommand(commandArg: string): Promise<any> {
|
2024-09-17 22:41:14 +00:00
|
|
|
if (!this.smartshellInstance) {
|
|
|
|
this.smartshellInstance = new plugins.smartshell.Smartshell({
|
|
|
|
executor: 'bash',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const result = await this.smartshellInstance.exec(commandArg);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-18 13:30:47 +00:00
|
|
|
public async createHttpsCert(
|
2024-09-18 16:43:46 +00:00
|
|
|
commonName: string = 'localhost',
|
|
|
|
allowSelfSigned: boolean = true
|
2024-09-18 13:30:47 +00:00
|
|
|
): Promise<{ key: string; cert: string }> {
|
2024-09-18 16:43:46 +00:00
|
|
|
if (allowSelfSigned) {
|
|
|
|
// set node to allow self-signed certificates
|
|
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
|
|
}
|
|
|
|
|
2024-09-18 15:56:53 +00:00
|
|
|
// Generate a key pair
|
|
|
|
const keys = plugins.smartcrypto.nodeForge.pki.rsa.generateKeyPair(2048);
|
2024-09-18 13:30:47 +00:00
|
|
|
|
|
|
|
// Create a self-signed certificate
|
2024-09-18 15:56:53 +00:00
|
|
|
const cert = plugins.smartcrypto.nodeForge.pki.createCertificate();
|
|
|
|
cert.publicKey = keys.publicKey;
|
|
|
|
cert.serialNumber = '01';
|
|
|
|
cert.validity.notBefore = new Date();
|
|
|
|
cert.validity.notAfter = new Date();
|
|
|
|
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
|
|
|
|
|
|
|
|
const attrs = [
|
|
|
|
{ name: 'commonName', value: commonName },
|
|
|
|
{ name: 'countryName', value: 'US' },
|
|
|
|
{ shortName: 'ST', value: 'California' },
|
|
|
|
{ name: 'localityName', value: 'San Francisco' },
|
|
|
|
{ name: 'organizationName', value: 'My Company' },
|
|
|
|
{ shortName: 'OU', value: 'Dev' },
|
|
|
|
];
|
|
|
|
cert.setSubject(attrs);
|
|
|
|
cert.setIssuer(attrs);
|
|
|
|
|
|
|
|
// Sign the certificate with its own private key (self-signed)
|
|
|
|
cert.sign(keys.privateKey, plugins.smartcrypto.nodeForge.md.sha256.create());
|
|
|
|
|
|
|
|
// PEM encode the private key and certificate
|
|
|
|
const pemKey = plugins.smartcrypto.nodeForge.pki.privateKeyToPem(keys.privateKey);
|
|
|
|
const pemCert = plugins.smartcrypto.nodeForge.pki.certificateToPem(cert);
|
2024-09-18 13:30:47 +00:00
|
|
|
|
2024-09-17 22:41:14 +00:00
|
|
|
return {
|
2024-09-18 15:56:53 +00:00
|
|
|
key: pemKey,
|
|
|
|
cert: pemCert,
|
2024-09-18 13:30:47 +00:00
|
|
|
};
|
2024-09-17 22:41:14 +00:00
|
|
|
}
|
2024-11-06 01:46:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* create and return a smartmongo instance
|
|
|
|
*/
|
|
|
|
public async createSmartmongo() {
|
|
|
|
const smartmongoMod = await import('@push.rocks/smartmongo');
|
|
|
|
const smartmongoInstance = new smartmongoMod.SmartMongo();
|
|
|
|
await smartmongoInstance.start();
|
|
|
|
return smartmongoInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create and return a smarts3 instance
|
|
|
|
*/
|
|
|
|
public async createSmarts3() {
|
|
|
|
const smarts3Mod = await import('@push.rocks/smarts3');
|
|
|
|
const smarts3Instance = new smarts3Mod.Smarts3({
|
|
|
|
cleanSlate: true,
|
|
|
|
});
|
|
|
|
await smarts3Instance.start();
|
|
|
|
return smarts3Instance;
|
|
|
|
}
|
2024-09-17 22:41:14 +00:00
|
|
|
}
|
|
|
|
|
2024-09-18 13:30:47 +00:00
|
|
|
export const tapNodeTools = new TapNodeTools();
|