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

91 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-08-15 04:54:24 -04:00
const express = require("express");
const fetch = require("node-fetch");
const { URL } = require("url");
// Array of hostnames that will be proxied
const URL_WHITELIST = [
2022-12-29 10:38:44 -05:00
"i.ytimg.com",
"yt3.googleusercontent.com",
"cdn.glitch.global",
"cdn.statically.io",
"site-assets.fontawesome.com",
"fonts.gstatic.com",
"yt3.ggpht.com",
"tube.kuylar.dev",
"lh3.googleusercontent.com",
"is4-ssl.mzstatic.com",
"is2-ssl.mzstatic.com",
"is1-ssl.mzstatic.com",
2023-01-31 15:21:36 -05:00
"fonts.bunny.net",
"demo.matomo.org",
2023-01-02 15:05:11 -05:00
"is5-ssl.mzstatic.com",
2022-12-29 10:38:44 -05:00
"is3-ssl.mzstatic.com",
"twemoji.maxcdn.com",
"unpkg.com",
2023-02-06 09:53:42 -05:00
"youtube.com",
];
2022-08-15 04:27:40 -04:00
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
app.use(function (req, res, next) {
2022-12-29 10:38:44 -05:00
console.log(`=> ${req.method} ${req.originalUrl.slice(1)}`);
next();
});
2022-11-16 05:45:08 -05:00
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
2023-02-06 09:53:42 -05:00
res.setHeader("Cache-Control", "public, max-age=870"); // cache header
res.setHeader("poketube-cacher", "PROXY_FILES");
2022-11-16 05:45:08 -05:00
next();
});
/**
2022-12-29 10:38:44 -05:00
* @param {express.Request} req
* @param {express.Response} res
*/
const proxy = async (req, res) => {
try {
let url;
try {
2022-12-20 02:58:57 -05:00
url = new URL("https://" + req.originalUrl.slice(10));
2022-12-29 10:38:44 -05:00
} catch (e) {
console.log("==> Cannot parse URL: " + e);
return res.status(400).send("Malformed URL");
2022-08-15 04:54:24 -04:00
}
if (!URL_WHITELIST.includes(url.host)) {
console.log(`==> Refusing to proxy host ${url.host}`);
res.status(401).send(`Hostname '${url.host}' is not permitted`);
2022-08-15 04:27:40 -04:00
return;
2022-08-15 04:54:24 -04:00
}
console.log(`==> Proxying request`);
let f = await fetch(url, {
method: req.method,
});
2022-08-15 04:27:40 -04:00
f.body.pipe(res);
2022-12-29 10:38:44 -05:00
} catch (e) {
console.log(`==> Error: ${e}`);
2022-12-29 10:38:44 -05:00
res.status(500).send("Internal server error");
2022-08-15 04:54:24 -04:00
}
2022-08-15 04:27:40 -04:00
};
const listener = (req, res) => {
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-12-29 10:38:44 -05:00
app.listen(3000, () => console.log("Listening on 0.0.0.0:3000"));