-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.html
418 lines (388 loc) · 14.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<!DOCTYPE html>
<html>
<head>
<title>Traceroute mapper</title>
<meta charset="utf-8">
<meta name="author" content="Stefan Sundin">
<meta name="description" content="Visualize traceroute on a map.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="referrer" content="origin">
<link rel="icon" href="img/icon32.png">
<link rel="license" href="https://www.gnu.org/licenses/gpl-3.0.html" title="GNU GPL v3 or later">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/ol.css" rel="stylesheet" integrity="sha384-Jpg1uvTciFJuHq+90Ly0/2SKRT9yS5OhbIN5FuFoFWji9fc44ok8xxO88CJi8Z5W" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.js" integrity="sha384-6+k98W9QpUIYlAQOPoPmvR2RCgoF5ng+7QrmqD8j1riKr/6Yn5hOagf2b6K1ko8D" crossorigin="anonymous"></script>
<style>
body {
padding: 0 15px;
margin-bottom: 100px;
}
a {
cursor: pointer;
}
.container {
padding: 0;
}
@media (min-width: 768px) {
.container {
max-width: 770px;
}
}
#example {
color: #337ab7;
}
#example:hover {
color: #23527c;
text-decoration: underline;
}
header {
margin-top: 25px;
text-align: center;
}
header a {
color: black;
text-decoration: none;
}
h2 {
border-bottom: 1px solid #ccc;
}
#trace {
font-family: monospace;
min-height: 200px;
resize: vertical;
}
#map {
height: 500px;
margin: 1em 0;
}
#map-controls {
top: .5em;
right: .5em;
}
#map-controls .map-btn {
width: auto;
padding: 0 0.75rem;
margin: 6px 4px;
}
#map-controls a {
text-decoration: none;
}
</style>
</head>
<body>
<header>
<h1 itemprop="name"><a href="/traceroute-mapper/">Traceroute mapper</a></h1>
</header>
<div class="container">
<hr>
<p>Run <code>traceroute</code> on your local machine, then paste the output here to map the route. <a id="example">Show example.</a></p>
<p>Thanks to <a href="https://ipinfo.io/">ipinfo.io</a> for supporting this project.</p>
<p><textarea class="form-control" id="trace" placeholder="traceroute to ..." spellcheck="false"></textarea></p>
<p><button class="form-control btn btn-primary" id="submit">Map it!</button></p>
<div id="map"></div>
<h2>Tips</h2>
<p>Linux: <code>traceroute -I -q1 example.com</code></p>
<p>Mac: <code>traceroute -I -q1 example.com</code></p>
<p>Windows: <code>tracert example.com</code></p>
<p><a href="https://github.com/stefansundin/traceroute-mapper#traceroute-mapper">Open from your terminal!</a></p>
<hr>
<ul>
<li>Made by <a href="https://stefansundin.github.io/">Stefan Sundin</a>.</li>
<li>View <a href="https://github.com/stefansundin/traceroute-mapper">source code</a>.</li>
<li>Uses <a href="https://ipinfo.io/">ipinfo.io</a> and <a href="https://openlayers.org/">OpenLayers</a>.</li>
</ul>
</div>
<div class="d-none">
<div class="ol-unselectable ol-control" id="map-controls">
<button class="map-btn" id="clear-map">Clear map</button>
<a id="permalink"><button class="map-btn">Permalink</button></a>
</div>
</div>
<script>
const mapElement = document.getElementById("map");
const mapControlsElement = document.getElementById("map-controls");
const submitButton = document.getElementById("submit");
const traceInput = document.getElementById("trace");
const clearMapButton = document.getElementById("clear-map");
const permalink = document.getElementById("permalink");
const exampleLink = document.getElementById("example");
const map = new ol.Map({
target: mapElement,
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([0, 35]),
zoom: 2,
})
});
map.addControl(new ol.control.Control({
element: mapControlsElement,
}));
let markers = [];
// store location lookups in a database to avoid repeated lookups
let db = {};
function draw(hosts) {
hosts = hosts.filter(host => host.ip in db);
// draw markers
const bounds = [];
for (let i=0; i < hosts.length; i++) {
const host = hosts[i];
const coords = ol.proj.fromLonLat(db[host.ip]);
bounds.push(coords);
const feature = new ol.Feature({
geometry: new ol.geom.Point(coords),
});
// Render a custom marker icon that look similar to a Google Maps pin
feature.setStyle([
new ol.style.Style({
renderer(coordinates, state) {
const [x, y] = coordinates;
const ctx = state.context;
// Curves taken from https://commons.wikimedia.org/wiki/File:Googlemap.svg
ctx.beginPath();
ctx.strokeStyle = 'rgb(0, 0, 0)';
ctx.fillStyle = 'rgb(232, 30, 37)';
ctx.moveTo(x, y);
ctx.bezierCurveTo(x+0.814645, y-3.999069, x+2.25098, y-7.326002, x+3.990706, y-10.411394);
ctx.bezierCurveTo(x+5.281138, y-12.699281, x+6.776043, y-14.811039, x+8.159216, y-17.02964);
ctx.bezierCurveTo(x+8.620945, y-17.770272, x+9.019418, y-18.552702, x+9.463097, y-19.321349);
ctx.bezierCurveTo(x+10.350242, y-20.858281, x+11.069525, y-22.640264, x+11.02382, y-24.951791);
ctx.bezierCurveTo(x+10.979164, y-27.210274, x+10.325971, y-29.02195, x+9.384063, y-30.503216);
ctx.bezierCurveTo(x+7.834899, y-32.939524, x+5.239993, y-34.937033, x+1.758259, y-35.462034);
ctx.bezierCurveTo(x-1.088484, y-35.891162, x-3.757507, y-35.166035, x-5.650237, y-34.059288);
ctx.bezierCurveTo(x-7.196902, y-33.154871, x-8.394636, y-31.946771, x-9.305067, y-30.522978);
ctx.bezierCurveTo(x-10.255342, y-29.036923, x-10.909707, y-27.281294, x-10.964597, y-24.991306);
ctx.bezierCurveTo(x-10.992707, y-23.818068, x-10.800581, y-22.731583, x-10.529938, y-21.830359);
ctx.bezierCurveTo(x-10.256035, y-20.918141, x-9.815529, y-20.155613, x-9.423611, y-19.341112);
ctx.bezierCurveTo(x-8.658558, y-17.75118, x-7.6994, y-16.294397, x-6.736807, y-14.836751);
ctx.bezierCurveTo(x-3.869663, y-10.495044, x-1.178574, y-6.067215, x, y);
ctx.closePath();
ctx.fill();
ctx.stroke();
// Add text
ctx.fillStyle = 'rgba(0, 0, 0)';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = '12px sans-serif';
ctx.fillText(host.n, x, y-23);
},
}),
]);
const marker = new ol.layer.Vector({
zIndex: host.n,
source: new ol.source.Vector({
features: [
feature
],
}),
});
map.addLayer(marker);
markers.push(marker);
}
// reposition map view
if (bounds.length > 0) {
const extent = ol.extent.boundingExtent(bounds);
// setTimeout is used since otherwise the animation blocks rendering of the markers and lines
setTimeout(() => {
map.getView().fit(extent, {
padding: [15, 15, 15, 15],
duration: 500,
});
});
}
// draw lines
for (let i=1; i < hosts.length; i++) {
const host1 = hosts[i-1];
const host2 = hosts[i];
// console.log("drawing line between", [ db[host1.ip], db[host2.ip] ]);
const line = new ol.layer.Vector({
source: new ol.source.Vector({
features: [
new ol.Feature({
geometry: new ol.geom.LineString([
ol.proj.fromLonLat(db[host1.ip]),
ol.proj.fromLonLat(db[host2.ip]),
])
})
]
}),
});
map.addLayer(line);
markers.push(line);
}
}
submitButton.addEventListener("click", async function() {
const text = traceInput.value;
permalink.href = "?trace="+encodeURIComponent(text);
const hosts = [];
const ips = [];
for (const line of text.split("\n")) {
let r1, r2;
if (
(r1=/^ *(\d+) /.exec(line)) !== null && (
(r2=/\(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\)/.exec(line)) !== null ||
(r2=/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/.exec(line)) !== null ||
(r2=/\((?:[0-9a-f]{1,4}::?){1,7}[0-9a-f]{1,4}\)/.exec(line)) !== null ||
(r2=/\b(?:[0-9a-f]{1,4}::?){1,7}[0-9a-f]{1,4}\b/.exec(line)) !== null
)
) {
const n = parseInt(r1[1], 10);
let ip = r2[0];
if (ip[0] === "(") {
ip = ip.substring(1, r2[0].length-1);
}
// Filter out RFC1918 private IP ranges
// ipinfo returns an "Invalid Token" error on these
// Maybe some IPv6 addresses should also be filtered?
if (ip.startsWith("10.") || ip.startsWith("192.168.")) {
console.warn("Skipping private IP", ip);
continue;
}
else if (ip.startsWith("172.")) {
const byte = parseInt(ip.split(".")[1], 10);
if (byte >= 16 && byte < 32) {
console.warn("Skipping private IP", ip);
continue;
}
}
// Add IP to hosts if not already present
if (!ips.includes(ip)) {
ips.push(ip);
hosts.push({ n: n, ip: ip });
}
}
}
if (hosts.length === 0) {
submitButton.classList.remove("btn-info", "btn-primary");
submitButton.classList.add("btn-danger");
submitButton.textContent = "Could not find any IP addresses in the input";
return;
}
let requestBody = hosts.filter(host => !(host.ip in db)).map(host => `${host.ip}/loc`);
if (requestBody.length > 100) {
console.warn("Max 100 IPs in a batch request. TODO: Make multiple batch requests. For now you can just click the button multiple times, and then clear the map and click it a final time.");
requestBody = requestBody.slice(0, 100);
}
if (requestBody.length === 0) {
draw(hosts);
return;
}
if (window.location.host !== "stefansundin.github.io") {
// please don't use my token on your fork!
// requests made without a token are limited based on domain name or source IP address, but will get less than 1000 requests/day (can't find good documentation on this)
// please get your own token by creating a free account at https://ipinfo.io/
// free accounts can make up to 1000 requests/day. note that batch requests still count every IP as a request.
// if you revert to an older version that doesn't use the batch API, then it seems like you can make an unlimited number of requests from file:// URLs
// try https://github.com/stefansundin/traceroute-mapper/tree/6e96144e61f01409157b2f4d7e4fbbfa4ac74d2f
this.classList.remove("btn-info");
this.classList.add("btn-danger");
this.textContent = "Please sign up and get an API token from ipinfo.io";
return;
}
submitButton.classList.remove("btn-danger");
submitButton.classList.add("btn-info");
submitButton.textContent = "Working...";
try {
const url = `https://ipinfo.io/batch?token=5886e32e75537e`;
const response = await fetch(url, {
method: "POST",
headers: new Headers({
"Content-Type": "application/json",
}),
body: JSON.stringify(requestBody),
});
if (!response.ok) {
console.error(response);
throw new Error(response.status);
}
const data = await response.json();
submitButton.classList.remove("btn-info");
submitButton.classList.add("btn-primary");
submitButton.textContent = "Map it!";
for (const entry of Object.entries(data)) {
if (entry[1].error) {
console.error(entry[0], entry[1].error);
// Do not report some errors in the button text
if (entry[1].error.title === "Invalid Token") {
return;
}
// Report error in the button text
submitButton.classList.remove("btn-primary");
submitButton.classList.add("btn-danger");
submitButton.textContent = (entry[1].error.title === "Rate limit exceeded") ? "ipinfo.io ratelimit exhausted. Please try again tomorrow." : `Error: ${entry[1].error.title}`;
continue;
}
const ip = entry[0].split("/")[0];
const loc = entry[1].trim();
if (loc === "undefined") {
db[ip] = loc;
return;
}
const ll = loc.split(",").map(parseFloat);
console.log(ip, ll);
db[ip] = [ll[1], ll[0]];
}
draw(hosts);
} catch (err) {
console.error(err);
const errorMessage = (err.message === "Failed to fetch" || err.message === "NetworkError when attempting to fetch resource.") ? `Request not sent to ipinfo.io. Are you using an adblocker?${navigator.userAgent.includes("Firefox/") ? " Or Firefox's Enhanced Tracking Protection?" : ''}` : "An error occurred (see browser console)";
submitButton.classList.remove("btn-primary");
submitButton.classList.add("btn-danger");
submitButton.textContent = errorMessage;
}
});
clearMapButton.addEventListener("click", function() {
markers.forEach((marker) => {
map.removeLayer(marker);
});
markers = [];
});
permalink.addEventListener("click", function() {
if (this.href === "") {
alert("Please map a trace first.");
}
});
const qs = window.location.search;
if (qs[0] === "?") {
const params = {};
qs.substr(1).split("&").forEach((param) => {
const [key, value] = param.split("=");
params[key] = decodeURIComponent(value);
});
if ('trace' in params) {
traceInput.value = params.trace.trim();
submitButton.click();
}
}
exampleLink.addEventListener("click", function() {
traceInput.value = "traceroute to china.cn (202.130.245.42), 30 hops max, 60 byte packets\n\
1 lc207-gw.campus.ltu.se (130.240.207.1) 0.219 ms\n\
2 gw22.pub.ltu.se (130.240.22.129) 0.974 ms\n\
3 sunetgreen.net.ltu.se (130.240.22.78) 1.336 ms\n\
4 ltu-br1.sunet.se (193.11.0.48) 1.302 ms\n\
5 ltu-br2-xe-0-0-3.sunet.se (130.242.85.94) 1.202 ms\n\
6 z3lla-xe-2-0-0.sunet.se (130.242.85.89) 0.542 ms\n\
7 se-lla.nordu.net (109.105.102.93) 0.574 ms\n\
8 se-fre.nordu.net (109.105.97.113) 13.105 ms\n\
9 s-b3-link.telia.net (213.248.97.17) 16.265 ms\n\
10 s-bb4-link.telia.net (80.91.253.226) 13.546 ms\n\
11 kbn-bb3-link.telia.net (62.115.139.167) 22.249 ms\n\
12 nyk-bb1-link.telia.net (62.115.141.99) 115.487 ms\n\
13 ash-bb4-link.telia.net (213.155.133.9) 126.166 ms\n\
14 las-b3-link.telia.net (213.155.137.59) 194.745 ms\n\
15 219.158.102.81 (219.158.102.81) 369.420 ms\n\
16 219.158.102.81 (219.158.102.81) 385.420 ms\n\
17 219.158.98.145 (219.158.98.145) 387.710 ms\n\
18 124.65.194.82 (124.65.194.82) 368.584 ms\n\
19 61.148.143.18 (61.148.143.18) 368.667 ms\n\
20 61.148.143.18 (61.148.143.18) 378.916 ms\n\
21 210.74.176.238 (210.74.176.238) 424.108 ms\n\
22 210.74.176.238 (210.74.176.238) 428.721 ms";
submitButton.click();
});
</script>
</body>
</html>