Skip to content

Commit

Permalink
Various changes to allow us to blend in more (FreeTubeApp#3718)
Browse files Browse the repository at this point in the history
* Let YouTube generate the Innertube session for the watch page

* Fix the user agent

* Set referer and origin for Innertube and googlevideo requests

* Fix sec-fetch-site header for Innertube requests

* Remove content-type header for googlevideo requests
  • Loading branch information
absidue authored Jul 3, 2023
1 parent 242c93e commit 6163ad7
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import baseHandlers from '../datastores/handlers/base'
import { extractExpiryTimestamp, ImageCache } from './ImageCache'
import { existsSync } from 'fs'

import packageDetails from '../../package.json'

if (process.argv.includes('--version')) {
app.exit()
} else {
Expand Down Expand Up @@ -263,6 +265,12 @@ function runApp() {
})
}

const fixedUserAgent = session.defaultSession.getUserAgent()
.split(' ')
.filter(part => !part.includes('Electron') && !part.includes(packageDetails.productName))
.join(' ')
session.defaultSession.setUserAgent(fixedUserAgent)

// Set CONSENT cookie on reasonable domains
const consentCookieDomains = [
'https://www.youtube.com',
Expand All @@ -279,10 +287,19 @@ function runApp() {

// make InnerTube requests work with the fetch function
// InnerTube rejects requests if the referer isn't YouTube or empty
const innertubeRequestFilter = { urls: ['https://www.youtube.com/youtubei/*'] }
const innertubeAndMediaRequestFilter = { urls: ['https://www.youtube.com/youtubei/*', 'https://*.googlevideo.com/videoplayback?*'] }

session.defaultSession.webRequest.onBeforeSendHeaders(innertubeAndMediaRequestFilter, ({ requestHeaders, url }, callback) => {
requestHeaders.Referer = 'https://www.youtube.com/'
requestHeaders.Origin = 'https://www.youtube.com'

if (url.startsWith('https://www.youtube.com/youtubei/')) {
requestHeaders['Sec-Fetch-Site'] = 'same-origin'
} else {
// YouTube doesn't send the Content-Type header for the media requests, so we shouldn't either
delete requestHeaders['Content-Type']
}

session.defaultSession.webRequest.onBeforeSendHeaders(innertubeRequestFilter, ({ requestHeaders }, callback) => {
requestHeaders.referer = 'https://www.youtube.com'
// eslint-disable-next-line n/no-callback-literal
callback({ requestHeaders })
})
Expand Down

0 comments on commit 6163ad7

Please sign in to comment.