-
Notifications
You must be signed in to change notification settings - Fork 0
/
onionmap.js
119 lines (98 loc) · 4.66 KB
/
onionmap.js
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
/** Utility **/
var fetchJSONFile = function(path, callback, errorCallback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200 || httpRequest.status === 304) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
else {if (errorCallback) errorCallback(httpRequest);}
}
};
httpRequest.open('GET', path);
httpRequest.setRequestHeader('accept-encoding','gzip');
httpRequest.send();
}
var percentage = function(number) {
if (typeof number === 'undefined') return "N/A";
if (number == 0) return "0";
return (number * 100).toFixed(4) + " %";
}
// Create map
var map = L.map('map').setView([27, 18], 2);
// Add background map layer
L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
attribution: 'Map tiles by<a href="http://cartodb.com/attributions#basemaps">CartoDB</a>, under <a href="https://creativecommons.org/licenses/by/3.0/">CC BY 3.0</a>. Data by <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, under ODbL.',
minZoom: 1,
maxZoom: 10
}).addTo(map);
// Control scale
L.control.scale({
position: 'bottomleft',
metric: true,
imperial: false,
updateWhenIdle: true
}).addTo(map);
// Set map on loading state
map.getContainer().classList.add('loading');
// Some config
L.Icon.Default.imagePath = 'dist/css/images';
// Get relay list from onionoo
// Real url is https://onionoo.torproject.org/details?fields=nickname,fingerprint,latitude,longitude,consensus_weight_fraction,guard_probability,middle_probability,exit_probability,dir_address,country,country_name,as_number,as_name
var jsonPath = "https://onionoo.torproject.org/details?fields=nickname,fingerprint,latitude,longitude,or_addresses,consensus_weight_fraction,guard_probability,middle_probability,exit_probability,dir_address,country,country_name,as_number,as_name";
// Additionnal Onionoo parameters, as query string
// See https://onionoo.torproject.org/protocol.html#methods
if (location.search) {
jsonPath += "&" + location.search.substring(1);
}
// var jsonPath = 'onionoo.json';
fetchJSONFile(jsonPath, function(data) {
// Remove loading state
map.getContainer().classList.remove('loading');
// Update information panel
(document.getElementById('relay-count')) ? document.getElementById('relay-count').innerHTML = data.relays.length : null;
// New feature group
relaysMarkers = L.featureGroup();
// Loop through relays
data.relays.forEach(function(relay, idx){
if (relay.latitude && relay.longitude) {
var marker = L.marker([relay.latitude, relay.longitude])
.bindPopup(
"<h3>"+relay.nickname+"</h3>"+
"<p><a href='https://atlas.torproject.org/#details/"+relay.fingerprint+"' title='View on Atlas'>"+relay.fingerprint+"</a></p>"+
"<p>" +
"IP: " + relay.or_addresses[0] +"<br />"+
"Country: "+relay.country_name+" ("+relay.country+")<br />"+
"AS Number: "+relay.as_number+"<br />"+
"AS Name: "+relay.as_name+"<br />"+
"Consensus weight fraction: "+ percentage(relay.consensus_weight_fraction) +"<br />"+
"Guard probability: "+ percentage(relay.guard_probability) +"<br />"+
"Middle probability: "+ percentage(relay.middle_probability) +"<br />"+
"Exit probability: "+percentage(relay.exit_probability) +"<br />" +
"</p>"
);
// Add relay to feature group
relaysMarkers.addLayer(marker);
}
});
// Marker cluster group
var relaysCluster = new L.MarkerClusterGroup({
showCoverageOnHover: true,
polygonOptions: {
stroke: false,
opacity: 0.2,
fillColor: '#885CA4',
fillOpacity: 0.1
},
maxClusterRadius: 60,
});
// Add relay markers to cluster
relaysCluster.addLayer(relaysMarkers);
// Add cluster to map
map.addLayer(relaysCluster);
}, function(httpRequest) {
// Error handling
alert("An error occurded");
console.log(httpRequest);
});