1
0
Fork 0
mirror of https://codeberg.org/ashley/poke.git synced 2024-11-17 09:14:39 -05:00
poke/p/server.js

52 lines
1.2 KiB
Text
Raw Normal View History

2022-08-15 04:27:40 -04:00
const fs = require("fs");
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
let f = await fetch(url, {
method: req.method,
});
if(false && f.headers.get('content-type').includes("html")){
const body = await f.text();
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) {
console.warn(`[ERROR] No <html> at ${url}`);
res.send(body);
return;
};
res.send(html.toString());
} else {
f.body.pipe(res);
};
};
const listener = (req, res) => {
Proxy(req, res)
};
app.get("/", (req, res) => res.redirect(`/https://www.google.com/`));
app.all('/*', listener);
app.listen(3000, () => {
})