2021-02-22 02:39:04 +00:00
|
|
|
import express from "express";
|
2022-03-02 15:35:59 +00:00
|
|
|
import fs from "fs";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { IncomingHttpHeaders } from "http";
|
|
|
|
import { Helmet } from "inferno-helmet";
|
|
|
|
import { matchPath, StaticRouter } from "inferno-router";
|
2021-02-22 02:39:04 +00:00
|
|
|
import { renderToString } from "inferno-server";
|
2021-07-17 20:42:55 +00:00
|
|
|
import IsomorphicCookie from "isomorphic-cookie";
|
2023-01-04 16:56:24 +00:00
|
|
|
import { GetSite, GetSiteResponse, LemmyHttp } from "lemmy-js-client";
|
2021-02-22 02:39:04 +00:00
|
|
|
import path from "path";
|
2021-07-17 20:42:55 +00:00
|
|
|
import process from "process";
|
|
|
|
import serialize from "serialize-javascript";
|
|
|
|
import { App } from "../shared/components/app/app";
|
2022-08-03 21:33:17 +00:00
|
|
|
import { httpBaseInternal } from "../shared/env";
|
2021-02-12 17:54:35 +00:00
|
|
|
import {
|
|
|
|
ILemmyConfig,
|
|
|
|
InitialFetchRequest,
|
|
|
|
IsoData,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "../shared/interfaces";
|
|
|
|
import { routes } from "../shared/routes";
|
2022-06-21 21:42:29 +00:00
|
|
|
import { initializeSite } from "../shared/utils";
|
2020-09-10 16:39:01 +00:00
|
|
|
|
2020-08-23 04:04:58 +00:00
|
|
|
const server = express();
|
2021-03-30 01:46:42 +00:00
|
|
|
const [hostname, port] = process.env["LEMMY_UI_HOST"]
|
|
|
|
? process.env["LEMMY_UI_HOST"].split(":")
|
2021-03-30 01:17:19 +00:00
|
|
|
: ["0.0.0.0", "1234"];
|
2022-03-02 15:35:59 +00:00
|
|
|
const extraThemesFolder =
|
|
|
|
process.env["LEMMY_UI_EXTRA_THEMES_FOLDER"] || "./extra_themes";
|
2020-08-23 04:04:58 +00:00
|
|
|
|
2022-08-16 20:39:09 +00:00
|
|
|
if (!process.env["LEMMY_UI_DISABLE_CSP"] && !process.env["LEMMY_UI_DEBUG"]) {
|
2022-06-01 16:43:54 +00:00
|
|
|
server.use(function (_req, res, next) {
|
|
|
|
res.setHeader(
|
|
|
|
"Content-Security-Policy",
|
2023-01-17 19:38:21 +00:00
|
|
|
`default-src 'self'; manifest-src *; connect-src *; img-src * data:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; form-action 'self'; base-uri 'self'; frame-src *`
|
2022-06-01 16:43:54 +00:00
|
|
|
);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
2022-05-25 03:33:11 +00:00
|
|
|
const customHtmlHeader = process.env["LEMMY_UI_CUSTOM_HTML_HEADER"] || "";
|
|
|
|
|
2020-08-23 04:04:58 +00:00
|
|
|
server.use(express.json());
|
|
|
|
server.use(express.urlencoded({ extended: false }));
|
2021-02-22 02:39:04 +00:00
|
|
|
server.use("/static", express.static(path.resolve("./dist")));
|
2020-08-23 04:04:58 +00:00
|
|
|
|
2021-09-06 14:25:48 +00:00
|
|
|
const robotstxt = `User-Agent: *
|
2021-09-05 22:34:40 +00:00
|
|
|
Disallow: /login
|
|
|
|
Disallow: /settings
|
|
|
|
Disallow: /create_community
|
|
|
|
Disallow: /create_post
|
2021-09-06 14:25:48 +00:00
|
|
|
Disallow: /create_private_message
|
2021-09-05 22:34:40 +00:00
|
|
|
Disallow: /inbox
|
2021-09-06 14:25:48 +00:00
|
|
|
Disallow: /setup
|
|
|
|
Disallow: /admin
|
|
|
|
Disallow: /password_change
|
2021-09-05 22:34:40 +00:00
|
|
|
Disallow: /search/
|
|
|
|
`;
|
2021-09-06 14:25:48 +00:00
|
|
|
|
|
|
|
server.get("/robots.txt", async (_req, res) => {
|
|
|
|
res.setHeader("content-type", "text/plain; charset=utf-8");
|
|
|
|
res.send(robotstxt);
|
2021-09-05 22:34:40 +00:00
|
|
|
});
|
2020-08-23 04:04:58 +00:00
|
|
|
|
2022-03-02 15:35:59 +00:00
|
|
|
server.get("/css/themes/:name", async (req, res) => {
|
|
|
|
res.contentType("text/css");
|
|
|
|
const theme = req.params.name;
|
2022-03-03 17:55:26 +00:00
|
|
|
if (!theme.endsWith(".css")) {
|
2022-03-02 15:35:59 +00:00
|
|
|
res.send("Theme must be a css file");
|
|
|
|
}
|
|
|
|
|
|
|
|
const customTheme = path.resolve(`./${extraThemesFolder}/${theme}`);
|
|
|
|
if (fs.existsSync(customTheme)) {
|
|
|
|
res.sendFile(customTheme);
|
|
|
|
} else {
|
|
|
|
const internalTheme = path.resolve(`./dist/assets/css/themes/${theme}`);
|
|
|
|
res.sendFile(internalTheme);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function buildThemeList(): string[] {
|
|
|
|
let themes = [
|
|
|
|
"litera",
|
|
|
|
"materia",
|
|
|
|
"minty",
|
|
|
|
"solar",
|
|
|
|
"united",
|
|
|
|
"cyborg",
|
|
|
|
"darkly",
|
2022-04-27 19:10:58 +00:00
|
|
|
"darkly-red",
|
2022-03-02 15:35:59 +00:00
|
|
|
"journal",
|
|
|
|
"sketchy",
|
|
|
|
"vaporwave",
|
|
|
|
"vaporwave-dark",
|
|
|
|
"i386",
|
|
|
|
"litely",
|
2022-04-27 19:10:58 +00:00
|
|
|
"litely-red",
|
2022-03-02 15:35:59 +00:00
|
|
|
"nord",
|
|
|
|
];
|
|
|
|
if (fs.existsSync(extraThemesFolder)) {
|
|
|
|
let dirThemes = fs.readdirSync(extraThemesFolder);
|
2022-03-03 17:55:26 +00:00
|
|
|
let cssThemes = dirThemes
|
|
|
|
.filter(d => d.endsWith(".css"))
|
|
|
|
.map(d => d.replace(".css", ""));
|
|
|
|
themes.push(...cssThemes);
|
2022-03-02 15:35:59 +00:00
|
|
|
}
|
|
|
|
return themes;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.get("/css/themelist", async (_req, res) => {
|
|
|
|
res.type("json");
|
|
|
|
res.send(JSON.stringify(buildThemeList()));
|
|
|
|
});
|
|
|
|
|
2021-09-06 14:25:48 +00:00
|
|
|
// server.use(cookieParser());
|
2021-02-22 02:39:04 +00:00
|
|
|
server.get("/*", async (req, res) => {
|
2021-11-17 21:23:46 +00:00
|
|
|
try {
|
2023-01-04 16:56:24 +00:00
|
|
|
const activeRoute = routes.find(route => matchPath(req.path, route));
|
2021-11-17 21:23:46 +00:00
|
|
|
const context = {} as any;
|
2023-01-04 16:56:24 +00:00
|
|
|
let auth: string | undefined = IsomorphicCookie.load("jwt", req);
|
2021-11-17 21:23:46 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
let getSiteForm: GetSite = { auth };
|
2021-11-17 21:23:46 +00:00
|
|
|
|
|
|
|
let promises: Promise<any>[] = [];
|
|
|
|
|
|
|
|
let headers = setForwardedHeaders(req.headers);
|
|
|
|
|
|
|
|
let initialFetchReq: InitialFetchRequest = {
|
|
|
|
client: new LemmyHttp(httpBaseInternal, headers),
|
|
|
|
auth,
|
|
|
|
path: req.path,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get site data first
|
|
|
|
// This bypasses errors, so that the client can hit the error on its own,
|
|
|
|
// in order to remove the jwt on the browser. Necessary for wrong jwts
|
|
|
|
let try_site: any = await initialFetchReq.client.getSite(getSiteForm);
|
|
|
|
if (try_site.error == "not_logged_in") {
|
|
|
|
console.error(
|
|
|
|
"Incorrect JWT token, skipping auth so frontend can remove jwt cookie"
|
|
|
|
);
|
2023-01-04 16:56:24 +00:00
|
|
|
getSiteForm.auth = undefined;
|
|
|
|
initialFetchReq.auth = undefined;
|
2021-11-17 21:23:46 +00:00
|
|
|
try_site = await initialFetchReq.client.getSite(getSiteForm);
|
|
|
|
}
|
|
|
|
let site: GetSiteResponse = try_site;
|
|
|
|
initializeSite(site);
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
if (activeRoute?.fetchInitialData) {
|
2021-11-17 21:23:46 +00:00
|
|
|
promises.push(...activeRoute.fetchInitialData(initialFetchReq));
|
|
|
|
}
|
|
|
|
|
|
|
|
let routeData = await Promise.all(promises);
|
|
|
|
|
|
|
|
// Redirect to the 404 if there's an API error
|
|
|
|
if (routeData[0] && routeData[0].error) {
|
|
|
|
let errCode = routeData[0].error;
|
2021-12-02 16:46:32 +00:00
|
|
|
console.error(errCode);
|
2021-12-30 15:26:45 +00:00
|
|
|
if (errCode == "instance_is_private") {
|
|
|
|
return res.redirect(`/signup`);
|
|
|
|
} else {
|
2022-03-24 20:34:04 +00:00
|
|
|
return res.send(`404: ${removeAuthParam(errCode)}`);
|
2021-12-30 15:26:45 +00:00
|
|
|
}
|
2021-11-17 21:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let isoData: IsoData = {
|
|
|
|
path: req.path,
|
|
|
|
site_res: site,
|
|
|
|
routeData,
|
|
|
|
};
|
|
|
|
|
|
|
|
const wrapper = (
|
|
|
|
<StaticRouter location={req.url} context={isoData}>
|
2022-06-21 21:42:29 +00:00
|
|
|
<App />
|
2021-11-17 21:23:46 +00:00
|
|
|
</StaticRouter>
|
|
|
|
);
|
|
|
|
if (context.url) {
|
|
|
|
return res.redirect(context.url);
|
|
|
|
}
|
|
|
|
|
2022-04-13 15:33:00 +00:00
|
|
|
const eruda = (
|
|
|
|
<>
|
|
|
|
<script src="//cdn.jsdelivr.net/npm/eruda"></script>
|
|
|
|
<script>eruda.init();</script>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
const erudaStr = process.env["LEMMY_UI_DEBUG"] ? renderToString(eruda) : "";
|
2021-11-17 21:23:46 +00:00
|
|
|
const root = renderToString(wrapper);
|
|
|
|
const helmet = Helmet.renderStatic();
|
2020-09-11 04:03:01 +00:00
|
|
|
|
2022-08-04 18:21:20 +00:00
|
|
|
const config: ILemmyConfig = { wsHost: process.env.LEMMY_UI_LEMMY_WS_HOST };
|
2021-02-12 17:54:35 +00:00
|
|
|
|
2021-11-17 21:23:46 +00:00
|
|
|
res.send(`
|
2020-09-06 16:15:25 +00:00
|
|
|
<!DOCTYPE html>
|
2020-09-11 04:03:01 +00:00
|
|
|
<html ${helmet.htmlAttributes.toString()} lang="en">
|
2020-09-06 16:15:25 +00:00
|
|
|
<head>
|
2023-01-04 16:56:24 +00:00
|
|
|
<script>window.isoData = ${JSON.stringify(isoData)}</script>
|
2021-02-12 17:54:35 +00:00
|
|
|
<script>window.lemmyConfig = ${serialize(config)}</script>
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2022-04-13 15:33:00 +00:00
|
|
|
<!-- A remote debugging utility for mobile -->
|
|
|
|
${erudaStr}
|
2021-07-18 15:08:24 +00:00
|
|
|
|
2022-05-25 03:33:11 +00:00
|
|
|
<!-- Custom injected script -->
|
|
|
|
${customHtmlHeader}
|
|
|
|
|
2020-09-11 04:03:01 +00:00
|
|
|
${helmet.title.toString()}
|
|
|
|
${helmet.meta.toString()}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
<!-- Required meta tags -->
|
|
|
|
<meta name="Description" content="Lemmy">
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
|
|
|
|
2020-09-10 02:38:57 +00:00
|
|
|
<!-- Web app manifest -->
|
2020-09-10 16:39:01 +00:00
|
|
|
<link rel="manifest" href="/static/assets/manifest.webmanifest">
|
2020-09-10 02:38:57 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
<!-- Styles -->
|
2020-09-07 03:41:46 +00:00
|
|
|
<link rel="stylesheet" type="text/css" href="/static/styles/styles.css" />
|
2020-10-26 14:28:17 +00:00
|
|
|
|
|
|
|
<!-- Current theme and more -->
|
|
|
|
${helmet.link.toString()}
|
2021-07-16 16:51:54 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
</head>
|
|
|
|
|
2020-09-11 04:03:01 +00:00
|
|
|
<body ${helmet.bodyAttributes.toString()}>
|
2020-09-09 20:33:40 +00:00
|
|
|
<noscript>
|
2020-09-14 15:26:24 +00:00
|
|
|
<div class="alert alert-danger rounded-0" role="alert">
|
2020-09-15 13:20:19 +00:00
|
|
|
<b>Javascript is disabled. Actions will not work.</b>
|
2020-09-14 15:26:24 +00:00
|
|
|
</div>
|
2020-09-09 20:33:40 +00:00
|
|
|
</noscript>
|
2020-10-26 07:09:44 +00:00
|
|
|
|
2020-09-11 04:03:01 +00:00
|
|
|
<div id='root'>${root}</div>
|
2020-09-24 22:03:03 +00:00
|
|
|
<script defer src='/static/js/client.js'></script>
|
2020-09-06 16:15:25 +00:00
|
|
|
</body>
|
|
|
|
</html>
|
2020-08-23 04:04:58 +00:00
|
|
|
`);
|
2021-11-17 21:23:46 +00:00
|
|
|
} catch (err) {
|
2021-12-02 16:46:32 +00:00
|
|
|
console.error(err);
|
2022-03-24 20:34:04 +00:00
|
|
|
return res.send(`404: ${removeAuthParam(err)}`);
|
2021-11-17 21:23:46 +00:00
|
|
|
}
|
2020-08-23 04:04:58 +00:00
|
|
|
});
|
2020-11-12 21:56:46 +00:00
|
|
|
|
2021-03-29 15:35:32 +00:00
|
|
|
server.listen(Number(port), hostname, () => {
|
|
|
|
console.log(`http://${hostname}:${port}`);
|
2020-08-23 04:04:58 +00:00
|
|
|
});
|
|
|
|
|
2021-07-16 18:29:22 +00:00
|
|
|
function setForwardedHeaders(headers: IncomingHttpHeaders): {
|
|
|
|
[key: string]: string;
|
|
|
|
} {
|
2023-01-04 16:56:24 +00:00
|
|
|
let out: { [key: string]: string } = {};
|
|
|
|
if (headers.host) {
|
|
|
|
out.host = headers.host;
|
2020-11-12 21:56:46 +00:00
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
let realIp = headers["x-real-ip"];
|
|
|
|
if (realIp) {
|
|
|
|
out["x-real-ip"] = realIp as string;
|
|
|
|
}
|
|
|
|
let forwardedFor = headers["x-forwarded-for"];
|
|
|
|
if (forwardedFor) {
|
|
|
|
out["x-forwarded-for"] = forwardedFor as string;
|
2020-11-12 21:56:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
2020-08-23 04:04:58 +00:00
|
|
|
}
|
2020-09-10 16:39:01 +00:00
|
|
|
|
2021-02-22 02:39:04 +00:00
|
|
|
process.on("SIGINT", () => {
|
|
|
|
console.info("Interrupted");
|
2020-09-10 16:39:01 +00:00
|
|
|
process.exit(0);
|
|
|
|
});
|
2022-03-24 20:34:04 +00:00
|
|
|
|
|
|
|
function removeAuthParam(err: any): string {
|
|
|
|
return removeParam(err.toString(), "auth");
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeParam(url: string, parameter: string): string {
|
|
|
|
return url
|
|
|
|
.replace(new RegExp("[?&]" + parameter + "=[^&#]*(#.*)?$"), "$1")
|
|
|
|
.replace(new RegExp("([?&])" + parameter + "=[^&]*&"), "$1");
|
|
|
|
}
|