2024-09-01 13:26:37 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
import "./polyfill.js";
|
|
|
|
import Koa from "koa";
|
|
|
|
import serve from "koa-static";
|
2024-10-04 10:32:57 -05:00
|
|
|
import path, { basename } from "node:path";
|
2024-09-01 13:26:37 -05:00
|
|
|
import cors from "@koa/cors";
|
|
|
|
import fs from "node:fs";
|
|
|
|
import { fileURLToPath } from "node:url";
|
2024-09-25 13:37:32 -05:00
|
|
|
import mime from "mime";
|
|
|
|
import morgan from "koa-morgan";
|
2024-10-04 10:32:57 -05:00
|
|
|
import send from "koa-send";
|
2024-10-06 10:01:15 -05:00
|
|
|
import { npubEncode } from "nostr-tools/nip19";
|
2025-01-22 12:09:06 -06:00
|
|
|
import { spawn } from "node:child_process";
|
|
|
|
import { nip19 } from "nostr-tools";
|
2024-09-01 13:26:37 -05:00
|
|
|
|
|
|
|
import { resolveNpubFromHostname } from "./helpers/dns.js";
|
2025-03-17 17:35:11 +00:00
|
|
|
import { getNsiteBlobs } from "./events.js";
|
|
|
|
import { downloadBlob } from "./blossom.js";
|
2024-10-04 11:46:17 -05:00
|
|
|
import {
|
|
|
|
BLOSSOM_SERVERS,
|
|
|
|
ENABLE_SCREENSHOTS,
|
|
|
|
HOST,
|
2025-01-22 12:09:06 -06:00
|
|
|
NSITE_HOMEPAGE,
|
|
|
|
NSITE_HOMEPAGE_DIR,
|
2024-10-04 11:46:17 -05:00
|
|
|
NSITE_HOST,
|
|
|
|
NSITE_PORT,
|
2024-10-06 10:01:15 -05:00
|
|
|
ONION_HOST,
|
2024-10-04 11:46:17 -05:00
|
|
|
SUBSCRIPTION_RELAYS,
|
|
|
|
} from "./env.js";
|
2024-09-26 12:48:13 -05:00
|
|
|
import { userDomains, userRelays, userServers } from "./cache.js";
|
2025-03-17 17:35:11 +00:00
|
|
|
import pool, { getUserBlossomServers, getUserOutboxes } from "./nostr.js";
|
2025-01-22 10:32:12 -06:00
|
|
|
import logger from "./logger.js";
|
2025-03-17 17:35:11 +00:00
|
|
|
import { watchInvalidation } from "./invalidation.js";
|
2024-09-01 13:26:37 -05:00
|
|
|
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
|
|
|
const app = new Koa();
|
|
|
|
|
2024-09-25 13:37:32 -05:00
|
|
|
morgan.token("host", (req) => req.headers.host ?? "");
|
|
|
|
|
|
|
|
app.use(morgan(":method :host:url :status :response-time ms - :res[content-length]"));
|
|
|
|
|
2024-09-01 13:26:37 -05:00
|
|
|
// set CORS headers
|
|
|
|
app.use(
|
|
|
|
cors({
|
|
|
|
origin: "*",
|
|
|
|
allowMethods: "*",
|
|
|
|
allowHeaders: "Authorization,*",
|
|
|
|
exposeHeaders: "*",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// handle errors
|
|
|
|
app.use(async (ctx, next) => {
|
|
|
|
try {
|
|
|
|
await next();
|
|
|
|
} catch (err) {
|
2024-09-25 13:37:32 -05:00
|
|
|
console.log(err);
|
|
|
|
ctx.status = 500;
|
2024-10-04 10:32:57 -05:00
|
|
|
if (err instanceof Error) ctx.body = { message: err.message };
|
2024-09-01 13:26:37 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-09-26 12:48:13 -05:00
|
|
|
// handle nsite requests
|
2024-09-01 13:26:37 -05:00
|
|
|
app.use(async (ctx, next) => {
|
2024-09-26 12:48:13 -05:00
|
|
|
let pubkey = await userDomains.get<string | undefined>(ctx.hostname);
|
|
|
|
|
|
|
|
// resolve pubkey if not in cache
|
2025-01-22 08:51:46 -06:00
|
|
|
if (pubkey === undefined) {
|
2025-01-22 12:09:06 -06:00
|
|
|
logger(`${ctx.hostname}: Resolving`);
|
2024-09-26 12:48:13 -05:00
|
|
|
pubkey = await resolveNpubFromHostname(ctx.hostname);
|
|
|
|
|
|
|
|
if (pubkey) {
|
|
|
|
await userDomains.set(ctx.hostname, pubkey);
|
2025-01-22 12:09:06 -06:00
|
|
|
logger(`${ctx.hostname}: Found ${pubkey}`);
|
2024-09-26 12:48:13 -05:00
|
|
|
} else {
|
|
|
|
await userDomains.set(ctx.hostname, "");
|
|
|
|
}
|
|
|
|
}
|
2024-09-01 13:26:37 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
if (!pubkey) return await next();
|
2024-09-25 15:28:28 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
const npub = npubEncode(pubkey);
|
|
|
|
const log = logger.extend(npub);
|
|
|
|
ctx.state.pubkey = pubkey;
|
2024-10-04 10:32:57 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
let relays = await userRelays.get<string[] | undefined>(pubkey);
|
2024-10-04 10:32:57 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
// fetch relays if not in cache
|
|
|
|
if (!relays) {
|
|
|
|
log(`Fetching relays`);
|
2024-09-07 17:15:12 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
relays = await getUserOutboxes(pubkey);
|
|
|
|
if (relays) {
|
|
|
|
await userRelays.set(pubkey, relays);
|
|
|
|
log(`Found ${relays.length} relays`);
|
|
|
|
} else {
|
|
|
|
relays = [];
|
|
|
|
await userServers.set(pubkey, [], 30_000);
|
|
|
|
log(`Failed to find relays`);
|
2024-10-18 11:29:29 +01:00
|
|
|
}
|
2025-03-17 17:35:11 +00:00
|
|
|
}
|
2024-10-18 11:29:29 +01:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
// always check subscription relays
|
|
|
|
relays.push(...SUBSCRIPTION_RELAYS);
|
2024-09-01 13:26:37 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
if (relays.length === 0) throw new Error("No nostr relays");
|
2024-09-26 12:48:13 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
log(`Searching for ${ctx.path}`);
|
|
|
|
let blobs = await getNsiteBlobs(pubkey, ctx.path, relays);
|
2024-09-25 13:37:32 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
if (blobs.length === 0) {
|
|
|
|
// fallback to custom 404 page
|
|
|
|
log(`Looking for custom 404 page`);
|
|
|
|
blobs = await getNsiteBlobs(pubkey, "/404.html", relays);
|
|
|
|
}
|
2024-09-25 13:37:32 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
if (blobs.length === 0) {
|
|
|
|
log(`Found 0 events`);
|
|
|
|
ctx.status = 404;
|
|
|
|
ctx.body = "Not Found";
|
|
|
|
return;
|
|
|
|
}
|
2024-09-25 13:37:32 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
let servers = await userServers.get<string[] | undefined>(pubkey);
|
2024-09-25 13:37:32 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
// fetch blossom servers if not in cache
|
|
|
|
if (!servers) {
|
|
|
|
log(`Fetching blossom servers`);
|
|
|
|
servers = await getUserBlossomServers(pubkey, relays);
|
2024-10-06 10:01:15 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
if (servers) {
|
|
|
|
await userServers.set(pubkey, servers);
|
|
|
|
log(`Found ${servers.length} servers`);
|
|
|
|
} else {
|
|
|
|
servers = [];
|
|
|
|
await userServers.set(pubkey, [], 30_000);
|
|
|
|
log(`Failed to find servers`);
|
2024-09-25 13:37:32 -05:00
|
|
|
}
|
2025-03-17 17:35:11 +00:00
|
|
|
}
|
2024-09-25 13:37:32 -05:00
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
// always fetch from additional servers
|
|
|
|
servers.push(...BLOSSOM_SERVERS);
|
|
|
|
|
|
|
|
for (const blob of blobs) {
|
|
|
|
const res = await downloadBlob(blob.sha256, servers);
|
|
|
|
if (!res) continue;
|
|
|
|
|
|
|
|
const type = mime.getType(blob.path);
|
|
|
|
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"]);
|
|
|
|
|
|
|
|
// 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(/\/$/, ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
// add cache headers
|
|
|
|
ctx.set("ETag", res.headers["etag"] || `"${blob.sha256}"`);
|
|
|
|
ctx.set("Cache-Control", "public, max-age=3600");
|
|
|
|
ctx.set("Last-Modified", res.headers["last-modified"] || new Date(blob.created_at * 1000).toUTCString());
|
|
|
|
|
|
|
|
ctx.status = 200;
|
|
|
|
ctx.body = res;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.status = 500;
|
|
|
|
ctx.body = "Failed to find blob";
|
2024-09-01 13:26:37 -05:00
|
|
|
});
|
|
|
|
|
2024-10-06 10:01:15 -05:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-04 10:32:57 -05:00
|
|
|
// get screenshots for websites
|
2024-10-04 11:46:17 -05:00
|
|
|
if (ENABLE_SCREENSHOTS) {
|
|
|
|
app.use(async (ctx, next) => {
|
|
|
|
if (ctx.method === "GET" && ctx.path.startsWith("/screenshot")) {
|
|
|
|
const [pubkey, etx] = basename(ctx.path).split(".");
|
2024-09-26 12:48:13 -05:00
|
|
|
|
2024-10-04 11:46:17 -05:00
|
|
|
if (pubkey) {
|
2024-10-04 11:59:50 -05:00
|
|
|
const { hasScreenshot, takeScreenshot, getScreenshotPath } = await import("./screenshots.js");
|
2024-10-04 11:46:17 -05:00
|
|
|
if (!(await hasScreenshot(pubkey))) await takeScreenshot(pubkey);
|
2024-09-26 12:48:13 -05:00
|
|
|
|
2024-10-04 11:46:17 -05:00
|
|
|
await send(ctx, getScreenshotPath(pubkey));
|
|
|
|
} else throw Error("Missing pubkey");
|
|
|
|
} else return next();
|
|
|
|
});
|
|
|
|
}
|
2024-10-04 10:32:57 -05:00
|
|
|
|
2025-01-22 12:09:06 -06:00
|
|
|
// download homepage
|
|
|
|
if (NSITE_HOMEPAGE) {
|
|
|
|
try {
|
|
|
|
const log = logger.extend("homepage");
|
|
|
|
// create the public dir
|
|
|
|
try {
|
|
|
|
fs.mkdirSync(NSITE_HOMEPAGE_DIR);
|
|
|
|
} catch (error) {}
|
|
|
|
|
|
|
|
const bin = (await import.meta.resolve("nsite-cli")).replace("file://", "");
|
|
|
|
|
|
|
|
const decode = nip19.decode(NSITE_HOMEPAGE);
|
|
|
|
if (decode.type !== "nprofile") throw new Error("NSITE_HOMEPAGE must be a valid nprofile");
|
|
|
|
|
|
|
|
// use nsite-cli to download the homepage
|
|
|
|
const args = [bin, "download", NSITE_HOMEPAGE_DIR, nip19.npubEncode(decode.data.pubkey)];
|
|
|
|
if (decode.data.relays) args.push("--relays", decode.data.relays?.join(","));
|
|
|
|
|
|
|
|
const child = spawn("node", args, { stdio: "pipe" });
|
|
|
|
|
|
|
|
child.on("spawn", () => log("Downloading..."));
|
|
|
|
child.stdout.on("data", (line) => log(line.toString("utf-8")));
|
|
|
|
child.on("error", (e) => log("Failed", e));
|
|
|
|
child.on("close", (code) => {
|
|
|
|
if (code === 0) log("Finished");
|
|
|
|
else log("Failed");
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log(`Failed to download homepage`);
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-22 08:51:46 -06:00
|
|
|
// serve static files from public
|
|
|
|
const serveOptions: serve.Options = {
|
|
|
|
hidden: true,
|
|
|
|
maxAge: 60 * 60 * 1000,
|
|
|
|
index: "index.html",
|
|
|
|
};
|
2025-01-22 12:09:06 -06:00
|
|
|
|
2025-01-22 08:51:46 -06:00
|
|
|
try {
|
2025-01-22 12:09:06 -06:00
|
|
|
const www = NSITE_HOMEPAGE_DIR;
|
2025-01-22 08:51:46 -06:00
|
|
|
fs.statSync(www);
|
|
|
|
app.use(serve(www, serveOptions));
|
|
|
|
} catch (error) {
|
|
|
|
const www = path.resolve(__dirname, "../public");
|
|
|
|
app.use(serve(www, serveOptions));
|
|
|
|
}
|
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
// start the server
|
2024-10-04 10:32:57 -05:00
|
|
|
app.listen({ host: NSITE_HOST, port: NSITE_PORT }, () => {
|
2025-01-22 12:09:06 -06:00
|
|
|
logger("Started on port", HOST);
|
2024-10-04 10:32:57 -05:00
|
|
|
});
|
|
|
|
|
2025-03-17 17:35:11 +00:00
|
|
|
// watch for invalidations
|
|
|
|
watchInvalidation();
|
2024-09-26 12:48:13 -05:00
|
|
|
|
|
|
|
process.on("unhandledRejection", (reason, promise) => {
|
|
|
|
console.error("Unhandled Rejection at:", promise, "reason:", reason);
|
2024-09-25 13:37:32 -05:00
|
|
|
});
|
2024-09-01 13:26:37 -05:00
|
|
|
|
|
|
|
async function shutdown() {
|
2025-01-22 12:09:06 -06:00
|
|
|
logger("Shutting down...");
|
2024-09-26 12:48:13 -05:00
|
|
|
pool.destroy();
|
2024-09-01 13:26:37 -05:00
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
process.addListener("SIGTERM", shutdown);
|
|
|
|
process.addListener("SIGINT", shutdown);
|