From 7c3c9c0d6ca9f444c8d3b6806165f65844bc6bbd Mon Sep 17 00:00:00 2001 From: hzrd149 Date: Sun, 6 Oct 2024 10:01:15 -0500 Subject: [PATCH] Add ONION_HOST env variable --- .changeset/pink-drinks-speak.md | 5 +++++ .env.example | 3 +++ src/env.ts | 3 +++ src/index.ts | 22 +++++++++++++++++++++- 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .changeset/pink-drinks-speak.md diff --git a/.changeset/pink-drinks-speak.md b/.changeset/pink-drinks-speak.md new file mode 100644 index 0000000..4eae772 --- /dev/null +++ b/.changeset/pink-drinks-speak.md @@ -0,0 +1,5 @@ +--- +"nsite-ts": minor +--- + +Add ONION_HOST env variable diff --git a/.env.example b/.env.example index 2ccd49e..d4d26cb 100644 --- a/.env.example +++ b/.env.example @@ -20,3 +20,6 @@ NGINX_CACHE_DIR='/var/nginx/cache' # screenshots require Puppeteer to be setup https://pptr.dev/troubleshooting#setting-up-chrome-linux-sandbox ENABLE_SCREENSHOTS="false" SCREENSHOTS_IDR="./screenshots" + +# If this is set, nsite will return the 'Onion-Location' header in responses +# ONION_HOST=https://.onion diff --git a/src/env.ts b/src/env.ts index d6389a3..e11001a 100644 --- a/src/env.ts +++ b/src/env.ts @@ -27,6 +27,8 @@ const HOST = `${NSITE_HOST}:${NSITE_PORT}`; const ENABLE_SCREENSHOTS = process.env.ENABLE_SCREENSHOTS === "true"; const SCREENSHOTS_DIR = process.env.SCREENSHOTS_DIR || "./screenshots"; +const ONION_HOST = process.env.ONION_HOST; + export { SUBSCRIPTION_RELAYS, LOOKUP_RELAYS, @@ -42,4 +44,5 @@ export { HOST, ENABLE_SCREENSHOTS, SCREENSHOTS_DIR, + ONION_HOST, }; diff --git a/src/index.ts b/src/index.ts index 9b7b6c2..c3bbc30 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import { fileURLToPath } from "node:url"; import mime from "mime"; import morgan from "koa-morgan"; import send from "koa-send"; +import { npubEncode } from "nostr-tools/nip19"; import { resolveNpubFromHostname } from "./helpers/dns.js"; import { getNsiteBlobs, parseNsiteEvent } from "./events.js"; @@ -20,6 +21,7 @@ import { NGINX_CACHE_DIR, NSITE_HOST, NSITE_PORT, + ONION_HOST, SUBSCRIPTION_RELAYS, } from "./env.js"; import { userDomains, userRelays, userServers } from "./cache.js"; @@ -131,13 +133,20 @@ app.use(async (ctx, next) => { if (res) { const type = mime.getType(blob.path); - if (type) ctx.set("Content-Type", type); + if (type) ctx.set("content-type", type); else if (res.headers["content-type"]) ctx.set("content-type", res.headers["content-type"]); // pass headers along if (res.headers["content-length"]) ctx.set("content-length", res.headers["content-length"]); if (res.headers["last-modified"]) ctx.set("last-modified", res.headers["last-modified"]); + // set Onion-Location header + if (ONION_HOST) { + const url = new URL(ONION_HOST); + url.hostname = npubEncode(pubkey) + "." + url.hostname; + ctx.set("Onion-Location", url.toString().replace(/\/$/, "")); + } + ctx.status = 200; ctx.body = res; return; @@ -149,6 +158,17 @@ app.use(async (ctx, next) => { } else await next(); }); +if (ONION_HOST) { + app.use((ctx, next) => { + // set Onion-Location header if it was not set before + if (!ctx.get("Onion-Location") && ONION_HOST) { + ctx.set("Onion-Location", ONION_HOST); + } + + return next(); + }); +} + // serve static files from public try { const www = path.resolve(process.cwd(), "public");