From d72be2c95a86b675a06f75948a7f0c136cc3653d Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Wed, 22 May 2024 16:45:32 +0000 Subject: [PATCH] api: clean up and remove unnecessary `res.destroy()`s --- src/core/api.js | 74 +++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/src/core/api.js b/src/core/api.js index f9b84c7a..d822136f 100644 --- a/src/core/api.js +++ b/src/core/api.js @@ -163,52 +163,48 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) { const checkBaseLength = id.length === 21 && exp.length === 13; const checkSafeLength = sig.length === 43 && sec.length === 43 && iv.length === 22; - if (checkQueries && checkBaseLength && checkSafeLength) { - // rate limit probe, will not return json after 8.0 - if (req.query.p) { - return res.status(200).json({ - status: "continue" - }) - } - try { - const streamInfo = verifyStream(id, sig, exp, sec, iv); - if (!streamInfo?.service) { - return res.sendStatus(streamInfo.status); - } - return stream(res, streamInfo); - } catch { - return res.destroy(); - } + if (!checkQueries || !checkBaseLength || !checkSafeLength) { + return res.sendStatus(400); } - return res.sendStatus(400); + + // rate limit probe, will not return json after 8.0 + if (req.query.p) { + return res.status(200).json({ + status: "continue" + }) + } + + const streamInfo = verifyStream(id, sig, exp, sec, iv); + if (!streamInfo?.service) { + return res.sendStatus(streamInfo.status); + } + return stream(res, streamInfo); }) app.get('/api/istream', (req, res) => { - try { - if (!req.ip.endsWith('127.0.0.1')) - return res.sendStatus(403); - if (String(req.query.id).length !== 21) - return res.sendStatus(400); - - const streamInfo = getInternalStream(req.query.id); - if (!streamInfo) return res.sendStatus(404); - streamInfo.headers = { - ...req.headers, - ...streamInfo.headers - }; - - return stream(res, { type: 'internal', ...streamInfo }); - } catch { - return res.destroy(); + if (!req.ip.endsWith('127.0.0.1')) { + return res.sendStatus(403); } + + if (String(req.query.id).length !== 21) { + return res.sendStatus(400); + } + + const streamInfo = getInternalStream(req.query.id); + if (!streamInfo) { + return res.sendStatus(404); + } + + streamInfo.headers = { + ...req.headers, + ...streamInfo.headers + }; + + return stream(res, { type: 'internal', ...streamInfo }); }) - app.get('/api/serverInfo', (req, res) => { - try { - return res.status(200).json(serverInfo); - } catch { - return res.destroy(); - } + app.get('/api/serverInfo', (_, res) => { + return res.status(200).json(serverInfo); }) app.get('/favicon.ico', (req, res) => {