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
|
<!DOCTYPE html>
<html>
<head>
<title>adblock Map</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
<style>
html,
body {
height: 100%;
margin: 0;
}
#map {
height: 100%;
width: 100%;
}
.title {
display: block;
text-align: center;
font-weight: bold;
}
.mono {
font-size: small;
font-family: monospace;
white-space: nowrap;
margin: 0.3em !important;
}
.entry {
margin-top: 0.5em !important;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<script>
'use strict';
/* maximum display length of the free form GeoIP fields */
const MAX_IP_LEN = 38;
const MAX_AS_LEN = 38;
const MAX_CITY_LEN = 33;
const MAX_DOMAIN_LEN = 38;
/* maximum number of domains listed in a shared popup */
const MAX_DOMAINS = 5;
/* zoom level applied when the view is centered on a single point */
const POINT_ZOOM = 6;
/* mapData key holding the geo data of the local uplink(s) */
const HOME_KEY = 'homeIP';
const HOME_STYLE = { color: '#378242', opacity: 1.0, fillColor: '#378242', fillOpacity: 0.5, radius: 6 };
const BLOCKED_STYLE = { color: '#C22121', opacity: 1.0, fillColor: '#C22121', fillOpacity: 0.5, radius: 3 };
/* initialize map */
const map = L.map('map', {
zoom: 3,
minZoom: 2,
maxZoom: 18,
center: [50, 10]
});
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
/* feature groups, so getBounds() is available for the initial view */
const blockedLayer = L.featureGroup().addTo(map);
const homeLayer = L.featureGroup().addTo(map);
/* accept a coordinate only if it is a finite number in range */
function toLatLng(geo) {
const lat = Number(geo.lat);
const lon = Number(geo.lon);
if (geo.lat === null || geo.lat === undefined || geo.lat === '' ||
geo.lon === null || geo.lon === undefined || geo.lon === '')
return null;
if (!Number.isFinite(lat) || !Number.isFinite(lon))
return null;
if (lat < -90 || lat > 90 || lon < -180 || lon > 180)
return null;
return [lat, lon];
}
/* GeoIP lookups regularly return empty city/as fields, keep the entry anyway */
function fieldText(value, maxLength) {
const text = String(value === null || value === undefined ? '' : value).trim();
if (!text)
return 'n/a';
return text.length > maxLength ? text.slice(0, maxLength - 1) + '\u2026' : text;
}
function plural(count, noun) {
return count + ' ' + noun + (count === 1 ? '' : 's');
}
/*
* Popups are built as DOM nodes, not as HTML strings, so neither the blocked
* domain nor the third party GeoIP fields can be interpreted as markup.
* Passing a function to bindPopup() defers this until the popup is opened.
*/
function buildPopup(title, blocks) {
const content = document.createElement('div');
const heading = document.createElement('span');
heading.className = 'title';
heading.textContent = title;
content.appendChild(heading);
blocks.forEach(function (lines, index) {
const body = document.createElement('p');
body.className = index ? 'mono entry' : 'mono';
lines.forEach(function (line, pos) {
if (pos)
body.appendChild(document.createElement('br'));
body.appendChild(document.createTextNode(line));
});
content.appendChild(body);
});
return content;
}
function locationLines(geo, latlng) {
return [
'City: ' + fieldText(geo.city, MAX_CITY_LEN) + ' (' + fieldText(geo.countryCode, 8) + ')',
'Latitude : ' + latlng[0],
'Longitude: ' + latlng[1]
];
}
function homePopup(entry) {
const geo = entry.items[0].geo;
const more = entry.items.length > 1 ? ' (+' + (entry.items.length - 1) + ' more)' : '';
return buildPopup('local IP', [locationLines(geo, entry.latlng).concat([
'IP: ' + fieldText(geo.query, MAX_IP_LEN) + more,
'AS: ' + fieldText(geo.as, MAX_AS_LEN)
])]);
}
/*
* Blocked ad domains cluster heavily on a few CDN endpoints, so one coordinate
* regularly carries several of them. List them instead of keeping the first one
* and dropping the rest.
*/
function blockedPopup(entry) {
const items = entry.items;
const geo = items[0].geo;
const blocks = [locationLines(geo, entry.latlng)];
if (items.length === 1)
return buildPopup(items[0].name, [blocks[0].concat([
'IP: ' + fieldText(geo.query, MAX_IP_LEN),
'AS: ' + fieldText(geo.as, MAX_AS_LEN)
])]);
items.slice(0, MAX_DOMAINS).forEach(function (item) {
blocks.push([
fieldText(item.name, MAX_DOMAIN_LEN),
fieldText(item.geo.query, MAX_IP_LEN),
fieldText(item.geo.as, MAX_AS_LEN)
]);
});
if (items.length > MAX_DOMAINS)
blocks.push(['(+' + (items.length - MAX_DOMAINS) + ' more)']);
return buildPopup(plural(items.length, 'blocked domain'), blocks);
}
function addMarker(layer, entry, style, popupBuilder) {
L.circleMarker(entry.latlng, style)
.bindPopup(function () {
return popupBuilder(entry);
})
.addTo(layer);
}
/* markers sharing a coordinate become one marker carrying every entry */
function addEntry(list, index, name, geo, latlng) {
const key = latlng[0] + ',' + latlng[1];
const known = index.get(key);
if (known) {
known.items.push({ name: name, geo: geo });
return;
}
const entry = { latlng: latlng, items: [{ name: name, geo: geo }] };
index.set(key, entry);
list.push(entry);
}
/*
* mapData is an array of single key objects, the key being either homeIP or
* a blocked domain:
*
* [ {}, { "homeIP": {...} }, { "ads.example.com": {...} }, ... ]
*/
function collectEntries(parsedData) {
const result = { home: [], blocked: [] };
const homeIndex = new Map();
const blockedIndex = new Map();
parsedData.forEach(function (item) {
if (!item || typeof item !== 'object')
return;
Object.keys(item).forEach(function (name) {
const geo = item[name];
if (!geo || typeof geo !== 'object')
return;
const latlng = toLatLng(geo);
if (!latlng)
return;
if (name === HOME_KEY)
addEntry(result.home, homeIndex, name, geo, latlng);
else
addEntry(result.blocked, blockedIndex, name, geo, latlng);
});
});
return result;
}
function fitLayer(layer) {
const layers = layer.getLayers();
if (!layers.length)
return false;
if (layers.length === 1)
map.setView(layers[0].getLatLng(), POINT_ZOOM);
else
map.fitBounds(layer.getBounds(), { maxZoom: POINT_ZOOM, padding: [30, 30] });
return true;
}
/* load the data handed over by the adblock report page */
const mapData = sessionStorage.getItem('mapData');
let parsedData = null;
if (mapData) {
try {
parsedData = JSON.parse(mapData);
} catch (error) {
console.error('adblock: unable to parse mapData,', error);
}
}
if (Array.isArray(parsedData)) {
const entries = collectEntries(parsedData);
/* blocked first, so the local uplink stays on top and clickable */
entries.blocked.forEach(function (entry) {
addMarker(blockedLayer, entry, BLOCKED_STYLE, blockedPopup);
});
entries.home.forEach(function (entry) {
addMarker(homeLayer, entry, HOME_STYLE, homePopup);
});
/* center on the local uplink(s), fall back to the blocked domains */
if (!fitLayer(homeLayer))
fitLayer(blockedLayer);
}
</script>
</body>
</html>
|