Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling traceroute to also take ports #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ export abstract class Process extends events.EventEmitter {
}

public trace(domainName: string): void {
if (!this.isValidDomainName(domainName)) {
throw "Invalid domain name or IP address";
if (validator.isFQDN(domainName) || validator.isIP(domainName)) {
this.args.push(domainName);
} else if (validator.isURL(domainName)) {
const url = new URL(domainName);
this.args.push(...[url.hostname, '-p', url.port]);
} else {
throw "Invalid domain";
}

this.args.push(domainName);

const process = spawn(this.command, this.args);
process.on('close', (code) => {
this.emit('close', code);
Expand Down Expand Up @@ -55,10 +58,6 @@ export abstract class Process extends events.EventEmitter {
}
}

private isValidDomainName(domainName: string): boolean {
return validator.isFQDN(domainName + '') || validator.isIP(domainName + '');
}

abstract parseDestination(data: string): string | null;
abstract parseHop(hopData: string): Hop | null;
}
27 changes: 22 additions & 5 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import validator from 'validator';

import Traceroute from '../src/index';

describe('Traceroute', () => {
it('should verify pid, destination, hops and close code', (wait) => {
function tracerVaildation(wait: jest.DoneCallback) {
const tracer = new Traceroute();

tracer
Expand All @@ -14,7 +13,7 @@ describe('Traceroute', () => {
expect(validator.isIP(destination)).toBeTruthy();
})
.on('hop', (hopObj) => {
const { hop, ip, rtt1 } = hopObj;
const {hop, ip, rtt1} = hopObj;

expect(Number.isInteger(hop)).toBeTruthy();
expect(validator.isIP(ip) || ip === '*').toBeTruthy();
Expand All @@ -25,7 +24,25 @@ describe('Traceroute', () => {

wait();
});
return tracer;
}

describe('Traceroute', () => {
it('URL: should verify pid, destination, hops and close code', (wait) => {
const tracer = tracerVaildation(wait);

tracer.trace('google.com');
}, 60000);

it('URL + Port: should verify pid, destination, hops and close code', (wait) => {
const tracer = tracerVaildation(wait);

tracer.trace('google.com:443');
}, 60000);

it('IP: should verify pid, destination, hops and close code', (wait) => {
const tracer = tracerVaildation(wait);

tracer.trace('github.com');
}, 60000);
tracer.trace('127.0.0.1');
}, 60000);
});
Loading