| 6 |
| // requires setTimeout + module-level state — both unavailable in Snippets): |
| 7 | // |
| 8 | // POST /aesgcm?e=<url> WebPush (token_type=10): embeds Encryption / |
| 9 | // Crypto-Key headers into the body, forwards to the |
| 10 | // UnifiedPush endpoint. |
| 11 | // PUT /<url> Simple Push (token_type=4): forwards the body |
| 12 | // directly. The client may briefly see both a POST |
| 13 | // and a PUT wake-up; the extra PUT only triggers a |
| 14 | // no-op aesgcm decryption failure on the device. |
| 15 | // |
| 16 | // Snippets budget: ≤ 1 subrequest, ≤ 5 ms CPU, ≤ 32 KiB. |
| 17 |
| 18 | const UPSTREAM_HEADERS = { |
| 19 | "TTL": "2592000", |
| 20 | "Urgency": "high", |
| 21 | "Content-Encoding": "aes128gcm", |
| 22 | }; |
| 23 |
| 24 | // Minimal IPv4 SSRF check — covers the common SSRF entry points without the |
| 25 | // CPU cost of full IPv6 parsing. Hostname → IP safety is enforced by the |
| 26 | // Cloudflare edge runtime (Snippets cannot reach RFC1918 from outbound fetch). |
| 27 | function ipv4Unsafe(host) { |
| 28 | const m = host.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/); |
| 29 | if (!m) return false; |
| 30 | const a = +m[1], b = +m[2]; |
| 31 | if (a > 255 || b > 255 || +m[3] > 255 || +m[4] > 255) return true; |
| 32 | return ( |
| 33 | a === 0 || a === 127 || a === 10 || |
| 34 | (a === 172 && b >= 16 && b <= 31) || |
| 35 | (a === 192 && b === 168) || |
| 36 | (a === 100 && b >= 64 && b <= 127) || |
| 37 | (a === 169 && b === 254) || |
| 38 | a >= 224 |
| 39 | ); |
| 40 | } |
| 41 |
| 42 | // Returns parsed URL on success, null on rejection. |
| 43 | function parseSafeEndpoint(raw) { |
| 44 | let u; |
| 45 | try { u = new URL(raw); } catch { return null; } |
| 46 | if (u.protocol !== "http:" && u.protocol !== "https:") return null; |
| 47 | if (u.username || u.password) return null; |
| 48 | const h = u.hostname; |
| 49 | if (!h) return null; |
| 50 | if (ipv4Unsafe(h)) return null; |
| 51 | // Bracketed IPv6 literals: cheap reject of obvious unsafe forms. |
| 52 | if (h.startsWith("[")) { |
| 53 | const inner = h.slice(1, -1).toLowerCase(); |
| 54 | if ( |
| 55 | inner === "::1" || inner === "::" || |
| 56 | inner.startsWith("fe8") || inner.startsWith("fe9") || |
| 57 | inner.startsWith("fea") || inner.startsWith("feb") || |
| 58 | inner.startsWith("fc") || inner.startsWith("fd") || |
| 59 | inner.startsWith("ff") |
| 60 | ) return null; |
| 61 | } |
| 62 | return u; |
| 63 | } |
| 64 |
| 65 | // Zero-copy body builder: prepends the aesgcm header block to the request |
| 66 | // stream without buffering the ciphertext in memory. |
| 67 | function prefixedStream(prefix, source) { |
| 68 | return new ReadableStream({ |
| 69 | start(controller) { controller.enqueue(prefix); }, |
| 70 | pull: source |
| 71 | ? async (controller) => { |
| 72 | const reader = source.getReader(); |
| 73 | for (;;) { |
| 74 | const { value, done } = await reader.read(); |
| 75 | if (done) { controller.close(); return; } |
| 76 | controller.enqueue(value); |
| 77 | } |
| 78 | } |
| 79 | : (controller) => controller.close(), |
| 80 | }); |
| 81 | } |
| 82 |
| 83 | async function handleAesgcm(request, url) { |
| 84 | const endpoint = url.searchParams.get("e"); |
| 85 | if (!endpoint) return new Response(null, { status: 400 }); |
| 86 | const target = parseSafeEndpoint(endpoint); |
| 87 | if (!target) return new Response(null, { status: 403 }); |
| 88 |
| 89 | const encryption = request.headers.get("encryption") || ""; |
| 90 | const cryptoKey = request.headers.get("crypto-key") || ""; |
| 91 |
| 92 | const prefix = new TextEncoder().encode( |
| 93 | "aesgcm\nEncryption: " + encryption + "\nCrypto-Key: " + cryptoKey + "\n" |
| 94 | ); |
| 95 |
| 96 | const resp = await fetch(target, { |
| 97 | method: "POST", |
| 98 | headers: UPSTREAM_HEADERS, |
| 99 | body: prefixedStream(prefix, request.body), |
| 100 | redirect: "manual", |
| 101 | }); |
| 102 |
| 103 | if (resp.status >= 200 && resp.status < 300) { |
| 104 | // Normalize 2xx → 201 Created per WebPush spec to avoid Telegram backoff. |
| 105 | return new Response(null, { |
| 106 | status: 201, |
| 107 | headers: { "location": resp.headers.get("Location") || endpoint }, |
| 108 | }); |
| 109 | } |
| 110 | return new Response(resp.body, { status: resp.status }); |
| 111 | } |
| 112 |
| 113 | async function handleSimplePush(request, url) { |
| 114 | const target = parseSafeEndpoint(url.pathname.slice(1) + url.search); |
| 115 | if (!target) return new Response(null, { status: 403 }); |
| 116 |
| 117 | const resp = await fetch(target, { |
| 118 | method: "POST", |
| 119 | headers: UPSTREAM_HEADERS, |
| 120 | body: request.body, |
| 121 | redirect: "manual", |
| 122 | }); |
| 123 | return new Response(resp.body, { status: resp.status }); |
| 124 | } |
| 125 |
| 126 | export default { |
| 127 | fetch(request) { |
| 128 | const url = new URL(request.url); |
| 129 | const m = request.method; |
| 130 | if (m === "POST" && url.pathname === "/aesgcm") return handleAesgcm(request, url); |
| 131 | if (m === "PUT" && url.pathname.length > 1) return handleSimplePush(request, url); |
| 132 | return new Response(null, { status: 404 }); |
| 133 | }, |
| 134 | }; |
| 135 |