Skip to content

Commit

Permalink
proxy url streams based on streaming server settings and behavior hints
Browse files Browse the repository at this point in the history
  • Loading branch information
unclekingpin committed Jun 9, 2024
1 parent 14ab8ee commit 090c697
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/withStreamingServer/convertStream.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
var url = require('url');
var magnet = require('magnet-uri');
var createTorrent = require('./createTorrent');

function convertStream(streamingServerURL, stream, seriesInfo) {
function buildProxyUrl(streamingServerURL, streamURL, requestHeaders, responseHeaders) {
var parsedStreamURL = new URL(streamURL);
var proxyOptions = new URLSearchParams();
proxyOptions.set('d', parsedStreamURL.origin);
Object.entries(requestHeaders).forEach(function([header, value]) {
proxyOptions.append('h', header + ':' + value);
});
Object.entries(responseHeaders).forEach(function([header, value]) {
proxyOptions.append('r', header + ':' + value);
});
return url.resolve(streamingServerURL, '/proxy/' + proxyOptions.toString() + parsedStreamURL.pathname) + parsedStreamURL.search;
}

function convertStream(streamingServerURL, stream, seriesInfo, streamingServerSettings) {
return new Promise(function(resolve, reject) {
if (typeof stream.url === 'string') {
if (stream.url.indexOf('magnet:') === 0) {
Expand Down Expand Up @@ -30,7 +44,15 @@ function convertStream(streamingServerURL, stream, seriesInfo) {
reject(error);
});
} else {
resolve({ url: stream.url });
var proxyStreamsEnabled = streamingServerSettings && streamingServerSettings.proxyStreamsEnabled;
var proxyHeaders = stream.behaviorHints && stream.behaviorHints.proxyHeaders;
if (proxyStreamsEnabled || proxyHeaders) {
var requestHeaders = proxyHeaders && proxyHeaders.request ? proxyHeaders.request : {};
var responseHeaders = proxyHeaders && proxyHeaders.response ? proxyHeaders.response : {};
resolve({ url: buildProxyUrl(streamingServerURL, stream.url, requestHeaders, responseHeaders) });
} else {
resolve({ url: stream.url });
}
}

return;
Expand Down
2 changes: 1 addition & 1 deletion src/withStreamingServer/withStreamingServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function withStreamingServer(Video) {
video.dispatch({ type: 'command', commandName: 'unload' });
loadArgs = commandArgs;
onPropChanged('stream');
convertStream(commandArgs.streamingServerURL, commandArgs.stream, commandArgs.seriesInfo)
convertStream(commandArgs.streamingServerURL, commandArgs.stream, commandArgs.seriesInfo, convertStream.streamingServerSettings)
.then(function(result) {
var mediaURL = result.url;
var infoHash = result.infoHash;
Expand Down

0 comments on commit 090c697

Please sign in to comment.