-import cookieParser = require('cookie-parser');
-import serialize from 'serialize-javascript';
-import express from 'express';
-import { StaticRouter } from 'inferno-router';
-import { renderToString } from 'inferno-server';
-import { matchPath } from 'inferno-router';
-import path = require('path');
-import { App } from '../shared/components/app';
-import { routes } from '../shared/routes';
-import IsomorphicCookie from 'isomorphic-cookie';
+import { None, Option } from "@sniptt/monads";
+import { serialize as serializeO } from "class-transformer";
+import express from "express";
+import fs from "fs";
+import { IncomingHttpHeaders } from "http";
+import { Helmet } from "inferno-helmet";
+import { matchPath, StaticRouter } from "inferno-router";
+import { renderToString } from "inferno-server";
+import IsomorphicCookie from "isomorphic-cookie";
+import { GetSite, GetSiteResponse, LemmyHttp, toOption } from "lemmy-js-client";
+import path from "path";
+import process from "process";
+import serialize from "serialize-javascript";
+import { App } from "../shared/components/app/app";
+import { SYMBOLS } from "../shared/components/common/symbols";
+import { httpBaseInternal } from "../shared/env";
+import {
+ ILemmyConfig,
+ InitialFetchRequest,
+ IsoData,
+} from "../shared/interfaces";
+import { routes } from "../shared/routes";
+import { initializeSite } from "../shared/utils";
+
const server = express();
-const port = 1234;
+const [hostname, port] = process.env["LEMMY_UI_HOST"]
+ ? process.env["LEMMY_UI_HOST"].split(":")
+ : ["0.0.0.0", "1234"];
+const extraThemesFolder =
+ process.env["LEMMY_UI_EXTRA_THEMES_FOLDER"] || "./extra_themes";
+
+if (!process.env["LEMMY_UI_DISABLE_CSP"]) {
+ server.use(function (_req, res, next) {
+ res.setHeader(
+ "Content-Security-Policy",
+ `default-src 'none'; connect-src *; img-src * data:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; form-action 'self'; base-uri 'self'`
+ );
+ next();
+ });
+}
+const customHtmlHeader = process.env["LEMMY_UI_CUSTOM_HTML_HEADER"] || "";
server.use(express.json());
server.use(express.urlencoded({ extended: false }));
-server.use('/assets', express.static(path.resolve('./dist/assets')));
-server.use('/static', express.static(path.resolve('./dist/client')));
+server.use("/static", express.static(path.resolve("./dist")));
-server.use(cookieParser());
+const robotstxt = `User-Agent: *
+Disallow: /login
+Disallow: /settings
+Disallow: /create_community
+Disallow: /create_post
+Disallow: /create_private_message
+Disallow: /inbox
+Disallow: /setup
+Disallow: /admin
+Disallow: /password_change
+Disallow: /search/
+`;
-server.get('/*', (req, res) => {
- const activeRoute = routes.find(route => matchPath(req.url, route)) || {};
- console.log(activeRoute);
- const context = {} as any;
- const isoData = {
- name: 'fishing sux',
- };
- let auth: string = IsomorphicCookie.load('jwt', req);
-
- const wrapper = (
- <StaticRouter location={req.url} context={context}>
- <App />
- </StaticRouter>
- );
- if (context.url) {
- return res.redirect(context.url);
+server.get("/robots.txt", async (_req, res) => {
+ res.setHeader("content-type", "text/plain; charset=utf-8");
+ res.send(robotstxt);
+});
+
+server.get("/css/themes/:name", async (req, res) => {
+ res.contentType("text/css");
+ const theme = req.params.name;
+ if (!theme.endsWith(".css")) {
+ res.send("Theme must be a css file");
}
- res.send(`
+ 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",
+ "darkly-red",
+ "journal",
+ "sketchy",
+ "vaporwave",
+ "vaporwave-dark",
+ "i386",
+ "litely",
+ "litely-red",
+ "nord",
+ ];
+ if (fs.existsSync(extraThemesFolder)) {
+ let dirThemes = fs.readdirSync(extraThemesFolder);
+ let cssThemes = dirThemes
+ .filter(d => d.endsWith(".css"))
+ .map(d => d.replace(".css", ""));
+ themes.push(...cssThemes);
+ }
+ return themes;
+}
+
+server.get("/css/themelist", async (_req, res) => {
+ res.type("json");
+ res.send(JSON.stringify(buildThemeList()));
+});
+
+// server.use(cookieParser());
+server.get("/*", async (req, res) => {
+ try {
+ const activeRoute = routes.find(route => matchPath(req.path, route)) || {};
+ const context = {} as any;
+ let auth: Option<string> = toOption(IsomorphicCookie.load("jwt", req));
+
+ let getSiteForm = new GetSite({ auth });
+
+ 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"
+ );
+ getSiteForm.auth = None;
+ initialFetchReq.auth = None;
+ try_site = await initialFetchReq.client.getSite(getSiteForm);
+ }
+ let site: GetSiteResponse = try_site;
+ initializeSite(site);
+
+ if (activeRoute.fetchInitialData) {
+ 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;
+ console.error(errCode);
+ if (errCode == "instance_is_private") {
+ return res.redirect(`/signup`);
+ } else {
+ return res.send(`404: ${removeAuthParam(errCode)}`);
+ }
+ }
+
+ let isoData: IsoData = {
+ path: req.path,
+ site_res: site,
+ routeData,
+ };
+
+ const wrapper = (
+ <StaticRouter location={req.url} context={isoData}>
+ <App />
+ </StaticRouter>
+ );
+ if (context.url) {
+ return res.redirect(context.url);
+ }
+
+ const eruda = (
+ <>
+ <script src="//cdn.jsdelivr.net/npm/eruda"></script>
+ <script>eruda.init();</script>
+ </>
+ );
+ const erudaStr = process.env["LEMMY_UI_DEBUG"] ? renderToString(eruda) : "";
+ const root = renderToString(wrapper);
+ const symbols = renderToString(SYMBOLS);
+ const helmet = Helmet.renderStatic();
+
+ const config: ILemmyConfig = { wsHost: process.env.LEMMY_UI_LEMMY_WS_HOST };
+
+ res.send(`
<!DOCTYPE html>
- <html lang="en">
+ <html ${helmet.htmlAttributes.toString()} lang="en">
<head>
- <script>window.isoData = ${serialize(isoData)}</script>
+ <script>window.isoData = ${serializeO(isoData)}</script>
+ <script>window.lemmyConfig = ${serialize(config)}</script>
+
+ <!-- A remote debugging utility for mobile -->
+ ${erudaStr}
+
+ <!-- Custom injected script -->
+ ${customHtmlHeader}
+
+ ${helmet.title.toString()}
+ ${helmet.meta.toString()}
<!-- 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">
- <!-- Icons -->
- <link rel="shortcut icon" type="image/svg+xml" href="/assets/favicon.svg" />
- <link rel="apple-touch-icon" href="/assets/apple-touch-icon.png" />
+ <!-- Web app manifest -->
+ <link rel="manifest" href="/static/assets/manifest.webmanifest">
<!-- Styles -->
- <link rel="stylesheet" type="text/css" href="/assets/css/tribute.css" />
- <link rel="stylesheet" type="text/css" href="/assets/css/toastify.css" />
- <link rel="stylesheet" type="text/css" href="/assets/css/choices.min.css" />
- <link rel="stylesheet" type="text/css" href="/assets/css/tippy.css" />
- <link rel="stylesheet" type="text/css" href="/assets/css/themes/litely.min.css" id="default-light" media="(prefers-color-scheme: light)" />
- <link rel="stylesheet" type="text/css" href="/assets/css/themes/darkly.min.css" id="default-dark" media="(prefers-color-scheme: no-preference), (prefers-color-scheme: dark)" />
- <link rel="stylesheet" type="text/css" href="/assets/css/main.css" />
-
- <!-- Scripts -->
- <script async src="/assets/libs/sortable/sortable.min.js"></script>
+ <link rel="stylesheet" type="text/css" href="/static/styles/styles.css" />
+
+ <!-- Current theme and more -->
+ ${helmet.link.toString()}
+
+ <!-- Icons -->
+ ${symbols}
+
</head>
- <body>
- <div id='root'>${renderToString(wrapper)}</div>
- <script src='./static/bundle.js'></script>
+ <body ${helmet.bodyAttributes.toString()}>
+ <noscript>
+ <div class="alert alert-danger rounded-0" role="alert">
+ <b>Javascript is disabled. Actions will not work.</b>
+ </div>
+ </noscript>
+
+ <div id='root'>${root}</div>
+ <script defer src='/static/js/client.js'></script>
</body>
</html>
`);
+ } catch (err) {
+ console.error(err);
+ return res.send(`404: ${removeAuthParam(err)}`);
+ }
});
-let Server = server.listen(port, () => {
- console.log(`http://localhost:${port}`);
+
+server.listen(Number(port), hostname, () => {
+ console.log(`http://${hostname}:${port}`);
});
-/**
- * Used to restart server by fuseBox
- */
-export async function shutdown() {
- Server.close();
- Server = undefined;
+function setForwardedHeaders(headers: IncomingHttpHeaders): {
+ [key: string]: string;
+} {
+ let out = {
+ host: headers.host,
+ };
+ if (headers["x-real-ip"]) {
+ out["x-real-ip"] = headers["x-real-ip"];
+ }
+ if (headers["x-forwarded-for"]) {
+ out["x-forwarded-for"] = headers["x-forwarded-for"];
+ }
+
+ return out;
+}
+
+process.on("SIGINT", () => {
+ console.info("Interrupted");
+ process.exit(0);
+});
+
+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");
}