Repository Public
1import { all, getNum, getSetting, run, now } from "./db";2import { env } from "./env";3import { readText, safeFetch, UrlRejected } from "./ssrf";45export interface StreamRow { id: number; url: string; referrer: string | null; user_agent: string | null }6export interface ProbeResult { status: "up" | "down"; httpStatus: number | null; error: string | null; cors: boolean | null; contentType: string | null; latencyMs: number }78const BROWSER_UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15";910function explain(e: unknown): string {11 if (e instanceof UrlRejected) return e.message;12 const err = e as Error & { cause?: { code?: string; message?: string }; code?: string };13 const code = err.cause?.code ?? err.code;14 if (err.name === "AbortError" || /Délai dépassé|aborted/i.test(err.message)) return "Délai d'attente dépassé";15 switch (code) {16 case "ENOTFOUND": case "EAI_AGAIN": return "Nom d'hôte introuvable (DNS)";17 case "ECONNREFUSED": return "Connexion refusée par le serveur";18 case "ECONNRESET": case "UND_ERR_SOCKET": return "Connexion interrompue par le serveur";19 case "UND_ERR_CONNECT_TIMEOUT": case "ETIMEDOUT": case "UND_ERR_HEADERS_TIMEOUT": return "Délai d'attente dépassé";20 case "ESSRF": return "Adresse privée refusée";21 case "CERT_HAS_EXPIRED": case "DEPTH_ZERO_SELF_SIGNED_CERT": case "UNABLE_TO_VERIFY_LEAF_SIGNATURE": case "ERR_TLS_CERT_ALTNAME_INVALID": case "SELF_SIGNED_CERT_IN_CHAIN":22 return "Certificat TLS invalide";23 }24 return (err.cause?.message || err.message || "Erreur réseau").slice(0, 200);25}26function httpReason(status: number): string {27 if (status === 401) return "HTTP 401 — authentification exigée par la source";28 if (status === 403) return "HTTP 403 — accès refusé par la source (souvent une restriction géographique ou d'en-têtes)";29 if (status === 404) return "HTTP 404 — flux introuvable";30 if (status === 410) return "HTTP 410 — flux retiré";31 if (status === 451) return "HTTP 451 — indisponible pour raisons légales dans cette région";32 if (status >= 500) return `HTTP ${status} — erreur du serveur d'origine`;33 return `HTTP ${status}`;34}3536/** One availability test. At most two small requests (playlist + first variant playlist); media segments are never downloaded. */37export async function probeStream(s: StreamRow, timeoutMs = 10_000): Promise<ProbeResult> {38 const t0 = now();39 const origin = env.appUrl;40 const headers: Record<string, string> = { "user-agent": s.user_agent || BROWSER_UA, accept: "*/*", origin };41 if (s.referrer) headers.referer = s.referrer;42 const done = (r: Partial<ProbeResult> & { status: "up" | "down" }): ProbeResult =>43 ({ httpStatus: null, error: null, cors: null, contentType: null, ...r, latencyMs: now() - t0 });44 try {45 if (s.url.startsWith("provider://loop/")) {46 const { loopFiles } = await import("./relay/sources"); const n = loopFiles(Number(s.url.split("/")[3])).length;47 return n ? { status: "up", httpStatus: null, error: null, cors: true, contentType: "application/vnd.apple.mpegurl", latencyMs: 0 } : { status: "down", httpStatus: null, error: "Fichiers de la chaîne maison introuvables", cors: null, contentType: null, latencyMs: 0 };48 }49 // A provider stream is stored as an opaque reference: the real address exists only here, for the time of the test.50 const target = s.url.startsWith("provider://") ? await (await import("./engine")).resolveProvider(s.url) : s.url;51 const { res, finalUrl, signal } = await safeFetch(target, { headers, timeoutMs });52 const ct = (res.headers.get("content-type") || "").split(";")[0].trim().toLowerCase() || null;53 const acao = res.headers.get("access-control-allow-origin");54 const cors = acao === "*" || acao === origin;55 if (!res.ok) { signal.abort(); return done({ status: "down", httpStatus: res.status, error: httpReason(res.status), cors, contentType: ct }); }56 const isHls = /\.m3u8?(\?|$)/i.test(new URL(finalUrl).pathname + "?") || /mpegurl/.test(ct ?? "");57 if (!isHls) {58 if (ct && /^(video|audio)\//.test(ct) || ct === "application/dash+xml" || ct === "application/octet-stream" || ct === "application/x-mpegurl") { signal.abort(); return done({ status: "up", httpStatus: res.status, cors, contentType: ct }); }59 const head = await readText(res, 2048, signal);60 if (head.trimStart().startsWith("#EXTM3U")) return done({ status: "up", httpStatus: res.status, cors, contentType: ct });61 if (/^\s*<(!doctype|html)/i.test(head)) return done({ status: "down", httpStatus: res.status, error: "Le serveur renvoie une page HTML au lieu d'un flux", cors, contentType: ct });62 if (/^\s*<\?xml|<MPD/i.test(head)) return done({ status: "up", httpStatus: res.status, cors, contentType: ct ?? "application/dash+xml" });63 return done({ status: "up", httpStatus: res.status, cors, contentType: ct });64 }65 const body = await readText(res, 256 * 1024, signal);66 if (!body.trimStart().startsWith("#EXTM3U")) return done({ status: "down", httpStatus: res.status, error: "Réponse qui n'est pas une playlist HLS valide", cors, contentType: ct });67 // Master playlist: make sure the first variant playlist answers too.68 if (/#EXT-X-STREAM-INF/.test(body)) {69 const lines = body.split(/\r?\n/);70 const i = lines.findIndex((l) => l.startsWith("#EXT-X-STREAM-INF"));71 const uri = lines.slice(i + 1).find((l) => l.trim() && !l.startsWith("#"))?.trim();72 if (uri) {73 const remain = Math.max(2000, timeoutMs - (now() - t0));74 const v = await safeFetch(new URL(uri, finalUrl).toString(), { headers, timeoutMs: remain });75 if (!v.res.ok) { v.signal.abort(); return done({ status: "down", httpStatus: v.res.status, error: `Playlist de variante : ${httpReason(v.res.status)}`, cors, contentType: ct }); }76 const vb = await readText(v.res, 64 * 1024, v.signal);77 if (!vb.trimStart().startsWith("#EXTM3U")) return done({ status: "down", httpStatus: v.res.status, error: "Playlist de variante invalide", cors, contentType: ct });78 }79 } else if (!/#EXTINF/.test(body)) {80 return done({ status: "down", httpStatus: res.status, error: "Playlist HLS vide (aucun segment)", cors, contentType: ct });81 }82 return done({ status: "up", httpStatus: res.status, cors, contentType: ct ?? "application/vnd.apple.mpegurl" });83 } catch (e) {84 return done({ status: "down", error: s.url.startsWith("provider://") ? explain(e).replace(/https?:\/\/\S+/g, "(adresse du fournisseur)") : explain(e) });85 }86}8788export function saveProbe(id: number, r: ProbeResult) {89 run(`UPDATE streams SET status = ?, http_status = ?, check_error = ?, checked_at = ?, cors = ?, content_type = ?, latency_ms = ?,90 fail_count = CASE WHEN ? = 'down' THEN fail_count + 1 ELSE 0 END WHERE id = ?`,91 r.status, r.httpStatus, r.error, now(), r.cors === null ? null : Number(r.cors), r.contentType, r.latencyMs, r.status, id);92}9394/** Streams whose last test is older than the configured interval. Favourites and watched channels come first and are re-tested more often; never-tested streams of the guide countries come next. */95export function dueStreams(limit: number): StreamRow[] {96 const t = now();97 const normal = t - getNum("check_recheck_hours") * 3600_000;98 const prio = t - getNum("check_priority_hours") * 3600_000;99 const countries = (getSetting("epg_countries") ?? "").split(",").filter((c) => /^[A-Z]{2}$/.test(c));100 const home = countries.length ? `c.country IN (${countries.map((c) => `'${c}'`).join(",")})` : "0";101 const rows = all<StreamRow>(`102 WITH p AS (SELECT channel_id FROM list_items UNION SELECT channel_id FROM history)103 SELECT s.id, s.url, s.referrer, s.user_agent FROM streams s JOIN channels c ON c.id = s.channel_id104 WHERE s.checked_at IS NULL OR s.checked_at < CASE WHEN s.channel_id IN (SELECT channel_id FROM p) THEN ? ELSE ? END105 ORDER BY (s.channel_id IN (SELECT channel_id FROM p)) DESC, s.checked_at IS NOT NULL, ${home} DESC, s.checked_at LIMIT ?`, prio, normal, limit * 8);106 // One host is tested one stream at a time: cap its share of a batch so a large CDN cannot stall the whole batch.107 const perHost = new Map<string, number>(); const out: StreamRow[] = [];108 for (const r of rows) {109 let h = ""; try { h = new URL(r.url).host; } catch { /* reported by the probe */ }110 const n = perHost.get(h) ?? 0; if (n >= HOST_BATCH_CAP) continue;111 perHost.set(h, n + 1); out.push(r); if (out.length >= limit) break;112 }113 return out;114}115const HOST_BATCH_CAP = 12;116117const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));118119/** Polite pool: `concurrency` simultaneous tests overall, one at a time per host, `hostDelayMs` between two tests of the same host. */120export async function runBatch(streams: StreamRow[], opts: { concurrency: number; hostDelayMs: number; timeoutMs: number; shouldStop?: () => boolean; onResult?: (s: StreamRow, r: ProbeResult) => void }) {121 const queues = new Map<string, StreamRow[]>();122 for (const s of streams) { let h = "invalid"; try { h = new URL(s.url).host; } catch { /* probe will report it */ } const q = queues.get(h); if (q) q.push(s); else queues.set(h, [s]); }123 const busy = new Set<string>(); const nextAt = new Map<string, number>();124 let up = 0, down = 0;125 const worker = async () => {126 for (;;) {127 if (opts.shouldStop?.() || !queues.size) return;128 const t = now();129 let host: string | undefined;130 for (const h of queues.keys()) if (!busy.has(h) && (nextAt.get(h) ?? 0) <= t) { host = h; break; }131 if (!host) { await sleep(150); continue; }132 const q = queues.get(host)!; const s = q.shift()!;133 if (!q.length) queues.delete(host);134 busy.add(host);135 try {136 const r = await probeStream(s, opts.timeoutMs);137 saveProbe(s.id, r); if (r.status === "up") up++; else down++;138 opts.onResult?.(s, r);139 } finally { busy.delete(host); nextAt.set(host, now() + opts.hostDelayMs); }140 }141 };142 await Promise.all(Array.from({ length: Math.max(1, opts.concurrency) }, worker));143 return { up, down };144}145