diff --git a/src/webui/daemon-server.ts b/src/webui/daemon-server.ts index 7011548..82e3da9 100644 --- a/src/webui/daemon-server.ts +++ b/src/webui/daemon-server.ts @@ -6,20 +6,16 @@ import express from "express"; //@ts-expect-error import type { InputPlebbitOptions } from "@plebbit/plebbit-js/dist/node/types"; -async function _writeModifiedIndexHtmlWithDefaultSettings(webuiPath: string, webuiName: string, ipfsGatewayPort: number) { +async function _generateModifiedIndexHtmlWithRpcSettings(webuiPath: string, webuiName: string, ipfsGatewayPort: number) { const indexHtmlString = (await fs.readFile(path.join(webuiPath, "index.html"))).toString(); const defaultRpcOptionString = `[window.location.origin.replace("https://", "wss://").replace("http://", "ws://") + window.location.pathname.split('/' + '${webuiName}')[0]]`; // Ipfs media only locally because ipfs gateway doesn't allow remote connections const defaultIpfsMedia = `if (window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1")window.defaultMediaIpfsGatewayUrl = 'http://' + window.location.hostname + ':' + ${ipfsGatewayPort}`; const defaultOptionsString = ``; - const modifiedIndexHtmlFileName = `index_with_rpc_settings.html`; - const modifiedIndexHtmlContent = "" + defaultOptionsString + indexHtmlString.replace("", ""); - await fs.writeFile(path.join(webuiPath, modifiedIndexHtmlFileName), modifiedIndexHtmlContent); - - return modifiedIndexHtmlFileName; + return modifiedIndexHtmlContent; } async function _generateRpcAuthKeyIfNotExisting(plebbitDataPath: string) { @@ -65,36 +61,35 @@ export async function startDaemonServer(rpcUrl: URL, ipfsGatewayUrl: URL, plebbi const webuiDirPath = path.join(webuisDir, webuiNameWithVersion); const webuiName = webuiNameWithVersion.split("-")[0]; // should be "seedit", "plebchan", "plebones" - const modifiedIndexHtmlFileName = await _writeModifiedIndexHtmlWithDefaultSettings( + const modifiedIndexHtmlString = await _generateModifiedIndexHtmlWithRpcSettings( webuiDirPath, webuiName, Number(ipfsGatewayUrl.port) ); const endpointLocal = `/${webuiName}`; + webuiExpressApp.use(endpointLocal, express.static(webuiDirPath, { index: false })); webuiExpressApp.get(endpointLocal, (req, res, next) => { - res.setHeader("Cache-Control", "public, max-age=3600"); const isLocal = req.socket.localAddress && req.socket.localAddress === req.socket.remoteAddress; log( "Received local connection request for webui", - endpointRemote, + endpointLocal, "with socket.localAddress", req.socket.localAddress, "and socket.remoteAddress", req.socket.remoteAddress ); if (!isLocal) res.status(403).send("This endpoint does not exist for remote connections"); - else next(); + else res.type("html").send(modifiedIndexHtmlString); }); - webuiExpressApp.use(endpointLocal, express.static(webuiDirPath, { index: modifiedIndexHtmlFileName, cacheControl: false })); const endpointRemote = `/${rpcAuthKey}/${webuiName}`; + webuiExpressApp.use(endpointRemote, express.static(webuiDirPath, { index: false })); webuiExpressApp.get(endpointRemote, (req, res, next) => { - res.setHeader("Cache-Control", "public, max-age=3600"); const isLocal = req.socket.localAddress && req.socket.localAddress === req.socket.remoteAddress; log( "Received remote connection request for webui", - endpointRemote, + endpointLocal, "with socket.localAddress", req.socket.localAddress, "and socket.remoteAddress", @@ -105,9 +100,8 @@ export async function startDaemonServer(rpcUrl: URL, ipfsGatewayUrl: URL, plebbi if (isLocal) { res.redirect(`http://localhost:${rpcUrl.port}/${webuiName}`); - } else next(); + } else res.type("html").send(modifiedIndexHtmlString); }); - webuiExpressApp.use(endpointRemote, express.static(webuiDirPath, { index: modifiedIndexHtmlFileName, cacheControl: false })); webuis.push({ name: webuiName, endpointLocal, endpointRemote }); }