/* eslint-disable no-console */ import Inject from "@entity-access/entity-access/dist/di/di.js"; import DBConfig from "../../server/services/DBConfig.js"; import { ImagesFolder } from "../../common/ImagesFolder.js"; import Content, { TempFileResult, FileResult } from "@entity-access/server-pages/dist/Content.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import { renderUIClient } from "../../server/services/ui/renderUIClient.js"; import ScopeVars from "../../common/ScopeVars.js"; import WebSiteContentService from "../../server/services/files/WebSiteContentService.js"; import HttpProxyService from "../../server/services/http/HttpProxyService.js"; import sleep from "../../common/sleep.js"; import { globalEnv } from "../../common/globalEnv.js"; import HtmlForEmailService from "../../server/services/html/HtmlForEmailService.js"; import HtmlDocument from "@entity-access/server-pages/dist/html/HtmlDocument.js"; import XNode from "@entity-access/server-pages/dist/html/XNode.js"; import notFoundRedirectRoot from "../../common/notFoundRedirectRoot.js"; const { http: { notFoundDelay } } = globalEnv; const isUnsafeFolder = (file: string) => /^\.(git|github|vscode|settings|config|docker)$/.test(file); const isUnsafeFile = (file: string) => /\.(aspx?|action|php|pl|rb|ts|env|backup|cgi|exe|sql|tar|7z|bz2|zst|gz|tgz|zip|bak|rar|env|yml|json)$/i.test(file); export default class extends Page { @Inject config: DBConfig; @Inject siteContentService: WebSiteContentService; @Inject htmlForEmail: HtmlForEmailService; req: any; async run() { const { forceDownload } = this.query; const file = this.childPath[0]; const last = this.childPath.at(-1); // check if the host name is part of any website.. const host = this.request.hostName; const webSite = await this.siteContentService.getCurrentSnapshotFolder(host); if (webSite) { // this is a static web site... // if path is empty... load /index.html.... ScopeVars.fromScope(this).fromWebSite = true; let notFound = false; let path = [ ... this.childPath]; if (!file) { path = ["index.html"]; } let appFile = await this.siteContentService.getSnapshotFile(webSite, path); if (!appFile) { if (webSite.remoteUrl) { // proxy remote url... const url = webSite.remoteUrl + this.request.URL.pathname + this.request.URL.search; if (webSite.isTemplate) { return this.redirect(url); } return HttpProxyService.fetch(this, url); } if (file === "favicon.ico") { this.cacheControl = "public, max-age=8600"; return new FileResult(ImagesFolder.file("favicon.ico")); } if (file === "robots.txt") { console.error(`404: ${this.request?.URL?.toString()}`); return this.notFound(true); } // we will check if slug exists... appFile = await this.siteContentService.getSnapshotFile(webSite, ["index.html"]); if (appFile === null) { if (notFoundDelay) { await sleep(notFoundDelay); } console.error(`404: ${this.request?.URL?.toString()}`); return this.notFound(true); } notFound = true; } if (notFound) { if (notFoundDelay) { await sleep(notFoundDelay); } if (appFile) { return notFoundRedirectRoot(); } return this.notFound(); } return new TempFileResult( appFile, { contentDisposition: forceDownload ? "attachment" : "inline", etag: false } ); } if (file) { if (isUnsafeFolder(file)) { if (notFoundDelay) { await sleep(notFoundDelay); } // rate limiter will catch 404 return this.notFoundWithPenalty(true); } if (isUnsafeFile(last)){ if (notFoundDelay) { await sleep(notFoundDelay); } // rate limiter will catch 404 return this.notFoundWithPenalty(true); } if (file.startsWith("robots") && file.endsWith(".txt")) { return this.notFound(true); } if (file === "favicon.ico") { return new FileResult(ImagesFolder.file("favicon.ico")); } } if (host !== globalEnv.host && host !== "localhost") { const isMailDomain = await this.siteContentService.isMailDomain(host); if (!isMailDomain) { if (notFoundDelay) { await sleep(notFoundDelay); } console.error(`404: Not Mail Domain ${this.request?.URL?.toString()} and not root ${globalEnv.host}`); return this.notFound(true); } // we need to do this only for active domains... // this is most likely a failed url... const c = await renderUIClient(this); // eslint-disable-next-line @typescript-eslint/ban-ts-comment // c.status = 404; c.suppressLog = true; console.error(`404: ${this.request?.URL?.toString()}`); return c; } return renderUIClient(this); } protected notFoundWithPenalty(suppressLog = true): Content | Promise { return Content.html( Not found The page you are looking for is not found.
{this.url} not found
, { status: 404, suppressLog, headers: { "x-error-penalty": "10" } } ); } }