n8n-nodes-nmap
Version:
Nmap Scan
76 lines • 2.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShellUtils = void 0;
const child_process_1 = require("child_process");
class ShellUtils {
async sudoCommand(command, workingDirectory, password) {
return new Promise((resolve, reject) => {
let child = (0, child_process_1.spawn)('sudo', ['-S', '-k', '-p', 'pwd:', 'sh', '-c', command], {
cwd: workingDirectory,
});
let commandOutput = '';
let commandError = '';
child.stdout.on('data', (data) => {
commandOutput += data.toString();
});
child.stderr.on('data', (error) => {
if (error.toString() == 'pwd:') {
child.stdin.write(password + '\n');
}
else {
closeFunction();
commandError = error.toString();
}
});
async function closeFunction() {
child.kill('SIGHUP');
}
child.on('exit', (code) => {
if (code === 0) {
resolve(commandOutput);
}
else {
reject(new Error(commandError));
}
});
});
}
async command(command, workingDirectory) {
return new Promise((resolve, reject) => {
let child = (0, child_process_1.spawn)('sh', ['-c', command], { cwd: workingDirectory });
let commandOutput = '';
child.stdout.on('data', (data) => {
commandOutput += data.toString();
});
child.stderr.on('data', (error) => {
closeFunction();
reject(new Error(error.toString()));
});
async function closeFunction() {
child.kill('SIGHUP');
}
child.on('exit', (code) => {
if (code === 0) {
resolve(commandOutput);
}
});
});
}
async resolveHomeFolder(path) {
if (path.startsWith('~/')) {
const command = 'echo $HOME';
let homeFolder = await this.command(command, '/');
homeFolder = homeFolder.replace('\n', '');
if (!homeFolder.endsWith('/')) {
homeFolder += '/';
}
return path.replace('~/', homeFolder);
}
if (path.startsWith('~')) {
throw new Error('Invalid path. Replace "~" with home directory or "~/"');
}
return path;
}
}
exports.ShellUtils = ShellUtils;
//# sourceMappingURL=ShellUtils.js.map