2022-07-08 18:17:56 +00:00
|
|
|
import "dotenv/config"
|
|
|
|
|
|
|
|
import express from "express";
|
2022-07-10 14:04:03 +00:00
|
|
|
import cors from "cors";
|
2022-07-08 18:17:56 +00:00
|
|
|
import * as fs from "fs";
|
|
|
|
import rateLimit from "express-rate-limit";
|
|
|
|
|
2022-07-17 11:21:51 +00:00
|
|
|
import { shortCommit } from "./modules/sub/currentCommit.js";
|
2022-07-08 18:17:56 +00:00
|
|
|
import { appName, genericUserAgent, version, internetExplorerRedirect } from "./modules/config.js";
|
|
|
|
import { getJSON } from "./modules/api.js";
|
2022-08-12 13:36:19 +00:00
|
|
|
import renderPage from "./modules/pageRender/page.js";
|
2022-07-24 10:54:05 +00:00
|
|
|
import { apiJSON, languageCode } from "./modules/sub/utils.js";
|
2022-07-17 11:21:51 +00:00
|
|
|
import { Bright, Cyan } from "./modules/sub/consoleText.js";
|
2022-07-08 18:17:56 +00:00
|
|
|
import stream from "./modules/stream/stream.js";
|
2022-08-12 13:36:19 +00:00
|
|
|
import loc from "./localization/manager.js";
|
2022-07-30 09:01:54 +00:00
|
|
|
import { buildFront } from "./modules/build.js";
|
2022-07-08 18:17:56 +00:00
|
|
|
|
2022-07-13 20:32:00 +00:00
|
|
|
const commitHash = shortCommit();
|
2022-07-08 18:17:56 +00:00
|
|
|
const app = express();
|
|
|
|
|
2022-07-10 14:04:03 +00:00
|
|
|
app.disable('x-powered-by');
|
|
|
|
|
2022-07-17 11:08:49 +00:00
|
|
|
if (fs.existsSync('./.env')) {
|
2022-07-08 18:17:56 +00:00
|
|
|
const apiLimiter = rateLimit({
|
|
|
|
windowMs: 20 * 60 * 1000,
|
|
|
|
max: 100,
|
|
|
|
standardHeaders: true,
|
|
|
|
legacyHeaders: false,
|
|
|
|
handler: (req, res, next, opt) => {
|
2022-07-24 10:54:05 +00:00
|
|
|
res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') });
|
2022-07-08 18:17:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const apiLimiterStream = rateLimit({
|
|
|
|
windowMs: 6 * 60 * 1000,
|
|
|
|
max: 24,
|
|
|
|
standardHeaders: true,
|
|
|
|
legacyHeaders: false,
|
|
|
|
handler: (req, res, next, opt) => {
|
2022-07-24 10:54:05 +00:00
|
|
|
res.status(429).json({ "status": "error", "text": loc(languageCode(req), 'ErrorRateLimit') });
|
2022-07-08 18:17:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-07-30 09:01:54 +00:00
|
|
|
await buildFront();
|
2022-07-08 18:17:56 +00:00
|
|
|
app.use('/api/', apiLimiter);
|
|
|
|
app.use('/api/stream', apiLimiterStream);
|
2022-07-30 09:01:54 +00:00
|
|
|
app.use('/', express.static('./min'));
|
|
|
|
app.use('/', express.static('./src/front'));
|
2022-07-08 18:17:56 +00:00
|
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
|
try {
|
|
|
|
decodeURIComponent(req.path)
|
|
|
|
}
|
2022-08-16 07:14:19 +00:00
|
|
|
catch (e) {
|
|
|
|
return res.redirect(process.env.selfURL);
|
2022-07-08 18:17:56 +00:00
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2022-07-10 14:04:03 +00:00
|
|
|
app.get('/api/:type', cors({ origin: process.env.selfURL, optionsSuccessStatus: 200 }), async (req, res) => {
|
2022-07-08 18:17:56 +00:00
|
|
|
try {
|
|
|
|
switch (req.params.type) {
|
|
|
|
case 'json':
|
|
|
|
if (req.query.url && req.query.url.length < 150) {
|
2022-08-23 14:43:56 +00:00
|
|
|
let j = await getJSON(req.query.url.trim(), languageCode(req), {
|
|
|
|
ip: req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip,
|
|
|
|
format: req.query.format ? req.query.format.slice(0, 5) : "webm",
|
|
|
|
quality: req.query.quality ? req.query.quality.slice(0, 3) : "max",
|
|
|
|
audioFormat: req.query.audioFormat ? req.query.audioFormat.slice(0, 4) : false,
|
|
|
|
isAudioOnly: req.query.audio ? true : false,
|
|
|
|
noWatermark: req.query.nw ? true : false,
|
|
|
|
fullAudio: req.query.ttfull ? true : false,
|
|
|
|
})
|
2022-07-08 18:17:56 +00:00
|
|
|
res.status(j.status).json(j.body);
|
|
|
|
} else {
|
2022-07-24 10:54:05 +00:00
|
|
|
let j = apiJSON(3, { t: loc(languageCode(req), 'ErrorNoLink', process.env.selfURL) })
|
2022-07-08 18:17:56 +00:00
|
|
|
res.status(j.status).json(j.body);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'stream':
|
|
|
|
if (req.query.p) {
|
|
|
|
res.status(200).json({ "status": "continue" });
|
|
|
|
} else if (req.query.t) {
|
|
|
|
let ip = req.header('x-forwarded-for') ? req.header('x-forwarded-for') : req.ip
|
2022-08-14 17:09:06 +00:00
|
|
|
stream(res, ip, req.query.t, req.query.h, req.query.e);
|
2022-07-08 18:17:56 +00:00
|
|
|
} else {
|
2022-09-03 15:32:39 +00:00
|
|
|
let j = apiJSON(0, { t: "no stream id" })
|
2022-07-08 18:17:56 +00:00
|
|
|
res.status(j.status).json(j.body);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2022-09-03 15:32:39 +00:00
|
|
|
let j = apiJSON(0, { t: "wrong response type" })
|
2022-07-08 18:17:56 +00:00
|
|
|
res.status(j.status).json(j.body);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).json({ 'status': 'error', 'text': 'something went wrong.' })
|
|
|
|
}
|
|
|
|
});
|
2022-09-01 13:51:18 +00:00
|
|
|
app.get("/api", (req, res) => {
|
2022-07-08 18:17:56 +00:00
|
|
|
res.redirect('/api/json')
|
|
|
|
});
|
2022-09-01 13:51:18 +00:00
|
|
|
app.get("/", (req, res) => {
|
2022-07-08 18:17:56 +00:00
|
|
|
// redirect masochists to a page where they can install a proper browser
|
|
|
|
if (req.header("user-agent") && req.header("user-agent").includes("Trident")) {
|
|
|
|
if (internetExplorerRedirect.newNT.includes(req.header("user-agent").split('NT ')[1].split(';')[0])) {
|
|
|
|
res.redirect(internetExplorerRedirect.new)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
res.redirect(internetExplorerRedirect.old)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.send(renderPage({
|
|
|
|
"hash": commitHash,
|
|
|
|
"type": "default",
|
2022-07-24 10:54:05 +00:00
|
|
|
"lang": languageCode(req),
|
2022-07-08 18:17:56 +00:00
|
|
|
"useragent": req.header('user-agent') ? req.header('user-agent') : genericUserAgent
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
});
|
2022-09-01 13:51:18 +00:00
|
|
|
app.get("/favicon.ico", (req, res) => {
|
2022-07-08 18:17:56 +00:00
|
|
|
res.redirect('/icons/favicon.ico');
|
|
|
|
});
|
2022-09-01 13:51:18 +00:00
|
|
|
app.get("/*", (req, res) => {
|
2022-07-08 18:17:56 +00:00
|
|
|
res.redirect('/')
|
|
|
|
});
|
|
|
|
app.listen(process.env.port, () => {
|
|
|
|
console.log(`\n${Bright(`${appName} (${version})`)}\n\nURL: ${Cyan(`${process.env.selfURL}`)}\nPort: ${process.env.port}\nCurrent commit: ${Bright(`${commitHash}`)}\nStart time: ${Bright(Math.floor(new Date().getTime()))}\n`)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.log('Required config files are missing. Please run "npm run setup" first.')
|
2022-08-01 15:48:37 +00:00
|
|
|
}
|