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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
<!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 {
color-scheme: light dark;
--adb-water: #d6e3ec;
--adb-land: #f4f1ea;
--adb-coast: #a9a396;
--adb-grid: #c2d2de;
--adb-label: #97a3ad;
}
@media (prefers-color-scheme: dark) {
html {
--adb-water: #151b21;
--adb-land: #2a3138;
--adb-coast: #4d5862;
--adb-grid: #232b33;
--adb-label: #5d6a75;
}
}
html,
body {
height: 100%;
margin: 0;
}
#map {
height: 100%;
width: 100%;
}
.leaflet-container {
background: var(--adb-water);
}
/* Leaflet positions the icon element itself, so center on the inner span */
.continent span {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
font-size: 11px;
font-weight: 600;
letter-spacing: 0.18em;
text-transform: uppercase;
color: var(--adb-label);
}
.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 of the initial view */
const OVERVIEW_ZOOM = 3;
/* continent labels are dropped as soon as the outlines carry the orientation */
const LABEL_ZOOM = 4;
/* The outlines are shipped without attributes, so the labels are placed here */
const CONTINENTS = [
{ name: 'North America', latlng: [48, -100] },
{ name: 'South America', latlng: [-13, -58] },
{ name: 'Europe', latlng: [56, 26] },
{ name: 'Africa', latlng: [4, 20] },
{ name: 'Asia', latlng: [46, 92] },
{ name: 'Oceania', latlng: [-25, 137] },
{ name: 'Antarctica', latlng: [-72, 15] }
];
/* country outlines shipped with the package, Natural Earth 1:110m, public domain */
const WORLD_FILE = 'world.json';
/* optional higher detail outlines, not shipped, see the adblock README */
const WORLD_FILE_HD = 'world-50m.json';
/* spacing of the graticule in degrees */
const GRID_STEP = 20;
/* 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: 6,
center: [50, 10]
});
map.attributionControl.addAttribution('<a href="https://www.naturalearthdata.com">Natural Earth</a>');
/* the basemap is drawn from local country outlines instead of a tile service */
map.createPane('basemap');
map.getPane('basemap').style.zIndex = 200;
const basemapRenderer = L.canvas({ pane: 'basemap' });
function cssColor(name) {
return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
}
function landStyle() {
return { color: cssColor('--adb-coast'), weight: 0.7, opacity: 1.0, fillColor: cssColor('--adb-land'), fillOpacity: 1.0 };
}
function gridStyle() {
return { color: cssColor('--adb-grid'), weight: 0.6, opacity: 1.0 };
}
/* the empty ocean is hard to read at low zoom levels, so add a plain graticule */
function graticule() {
const lines = [];
let value;
for (value = -80; value <= 80; value += GRID_STEP)
lines.push([[value, -180], [value, 180]]);
for (value = -180; value <= 180; value += GRID_STEP)
lines.push([[-85, value], [85, value]]);
return lines;
}
const gridLayer = L.polyline(graticule(), Object.assign({
pane: 'basemap',
renderer: basemapRenderer,
interactive: false
}, gridStyle())).addTo(map);
let landLayer = null;
const labelLayer = L.layerGroup(CONTINENTS.map(function (item) {
return L.marker(item.latlng, {
pane: 'basemap',
interactive: false,
keyboard: false,
icon: L.divIcon({
className: 'continent',
html: '<span>' + item.name + '</span>',
iconSize: [0, 0]
})
});
}));
function updateLabels() {
const wanted = map.getZoom() <= LABEL_ZOOM;
if (wanted !== map.hasLayer(labelLayer))
wanted ? labelLayer.addTo(map) : labelLayer.remove();
}
map.on('zoomend', updateLabels);
updateLabels();
function loadWorld(file) {
return fetch(file).then(function (response) {
if (!response.ok)
throw new Error(file + ': HTTP ' + response.status);
return response.json();
});
}
/* prefer the optional outlines, their absence is the normal case */
loadWorld(WORLD_FILE_HD)
.catch(function () {
return loadWorld(WORLD_FILE);
})
.then(function (world) {
landLayer = L.geoJSON(world, {
pane: 'basemap',
renderer: basemapRenderer,
interactive: false,
smoothFactor: 1.2,
style: landStyle
}).addTo(map);
})
.catch(function (error) {
/* the markers stay usable on the plain background */
console.error('adblock: unable to load the basemap,', error);
});
/* restyle in place when the browser switches between the light and dark scheme */
const colorScheme = window.matchMedia('(prefers-color-scheme: dark)');
if (colorScheme.addEventListener)
colorScheme.addEventListener('change', function () {
gridLayer.setStyle(gridStyle());
if (landLayer)
landLayer.setStyle(landStyle());
});
/* 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;
}
/* 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);
});
/* The overall view stays zoomed out, center on the local uplink(s) */
const home = homeLayer.getLayers();
if (home.length)
map.setView(home[0].getLatLng(), OVERVIEW_ZOOM);
else if (blockedLayer.getLayers().length)
map.setView(blockedLayer.getBounds().getCenter(), OVERVIEW_ZOOM);
}
</script>
</body>
</html>
|