From b0bed82167cb86aaacc44003946f4acb487ba082 Mon Sep 17 00:00:00 2001 From: dumbmoron <136796770+dumbmoron@users.noreply.github.com> Date: Thu, 17 Aug 2023 21:04:17 +0000 Subject: [PATCH 1/2] always send something on stream failure prevents reverse proxies (namely nginx) from assuming the server died because of an empty response --- src/modules/stream/types.js | 48 +++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/modules/stream/types.js b/src/modules/stream/types.js index 6fee9a43..f1725bee 100644 --- a/src/modules/stream/types.js +++ b/src/modules/stream/types.js @@ -4,6 +4,14 @@ import got from "got"; import { ffmpegArgs, genericUserAgent } from "../config.js"; import { getThreads, metadataManager, msToTime } from "../sub/utils.js"; +function fail(res) { + if (!res.headersSent) { + res.sendStatus(500); + } + + return res.destroy(); +} + export function streamDefault(streamInfo, res) { try { let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1]; @@ -15,25 +23,19 @@ export function streamDefault(streamInfo, res) { }, isStream: true }); - stream.pipe(res).on('error', () => { - res.destroy(); - }); - stream.on('error', () => { - res.destroy(); - }); - stream.on('aborted', () => { - res.destroy(); - }); + stream.pipe(res).on('error', () => fail(res)); + stream.on('error', () => fail(res)); + stream.on('aborted', () => fail(res)); } catch (e) { - res.destroy(); + fail(res); } } export function streamLiveRender(streamInfo, res) { try { if (streamInfo.urls.length !== 2) { - res.destroy(); - return; + return fail(res); } + let audio = got.get(streamInfo.urls[1], { isStream: true }); let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1], args = [ '-loglevel', '-8', @@ -57,24 +59,24 @@ export function streamLiveRender(streamInfo, res) { res.setHeader('Content-Disposition', `attachment; filename="${streamInfo.filename}"`); res.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); ffmpegProcess.stdio[4].pipe(res).on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); audio.pipe(ffmpegProcess.stdio[3]).on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); audio.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); audio.on('aborted', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); ffmpegProcess.on('disconnect', () => ffmpegProcess.kill()); @@ -84,11 +86,11 @@ export function streamLiveRender(streamInfo, res) { res.on('close', () => ffmpegProcess.kill()); ffmpegProcess.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); } catch (e) { - res.destroy(); + fail(res); } } export function streamAudioOnly(streamInfo, res) { @@ -132,10 +134,10 @@ export function streamAudioOnly(streamInfo, res) { res.on('close', () => ffmpegProcess.kill()); ffmpegProcess.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); } catch (e) { - res.destroy(); + fail(res); } } export function streamVideoOnly(streamInfo, res) { @@ -168,9 +170,9 @@ export function streamVideoOnly(streamInfo, res) { res.on('close', () => ffmpegProcess.kill()); ffmpegProcess.on('error', () => { ffmpegProcess.kill(); - res.destroy(); + fail(res); }); } catch (e) { - res.destroy(); + fail(res); } } From 36622fc7acf6718a9c3964f8b50199b3057821be Mon Sep 17 00:00:00 2001 From: wukko Date: Sun, 20 Aug 2023 15:42:57 +0600 Subject: [PATCH 2/2] collapsed two lines --- src/modules/stream/types.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/modules/stream/types.js b/src/modules/stream/types.js index f1725bee..dc53405a 100644 --- a/src/modules/stream/types.js +++ b/src/modules/stream/types.js @@ -5,10 +5,7 @@ import { ffmpegArgs, genericUserAgent } from "../config.js"; import { getThreads, metadataManager, msToTime } from "../sub/utils.js"; function fail(res) { - if (!res.headersSent) { - res.sendStatus(500); - } - + if (!res.headersSent) res.sendStatus(500); return res.destroy(); } @@ -32,9 +29,7 @@ export function streamDefault(streamInfo, res) { } export function streamLiveRender(streamInfo, res) { try { - if (streamInfo.urls.length !== 2) { - return fail(res); - } + if (streamInfo.urls.length !== 2) return fail(res); let audio = got.get(streamInfo.urls[1], { isStream: true }); let format = streamInfo.filename.split('.')[streamInfo.filename.split('.').length - 1], args = [