diff --git a/src/main/main.ts b/src/main/main.ts index 9614ffb..b7761be 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -36,7 +36,7 @@ import { fetchPeers } from './peers'; import { SYNTHETIX_NODE_APP_CONFIG } from '../const'; import * as settings from './settings'; import http from 'http'; -import { proxy } from './proxy'; +import fetch from 'node-fetch'; logger.transports.file.level = 'info'; @@ -374,13 +374,27 @@ waitForIpfs().then(debouncedDappsUpdater).catch(logger.error); ipcMain.handle('peers', async () => fetchPeers()); http - .createServer((req, res) => { + .createServer(async (req, res) => { const id = `${req.headers.host}`.replace('.localhost:8888', ''); const dapp = DAPPS.find((dapp) => dapp.id === id); - if (dapp && dapp.url) { - req.headers.host = dapp.url; - proxy({ host: '127.0.0.1', port: 8080 }, req, res); - return; + if (dapp && dapp.qm) { + try { + const response = await fetch(`http://127.0.0.1:8080/ipfs/${dapp.qm}${req.url}`); + if (response.status !== 404) { + // @ts-ignore + res.writeHead(response.status, { + 'Content-Length': response.headers.get('content-length'), + 'Content-Type': response.headers.get('content-type'), + }); + res.end(await response.buffer()); + return; + } + } catch (e: any) { + logger.error(e); + res.writeHead(500); + res.end(e.message); + return; + } } res.writeHead(404); res.end('Not found'); diff --git a/src/main/proxy.ts b/src/main/proxy.ts deleted file mode 100644 index 5102879..0000000 --- a/src/main/proxy.ts +++ /dev/null @@ -1,32 +0,0 @@ -import http, { IncomingMessage, ServerResponse } from 'http'; -import logger from 'electron-log'; - -export function proxy( - upstream: { - host: string; - port: number; - }, - req: IncomingMessage, - res: ServerResponse -) { - const options = { - hostname: upstream.host, - port: upstream.port, - path: req.url, - method: req.method, - headers: req.headers, - }; - - const proxyReq = http.request(options, (proxyRes: IncomingMessage) => { - res.writeHead(proxyRes.statusCode!, proxyRes.headers); - proxyRes.pipe(res, { end: true }); - }); - - req.pipe(proxyReq, { end: true }); - - proxyReq.once('error', (err) => { - logger.error(`Error in proxy request: ${err.message}`); - res.writeHead(500, { 'Content-Type': 'text/plain' }); - res.end('Error occurred while processing the request.'); - }); -} diff --git a/src/renderer/DApps/Dapps.tsx b/src/renderer/DApps/Dapps.tsx index eed6584..71eb916 100644 --- a/src/renderer/DApps/Dapps.tsx +++ b/src/renderer/DApps/Dapps.tsx @@ -1,4 +1,4 @@ -import { Box, Button, Heading, Image, Link, Spinner, Flex } from '@chakra-ui/react'; +import { Box, Button, Heading, Image, Link, Spinner, Flex, Skeleton } from '@chakra-ui/react'; import { ExternalLinkIcon } from '@chakra-ui/icons'; import { useDapps } from './useDapps'; import { DappType } from '../../config'; @@ -31,9 +31,11 @@ export function Dapps() { Available DApps: - {dapps.map((dapp: DappType) => ( - - ))} + {dapps.length > 0 ? ( + dapps.map((dapp: DappType) => ) + ) : ( + + )}