fix(stealth): improve stealth mode by removing unnecessary scripts
It doesn't add anything to the table as before (it might turn out I was wrong and add them again lol)
This commit is contained in:
@@ -1,38 +1,4 @@
|
||||
from functools import lru_cache
|
||||
|
||||
from scrapling.engines.toolbelt.navigation import js_bypass_path
|
||||
from scrapling.engines.toolbelt.fingerprints import generate_headers
|
||||
|
||||
__default_useragent__ = generate_headers(browser_mode=True).get("User-Agent")
|
||||
__default_chrome_useragent__ = generate_headers(browser_mode="chrome").get("User-Agent")
|
||||
|
||||
|
||||
@lru_cache(1)
|
||||
def _compiled_stealth_scripts():
|
||||
"""Pre-read and compile stealth scripts"""
|
||||
# Basic bypasses nothing fancy as I'm still working on it
|
||||
# But with adding these bypasses to the above config, it bypasses many online tests like
|
||||
# https://bot.sannysoft.com/
|
||||
# https://kaliiiiiiiiii.github.io/brotector/
|
||||
# https://pixelscan.net/
|
||||
# https://iphey.com/
|
||||
# https://www.browserscan.net/bot-detection <== this one also checks for the CDP runtime fingerprint
|
||||
# https://arh.antoinevastel.com/bots/areyouheadless/
|
||||
# https://prescience-data.github.io/execution-monitor.html
|
||||
stealth_scripts_paths = tuple(
|
||||
js_bypass_path(script)
|
||||
for script in (
|
||||
# Order is important
|
||||
"webdriver_fully.js",
|
||||
"window_chrome.js",
|
||||
"navigator_plugins.js",
|
||||
"notification_permission.js",
|
||||
"screen_props.js",
|
||||
"playwright_fingerprint.js",
|
||||
)
|
||||
)
|
||||
scripts = []
|
||||
for script_path in stealth_scripts_paths:
|
||||
with open(script_path, "r") as f:
|
||||
scripts.append(f.read())
|
||||
return tuple(scripts)
|
||||
|
||||
@@ -17,7 +17,6 @@ from scrapling.core._types import Any, Optional, ProxyType, Unpack
|
||||
from scrapling.engines.toolbelt.proxy_rotation import is_proxy_error
|
||||
from scrapling.engines.toolbelt.convertor import Response, ResponseFactory
|
||||
from scrapling.engines.toolbelt.fingerprints import generate_convincing_referer
|
||||
from scrapling.engines._browsers._config_tools import _compiled_stealth_scripts
|
||||
from scrapling.engines._browsers._types import StealthSession, StealthFetchParams
|
||||
from scrapling.engines._browsers._base import SyncSession, AsyncSession, StealthySessionMixin
|
||||
from scrapling.engines._browsers._validators import validate_fetch as _validate, StealthConfig
|
||||
@@ -109,14 +108,6 @@ class StealthySession(SyncSession, StealthySessionMixin):
|
||||
else:
|
||||
raise RuntimeError("Session has been already started")
|
||||
|
||||
def _initialize_context(self, config, ctx: BrowserContext) -> BrowserContext:
|
||||
"""Initialize the browser context."""
|
||||
for script in _compiled_stealth_scripts():
|
||||
ctx.add_init_script(script=script)
|
||||
|
||||
ctx = super()._initialize_context(config, ctx)
|
||||
return ctx
|
||||
|
||||
def _cloudflare_solver(self, page: Page) -> None: # pragma: no cover
|
||||
"""Solve the cloudflare challenge displayed on the playwright page passed
|
||||
|
||||
@@ -372,14 +363,6 @@ class AsyncStealthySession(AsyncSession, StealthySessionMixin):
|
||||
else:
|
||||
raise RuntimeError("Session has been already started")
|
||||
|
||||
async def _initialize_context(self, config: Any, ctx: AsyncBrowserContext) -> AsyncBrowserContext:
|
||||
"""Initialize the browser context."""
|
||||
for script in _compiled_stealth_scripts():
|
||||
await ctx.add_init_script(script=script)
|
||||
|
||||
ctx = await super()._initialize_context(config, ctx)
|
||||
return ctx
|
||||
|
||||
async def _cloudflare_solver(self, page: async_Page) -> None: # pragma: no cover
|
||||
"""Solve the cloudflare challenge displayed on the playwright page passed
|
||||
|
||||
|
||||
Reference in New Issue
Block a user