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

177 lines
4.4 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");
2023-11-22 11:11:01 -05:00
const { Readable } = require("node:stream");
// 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",
2024-04-12 03:23:49 -04:00
"cdn.jsdelivr.net",
2022-12-29 10:38:44 -05:00
"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-11-06 13:58:30 -05:00
"lite.duckduckgo.com",
2023-02-06 09:53:42 -05:00
"youtube.com",
2023-03-02 14:52:39 -05:00
"returnyoutubedislikeapi.com",
2023-09-28 12:31:35 -04:00
"cdn.zptr.cc",
2023-03-02 14:52:39 -05:00
"inv.vern.cc",
"invidious.privacydev.net",
"inv.zzls.xyz",
"vid.puffyan.us",
"invidious.lidarshield.cloud",
"invidious.epicsite.xyz",
2023-09-27 14:51:45 -04:00
"invidious.esmailelbob.xyz",
];
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();
});
2023-11-29 14:35:08 -05:00
app.use(function (_req, res, next) {
2022-11-16 05:45:08 -05:00
res.header("Access-Control-Allow-Origin", "*");
2024-01-20 12:11:42 -05:00
res.setHeader("Cache-Control", "public, max-age=864000"); // cache header
2023-02-06 09:53:42 -05:00
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) => {
2023-11-22 11:11:01 -05:00
const { fetch } = await import("undici")
2024-01-20 12:11:42 -05:00
res.setHeader("Cache-Control", "public, max-age=864000"); // cache header
2023-11-22 11:11:01 -05:00
try {
let url;
try {
2023-11-06 23:07:18 -05:00
url = new URL("https://" + req.originalUrl.slice(8));
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
}
2024-06-17 09:53:54 -04:00
// Sanity check, to avoid being used as an open proxy
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`);
2023-09-23 03:11:51 -04:00
let f = await fetch(url + `?cachefixer=${btoa(Date.now())}`, {
method: req.method,
});
2023-11-22 11:11:01 -05:00
Readable.fromWeb(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
};
2023-09-23 14:26:08 -04:00
app.get("/", (req, res) => {
var json = {
2023-09-27 14:51:45 -04:00
status: "200",
2024-01-20 12:11:42 -05:00
version: "1.3.0",
2023-09-23 14:26:08 -04:00
URL_WHITELIST,
2024-01-20 12:11:42 -05:00
cache: "max-age-864000",
2023-09-27 14:51:45 -04:00
};
res.json(json);
2023-09-23 14:26:08 -04:00
});
2023-09-27 14:51:45 -04:00
2023-11-06 13:58:30 -05:00
const apiUrls = [
2023-11-07 12:33:19 -05:00
"https://returnyoutubedislikeapi.com/votes?videoId=",
2023-11-09 02:05:55 -05:00
"https://prod-poketube.testing.poketube.fun/api?v=",
2023-11-09 13:34:57 -05:00
"https://ipv6-t.poketube.fun/api?v="
2023-11-06 13:58:30 -05:00
];
2023-03-09 14:41:01 -05:00
// Define a cache object
const cache = {};
2023-11-09 13:36:55 -05:00
app.get("/api", async (req, res) => {
2023-11-07 04:34:14 -05:00
const { fetch } = await import("undici")
2023-11-06 13:58:30 -05:00
try {
const cacheKey = req.query.v;
// Check if the result is already cached
if (cache[cacheKey] && Date.now() - cache[cacheKey].timestamp < 3600000) {
// If the cached result is less than 1 hour old, return it
const cachedData = cache[cacheKey].data;
const cachedDate = new Date(cache[cacheKey].timestamp);
return res.json(cachedData);
}
// Initialize an array to store errors when trying different URLs
const errors = [];
for (const apiUrl of apiUrls) {
try {
// Fetch data from the current URL
const engagement = await fetch(apiUrl + req.query.v).then((res) => res.json());
// Cache the result for future requests
cache[cacheKey] = {
data: engagement,
timestamp: Date.now(),
};
res.json(engagement);
return; // Exit the loop if successful
} catch (err) {
// Log the error for this URL and continue to the next URL
2023-11-09 02:05:55 -05:00
console.log(`Error fetching data from ${apiUrl}: ${err.message}`);
2023-11-06 13:58:30 -05:00
errors.push(err.message);
2023-11-09 02:05:55 -05:00
return "";
2023-09-27 14:51:45 -04:00
}
2023-11-06 13:58:30 -05:00
}
2023-09-27 14:51:45 -04:00
2023-11-06 13:58:30 -05:00
// If all URLs fail, return an error response
res.status(500).json({ error: "All API endpoints failed", errors });
} catch (err) {
console.log(err);
2023-03-09 14:41:01 -05:00
}
});
2023-11-06 13:58:30 -05:00
app.get("/bangs", async (req, res) => {
let f = await fetch("https://lite.duckduckgo.com/lite/?q=" + req.query.q, {
method: req.method,
});
res.redirect(f);
});
2022-08-15 04:54:24 -04:00
app.all("/*", listener);
2022-08-15 04:27:40 -04:00
2023-11-06 23:09:18 -05:00
app.listen(6014, () => console.log("Listening on 0.0.0.0:6014"));