poke/server.js

323 lines
8.7 KiB
JavaScript
Raw Normal View History

2022-09-21 08:36:21 +02:00
/*
2022-07-29 14:26:00 +02:00
PokeTube is an Free/Libre youtube front-end. this is our main file.
Copyright (C) 2021-2024 POKETUBE (https://codeberg.org/Ashley/poketube)
2022-02-28 18:33:58 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2023-06-30 12:54:53 +02:00
2022-02-28 18:33:58 +01:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see https://www.gnu.org/licenses/.
*/
2022-11-06 12:09:28 +01:00
2023-01-11 17:20:38 +01:00
(async function () {
const {
fetcher,
core,
wiki,
musicInfo,
modules,
version,
initlog,
init,
} = require("./src/libpoketube/libpoketube-initsys.js");
const media_proxy = require("./src/libpoketube/libpoketube-video.js");
const { sinit } = require("./src/libpoketube/init/superinit.js");
2024-02-15 21:08:45 +01:00
const innertube = require("./src/libpoketube/libpoketube-youtubei-objects.json");
2024-05-04 18:41:41 +02:00
const fs = require("fs");
2023-04-12 22:48:24 +02:00
const config = require("./config.json");
2023-01-11 17:20:38 +01:00
const u = await media_proxy();
2024-05-04 18:41:41 +02:00
fs.readFile("ascii_txt.txt", "utf8", (err, data) => {
if (err) {
console.error("Error reading the file:", err);
return;
}
// Log the ASCII art to the console
console.log(data);
});
2023-01-11 17:20:38 +01:00
initlog("Loading...");
2023-06-30 12:54:53 +02:00
initlog(
"[Welcome] Welcome To Poke - The ultimate privacy app - :3 " +
2023-06-30 12:54:53 +02:00
"Running " +
`Node ${process.version} - V8 v${
process.versions.v8
} - ${process.platform.replace("linux", "GNU/Linux")} ${
process.arch
} Server - libpt ${version}`
2022-02-23 19:54:21 +01:00
);
2022-07-12 19:06:02 +02:00
2023-01-11 17:20:38 +01:00
const {
IsJsonString,
convert,
getFirstLine,
capitalizeFirstLetter,
turntomins,
getRandomInt,
getRandomArbitrary,
} = require("./src/libpoketube/ptutils/libpt-coreutils.js");
initlog("Loaded libpt-coreutils");
const templateDir = modules.path.resolve(
`${process.cwd()}${modules.path.sep}html`
);
const sha384 = modules.hash;
2023-08-24 21:34:52 +02:00
const rateLimit = require("express-rate-limit");
2024-10-19 21:13:47 +02:00
const limiter = rateLimit({
windowMs: 30 * 1000, // 30 second window
max: 200, // limit each IP to 200 requests per 30 seconds
});
2023-08-24 21:34:52 +02:00
2023-01-11 17:20:38 +01:00
var app = modules.express();
2023-08-24 21:34:52 +02:00
app.use(limiter);
2023-01-11 17:20:38 +01:00
initlog("Loaded express.js");
app.engine("html", require("ejs").renderFile);
app.use(modules.express.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(modules.useragent.express());
app.use(modules.express.json()); // for parsing application/json
2023-01-12 21:11:35 +01:00
app.enable("trust proxy");
2024-05-04 18:41:41 +02:00
var toobusy = require("toobusy-js");
2023-01-11 17:20:38 +01:00
const renderTemplate = async (res, req, template, data = {}) => {
2023-06-30 12:54:53 +02:00
res.render(
modules.path.resolve(`${templateDir}${modules.path.sep}${template}`),
Object.assign(data)
);
2023-01-11 17:20:38 +01:00
};
2023-09-13 19:29:25 +02:00
2024-05-04 18:41:41 +02:00
// Set check interval to a faster value. This will catch more latency spikes
// but may cause the check to be too sensitive.
toobusy.interval(110);
toobusy.maxLag(3500);
app.use(function (req, res, next) {
if (toobusy()) {
res.send(503, "I'm busy right now, sorry.");
} else {
next();
}
});
toobusy.onLag(function (currentLag) {
process.exit(1);
console.log("Event loop lag detected! Latency: " + currentLag + "ms");
});
2023-09-13 19:29:25 +02:00
2023-01-11 17:20:38 +01:00
const random_words = [
2024-10-28 20:25:27 +01:00
"Woke!",
"Gay gay homosexaul gay!",
"free Palestine!",
"free software!",
"im... stuff!",
"frick capitalism!",
"still calling it twitter btw!",
"boop!",
"no way!",
"traaaa rightssss!",
"XD!",
"nya!",
"say gex!",
"ur valid :3",
"gay space communism!",
"doesnt have AI!",
"no web3!",
"keemstar is a bald ___!",
"No One calls it 'X'! ",
"Eat the rich!",
"Does Not include Nazis!",
"also try piped!",
"not alt-right!",
"coke zero > coke classic!",
"poke & chill!",
"can play HD!",
"also try invidious!",
"also try vencord!",
"rms <3!",
"du hast",
"can u belive no one bought this?",
"reee",
"1.000.000€!",
"pika!",
"fsf.org",
"ssfffssfssfffaassssfsdf!",
"they not like us!",
"to pimp a butterfly!",
"king kunta!",
"HUMBLE.",
"can you save my hds?",
"sahlo folina!",
"we come for you!",
"no chances!",
"dema dont control us!",
"i see your problem is, your proctologist",
"got both hands on your shoulder",
"while ur bottomless!",
"you should bounce bounce bounce man!",
"its lavish!",
"im vibin, vibin!",
"i would swim the paladin strait",
"hello clancy!",
"NO NOT ME,ITS FOR A FRIEND",
"im fairly local!",
"i dont wanna go like this!",
"east is up!",
"not done, josh dun!",
"your the judge, oh no!",
"I dont wanna backslide",
"welcome back to trench!",
"sai is propaganda!",
" •|i|• Ø i+! ].[",
"stay alive! |-/",
"the few, the proud, the Emotional!",
"ill morph into someone else",
"still alive",
"follow the torches",
"i created this world!",
"to feel some control!",
"destory it if i want!",
"o7 keons",
"at least let me clean my room",
"100+ stars on gh!",
"let the vibe slide over me!",
"sip a capri sun like its don peregon",
"now even gayer!",
"its joever..",
"lesbiam,,,",
"poke!!!",
"discord.poketube.fun!",
"women are pretty!",
"men are handsome!",
"enbys are cute!",
"you are cute :3",
"read if cute!",
"this shit awesome!",
"ur pawsome!",
"meows at u",
"hai i am gay",
"yay, GEX!",
"say gex..,,",
"wha if we um erm",
"turkey is literally 1984!",
"turkey is literally 1984!",
"turkey is literally 1984!",
"turkey is literally 1984!",
"turkey is literally 1984!",
"turkey is literally 1984!",
"awesome screen!",
"awesome camera!",
"long lasting battery life",
"stallmansupport.org!!!",
"does include nya~!!!",
"actually stable! :3",
2023-01-11 17:20:38 +01:00
];
2023-06-30 12:54:53 +02:00
const initPokeTube = function () {
sinit(app, config, renderTemplate);
initlog("inited super init");
init(app);
initlog("inited app");
};
2023-01-13 16:33:36 +01:00
2023-06-30 12:54:53 +02:00
try {
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
if (req.secure) {
res.header(
"Strict-Transport-Security",
"max-age=31536000; includeSubDomains; preload"
);
}
res.header("secure-poketube-instance", "1");
// opt out of googles "FLOC" bullcrap :p See https://spreadprivacy.com/block-floc-with-duckduckgo/
2024-05-04 18:41:41 +02:00
res.header("Permissions-Policy", "interest-cohort=()");
res.header("software-name", "poke");
2023-06-30 12:54:53 +02:00
next();
});
app.use(function (request, response, next) {
if (config.enablealwayshttps && !request.secure) {
if (
!/^https:/i.test(
request.headers["x-forwarded-proto"] || request.protocol
)
) {
return response.redirect(
"https://" + request.headers.host + request.url
);
}
}
2022-12-05 18:44:37 +01:00
2023-06-30 12:54:53 +02:00
next();
});
app.use(function (req, res, next) {
2024-10-28 20:25:27 +01:00
const random = random_words[Math.floor(Math.random() * random_words.length)];
2024-05-04 18:41:41 +02:00
res.header(
"X-PokeTube-Youtube-Client-Name",
innertube.innertube.CONTEXT_CLIENT.INNERTUBE_CONTEXT_CLIENT_NAME
);
res.header(
"Hey-there",
2024-10-28 20:25:27 +01:00
"Do u wanna help poke? contributions are welcome :3 https://codeberg.org/Ashley/poke"
);
2024-05-04 18:41:41 +02:00
res.header(
"X-PokeTube-Youtube-Client-Version",
innertube.innertube.CLIENT.clientVersion
);
res.header(
"X-PokeTube-Client-name",
innertube.innertube.CLIENT.projectClientName
);
res.header("X-PokeTube-Speeder", "3 seconds no cache, 280ms w/cache");
2024-02-15 21:08:45 +01:00
res.header("X-HOSTNAME", req.hostname);
2023-06-30 12:54:53 +02:00
if (req.url.match(/^\/(css|js|img|font)\/.+/)) {
res.setHeader(
"Cache-Control",
"public, max-age=" + config.cacher_max_age
); // cache header
res.setHeader("poketube-cacher", "STATIC_FILES");
}
2023-06-30 12:54:53 +02:00
const a = 890;
2024-05-04 18:41:41 +02:00
2023-06-30 12:54:53 +02:00
if (!req.url.match(/^\/(css|js|img|font)\/.+/)) {
res.setHeader("Cache-Control", "public, max-age=" + a); // cache header
res.setHeader("poketube-cacher", "PAGE");
}
next();
});
2023-02-06 15:23:19 +01:00
2023-06-30 12:54:53 +02:00
initlog("[OK] Load headers");
} catch {
2023-06-30 12:54:53 +02:00
initlog("[FAILED] load headers");
}
2023-06-30 12:54:53 +02:00
try {
2023-06-30 12:54:53 +02:00
app.get("/robots.txt", (req, res) => {
res.sendFile(__dirname + "/robots.txt");
});
initlog("[OK] Load robots.txt");
} catch {
2023-06-30 12:54:53 +02:00
initlog("[FAILED] load robots.txt");
}
2023-06-30 12:54:53 +02:00
initPokeTube();
2023-01-11 17:20:38 +01:00
})();