Skip to content

Commit

Permalink
api: clean up and remove unnecessary res.destroy()s
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbmoron committed May 23, 2024
1 parent 5db4d4c commit 15fa6c5
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions src/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
})

app.get('/api/serverInfo', (req, res) => {
try {
return res.status(200).json(serverInfo);
} catch {
return res.destroy();
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', (_, res) => {
return res.status(200).json(serverInfo);
})

app.get('/favicon.ico', (req, res) => {
Expand Down

0 comments on commit 15fa6c5

Please sign in to comment.