From c8ef0ab0229b6aec4fdd000de04479a3f688c2c9 Mon Sep 17 00:00:00 2001 From: Ashley Date: Mon, 15 Aug 2022 10:54:24 +0200 Subject: [PATCH] reformat file --- p/server.js | 53 +++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/p/server.js b/p/server.js index a6686d3e..d8dd59ef 100644 --- a/p/server.js +++ b/p/server.js @@ -1,51 +1,48 @@ const fs = require("fs"); -const express = require('express'); -const fetch = require('node-fetch'); +const express = require("express"); +const fetch = require("node-fetch"); const htmlParser = require("node-html-parser"); const app = express(); - -app.use(express.json()) // for parsing application/json -app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded - let Proxy = async (req, res) => { - var url = req.originalUrl - +app.use(express.json()); // for parsing application/json +app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded + +let Proxy = async (req, res) => { + const url = "https://" + req.originalUrl.slice(10); + let f = await fetch(url, { method: req.method, }); - if(false && f.headers.get('content-type').includes("html")){ + if (false && f.headers.get("content-type").includes("html")) { const body = await f.text(); - if(false && !htmlParser.valid(body)) { + if (false && !htmlParser.valid(body)) { console.warn(`[ERROR] Invalid HTML at ${url}`); f.body.pipe(res); return; - }; + } const root = htmlParser.parse(body); - let html = root.childNodes.filter((x) => x.tagName && x.tagName.toLowerCase() == "html")[0]; - - if(!html) { + let html = root.childNodes.filter( + (x) => x.tagName && x.tagName.toLowerCase() == "html" + )[0]; + + if (!html) { console.warn(`[ERROR] No at ${url}`); res.send(body); return; - }; - - - res.send(html.toString()); - } else { - f.body.pipe(res); - }; -}; + } + res.send(html.toString()); + } else { + f.body.pipe(res); + } +}; const listener = (req, res) => { - Proxy(req, res) + Proxy(req, res); }; - app.get("/", (req, res) => res.redirect(`/https://www.google.com/`)); -app.all('/*', listener); +app.all("/*", listener); - -app.listen(3000, () => { - }) +app.listen(3000, () => {});