/* ===================================================================== chat-linkvorschau.js — Link-Vorschau im Rudel-Chat (11.11.2026) Rudel-Chat-Plan, Stufe 3 (E18). Schickt jemand einen Link, holt der SERVER Titel und Beschreibung der Seite — nie der Browser der Leser. Sonst erführe jede verlinkte Seite die Adresse jedes Menschen, der die Nachricht nur ansieht. DER SCHUTZ (der eigentliche Grund für diese Datei): · nur http und https, nur die Ports 80 und 443 · der Name wird HIER aufgelöst, jede Adresse geprüft (keine internen Netze: 127/8, 10/8, 172.16/12, 192.168/16, 169.254/16, 100.64/10, 0/8, Multicast, ::1, fc00::/7, fe80::/10, IPv4-in-IPv6 …), und verbunden wird mit GENAU der geprüften Adresse. Ein zweites Auflösen beim Verbinden (DNS-Rebinding) gibt es nicht. · Umleitungen werden einzeln verfolgt (höchstens 3), jede neue Adresse wird wieder geprüft. · höchstens 5 Sekunden, höchstens 1 MB, nur text/html · keine Kekse, kein Referer, eine ehrliche Kennung des Abrufers Ein Bild wird bewusst NICHT geholt: Es müsste gespeichert oder durchgereicht werden, und beides wäre ein zweiter Weg nach draußen. Die Domain steht groß in der Karte (Schutz vor Täuschung, S4). ===================================================================== */ import { lookup } from "node:dns/promises"; import { request as httpAnfrage } from "node:http"; import { request as httpsAnfrage } from "node:https"; import { isIP } from "node:net"; export const VORSCHAU_FRIST_MS = 5000; export const VORSCHAU_MAX_BYTES = 1024 * 1024; const UMLEITUNGEN_MAX = 3; /** Ist diese IP-Adresse von außen erreichbar und öffentlich? */ export function adresseOeffentlich(ip) { const art = isIP(ip); if (art === 4) { const t = ip.split(".").map(Number); if (t.length !== 4 || t.some((x) => !Number.isInteger(x) || x < 0 || x > 255)) return false; const [a, b] = t; if (a === 0 || a === 10 || a === 127) return false; if (a === 100 && b >= 64 && b <= 127) return false; // CGNAT if (a === 169 && b === 254) return false; // link-local if (a === 172 && b >= 16 && b <= 31) return false; if (a === 192 && b === 168) return false; if (a === 192 && b === 0 && t[2] === 0) return false; // IETF if (a === 192 && b === 0 && t[2] === 2) return false; // TEST-NET-1 if (a === 198 && (b === 18 || b === 19)) return false; // Benchmark if (a === 198 && b === 51 && t[2] === 100) return false; if (a === 203 && b === 0 && t[2] === 113) return false; if (a >= 224) return false; // Multicast, reserviert, Broadcast return true; } if (art === 6) { const s = ip.toLowerCase(); if (s === "::" || s === "::1") return false; const v4 = s.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/) || s.match(/^::(\d+\.\d+\.\d+\.\d+)$/); if (v4) return adresseOeffentlich(v4[1]); if (/^::ffff:/.test(s)) return false; // andere Schreibweisen gemappter Adressen const erstes = parseInt(s.split(":")[0] || "0", 16); if ((erstes & 0xfe00) === 0xfc00) return false; // fc00::/7 if ((erstes & 0xffc0) === 0xfe80) return false; // fe80::/10 if ((erstes & 0xff00) === 0xff00) return false; // Multicast if (s.startsWith("64:ff9b:")) return false; // NAT64 -> könnte intern zeigen if (s.startsWith("2001:db8:") || s.startsWith("2001:0db8:")) return false; return true; } return false; } /** Prüft eine Adresse und löst sie EINMAL auf. Wirft mit Grund. */ export async function zielPruefen(roh, aufloesen = lookup) { let u; try { u = new URL(roh); } catch { throw new Error("keine_adresse"); } if (u.protocol !== "http:" && u.protocol !== "https:") throw new Error("nur_http"); if (u.username || u.password) throw new Error("zugangsdaten_in_adresse"); const port = u.port ? Number(u.port) : (u.protocol === "https:" ? 443 : 80); if (port !== 80 && port !== 443) throw new Error("port"); const host = u.hostname.replace(/^\[|\]$/g, ""); if (!host || /^localhost$/i.test(host) || /\.(local|internal|lan|home|localhost)$/i.test(host)) { throw new Error("intern"); } let adressen; if (isIP(host)) adressen = [{ address: host, family: isIP(host) }]; else adressen = await aufloesen(host, { all: true, verbatim: true }); if (!adressen?.length) throw new Error("unbekannt"); /* ALLE Adressen müssen öffentlich sein -- ein Name mit einer internen und einer öffentlichen wäre sonst ein Würfelspiel. */ if (adressen.some((x) => !adresseOeffentlich(x.address))) throw new Error("intern"); return { url: u, host, port, adresse: adressen[0].address, familie: adressen[0].family }; } function einmalHolen(ziel) { return new Promise((fertig, schief) => { const anfrage = (ziel.url.protocol === "https:" ? httpsAnfrage : httpAnfrage)({ host: ziel.adresse, family: ziel.familie, port: ziel.port, path: ziel.url.pathname + ziel.url.search, method: "GET", servername: isIP(ziel.host) ? undefined : ziel.host, headers: { Host: ziel.url.host, "User-Agent": "Mozilla/5.0 (compatible; RudelChat-Linkvorschau/1.0; +https://crew.dogfather-universe.com)", Accept: "text/html,application/xhtml+xml;q=0.9", "Accept-Language": "de,en;q=0.7", }, /* Verbunden wird mit der geprüften Adresse -- ohne erneutes Auflösen. */ lookup: (_n, _o, cb) => cb(null, ziel.adresse, ziel.familie), }, (antwort) => { const code = antwort.statusCode || 0; if (code >= 300 && code < 400 && antwort.headers.location) { antwort.resume(); return fertig({ umleitung: new URL(antwort.headers.location, ziel.url).toString() }); } if (code !== 200) { antwort.resume(); return schief(new Error(`status_${code}`)); } const typ = String(antwort.headers["content-type"] || ""); if (!/text\/html|application\/xhtml/i.test(typ)) { antwort.resume(); return schief(new Error("kein_html")); } let menge = 0; const teile = []; antwort.on("data", (s) => { menge += s.length; if (menge > VORSCHAU_MAX_BYTES) { anfrage.destroy(); fertig({ html: Buffer.concat(teile).toString("utf8") }); return; } teile.push(s); /* Der Kopf reicht: Ist da, wird nicht weitergelesen. */ if (/<\/head>/i.test(s.toString("latin1"))) { anfrage.destroy(); fertig({ html: Buffer.concat(teile).toString("utf8") }); } }); antwort.on("end", () => fertig({ html: Buffer.concat(teile).toString("utf8") })); antwort.on("error", schief); }); anfrage.setTimeout(VORSCHAU_FRIST_MS, () => anfrage.destroy(new Error("frist"))); anfrage.on("error", schief); anfrage.end(); }); } const ENTITAETEN = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " " }; function entschluesseln(s) { return String(s || "") .replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(Math.min(Number(n), 0x10ffff))) .replace(/&#x([0-9a-f]+);/gi, (_, n) => String.fromCodePoint(Math.min(parseInt(n, 16), 0x10ffff))) .replace(/&([a-z]+);/gi, (m, n) => ENTITAETEN[n.toLowerCase()] ?? m) .replace(/[\u0000-\u001f\u007f​-‏‪-‮⁦-⁩]/g, " ") .replace(/\s+/g, " ").trim(); } function meta(html, namen) { for (const n of namen) { const re1 = new RegExp(`]+(?:property|name)=["']${n}["'][^>]*content=["']([^"']*)["']`, "i"); const re2 = new RegExp(`]+content=["']([^"']*)["'][^>]*(?:property|name)=["']${n}["']`, "i"); const t = html.match(re1) || html.match(re2); if (t && t[1].trim()) return entschluesseln(t[1]); } return ""; } /** Holt die Vorschau oder wirft. Rückgabe: { url, domain, titel, text }. */ export async function vorschauHolen(roh, { aufloesen = lookup, holen = einmalHolen } = {}) { let adresse = roh; for (let i = 0; i <= UMLEITUNGEN_MAX; i++) { const ziel = await zielPruefen(adresse, aufloesen); const r = await holen(ziel); if (r.umleitung) { adresse = r.umleitung; continue; } const html = r.html || ""; const titel = (meta(html, ["og:title", "twitter:title"]) || entschluesseln((html.match(/]*>([\s\S]*?)<\/title>/i) || [])[1] || "")).slice(0, 140); const text = meta(html, ["og:description", "twitter:description", "description"]).slice(0, 220); if (!titel && !text) throw new Error("leer"); return { url: ziel.url.toString(), domain: ziel.url.hostname.replace(/^www\./, ""), titel, text }; } throw new Error("zu_viele_umleitungen"); } /** Der erste Link in einem Text (dieselbe Form wie im Browser). */ export function ersterLink(text) { const t = String(text || "").match(/\bhttps?:\/\/[^\s<>"']+/i); if (!t) return null; return t[0].replace(/[.,;:!?)\]]+$/, ""); }