forked from stefansundin/traceroute-mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
329 lines (303 loc) · 11.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Traceroute mapper</title>
<meta charset="utf-8">
<meta name="author" content="Stefan Sundin, Pascal Syma">
<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 rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/leaflet.extra-markers.min.css" integrity="sha256-7Zr0s0MoLVy6pFTRq2DKuki8xw1OzRYY1Q3S2pTDtvo=" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/leaflet.extra-markers.min.js" integrity="sha256-nm8fKiOVLndTf7KD5YEPuMSb0kv9IH7fDyZ+zlF8s/0=" 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;
}
header a:hover {
color: black;
cursor: default;
text-decoration: none;
}
header a:focus {
color: black;
text-decoration: none;
}
h2 {
border-bottom: 1px solid #ccc;
}
textarea {
font-family: monospace;
min-height: 200px;
resize: vertical;
}
#map {
height: 500px;
margin: 1em 0;
}
</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 ..."></textarea></p>
<p><button class="form-control btn btn-primary" id="submit">Map it!</button></p>
<div class="btn-group" role="group" aria-label="Map Buttons">
<button type="button" class="btn btn-danger" id="clear-map">Clear map</button>
<a type="button" class="btn btn-primary" id="permalink">Permalink</a>
</div>
<div id="map"></div>
<h2>Tips</h2>
<p>Linux: <code>traceroute -q1 google.com | xclip -sel clip</code></p>
<p>Mac: <code>traceroute -q1 google.com | pbcopy</code></p>
<p>Windows: <code>tracert google.com | clip</code></p>
<p><a href="https://github.com/passidel/traceroute-mapper#traceroute-mapper">Open from your terminal!</a></p>
<hr>
<ul>
<li>Made by <a href="https://stefansundin.github.io/" target="_blank">Stefan Sundin</a>, modified by <a href="https://syma.dev" target="_blank">Pascal Syma</a>.</li>
<li>View <a href="https://github.com/passidel/traceroute-mapper" target="_blank">source code</a>.</li>
<li>Uses <a href="https://ipinfo.io/" target="_blank">ipinfo.io</a> and <s>Google Maps API</s> <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a>.</li>
</ul>
</div>
<script>
let map = L.map('map').setView([35, 0], 2)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
let markers = [];
// store location lookups in a database to prevent duplicate lookups
let db = {};
function draw(hosts) {
hosts = hosts.filter(host => host.ip in db);
const latlngs = hosts.map(host => db[host.ip].loc)
// draw markers
for (let i=0; i < hosts.length; i++) {
const host = hosts[i];
const latlng = latlngs[i];
const data = db[host.ip]
const numMarker = L.ExtraMarkers.icon({
icon: 'fa-number',
number: host.n,
markerColor: 'red'
});
const marker = L.marker(latlng, {
title: `${host.n}: ${host.ip}`,
icon: numMarker
}).bindPopup(`<b># ${host.n}</b><br><table><tr>` + Object.keys(data).map(key => `<th>${key}</th><td>${data[key]}</td>`).join("</tr><tr>")+ '</tr></table>').addTo(map).openPopup();
markers.push(marker);
}
map.fitBounds(latlngs)
// draw lines
markers.push(L.polyline(latlngs, {
color: '#ff0000',
weight: 2
}).addTo(map))
}
$("#submit").click(function() {
const text = $("#trace").val();
$("#permalink").attr("href", "?trace="+encodeURIComponent(text));
let hosts = [];
let ips = [];
text.split("\n").forEach(function(line) {
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]);
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);
return;
}
else if (ip.startsWith("172.")) {
const byte = parseInt(ip.split(".")[1], 10);
if (byte >= 16 && byte < 32) {
console.warn("Skipping private IP", ip);
return;
}
}
// 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) {
$(this).attr("class","form-control btn btn-danger").text("Could not find any IP addresses in the input.");
return;
}
let requestBody = hosts.filter(host => !(host.ip in db)).map(host => host.ip);
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 !== "passidel.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).attr("class","form-control btn btn-danger").text("Please sign up and get an API token from ipinfo.io.");
return;
}
$(this).attr("class","form-control btn btn-info").text("Working...");
const url = `https://ipinfo.io/batch?token=f0e5d842881cd6`;
fetch(url, {
method: "POST",
headers: new Headers({
"Content-Type": "application/json",
}),
body: JSON.stringify(requestBody),
}).then(response => {
if (response.ok) {
return response.json();
}
console.error(response);
throw new Error(response.status);
}).then(data => {
$("#submit").attr("class","form-control btn btn-primary").text("Map it!");
Object.entries(data).forEach(entry => {
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
$("#submit").attr("class","form-control btn btn-danger").text(
(entry[1].error.title === "Rate limit exceeded") ? "ipinfo.io ratelimit exhausted. Please try again tomorrow." : `Error: ${entry[1].error.title}`
);
return;
}
const ip = entry[0].split("/")[0];
const loc = entry[1].loc.trim();
if (loc === "undefined") {
db[ip] = loc;
return;
}
const ll = loc.split(",").map(parseFloat);
entry[1].loc = ll
console.log(ip, ll);
db[ip] = entry[1];
});
draw(hosts);
}).catch(error => {
console.error(error);
let err = (error.message === "Failed to fetch" || error.message === "NetworkError when attempting to fetch resource.") ? "Request not sent to ipinfo.io. Are you using an adblocker?" : "An error occurred.";
$("#submit").attr("class","form-control btn btn-danger").text(err);
});
});
$("#clear-map").click(function() {
markers.forEach(function(marker) {
marker.removeFrom(map);
});
markers = [];
});
$("#permalink").click(function() {
if (this.href === "") {
alert("Please map a trace first.");
}
});
const qs = window.location.search;
if (qs[0] === "?") {
let params = {};
qs.substr(1).split("&").forEach(function(param) {
param = param.split("=");
params[param[0]] = decodeURIComponent(param[1]);
});
if (params["trace"]) {
$("#trace").val(params["trace"].trim());
$("#submit").click();
setTimeout(function() {
if (markers.length > 0) {
$("#map")[0].scrollIntoView();
}
}, 200);
}
}
$("#example").click(function() {
$("#trace").val("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");
$("#submit").click();
});
</script>
</body>
</html>