// clearwake.ai/fetch: loads a page or picture for Clearwake on the web. // Every address and every redirect is checked by Clearwake's own filter // (Rust, compiled to clearwake_fetch.wasm) before it is requested. Only // pages, text and pictures come back. No cookies are sent or kept. import filterModule from './clearwake_fetch.wasm'; const MAX_BYTES = 10 * 1024 * 1024; const USER_AGENT = 'Mozilla/5.0 (compatible; Clearwake/0.1; +https://clearwake.ai)'; const ORIGINS = ['https://clearwake.ai', 'https://www.clearwake.ai']; const enc = new TextEncoder(); function check(url) { const f = new WebAssembly.Instance(filterModule, {}).exports; const b = enc.encode(url); const p = f.cw_alloc(b.length); new Uint8Array(f.memory.buffer, p, b.length).set(b); return f.cw_check(p, b.length); } function reply(status, text, extra = {}) { return new Response(text, { status, headers: { 'content-type': 'text/plain; charset=utf-8', 'cache-control': 'no-store', 'x-content-type-options': 'nosniff', ...extra }, }); } export async function clearwakeFetch(request) { const origin = request.headers.get('origin') || ''; const site = request.headers.get('sec-fetch-site') || ''; if (!(site === 'same-origin' || ORIGINS.includes(origin))) return reply(403, 'This loader only works inside Clearwake.'); if (request.method !== 'GET') return reply(405, 'Only pages can be loaded.'); let target = new URL(request.url).searchParams.get('url') || ''; for (let hop = 0; hop < 8; hop++) { const verdict = check(target); if (verdict === 1) return reply(403, 'Clearwake blocked this address because it serves ads or tracking.'); if (verdict === 2) return reply(400, "That address doesn't look right."); if (verdict === 3) return reply(400, 'Clearwake on the web can only open public sites.'); let res; try { res = await fetch(target, { redirect: 'manual', headers: { 'user-agent': USER_AGENT, accept: 'text/html,application/xhtml+xml,text/plain;q=0.9,image/*;q=0.8', 'accept-language': 'en-US,en;q=0.8' }, }); } catch { return reply(502, "Clearwake couldn't reach that site."); } if (res.status >= 300 && res.status < 400) { const loc = res.headers.get('location'); if (!loc) return reply(502, 'The site sent Clearwake somewhere it won\'t go.'); target = new URL(loc, target).toString(); continue; } if (res.status >= 400) return reply(502, `The site answered with an error (${res.status}).`); const ct = res.headers.get('content-type') || ''; const l = ct.toLowerCase(); if (!(l === '' || l.includes('html') || l.includes('xml') || l.startsWith('text/') || l.startsWith('image/') || l.includes('json'))) { return reply(415, "This is a file, not a page. Clearwake doesn't download files."); } const body = await res.arrayBuffer(); if (body.byteLength > MAX_BYTES) return reply(413, 'This page is too large to open.'); return new Response(body, { status: 200, headers: { 'content-type': ct || 'application/octet-stream', 'x-clearwake-url': target, 'cache-control': 'no-store', 'x-content-type-options': 'nosniff' }, }); } return reply(508, 'The site redirected too many times.'); }