62 lines
1.7 KiB
JavaScript
Executable File
62 lines
1.7 KiB
JavaScript
Executable File
'use strict';
|
|
|
|
const spawn = require('child_process').spawn;
|
|
const events = require('events');
|
|
const readline = require('readline');
|
|
const validator = require('validator');
|
|
|
|
class Process extends events.EventEmitter {
|
|
constructor(command, args) {
|
|
super();
|
|
|
|
this.command = command;
|
|
this.args = args;
|
|
}
|
|
|
|
trace(domainName) {
|
|
if (!this.isValidDomainName(domainName)) {
|
|
throw "Invalid domain name or IP address";
|
|
}
|
|
|
|
this.args.push(domainName);
|
|
|
|
const process = spawn(this.command, this.args);
|
|
process.on('close', (code) => {
|
|
this.emit('close', code);
|
|
});
|
|
|
|
this.emit('pid', process.pid);
|
|
|
|
let isDestinationCaptured = false;
|
|
if (process.pid) {
|
|
readline.createInterface({
|
|
input: process.stdout,
|
|
terminal: false
|
|
})
|
|
.on('line', (line) => {
|
|
if (!isDestinationCaptured) {
|
|
const destination = this.parseDestination(line);
|
|
if (destination !== null) {
|
|
this.emit('destination', destination);
|
|
|
|
isDestinationCaptured = true;
|
|
}
|
|
}
|
|
|
|
const hop = this.parseHop(line);
|
|
if (hop !== null) {
|
|
this.emit('hop', hop);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
isValidDomainName(domainName) {
|
|
return validator.isFQDN(domainName + '') || validator.isIP(domainName + '');
|
|
}
|
|
|
|
parseDestination(data) {}
|
|
parseHop(hopData) {}
|
|
}
|
|
|
|
module.exports = Process; |