From a5e081e2bfe8685957215350d126ab9f87f2abb6 Mon Sep 17 00:00:00 2001 From: wukko Date: Sun, 14 Aug 2022 23:09:06 +0600 Subject: [PATCH] prevent crash if youtube video is fucked up ill remake a youtube function to pick a good video instead of broken one later, i just dont want cobalt to hang if one youtube cdn is down i hate youtube so much --- src/cobalt.js | 2 +- src/modules/stream/stream.js | 6 ++--- src/modules/stream/types.js | 48 ++++++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/cobalt.js b/src/cobalt.js index 10dd209..1918e68 100644 --- a/src/cobalt.js +++ b/src/cobalt.js @@ -81,7 +81,7 @@ if (fs.existsSync('./.env')) { 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 - stream(res, ip, req.query.t, req.query.h, req.query.e, languageCode(req)); + stream(res, ip, req.query.t, req.query.h, req.query.e); } else { let j = apiJSON(0, { t: loc(languageCode(req), 'ErrorNoStreamID') }) res.status(j.status).json(j.body); diff --git a/src/modules/stream/stream.js b/src/modules/stream/stream.js index 541bedb..a575ad2 100644 --- a/src/modules/stream/stream.js +++ b/src/modules/stream/stream.js @@ -2,7 +2,7 @@ import { apiJSON } from "../sub/utils.js"; import { verifyStream } from "./manage.js"; import { streamAudioOnly, streamDefault, streamLiveRender } from "./types.js"; -export default function(res, ip, id, hmac, exp, lang) { +export default function(res, ip, id, hmac, exp) { try { let streamInfo = verifyStream(ip, id, hmac, exp, process.env.streamSalt); if (!streamInfo.error) { @@ -11,10 +11,10 @@ export default function(res, ip, id, hmac, exp, lang) { } else { switch (streamInfo.type) { case "render": - streamLiveRender(streamInfo, res, lang); + streamLiveRender(streamInfo, res); break; default: - streamDefault(streamInfo, res, lang); + streamDefault(streamInfo, res); break; } } diff --git a/src/modules/stream/types.js b/src/modules/stream/types.js index 282ccb9..72d5a77 100644 --- a/src/modules/stream/types.js +++ b/src/modules/stream/types.js @@ -3,8 +3,6 @@ import ffmpeg from "ffmpeg-static"; import got from "got"; import { ffmpegArgs, genericUserAgent } from "../config.js"; import { msToTime } from "../sub/utils.js"; -import { internalError } from "../sub/errors.js"; -import loc from "../../localization/manager.js"; export async function streamDefault(streamInfo, res) { try { @@ -16,16 +14,16 @@ export async function streamDefault(streamInfo, res) { isStream: true }); stream.pipe(res).on('error', (err) => { - internalError(res); + res.end(); }); stream.on('error', (err) => { - internalError(res); + res.end(); }); } catch (e) { - internalError(res); + res.end(); } } -export async function streamLiveRender(streamInfo, res, lang) { +export async function streamLiveRender(streamInfo, res) { try { if (streamInfo.urls.length == 2) { let headers = {}; @@ -51,28 +49,39 @@ export async function streamLiveRender(streamInfo, res, lang) { 'pipe', 'pipe', 'pipe' ], }); - ffmpegProcess.on('error', (err) => { - ffmpegProcess.kill(); - }); - audio.on('error', (err) => { - ffmpegProcess.kill(); - }); - video.on('error', (err) => { - ffmpegProcess.kill(); - }); res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}"`); ffmpegProcess.stdio[5].pipe(res); + + ffmpegProcess.on('error', (err) => { + ffmpegProcess.kill(); + res.end(); + return; + }); video.pipe(ffmpegProcess.stdio[3]).on('error', (err) => { ffmpegProcess.kill(); + res.end(); + return; }); audio.pipe(ffmpegProcess.stdio[4]).on('error', (err) => { ffmpegProcess.kill(); + res.end(); + return; + }); + audio.on('error', (err) => { + ffmpegProcess.kill(); + res.end(); + return; + }); + video.on('error', (err) => { + ffmpegProcess.kill(); + res.end(); + return; }); } else { - res.status(400).json({ status: "error", text: loc(lang, 'ErrorCorruptedStream') }); + res.end(); } } catch (e) { - internalError(res); + res.end(); } } export async function streamAudioOnly(streamInfo, res) { @@ -100,16 +109,19 @@ export async function streamAudioOnly(streamInfo, res) { }); ffmpegProcess.on('error', (err) => { ffmpegProcess.kill(); + res.end(); }); audio.on('error', (err) => { ffmpegProcess.kill(); + res.end(); }); res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}.${streamInfo.audioFormat}"`); ffmpegProcess.stdio[4].pipe(res); audio.pipe(ffmpegProcess.stdio[3]).on('error', (err) => { ffmpegProcess.kill(); + res.end(); }); } catch (e) { - internalError(res); + res.end(); } }