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

56 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-08-15 13:41:52 -04:00
const fs = require("fs");
2022-08-15 04:54:24 -04:00
const express = require("express");
const fetch = require("node-fetch");
2022-08-15 04:27:40 -04:00
const htmlParser = require("node-html-parser");
const app = express();
2022-08-15 04:54:24 -04:00
app.use(express.json()); // for parsing application/json
app.use(express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
2022-11-16 05:45:08 -05:00
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
next();
});
2022-08-15 04:54:24 -04:00
let Proxy = async (req, res) => {
const url = "https://" + req.originalUrl.slice(10);
2022-08-15 04:27:40 -04:00
let f = await fetch(url, {
method: req.method,
});
2022-08-15 04:54:24 -04:00
if (false && f.headers.get("content-type").includes("html")) {
2022-08-15 04:27:40 -04:00
const body = await f.text();
2022-08-15 04:54:24 -04:00
if (false && !htmlParser.valid(body)) {
2022-08-15 04:27:40 -04:00
console.warn(`[ERROR] Invalid HTML at ${url}`);
f.body.pipe(res);
return;
2022-08-15 04:54:24 -04:00
}
2022-08-15 04:27:40 -04:00
const root = htmlParser.parse(body);
2022-08-15 04:54:24 -04:00
let html = root.childNodes.filter(
(x) => x.tagName && x.tagName.toLowerCase() == "html"
)[0];
if (!html) {
2022-08-15 04:27:40 -04:00
console.warn(`[ERROR] No <html> at ${url}`);
res.send(body);
return;
2022-08-15 04:54:24 -04:00
}
2022-08-15 04:27:40 -04:00
res.send(html.toString());
2022-08-15 04:54:24 -04:00
} else {
2022-08-15 04:27:40 -04:00
f.body.pipe(res);
2022-08-15 04:54:24 -04:00
}
2022-08-15 04:27:40 -04:00
};
const listener = (req, res) => {
2022-08-15 04:54:24 -04:00
Proxy(req, res);
2022-08-15 04:27:40 -04:00
};
2022-11-16 05:45:08 -05:00
app.get("/", (req, res) =>
res.redirect(`https://poketube.fun/watch?v=l3eww1dnd0k`)
);
2022-08-15 05:58:08 -04:00
2022-08-15 04:54:24 -04:00
app.all("/*", listener);
2022-08-15 04:27:40 -04:00
2022-08-15 04:54:24 -04:00
app.listen(3000, () => {});