// Ruft je Website einmal die Startseite ab: folgt Weiterleitungen, erkennt geparkte Domains und prüft, // ob die Seite die Gemeinde nennt. Beachtet robots.txt, höchstens 2 gleichzeitige Abrufe je IP. // Es wird kein Seiteninhalt gespeichert, nur Titel und abgeleitete Merkmale. // Aufruf: node 06-startseiten.mjs alle Hosts aus der Messung // node 06-startseiten.mjs --test 40 Stichprobe, eigene Ergebnisdatei import { createHash } from 'node:crypto'; import dnsCb from 'node:dns'; import { appendFileSync, existsSync, readFileSync } from 'node:fs'; import http from 'node:http'; import https from 'node:https'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import zlib from 'node:zlib'; const hier = dirname(fileURLToPath(import.meta.url)); const testIdx = process.argv.indexOf('--test'); const test = testIdx > -1 ? Number(process.argv[testIdx + 1] || 40) : 0; const ausgabe = join(hier, 'daten', test ? 'startseiten-test.jsonl' : 'startseiten.jsonl'); const UA = 'Mozilla/5.0 (compatible; municipal-hosting-study/1.0)'; const MAX_BYTES = 512 * 1024; const GLEICHZEITIG = 16; const JE_IP = 2; const ZEITLIMIT_MS = 15000; setTimeout(() => { console.error('Gesamtzeit erreicht, Lauf ist fortsetzbar'); process.exit(3); }, 120 * 60 * 1000).unref(); const jsonl = (d) => readFileSync(join(hier, 'daten', d), 'utf8').trim().split('\n').filter(Boolean).map((z) => JSON.parse(z)); function privat(ip) { const a = ip.toLowerCase().replace(/^::ffff:/, ''); if (a.includes(':')) return a === '::1' || a === '::' || /^f[cd]/.test(a) || /^fe[89ab]/.test(a); const [x, y] = a.split('.').map(Number); return x === 0 || x === 10 || x === 127 || (x === 169 && y === 254) || (x === 172 && y >= 16 && y <= 31) || (x === 192 && y === 168) || (x === 100 && y >= 64 && y <= 127) || x >= 224; } function sichereSuche(host, opts, cb) { const optionen = typeof opts === 'object' ? opts : {}; dnsCb.lookup(host, { all: true }, (fehler, adressen) => { if (fehler) return cb(fehler); const gut = adressen.filter((a) => !privat(a.address)); if (!gut.length) return cb(Object.assign(new Error('PRIVATE_IP'), { code: 'PRIVATE_IP' })); if (optionen.all) return cb(null, gut); return cb(null, gut[0].address, gut[0].family); }); } function hole(url, grenze = MAX_BYTES) { return new Promise((fertig) => { let u; try { u = new URL(url); } catch { return fertig({ fehler: 'URL_UNGUELTIG' }); } if (!/^https?:$/.test(u.protocol)) return fertig({ fehler: 'PROTOKOLL' }); const modul = u.protocol === 'https:' ? https : http; let ende = false; let tls = null; const schluss = (w) => { if (!ende) { ende = true; clearTimeout(uhr); fertig({ ...w, tls }); } }; const anfrage = modul.request(u, { method: 'GET', lookup: sichereSuche, rejectUnauthorized: false, agent: false, headers: { 'User-Agent': UA, Accept: 'text/html,*/*;q=0.5', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'de', Connection: 'close' }, }, (antwort) => { if (antwort.socket?.encrypted) tls = { ok: antwort.socket.authorized === true, fehler: antwort.socket.authorizationError ? String(antwort.socket.authorizationError) : null }; const kopf = antwort.headers; if (antwort.statusCode >= 300 && antwort.statusCode < 400) { antwort.destroy(); return schluss({ status: antwort.statusCode, kopf, koerper: Buffer.alloc(0) }); } const art = String(kopf['content-encoding'] ?? '').toLowerCase(); const strom = art.includes('br') ? antwort.pipe(zlib.createBrotliDecompress()) : art.includes('gzip') ? antwort.pipe(zlib.createGunzip()) : art.includes('deflate') ? antwort.pipe(zlib.createInflate()) : antwort; const teile = []; let summe = 0; const abschluss = () => schluss({ status: antwort.statusCode, kopf, koerper: Buffer.concat(teile) }); strom.on('data', (t) => { teile.push(t); summe += t.length; if (summe >= grenze) { abschluss(); antwort.destroy(); } }); strom.on('end', abschluss); strom.on('error', abschluss); antwort.on('error', abschluss); }); const uhr = setTimeout(() => { anfrage.destroy(); schluss({ fehler: 'ZEITLIMIT' }); }, ZEITLIMIT_MS); anfrage.on('error', (e) => schluss({ fehler: e.code ?? e.message })); anfrage.end(); }); } function dekodiere(koerper, kopf) { const roh = koerper.subarray(0, 4096).toString('latin1'); const satz = /charset=["']?([\w-]+)/i.exec(String(kopf?.['content-type'] ?? ''))?.[1] ?? /charset=["']?([\w-]+)/i.exec(roh)?.[1] ?? 'utf-8'; try { return new TextDecoder(satz.toLowerCase()).decode(koerper); } catch { return new TextDecoder('utf-8').decode(koerper); } } const robotsCache = new Map(); function robotsRegeln(ursprung) { if (!robotsCache.has(ursprung)) { robotsCache.set(ursprung, (async () => { const r = await hole(`${ursprung}/robots.txt`, 128 * 1024); if (r.fehler || r.status !== 200) return []; const zeilen = dekodiere(r.koerper, r.kopf).split(/\r?\n/).map((z) => z.replace(/#.*/, '').trim()).filter(Boolean); const gruppen = []; let aktuell = null; let warAgent = false; for (const z of zeilen) { const p = z.indexOf(':'); if (p < 0) continue; const k = z.slice(0, p).trim().toLowerCase(); const w = z.slice(p + 1).trim(); if (k === 'user-agent') { if (!warAgent) { aktuell = { agenten: [], regeln: [] }; gruppen.push(aktuell); } aktuell.agenten.push(w.toLowerCase()); warAgent = true; } else if (aktuell && (k === 'allow' || k === 'disallow')) { warAgent = false; if (w) aktuell.regeln.push({ erlaubt: k === 'allow', pfad: w }); } else warAgent = false; } const eigene = gruppen.filter((g) => g.agenten.some((a) => a !== '*' && 'municipal-hosting-study'.includes(a))); const passend = eigene.length ? eigene : gruppen.filter((g) => g.agenten.includes('*')); return passend.flatMap((g) => g.regeln); })()); } return robotsCache.get(ursprung); } function robotsErlaubt(regeln, pfad) { let best = null; for (const r of regeln) { const muster = r.pfad.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\\\$$/, '$'); if (!new RegExp(`^${muster}`).test(pfad)) continue; if (!best || r.pfad.length > best.pfad.length || (r.pfad.length === best.pfad.length && r.erlaubt)) best = r; } return !best || best.erlaubt; } const norm = (s) => String(s ?? '').toLowerCase().replace(/ä/g, 'ae').replace(/ö/g, 'oe').replace(/ü/g, 'ue').replace(/ß/g, 'ss').replace(/[^a-z0-9]/g, ''); const namensKern = (name) => norm(String(name ?? '').split(',')[0].split('(')[0].split('/')[0].replace(/\b(Stadt|Gemeinde|Flecken|Markt)\b/gi, '')); const PARK = /(domain|website)[^.]{0,40}(for sale|zu verkaufen|steht zum verkauf|kaufen|is parked|geparkt)|buy this domain|domain parking|parkingcrew|sedoparking|sedo\.com|bodis\.com|dan\.com|afternic|this domain (has|is) (been )?(registered|available)|diese domain (wurde|ist|kann)|domain ist reserviert|hier entsteht|under construction|baustelle|coming soon|default web site page|apache2 (debian|ubuntu) default page|welcome to nginx|plesk.{0,30}default|web server's default page/i; const KOMMUNAL = /rathaus|buergermeister|bürgermeister|gemeinderat|stadtrat|ortsbuergermeister|ortsbürgermeister|verbandsgemeinde|samtgemeinde|amtsverwaltung|verwaltungsgemeinschaft|gemeindeverwaltung|stadtverwaltung|ortsgemeinde/i; const CDN_KOEPFE = ['cf-ray', 'x-amz-cf-id', 'x-served-by', 'x-azure-ref', 'x-vercel-id', 'x-sucuri-id', 'x-akamai-transformed', 'x-fastly-request-id', 'x-cdn', 'x-iinfo']; async function pruefe(eintrag) { const start = `https://${eintrag.host}/`; let url = eintrag.url?.startsWith('http') ? eintrag.url : start; const besucht = []; let r = null; let robotsGesperrt = false; for (let i = 0; i < 7; i++) { const u = new URL(url); besucht.push(url); if (!robotsErlaubt(await robotsRegeln(u.origin), u.pathname + u.search)) { robotsGesperrt = true; break; } r = await hole(url); if (r.fehler && i === 0 && u.protocol === 'https:') { const alternativ = `http://${u.host}${u.pathname}${u.search}`; const zweite = await hole(alternativ); if (!zweite.fehler) { url = alternativ; besucht.push(alternativ); r = zweite; } } if (r.fehler) break; let ziel = null; if (r.status >= 300 && r.status < 400 && r.kopf.location) ziel = r.kopf.location; else if (r.status === 200) { const anfang = dekodiere(r.koerper.subarray(0, 8192), r.kopf); ziel = /]+http-equiv=["']?refresh["']?[^>]+content=["']?\s*\d+\s*;\s*url=['"]?([^"'>\s]+)/i.exec(anfang)?.[1] ?? null; } if (!ziel) break; try { const naechste = new URL(ziel, url).href; if (besucht.includes(naechste)) break; url = naechste; } catch { break; } } const zeile = { host: eintrag.host, zeit: new Date().toISOString(), start: besucht[0], finalUrl: url, finalHost: new URL(url).hostname.toLowerCase(), hops: besucht.length - 1, status: r?.status ?? null, fehler: r?.fehler ?? null, robotsGesperrt, https: url.startsWith('https:'), tlsOk: r?.tls?.ok ?? null, tlsFehler: r?.tls?.fehler ?? null, namenGesamt: eintrag.namen.length, nameTreffer: 0, kommunal: false, parkverdacht: false, titel: null, groesse: 0, server: r?.kopf?.server ? String(r.kopf.server).slice(0, 60) : null, cdnKoepfe: r?.kopf ? CDN_KOEPFE.filter((k) => r.kopf[k] !== undefined) : [], }; if (r?.koerper?.length) { const html = dekodiere(r.koerper, r.kopf); zeile.groesse = r.koerper.length; zeile.titel = (/