Skip to content

Commit

Permalink
add option to include /next request
Browse files Browse the repository at this point in the history
  • Loading branch information
zerodytrash committed Jun 20, 2022
1 parent 12b9480 commit 485b2e7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 33 deletions.
89 changes: 61 additions & 28 deletions account-proxy/controllers/proxyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ const stats = require('../lib/stats');
const credentials = new YouTubeCredentials();
const proxy = process.env.PROXY;

const relevantAttributes = [
'playabilityStatus',
'videoDetails',
'streamingData',
'contents',
'engagementPanels'
]

function getPlayer(req, res) {
handleProxyRequest(req, res, 'player');
}

function getNext(req, res) {
handleProxyRequest(req, res, 'next');
}

async function handleProxyRequest(req, res, endpoint) {
const tsStart = new Date().getTime();

Expand All @@ -22,34 +38,18 @@ async function handleProxyRequest(req, res, endpoint) {
clientParams.clientVersion = '2.20220228.01.00';
}

const youtubeResponse = await innertubeApi.sendApiRequest(endpoint, clientParams, credentials, proxy);
let endpointList = [endpoint];

if (typeof youtubeResponse.data !== 'object') {
throw new Error('Invalid YouTube response received');
// Include /next (sidebar) if flag set
if (clientParams.includeNext) {
endpointList.push('next');
}

const youtubeData = youtubeResponse.data;
const youtubeStatus = getYoutubeResponseStatus(youtubeResponse);
const youtubeGcrFlagSet = checkForGcrFlag(youtubeData);
const relevantData = extractAttributes(youtubeData,
[
'playabilityStatus',
'videoDetails',
'streamingData',
'contents',
'engagementPanels'
]
)

relevantData.proxy = {
clientParams,
youtubeGcrFlagSet,
youtubeStatus
}
// Wait until all requests are done
let youtubeResponses = await requestChunk(clientParams, endpointList);
let result = handleResponses(youtubeResponses, clientParams);

res.status(200).send(relevantData);

stats.countResponse(endpoint, youtubeStatus, youtubeGcrFlagSet);
res.status(200).send(result);

} catch (err) {
console.error(endpoint, err.message);
Expand All @@ -62,12 +62,45 @@ async function handleProxyRequest(req, res, endpoint) {
}
}

function getPlayer(req, res) {
handleProxyRequest(req, res, 'player');
function requestChunk(clientParams, endpointList) {
let pendingRequests = [];

endpointList.forEach(endpoint => {
pendingRequests.push(innertubeApi.sendApiRequest(endpoint, clientParams, credentials, proxy));
});

return Promise.all(pendingRequests);
}

function getNext(req, res) {
handleProxyRequest(req, res, 'next');
function handleResponses(youtubeResponses, clientParams) {
let responseData = {};

youtubeResponses.forEach((response, index) => {
if (response.data === null || typeof response.data !== 'object') {
throw new Error(`Invalid YouTube response received for endpoint /${response.config.endpoint}`);
}

const youtubeData = response.data;
const youtubeStatus = getYoutubeResponseStatus(response);
const youtubeGcrFlagSet = checkForGcrFlag(youtubeData);
const relevantData = extractAttributes(youtubeData, relevantAttributes);

stats.countResponse(response.config.endpoint, youtubeStatus, youtubeGcrFlagSet);

if (index === 0) {
responseData = relevantData;

responseData.proxy = {
clientParams,
youtubeGcrFlagSet,
youtubeStatus
};
} else {
responseData[response.config.endpoint + 'Response'] = relevantData;
}
});

return responseData;
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions account-proxy/lib/innertubeApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const sendApiRequest = function (endpoint, clientParams, credentials, proxy) {
method: "POST",
url,
headers,
endpoint,
data: JSON.stringify(data),
timeout: 5000
}
Expand Down
1 change: 1 addition & 0 deletions account-proxy/lib/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class YouTubeClientParams {
this.signatureTimestamp = 18834;
this.hl = 'en';
this.startTimeSecs = 0;
this.includeNext = false;
}

fromRequest(request) {
Expand Down
13 changes: 9 additions & 4 deletions account-proxy/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ function fillObjectFromRequest(obj, request) {
continue;
}

if (typeof obj[propName] === 'number') {
obj[propName] = parseInt(request.query[propName])
} else {
obj[propName] = request.query[propName];
switch(typeof obj[propName]) {
case 'number':
obj[propName] = parseInt(request.query[propName]);
break;
case 'boolean':
obj[propName] = request.query[propName] === '1' || request.query[propName] === 'true';
break;
default:
obj[propName] = request.query[propName];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion account-proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-youtube-age-restriction-bypass-server",
"version": "1.0.2",
"version": "1.0.3",
"description": "simple-youtube-age-restriction-bypass proxy backend",
"main": "server.js",
"scripts": {
Expand Down

0 comments on commit 485b2e7

Please sign in to comment.