Skip to content

Commit

Permalink
fix: mtr duplicate routes (#62)
Browse files Browse the repository at this point in the history
* add: prevent duplicate hosts from rendering

* add: forward resolved host from a duplicate hop

* fix: unit tests
  • Loading branch information
patrykcieszkowski authored Jun 20, 2022
1 parent 30115a0 commit a8202b4
Show file tree
Hide file tree
Showing 5 changed files with 374 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/command/handlers/mtr/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const MtrParser = {
rawOutput.push(header.join(' '));

for (const [i, hop] of hops.entries()) {
if (!hop) {
if (!hop || hop.duplicate) {
continue;
}

Expand Down Expand Up @@ -132,12 +132,14 @@ export const MtrParser = {
switch (action) {
case 'h': {
const [host] = value;
const previousHostMatch = hops.find((h: HopType, hIndex: number) => h.host === host && hIndex < Number(index));

if (!host) {
break;
}

entry.host = host;
entry.duplicate = Boolean(previousHostMatch);
break;
}

Expand All @@ -149,6 +151,14 @@ export const MtrParser = {
}

entry.resolvedHost = host;
for (const [hIndex, hop] of hops.entries()) {
if (hop.host !== entry.host || (hop.resolvedHost && hop.resolvedHost !== hop.host)) {
continue;
}

(hops[hIndex]!).resolvedHost = host;
}

break;
}

Expand Down
1 change: 1 addition & 0 deletions src/command/handlers/mtr/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type HopType = {
resolvedHost?: string;
stats: HopStatsType;
times: HopTimesType[];
duplicate?: boolean;
};

export type ResultType = {
Expand Down
12 changes: 10 additions & 2 deletions test/mocks/mtr-success-raw-helper-final.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
}
],
"host": "192.168.0.1",
"duplicate": false,
"resolvedHost": "192.168.0.1"
},
{
Expand Down Expand Up @@ -91,6 +92,7 @@
}
],
"host": "62.252.67.181",
"duplicate": false,
"resolvedHost": "62.252.67.181"
},
{
Expand Down Expand Up @@ -150,6 +152,7 @@
}
],
"host": "62.254.59.130",
"duplicate": false,
"resolvedHost": "62.254.59.130"
},
{
Expand Down Expand Up @@ -182,6 +185,7 @@
}
],
"host": "142.250.160.116",
"duplicate": false,
"resolvedHost": "142.250.160.116"
},
{
Expand Down Expand Up @@ -214,6 +218,7 @@
}
],
"host": "216.239.41.193",
"duplicate": false,
"resolvedHost": "216.239.41.193"
},
{
Expand Down Expand Up @@ -246,6 +251,7 @@
}
],
"host": "142.251.54.27",
"duplicate": false,
"resolvedHost": "142.251.54.27"
},
{
Expand Down Expand Up @@ -278,7 +284,8 @@
}
],
"host": "142.250.179.238",
"resolvedHost": "142.250.179.238"
"duplicate": false,
"resolvedHost": "lhr25s31-in-f14.1e100.net"
},
{
"stats":
Expand All @@ -302,10 +309,11 @@
}
],
"host": "142.250.179.238",
"duplicate": true,
"resolvedHost": "lhr25s31-in-f14.1e100.net"
}
],
"rawOutput": "Host Loss% Drop Rcv Avg StDev Javg \n 1. AS??? _gateway (192.168.0.1) 66.7% 2 1 2.3 0.0 2.3\n 2. AS??? (waiting for reply) \n 3. AS??? 62.252.67.181 (62.252.67.181) 0.0% 0 3 10.3 0.8 6.2\n 4. AS??? (waiting for reply) \n 5. AS??? 62.254.59.130 (62.254.59.130) 0.0% 0 3 11.5 0.5 6.5\n 6. AS??? 142.250.160.116 (142.250.160.116) 0.0% 0 3 11.3 0.3 6.0\n 7. AS??? 216.239.41.193 (216.239.41.193) 0.0% 0 3 15.9 0.6 8.0\n 8. AS??? 142.251.54.27 (142.251.54.27) 0.0% 0 3 13.6 1.5 8.2\n 9. AS??? 142.250.179.238 (142.250.179.238) 0.0% 0 3 11.0 0.7 6.4\n10. AS??? lhr25s31-in-f14.1e100.net (142.250.179.238) 0.0% 0 1 11.4 0.0 11.4\n",
"rawOutput": "Host Loss% Drop Rcv Avg StDev Javg \n 1. AS??? _gateway (192.168.0.1) 66.7% 2 1 2.3 0.0 2.3\n 2. AS??? (waiting for reply) \n 3. AS??? 62.252.67.181 (62.252.67.181) 0.0% 0 3 10.3 0.8 6.2\n 4. AS??? (waiting for reply) \n 5. AS??? 62.254.59.130 (62.254.59.130) 0.0% 0 3 11.5 0.5 6.5\n 6. AS??? 142.250.160.116 (142.250.160.116) 0.0% 0 3 11.3 0.3 6.0\n 7. AS??? 216.239.41.193 (216.239.41.193) 0.0% 0 3 15.9 0.6 8.0\n 8. AS??? 142.251.54.27 (142.251.54.27) 0.0% 0 3 13.6 1.5 8.2\n 9. AS??? lhr25s31-in-f14.1e100.net (142.250.179.238) 0.0% 0 3 11.0 0.7 6.4\n",
"data":
[
"x 0 33000",
Expand Down
Loading

0 comments on commit a8202b4

Please sign in to comment.