MCPcopy Create free account
hub / github.com/TheSmallHanCat/flow2api / _create_browser

Method _create_browser

src/services/browser_captcha.py:1269–1356  ·  view source on GitHub ↗

Create a browser instance; shared-slot browsers track PIDs while temporary browsers do not.

(self, token_proxy_url: Optional[str] = None, manage_slot_pid: bool = True)

Source from the content-addressed store, hash-verified

1267 return proxy_option, raw_proxy_url, proxy_source
1268
1269 async def _create_browser(self, token_proxy_url: Optional[str] = None, manage_slot_pid: bool = True) -> tuple:
1270 """Create a browser instance; shared-slot browsers track PIDs while temporary browsers do not."""
1271 width = self._profile_viewport["width"]
1272 height = self._profile_viewport["height"]
1273 viewport = {"width": width, "height": height}
1274 launch_in_background = bool(getattr(config, "browser_launch_background", True))
1275
1276 if manage_slot_pid:
1277 await self._cleanup_stale_slot_process()
1278 playwright = await async_playwright().start()
1279 browser_executable_path = os.environ.get("BROWSER_EXECUTABLE_PATH", "").strip() or None
1280 proxy_option, raw_proxy_url, _ = await self._resolve_proxy_runtime_config(token_proxy_url=token_proxy_url)
1281
1282 # 先只记录代理,真实 UA/UA-CH 交给浏览器自己暴露,避免 user-agent 与 sec-ch-ua 版本错位。
1283 self._last_fingerprint = {
1284 "proxy_url": raw_proxy_url if raw_proxy_url else None,
1285 }
1286
1287 try:
1288 browser_args = [
1289 '--disable-blink-features=AutomationControlled',
1290 '--disable-quic',
1291 '--disable-features=UseDnsHttpsSvcb',
1292 '--lang=en-US',
1293 '--no-sandbox',
1294 '--disable-dev-shm-usage',
1295 '--disable-setuid-sandbox',
1296 '--no-first-run',
1297 '--no-zygote',
1298 f'--window-size={width},{height}',
1299 '--disable-infobars',
1300 '--hide-scrollbars',
1301 '--profile-directory=Default',
1302 '--disable-extensions',
1303 '--disable-background-networking',
1304 '--disable-sync',
1305 '--disable-translate',
1306 '--disable-default-apps',
1307 '--no-default-browser-check',
1308 ]
1309
1310 if launch_in_background:
1311 browser_args.extend([
1312 '--start-minimized',
1313 '--disable-background-timer-throttling',
1314 '--disable-renderer-backgrounding',
1315 '--disable-backgrounding-occluded-windows',
1316 f'--flow2api-browser-slot={self.token_id}',
1317 ])
1318 if sys.platform.startswith("win"):
1319 browser_args.append('--window-position=-32000,-32000')
1320 debug_logger.log_info(
1321 f"[BrowserCaptcha] Token-{self.token_id} headed browser will launch in background mode"
1322 )
1323
1324 if browser_executable_path:
1325 debug_logger.log_info(
1326 f"[BrowserCaptcha] Token-{self.token_id} using custom browser executable: {browser_executable_path}"

Callers 3

get_custom_tokenMethod · 0.95
get_custom_scoreMethod · 0.95

Calls 7

_extract_browser_pidMethod · 0.95
_write_pid_fileMethod · 0.95
log_infoMethod · 0.80
log_errorMethod · 0.80

Tested by

no test coverage detected