Skip to content

Commit

Permalink
fix(webuis): no longer write modified index.html to disk, should fix …
Browse files Browse the repository at this point in the history
…windows problem
  • Loading branch information
Rinse12 committed Dec 1, 2024
1 parent ecf7b29 commit f674144
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/webui/daemon-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<script>window.defaultPlebbitOptions = {plebbitRpcClientsOptions: ${defaultRpcOptionString}};${defaultIpfsMedia};console.log(window.defaultPlebbitOptions, window.defaultMediaIpfsGatewayUrl)</script>`;

const modifiedIndexHtmlFileName = `index_with_rpc_settings.html`;

const modifiedIndexHtmlContent = "<!DOCTYPE html>" + defaultOptionsString + indexHtmlString.replace("<!DOCTYPE html>", "");

await fs.writeFile(path.join(webuiPath, modifiedIndexHtmlFileName), modifiedIndexHtmlContent);

return modifiedIndexHtmlFileName;
return modifiedIndexHtmlContent;
}

async function _generateRpcAuthKeyIfNotExisting(plebbitDataPath: string) {
Expand Down Expand Up @@ -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",
Expand All @@ -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 });
}
Expand Down

0 comments on commit f674144

Please sign in to comment.