|
| 1 | +// VPS wrapper for exit_node.ts — used when you run the exit node on your |
| 2 | +// own server (any platform that can run Deno or Bun) instead of on a |
| 3 | +// platform that auto-invokes the default export (Deno Deploy, Val.town, |
| 4 | +// Cloudflare Workers, etc.). |
| 5 | +// |
| 6 | +// Pick ONE runtime + matching command: |
| 7 | +// |
| 8 | +// Deno (recommended, comes with HTTPS support out of the box): |
| 9 | +// deno run --allow-net --allow-env wrapper.ts |
| 10 | +// |
| 11 | +// Bun (also works, slightly faster cold start): |
| 12 | +// bun run wrapper.ts |
| 13 | +// |
| 14 | +// Node 22+ (no extra runtime; needs `--experimental-fetch` only on <22): |
| 15 | +// node wrapper.ts # if your Node has fetch + Bun's |
| 16 | +// # global Request/Response (22+) |
| 17 | +// |
| 18 | +// ENV VARS (all optional): |
| 19 | +// PORT — TCP port to bind. Default 8443. |
| 20 | +// HOST — bind address. Default 0.0.0.0 (all interfaces). |
| 21 | +// CERT_FILE — path to TLS cert PEM. Omit for plain HTTP (use a reverse |
| 22 | +// proxy like Caddy / nginx / Cloudflare Tunnel for TLS). |
| 23 | +// KEY_FILE — path to TLS key PEM (must be set together with CERT_FILE). |
| 24 | +// |
| 25 | +// Behind a reverse proxy (Caddy, nginx, Cloudflare Tunnel) you typically |
| 26 | +// run this on plain HTTP and let the proxy terminate TLS — that's the |
| 27 | +// simpler setup if you already have a domain. Set PORT=8443 and point |
| 28 | +// your reverse proxy at http://localhost:8443. |
| 29 | +// |
| 30 | +// Standalone TLS (rare but supported): set CERT_FILE + KEY_FILE to |
| 31 | +// matching PEM-encoded files and Deno will terminate TLS itself. Use |
| 32 | +// Let's Encrypt's certbot / acme.sh to fetch a real cert; self-signed |
| 33 | +// will not work (Apps Script's UrlFetchApp validates the chain). |
| 34 | +// |
| 35 | +// EDIT exit_node.ts FIRST: replace the placeholder PSK with a strong |
| 36 | +// secret. The wrapper imports the handler from exit_node.ts directly, |
| 37 | +// so changing the constant in exit_node.ts is all you need. |
| 38 | + |
| 39 | +import handler from "./exit_node.ts"; |
| 40 | + |
| 41 | +// Deno (preferred) |
| 42 | +if (typeof (globalThis as any).Deno !== "undefined") { |
| 43 | + const Deno = (globalThis as any).Deno; |
| 44 | + const port = Number(Deno.env.get("PORT") ?? 8443); |
| 45 | + const hostname = Deno.env.get("HOST") ?? "0.0.0.0"; |
| 46 | + const certFile = Deno.env.get("CERT_FILE"); |
| 47 | + const keyFile = Deno.env.get("KEY_FILE"); |
| 48 | + |
| 49 | + if (certFile && keyFile) { |
| 50 | + Deno.serve( |
| 51 | + { |
| 52 | + port, |
| 53 | + hostname, |
| 54 | + cert: Deno.readTextFileSync(certFile), |
| 55 | + key: Deno.readTextFileSync(keyFile), |
| 56 | + }, |
| 57 | + handler, |
| 58 | + ); |
| 59 | + console.log(`exit_node listening on https://${hostname}:${port}`); |
| 60 | + } else { |
| 61 | + Deno.serve({ port, hostname }, handler); |
| 62 | + console.log( |
| 63 | + `exit_node listening on http://${hostname}:${port} ` + |
| 64 | + `(no TLS — terminate it with a reverse proxy like Caddy/nginx)`, |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
| 68 | +// Bun |
| 69 | +else if (typeof (globalThis as any).Bun !== "undefined") { |
| 70 | + const Bun = (globalThis as any).Bun; |
| 71 | + const port = Number(process.env.PORT ?? 8443); |
| 72 | + const hostname = process.env.HOST ?? "0.0.0.0"; |
| 73 | + |
| 74 | + Bun.serve({ |
| 75 | + port, |
| 76 | + hostname, |
| 77 | + fetch: handler, |
| 78 | + tls: process.env.CERT_FILE && process.env.KEY_FILE |
| 79 | + ? { |
| 80 | + cert: Bun.file(process.env.CERT_FILE), |
| 81 | + key: Bun.file(process.env.KEY_FILE), |
| 82 | + } |
| 83 | + : undefined, |
| 84 | + }); |
| 85 | + console.log(`exit_node listening on ${hostname}:${port}`); |
| 86 | +} |
| 87 | +// Node 22+ — uses the built-in `node:http` module + globalThis.Request/Response |
| 88 | +else if (typeof (globalThis as any).process !== "undefined") { |
| 89 | + const { createServer } = await import("node:http"); |
| 90 | + const port = Number(process.env.PORT ?? 8443); |
| 91 | + const hostname = process.env.HOST ?? "0.0.0.0"; |
| 92 | + |
| 93 | + createServer(async (req, res) => { |
| 94 | + // Build a web-standard Request from Node's IncomingMessage. |
| 95 | + const chunks: Uint8Array[] = []; |
| 96 | + for await (const c of req) chunks.push(c as Uint8Array); |
| 97 | + const body = chunks.length ? Buffer.concat(chunks) : undefined; |
| 98 | + |
| 99 | + const url = `http://${req.headers.host ?? hostname}${req.url ?? "/"}`; |
| 100 | + const webReq = new Request(url, { |
| 101 | + method: req.method, |
| 102 | + headers: req.headers as Record<string, string>, |
| 103 | + body, |
| 104 | + }); |
| 105 | + |
| 106 | + const webRes = await handler(webReq); |
| 107 | + |
| 108 | + res.statusCode = webRes.status; |
| 109 | + webRes.headers.forEach((v: string, k: string) => res.setHeader(k, v)); |
| 110 | + const buf = new Uint8Array(await webRes.arrayBuffer()); |
| 111 | + res.end(buf); |
| 112 | + }).listen(port, hostname, () => { |
| 113 | + console.log(`exit_node listening on http://${hostname}:${port}`); |
| 114 | + }); |
| 115 | +} else { |
| 116 | + throw new Error( |
| 117 | + "No supported runtime detected. Run this file with Deno, Bun, or Node 22+.", |
| 118 | + ); |
| 119 | +} |
0 commit comments