/* eslint-disable no-console */ import Inject from "@entity-access/entity-access/dist/di/di.js"; import { Prepare } from "@entity-access/server-pages/dist/decorators/Prepare.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import SocialMailContext from "../../../../../server/model/SocialMailContext.js"; import EncryptionService from "../../../../../server/services/encryption/EncryptionService.js"; import { secureFetch } from "../../../../../server/services/root-certs/RootCertificates.js"; import { Agent } from "undici"; import { constants } from "crypto"; import Content from "@entity-access/server-pages/dist/Content.js"; import { Readable } from "stream"; import ServerLogger from "@entity-access/server-pages/dist/core/ServerLogger.js"; const dispatcher = new Agent({ connect: { secureOptions: constants.SSL_OP_LEGACY_SERVER_CONNECT } }); @Prepare.authorize export default class extends Page { @Inject db: SocialMailContext; @Inject private encryptionService: EncryptionService; async run() { const [eEmailID, emailID, ... link] = this.childPath as any[]; // we will do some validation here ... const dEmailID = this.encryptionService.general.decrypt(eEmailID) as any; if (dEmailID !== emailID.toString()) { return this.notFound(); } const q = this.request.URL.search; const trailingSlash = this.request.path.endsWith("/") ? "/" : ""; const url = `https://${link.join("/")}${trailingSlash}${q}`; try { this.cacheControl = "public, max-age=2592000, immutable"; const rs = await secureFetch(url, { redirect : "follow", dispatcher } as any); const status = rs.status; const headers = {}; // let compress = false; let contentType = void 0; for (const [key, value] of rs.headers.entries()) { if (key.startsWith(":")) { continue; } switch(key) { case "keep-alive": case "connection": case "cache-control": case "upgrade": case "transfer-encoding": case "alt-svc": continue; case "content-type": case "content-length": contentType = value; headers[key] = value; continue; case "content-encoding": // if (value === "gzip") { // compress = true; // } headers[key] = value; continue; default: if (/^access\-/i.test(key)) { headers[key] = value; } continue; } } if (status >= 400) { ServerLogger.error(`Email Content Fetch Failed ${status} for ${url}`); } const rsHeaders = rs.headers; if (rsHeaders.has("content-length")) { headers["content-length"] = rsHeaders.get("content-length"); } if (rsHeaders.has("content-encoding")) { headers["content-encoding"] = rsHeaders.get("content-encoding"); } headers["x-error-penalty"] = "0"; return new Content({ reader: Readable.fromWeb(rs.body as any), headers, contentType, status, suppressLog: true }); } catch (error) { this.cacheControl = "public, max-age=8600, immutable"; ServerLogger.error(`Email Content Fetch Failed for ${url}`); const cause = error.cause; let body = error.stack ?? error; if (cause) { body += `\n${cause?.stack ?? cause}`; } return this.content({ body, headers: { "x-error-penalty": "0" }, contentType: "text/plain", status: 500, suppressLog: true }); } } }