forked from MeiK2333/pyppeteer_stealth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add media codecs evasion strategy module
- Loading branch information
granitosaurus
committed
Jan 29, 2020
1 parent
e0b8afc
commit 6327d21
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from pyppeteer.page import Page | ||
|
||
|
||
async def media_codecs(page: Page) -> None: | ||
await page.evaluateOnNewDocument( | ||
""" | ||
() => { | ||
try { | ||
/** | ||
* Input might look funky, we need to normalize it so e.g. whitespace isn't an issue for our spoofing. | ||
* | ||
* @example | ||
* video/webm; codecs="vp8, vorbis" | ||
* video/mp4; codecs="avc1.42E01E" | ||
* audio/x-m4a; | ||
* audio/ogg; codecs="vorbis" | ||
* @param {String} arg | ||
*/ | ||
const parseInput = arg => { | ||
const [mime, codecStr] = arg.trim().split(';') | ||
let codecs = [] | ||
if (codecStr && codecStr.includes('codecs="')) { | ||
codecs = codecStr | ||
.trim() | ||
.replace(`codecs="`, '') | ||
.replace(`"`, '') | ||
.trim() | ||
.split(',') | ||
.filter(x => !!x) | ||
.map(x => x.trim()) | ||
} | ||
return { mime, codecStr, codecs } | ||
} | ||
/* global HTMLMediaElement */ | ||
const canPlayType = { | ||
// Make toString() native | ||
get(target, key) { | ||
// Mitigate Chromium bug (#130) | ||
if (typeof target[key] === 'function') { | ||
return target[key].bind(target) | ||
} | ||
return Reflect.get(target, key) | ||
}, | ||
// Intercept certain requests | ||
apply: function(target, ctx, args) { | ||
if (!args || !args.length) { | ||
return target.apply(ctx, args) | ||
} | ||
const { mime, codecs } = parseInput(args[0]) | ||
// This specific mp4 codec is missing in Chromium | ||
if (mime === 'video/mp4') { | ||
if (codecs.includes('avc1.42E01E')) { | ||
return 'probably' | ||
} | ||
} | ||
// This mimetype is only supported if no codecs are specified | ||
if (mime === 'audio/x-m4a' && !codecs.length) { | ||
return 'maybe' | ||
} | ||
// This mimetype is only supported if no codecs are specified | ||
if (mime === 'audio/aac' && !codecs.length) { | ||
return 'probably' | ||
} | ||
// Everything else as usual | ||
return target.apply(ctx, args) | ||
} | ||
} | ||
HTMLMediaElement.prototype.canPlayType = new Proxy( | ||
HTMLMediaElement.prototype.canPlayType, | ||
canPlayType | ||
) | ||
} catch (err) {} | ||
} | ||
""" | ||
) | ||
|