mirror of
https://github.com/hzrd149/nsite-gateway.git
synced 2025-06-23 20:05:03 +00:00
Add ONION_HOST env variable
This commit is contained in:
parent
7dfcff462b
commit
7c3c9c0d6c
5
.changeset/pink-drinks-speak.md
Normal file
5
.changeset/pink-drinks-speak.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"nsite-ts": minor
|
||||
---
|
||||
|
||||
Add ONION_HOST env variable
|
@ -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
|
||||
|
@ -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,
|
||||
};
|
||||
|
22
src/index.ts
22
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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user