What I’m trying to achieve here is to listen to a port for incoming data through a ngrok tcp tunnel.
I’m a tester and have very little knowledge on networking.
Can this be achieved using any npm package??
What I did so far:
async function startNcatListener(port) {
return new Promise((resolve, reject) => {
let dataReceived = false; // Flag to track whether data has been received
const ncat = new childProcess.spawn('ncat', ['-nvlp', port]);
ncat.stdout.on('data', (data) => {
// Validate incoming data
console.log('data', data)
expect(data.toString()).toContain('node@scriptworker'); // Modify 'ExpectedData' to match your expected data
dataReceived = true; // Set flag to true when data is received
resolve(); // Resolve the promise if data is as expected
});
ncat.stderr.on('data', (data) => {
console.error(`ncat stderr: ${data}`);
reject(new Error(`ncat stderr: ${data}`)); // Reject the promise if there's an error
});
ncat.on('close', (code) => {
console.log(`ncat process exited with code ${code}`);
if (!dataReceived) {
reject(new Error('Timeout: No data received within 2 minutes')); // Reject the promise if no data is received within 2 minutes
}
});
ncat.on('error', (err) => {
console.error('Failed to start ncat process:', err);
reject(err); // Reject the promise if there's an error
});
// Set a timeout to exit if no data is received within 2 minutes
setTimeout(() => {
if (!dataReceived) {
console.log('Timeout: No data received within 2 minutes');
ncat.kill(); // Kill the ncat process
}
}, 2 * 60 * 1000);
});
And yes the code is from chatgpt.
But it throws an error which I’m unable to solve:
console.error
ncat stderr: Ncat: Version 7.95 ( https://nmap.org/ncat )
158 |
159 | ncat.stderr.on('data', (data) => {
> 160 | console.error(`ncat stderr: ${data}`);
| ^
161 | reject(new Error(`ncat stderr: ${data}`)); // Reject the promise if there's an error
162 | });
1