Add ONION_HOST env variable

This commit is contained in:
hzrd149 2024-10-06 10:01:15 -05:00
parent 7dfcff462b
commit 7c3c9c0d6c
4 changed files with 32 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"nsite-ts": minor
---
Add ONION_HOST env variable

View File

@ -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://<hostname>.onion

View File

@ -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,
};

View File

@ -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");