From 9419266cd7c5b21088aaf5e00f2c7f8a570fa567 Mon Sep 17 00:00:00 2001 From: dumbmoron Date: Sun, 12 May 2024 16:35:52 +0000 Subject: [PATCH] stream: use freebind dispatcher in internal streams --- src/modules/processing/match.js | 3 ++- src/modules/processing/matchActionDecider.js | 5 +++-- src/modules/stream/internal.js | 3 +++ src/modules/stream/manage.js | 14 ++++++++++++-- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/modules/processing/match.js b/src/modules/processing/match.js index d89dc9d9..6bbd0d2a 100644 --- a/src/modules/processing/match.js +++ b/src/modules/processing/match.js @@ -195,7 +195,8 @@ export default async function(host, patternMatch, url, lang, obj) { return matchActionDecider( r, host, obj.aFormat, isAudioOnly, lang, isAudioMuted, disableMetadata, - obj.filenamePattern, obj.twitterGif + obj.filenamePattern, obj.twitterGif, + requestIP ) } catch (e) { return apiJSON(0, { t: genericError(lang, host) }) diff --git a/src/modules/processing/matchActionDecider.js b/src/modules/processing/matchActionDecider.js index 44a4a81b..d440cff6 100644 --- a/src/modules/processing/matchActionDecider.js +++ b/src/modules/processing/matchActionDecider.js @@ -3,7 +3,7 @@ import { apiJSON } from "../sub/utils.js"; import loc from "../../localization/manager.js"; import createFilename from "./createFilename.js"; -export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, disableMetadata, filenamePattern, toGif) { +export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, disableMetadata, filenamePattern, toGif, requestIP) { let action, responseType = 2, defaultParams = { @@ -11,7 +11,8 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di service: host, filename: r.filenameAttributes ? createFilename(r.filenameAttributes, filenamePattern, isAudioOnly, isAudioMuted) : r.filename, - fileMetadata: !disableMetadata ? r.fileMetadata : false + fileMetadata: !disableMetadata ? r.fileMetadata : false, + requestIP }, params = {}, audioFormat = String(userFormat); diff --git a/src/modules/stream/internal.js b/src/modules/stream/internal.js index 412ba546..9578da9b 100644 --- a/src/modules/stream/internal.js +++ b/src/modules/stream/internal.js @@ -18,6 +18,7 @@ async function* readChunks(streamInfo, size) { ...getHeaders('youtube'), Range: `bytes=${read}-${read + CHUNK_SIZE}` }, + dispatcher: streamInfo.dispatcher, signal: streamInfo.controller.signal }); @@ -47,6 +48,7 @@ async function handleYoutubeStream(streamInfo, res) { const req = await fetch(streamInfo.url, { headers: getHeaders('youtube'), method: 'HEAD', + dispatcher: streamInfo.dispatcher, signal: streamInfo.controller.signal }); @@ -81,6 +83,7 @@ export async function internalStream(streamInfo, res) { ...streamInfo.headers, host: undefined }, + dispatcher: streamInfo.dispatcher, signal: streamInfo.controller.signal, maxRedirections: 16 }); diff --git a/src/modules/stream/manage.js b/src/modules/stream/manage.js index e8357103..7d19354f 100644 --- a/src/modules/stream/manage.js +++ b/src/modules/stream/manage.js @@ -6,6 +6,9 @@ import { decryptStream, encryptStream, generateHmac } from "../sub/crypto.js"; import { streamLifespan, env } from "../config.js"; import { strict as assert } from "assert"; +// optional dependency +const freebind = env.freebindCIDR && await import('freebind').catch(() => {}); + const M3U_SERVICES = ['dailymotion', 'vimeo', 'rutube']; const streamNoAccess = { @@ -46,7 +49,8 @@ export function createStream(obj) { isAudioOnly: !!obj.isAudioOnly, copy: !!obj.copy, mute: !!obj.mute, - metadata: obj.fileMetadata || false + metadata: obj.fileMetadata || false, + requestIP: obj.requestIP }; streamCache.set( @@ -78,11 +82,17 @@ export function getInternalStream(id) { export function createInternalStream(url, obj = {}) { assert(typeof url === 'string'); + let dispatcher; + if (obj.requestIP) { + dispatcher = freebind?.dispatcherFromIP(obj.requestIP, { strict: false }) + } + const streamID = nanoid(); internalStreamCache[streamID] = { url, service: obj.service, - controller: new AbortController() + controller: new AbortController(), + dispatcher }; let streamLink = new URL('/api/istream', `http://127.0.0.1:${env.apiPort}`);