browserwright 0.24.0__tar.gz → 0.24.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {browserwright-0.24.0/src/browserwright.egg-info → browserwright-0.24.2}/PKG-INFO +3 -2
- {browserwright-0.24.0 → browserwright-0.24.2}/pyproject.toml +8 -2
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/_executor/process.py +35 -1
- browserwright-0.24.2/src/browserwright/daemon/_net.py +218 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/backends/cdp.py +18 -19
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/config.py +4 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/facade.py +51 -22
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/upstream.py +53 -64
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/upstream_context.py +23 -3
- browserwright-0.24.2/src/browserwright/daemon/server/ws_liveness.py +77 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/human_input.py +21 -24
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/session_create.py +11 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/session_registry.py +9 -6
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/skill_runtime.md +7 -3
- {browserwright-0.24.0 → browserwright-0.24.2/src/browserwright.egg-info}/PKG-INFO +3 -2
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright.egg-info/SOURCES.txt +1 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright.egg-info/requires.txt +2 -1
- browserwright-0.24.0/src/browserwright/daemon/_net.py +0 -94
- {browserwright-0.24.0 → browserwright-0.24.2}/LICENSE +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/README.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/setup.cfg +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/__init__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/__main__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/_executor/__init__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/_executor/__main__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/_executor/client.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/_executor/protocol.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/_text.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/cdp.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/cli.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/__init__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/_ipc.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/_rpc.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/_stale.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/backends/__init__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/backends/base.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/backends/extension.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/cli.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/doctor.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/errors.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/launch_chrome.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/launchagent.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/observability.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/platforms.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/probe.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/relay_status.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/resolver.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/restart_guard.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/__init__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/call_deadline.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/daemon.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/exec_relay.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/executor_registry.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/extension_contexts.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/extension_upstream.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/facade_extension.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/listener.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/proxy.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/relay.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/session_state.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/state.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/status.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/server/verbs.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/supervise.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon/userscripts.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon_lifecycle.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/daemon_url.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/discovery.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/errors.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/health.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/install.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/memory/__init__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/memory/_lock.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/memory/_md.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/memory/_yaml.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/memory/global_mem.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/memory/site_mem.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/mode_b_client.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/output_schema.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/primitives/__init__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/primitives/discovery_api.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/primitives/http.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/primitives/site.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/__init__.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/_md_convert.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/_md_normalize.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/_namespace.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/_smart_goto.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/inline.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/isolated_world.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/markdown.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/playwright_handle.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/screenshot_defaults.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/search.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/snapshot.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/vendor/README.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/repl/vendor/readability.js +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/session.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/session_ctx.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/session_runtime.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/github.com/SKILL.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/github.com/memory.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/github.com/tasks/list_issues.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/google.com/SKILL.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/google.com/memory.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/google.com/tasks/search.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/producthunt.com/SKILL.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/producthunt.com/memory.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/producthunt.com/tasks/today.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/wikipedia.org/SKILL.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/wikipedia.org/memory.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/wikipedia.org/tasks/lookup.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/ycombinator.com/SKILL.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/ycombinator.com/memory.md +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/site_skills_starter/ycombinator.com/tasks/front_page.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/skill_doc.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/tab_surface.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/task_runner.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright/version.py +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright.egg-info/dependency_links.txt +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright.egg-info/entry_points.txt +0 -0
- {browserwright-0.24.0 → browserwright-0.24.2}/src/browserwright.egg-info/top_level.txt +0 -0
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: browserwright
|
|
3
|
-
Version: 0.24.
|
|
3
|
+
Version: 0.24.2
|
|
4
4
|
Summary: Browserwright — let AI/code agents drive a real or isolated browser and author userscripts. Single package: the agent-facing REPL/site-skills/memory layer plus the bundled browser-resolving daemon (CDP proxy + extension/cloud backends).
|
|
5
5
|
License-Expression: AGPL-3.0-only
|
|
6
6
|
Requires-Python: >=3.11
|
|
7
7
|
License-File: LICENSE
|
|
8
8
|
Requires-Dist: websockets==15.0.1
|
|
9
9
|
Requires-Dist: pillow==12.2.0
|
|
10
|
-
Requires-Dist: httpx>=0.27
|
|
10
|
+
Requires-Dist: httpx[socks]>=0.27
|
|
11
|
+
Requires-Dist: python-socks>=2.7
|
|
11
12
|
Requires-Dist: playwright>=1.60.0
|
|
12
13
|
Requires-Dist: pyyaml>=6.0.3
|
|
13
14
|
Requires-Dist: html-to-markdown<4,>=3.10.6
|
|
@@ -15,14 +15,20 @@ name = "browserwright"
|
|
|
15
15
|
# stamping regex is `^(version\s*=\s*)["'][^"']+["']\s*$` and CI aborts unless
|
|
16
16
|
# it matches exactly once. `tests/skill/test_release_versioning.py` enforces all
|
|
17
17
|
# of that before a tag is ever pushed. See RELEASING.md.
|
|
18
|
-
version = "0.24.
|
|
18
|
+
version = "0.24.2"
|
|
19
19
|
description = "Browserwright — let AI/code agents drive a real or isolated browser and author userscripts. Single package: the agent-facing REPL/site-skills/memory layer plus the bundled browser-resolving daemon (CDP proxy + extension/cloud backends)."
|
|
20
20
|
requires-python = ">=3.11"
|
|
21
21
|
license = "AGPL-3.0-only"
|
|
22
22
|
dependencies = [
|
|
23
23
|
"websockets==15.0.1",
|
|
24
24
|
"pillow==12.2.0",
|
|
25
|
-
|
|
25
|
+
# SOCKS support is default, not an extra (#136): an `--attach` session's
|
|
26
|
+
# proxy comes from the caller's environment, and macOS proxy apps (Surge,
|
|
27
|
+
# Clash) routinely export `all_proxy=socks5://...`. `socks` gives httpx
|
|
28
|
+
# socksio for CDP discovery; python-socks is what websockets 15 needs for
|
|
29
|
+
# the CDP websocket itself.
|
|
30
|
+
"httpx[socks]>=0.27",
|
|
31
|
+
"python-socks>=2.7",
|
|
26
32
|
# Phase C: the skill heredoc injects a real Playwright `page`/`context`
|
|
27
33
|
# bound to the daemon facade, so playwright is a runtime dependency now (it
|
|
28
34
|
# was previously dev-only). `playwright install chromium` is still needed
|
|
@@ -809,7 +809,8 @@ class _Worker:
|
|
|
809
809
|
return
|
|
810
810
|
sys.stderr.write(
|
|
811
811
|
f"executor {self._session_id}: facade transport dropped "
|
|
812
|
-
"(daemon restart
|
|
812
|
+
"(daemon restart, or the link to the browser); will reconnect on "
|
|
813
|
+
"the next call\n")
|
|
813
814
|
self._connected = False
|
|
814
815
|
self._facade_death_handler = None
|
|
815
816
|
self._facade_daemon_pid = None
|
|
@@ -1003,6 +1004,20 @@ class _Worker:
|
|
|
1003
1004
|
outcomes, no loop.
|
|
1004
1005
|
"""
|
|
1005
1006
|
sid = self._session_id or "<id>"
|
|
1007
|
+
if self._transport_lost():
|
|
1008
|
+
# Not the tab: the executor's connection to the browser is gone,
|
|
1009
|
+
# and with it every Page and the context. Rebinding here would run
|
|
1010
|
+
# on no context, and forgetting the binding would abandon a tab
|
|
1011
|
+
# that is still alive. The next call reconnects and re-binds the
|
|
1012
|
+
# SAME tab (`_ensure_connected`), with `state` intact.
|
|
1013
|
+
error["fix"] = (
|
|
1014
|
+
"the executor's connection to the browser dropped mid-call "
|
|
1015
|
+
"(the tab itself was not closed). It reconnects to the same "
|
|
1016
|
+
"tab on the next call and `state` is kept — RETRY the call. "
|
|
1017
|
+
"Do not create a new session; if this repeats, check the link "
|
|
1018
|
+
"to the browser (`browserwright-daemon` log names why the "
|
|
1019
|
+
"connection ended).")
|
|
1020
|
+
return self._finish(buf, error=error, exit_code=exit_code)
|
|
1006
1021
|
failure: str | None = None
|
|
1007
1022
|
try:
|
|
1008
1023
|
if not self._rebind_dead_page():
|
|
@@ -1034,6 +1049,25 @@ class _Worker:
|
|
|
1034
1049
|
)
|
|
1035
1050
|
return self._finish(buf, error=error, exit_code=exit_code)
|
|
1036
1051
|
|
|
1052
|
+
def _transport_lost(self) -> bool:
|
|
1053
|
+
"""Is the Playwright Browser disconnected from the cdp surface?
|
|
1054
|
+
|
|
1055
|
+
A dropped connection closes every Page at once, so the call fails with
|
|
1056
|
+
the same TargetClosedError a dead tab produces. Playwright usually
|
|
1057
|
+
dispatched ``disconnected`` while the failing call pumped events; if it
|
|
1058
|
+
has not yet, its ``is_connected()`` already reads False and the armed
|
|
1059
|
+
handler is run here."""
|
|
1060
|
+
if not self._connected:
|
|
1061
|
+
return True
|
|
1062
|
+
is_connected = getattr(self._browser, "is_connected", None)
|
|
1063
|
+
try:
|
|
1064
|
+
alive = bool(is_connected()) if callable(is_connected) else True
|
|
1065
|
+
except Exception: # noqa: BLE001 - a failed probe is disconnected
|
|
1066
|
+
alive = False
|
|
1067
|
+
if not alive:
|
|
1068
|
+
self._on_facade_dead(self._browser)
|
|
1069
|
+
return not alive
|
|
1070
|
+
|
|
1037
1071
|
@staticmethod
|
|
1038
1072
|
def _exec_with_return(code: str, globals_: dict[str, Any]) -> str | None:
|
|
1039
1073
|
"""Exec ``code``; if the LAST statement is a bare expression, return its
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
"""Network primitives shared across the daemon.
|
|
2
|
+
|
|
3
|
+
A leaf module by construction — it imports nothing from `browserwright`, so
|
|
4
|
+
`daemon.server.*`, `daemon.backends.*`, and Layer 2 can all use it without a
|
|
5
|
+
cycle. Same role as `_ipc.py` / `_rpc.py` / `_stale.py`.
|
|
6
|
+
|
|
7
|
+
Two rules live here because each answers a question that is asked from more
|
|
8
|
+
than one place, and answering it twice is how the two copies drift apart:
|
|
9
|
+
|
|
10
|
+
- **`is_loopback_host`** — "is this browser on *my* machine?" That single
|
|
11
|
+
question decides both whether the user's `ALL_PROXY` should apply and
|
|
12
|
+
whether Chrome's `DevToolsActivePort` file is worth reading. Neither is
|
|
13
|
+
correct for a remote endpoint.
|
|
14
|
+
- **`proxy_for`** — "which proxy reaches this attach target?" Answered once,
|
|
15
|
+
by the CLI from *its* environment when a session is opened, and stored on
|
|
16
|
+
the session (#136). The daemon never reads proxy env vars of its own.
|
|
17
|
+
- **`redact_url`** — a CDP endpoint routinely carries a reusable token, in the
|
|
18
|
+
userinfo or the query string. Anything that prints an endpoint (daemon logs,
|
|
19
|
+
`daemon ps --json`, `session list --json`, error messages) must go through
|
|
20
|
+
this first.
|
|
21
|
+
"""
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import ipaddress
|
|
25
|
+
from collections.abc import Mapping
|
|
26
|
+
from urllib.parse import urlsplit, urlunsplit
|
|
27
|
+
|
|
28
|
+
#: Hostnames that mean "this machine" without being parseable as an address.
|
|
29
|
+
#: The trailing-dot form is a legal FQDN spelling of the same name.
|
|
30
|
+
_LOOPBACK_NAMES = frozenset({"localhost", "localhost."})
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def is_loopback_host(host_or_url: str) -> bool:
|
|
34
|
+
"""True when `host_or_url` names this machine.
|
|
35
|
+
|
|
36
|
+
Accepts either a bare host (`127.0.0.1`, `::1`, `[::1]`, `localhost`) or a
|
|
37
|
+
full URL to take the host from (`ws://127.0.0.1:9222/devtools/browser/x`).
|
|
38
|
+
|
|
39
|
+
Uses `ipaddress` rather than a literal allowlist, so the whole `127.0.0.0/8`
|
|
40
|
+
range answers True — a hand-written tuple of `("127.0.0.1", "localhost",
|
|
41
|
+
"::1")` silently misses `127.0.0.2`, which Chrome will happily bind to.
|
|
42
|
+
|
|
43
|
+
Anything unparseable is False: a host we cannot identify is treated as
|
|
44
|
+
remote, which is the safe direction for both callers (proxy stays applied,
|
|
45
|
+
local-only fallbacks stay off).
|
|
46
|
+
"""
|
|
47
|
+
if not isinstance(host_or_url, str) or not host_or_url:
|
|
48
|
+
return False
|
|
49
|
+
host = host_or_url
|
|
50
|
+
if "://" in host:
|
|
51
|
+
try:
|
|
52
|
+
host = urlsplit(host).hostname or ""
|
|
53
|
+
except ValueError:
|
|
54
|
+
return False
|
|
55
|
+
# `urlsplit().hostname` already strips IPv6 brackets and lowercases; a bare
|
|
56
|
+
# host string handed in directly may still carry either.
|
|
57
|
+
host = host.strip().strip("[]").lower()
|
|
58
|
+
if not host:
|
|
59
|
+
return False
|
|
60
|
+
# RFC 6761: every `*.localhost` name resolves to loopback.
|
|
61
|
+
if host in _LOOPBACK_NAMES or host.rstrip(".").endswith(".localhost"):
|
|
62
|
+
return True
|
|
63
|
+
try:
|
|
64
|
+
addr = ipaddress.ip_address(host)
|
|
65
|
+
except ValueError:
|
|
66
|
+
return False
|
|
67
|
+
# `0.0.0.0` / `::` as a *destination* is this machine on every OS we run on.
|
|
68
|
+
return addr.is_loopback or addr.is_unspecified
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
#: Which env var serves which target scheme. `ws` rides the same proxy as
|
|
72
|
+
#: `http` and `wss` the same as `https`, matching curl and `websockets`.
|
|
73
|
+
_PROXY_VAR_FOR_SCHEME = {"http": "http", "ws": "http",
|
|
74
|
+
"https": "https", "wss": "https"}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _env_value(env: Mapping[str, str], name: str) -> str:
|
|
78
|
+
"""`name_proxy` from `env`, lowercase winning over uppercase as in curl."""
|
|
79
|
+
for key in (name.lower(), name.upper()):
|
|
80
|
+
value = env.get(key)
|
|
81
|
+
if value and value.strip():
|
|
82
|
+
return value.strip()
|
|
83
|
+
return ""
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _no_proxy_matches(host: str, port: int | None, no_proxy: str) -> bool:
|
|
87
|
+
"""Does a `NO_PROXY` list exempt `host`?
|
|
88
|
+
|
|
89
|
+
Entries are comma- or space-separated: `*` (everything), an IP or CIDR
|
|
90
|
+
(`100.64.0.0/10`), or a domain, which also covers its subdomains whether
|
|
91
|
+
written `example.com` or `.example.com`. An entry may carry a `:port`,
|
|
92
|
+
which then has to match too. `urllib`'s own bypass check does not
|
|
93
|
+
understand CIDR, which is the form a tailnet or LAN is usually written in.
|
|
94
|
+
"""
|
|
95
|
+
try:
|
|
96
|
+
addr = ipaddress.ip_address(host)
|
|
97
|
+
except ValueError:
|
|
98
|
+
addr = None
|
|
99
|
+
for raw in no_proxy.replace(" ", ",").split(","):
|
|
100
|
+
entry = raw.strip().lower()
|
|
101
|
+
if not entry:
|
|
102
|
+
continue
|
|
103
|
+
if entry == "*":
|
|
104
|
+
return True
|
|
105
|
+
entry_port: int | None = None
|
|
106
|
+
if entry.startswith("[") and "]" in entry:
|
|
107
|
+
body, _, rest = entry[1:].partition("]")
|
|
108
|
+
if rest.startswith(":") and rest[1:].isdigit():
|
|
109
|
+
entry_port = int(rest[1:])
|
|
110
|
+
entry = body
|
|
111
|
+
elif entry.count(":") == 1:
|
|
112
|
+
body, _, maybe_port = entry.partition(":")
|
|
113
|
+
if maybe_port.isdigit():
|
|
114
|
+
entry, entry_port = body, int(maybe_port)
|
|
115
|
+
if entry_port is not None and entry_port != port:
|
|
116
|
+
continue
|
|
117
|
+
if addr is not None:
|
|
118
|
+
try:
|
|
119
|
+
if addr in ipaddress.ip_network(entry, strict=False):
|
|
120
|
+
return True
|
|
121
|
+
except ValueError:
|
|
122
|
+
pass
|
|
123
|
+
continue
|
|
124
|
+
domain = entry.lstrip(".").rstrip(".")
|
|
125
|
+
if domain and (host == domain or host.endswith("." + domain)):
|
|
126
|
+
return True
|
|
127
|
+
return False
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def proxy_for(url: str, env: Mapping[str, str]) -> str | None:
|
|
131
|
+
"""The proxy `env` says reaches `url`, or None for a direct connection.
|
|
132
|
+
|
|
133
|
+
Reads exactly the `http_proxy` / `https_proxy` / `all_proxy` / `no_proxy`
|
|
134
|
+
variables (either case) in `env` — never the OS proxy settings, which
|
|
135
|
+
`urllib.request.getproxies()` falls back to on macOS. Loopback targets are
|
|
136
|
+
always direct: proxying to your own machine is never what anyone wants.
|
|
137
|
+
|
|
138
|
+
The caller passes `env` explicitly because *whose* environment it is is
|
|
139
|
+
the whole decision (#136): the CLI that opens a session passes its own,
|
|
140
|
+
and the daemon never calls this with its own.
|
|
141
|
+
"""
|
|
142
|
+
if is_loopback_host(url):
|
|
143
|
+
return None
|
|
144
|
+
try:
|
|
145
|
+
parts = urlsplit(url)
|
|
146
|
+
host = (parts.hostname or "").lower()
|
|
147
|
+
port = parts.port
|
|
148
|
+
except ValueError:
|
|
149
|
+
return None
|
|
150
|
+
var = _PROXY_VAR_FOR_SCHEME.get(parts.scheme.lower())
|
|
151
|
+
if not host or var is None:
|
|
152
|
+
return None
|
|
153
|
+
if _no_proxy_matches(host, port, _env_value(env, "no_proxy")):
|
|
154
|
+
return None
|
|
155
|
+
proxy = _env_value(env, f"{var}_proxy") or _env_value(env, "all_proxy")
|
|
156
|
+
if not proxy:
|
|
157
|
+
return None
|
|
158
|
+
# `host:port` without a scheme is a common way to write an HTTP proxy.
|
|
159
|
+
return proxy if "://" in proxy else f"http://{proxy}"
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def proxy_toward(url: str, proxy: str | None) -> str | None:
|
|
163
|
+
"""`proxy`, unless `url` is on this machine.
|
|
164
|
+
|
|
165
|
+
A session's proxy was resolved for its attach URL, but discovery can hand
|
|
166
|
+
back a websocket URL on a different host. A loopback one is still direct.
|
|
167
|
+
"""
|
|
168
|
+
return None if proxy is None or is_loopback_host(url) else proxy
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def proxy_hint(proxy: str | None, url: str) -> str:
|
|
172
|
+
"""The suffix that turns a connect failure through `proxy` into an
|
|
173
|
+
actionable one, or "" when the connection was direct.
|
|
174
|
+
|
|
175
|
+
Without it a dead proxy reads as a dead remote browser.
|
|
176
|
+
"""
|
|
177
|
+
if proxy is None:
|
|
178
|
+
return ""
|
|
179
|
+
try:
|
|
180
|
+
host = urlsplit(url).hostname or url
|
|
181
|
+
except ValueError:
|
|
182
|
+
host = url
|
|
183
|
+
return (f" (via proxy {redact_url(proxy)}, taken from the environment "
|
|
184
|
+
f"that opened this session; if that proxy is not reachable, end "
|
|
185
|
+
f"the session and open it again with NO_PROXY={host} set)")
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def redact_url(url: object) -> object:
|
|
189
|
+
"""Strip credentials from a URL before it is reported anywhere.
|
|
190
|
+
|
|
191
|
+
A CDP endpoint for a cloud or anti-detect browser routinely carries a
|
|
192
|
+
reusable token — in the userinfo, or as a query parameter. Keep enough to
|
|
193
|
+
identify the endpoint (scheme, host, port, path) and drop the rest: these
|
|
194
|
+
fields exist to tell you *which* endpoint is in play, never to authenticate
|
|
195
|
+
to it.
|
|
196
|
+
|
|
197
|
+
Non-string input and strings that are not URLs are returned unchanged, so
|
|
198
|
+
this is safe to apply blindly to a field that may hold anything.
|
|
199
|
+
"""
|
|
200
|
+
if not isinstance(url, str) or "://" not in url:
|
|
201
|
+
return url
|
|
202
|
+
# `urlsplit` itself is lazy and does not validate — `.port` is what raises
|
|
203
|
+
# on a non-numeric port, so the whole field-access block must be inside the
|
|
204
|
+
# guard. (It wasn't, before this moved out of `status.py`: `daemon ps
|
|
205
|
+
# --json` raised on such a URL and the `<unparseable>` branch was dead.)
|
|
206
|
+
try:
|
|
207
|
+
parts = urlsplit(url)
|
|
208
|
+
netloc = parts.hostname or ""
|
|
209
|
+
if parts.port is not None:
|
|
210
|
+
netloc = f"{netloc}:{parts.port}"
|
|
211
|
+
if parts.username or parts.password:
|
|
212
|
+
netloc = f"<redacted>@{netloc}"
|
|
213
|
+
query = "<redacted>" if parts.query else ""
|
|
214
|
+
return urlunsplit((parts.scheme, netloc, parts.path, query, ""))
|
|
215
|
+
except ValueError:
|
|
216
|
+
# Never fall through returning the raw string: a malformed endpoint can
|
|
217
|
+
# still carry a live token.
|
|
218
|
+
return "<unparseable>"
|
|
@@ -36,7 +36,7 @@ from urllib.parse import urlparse
|
|
|
36
36
|
|
|
37
37
|
import httpx
|
|
38
38
|
|
|
39
|
-
from .._net import is_loopback_host, redact_url
|
|
39
|
+
from .._net import is_loopback_host, proxy_hint, redact_url
|
|
40
40
|
from ..config import Config
|
|
41
41
|
from ..errors import Unavailable
|
|
42
42
|
from ..platforms import profile_paths
|
|
@@ -62,12 +62,10 @@ class RealCdpBackend:
|
|
|
62
62
|
kind = "UPSTREAM_WS"
|
|
63
63
|
recommended_mode: str = "A"
|
|
64
64
|
ux_cost = "none"
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
#
|
|
68
|
-
|
|
69
|
-
# host where the proxy is intentional.
|
|
70
|
-
_trust_env: bool = True
|
|
65
|
+
# The proxy discovery goes through, or None for direct. Never read from
|
|
66
|
+
# the daemon's environment (#136): `CdpBackend` takes it from the
|
|
67
|
+
# session's Config, where the CLI that opened the session pinned it.
|
|
68
|
+
_proxy: str | None = None
|
|
71
69
|
|
|
72
70
|
def __init__(self, cfg: Config):
|
|
73
71
|
self._cfg = cfg
|
|
@@ -97,7 +95,12 @@ class RealCdpBackend:
|
|
|
97
95
|
# ---- shared plumbing ----------------------------------------------------
|
|
98
96
|
|
|
99
97
|
async def _get(self, url: str, timeout: float) -> httpx.Response:
|
|
100
|
-
|
|
98
|
+
# `trust_env=False`: httpx would otherwise build a transport for every
|
|
99
|
+
# proxy env var at construction — before `NO_PROXY` is consulted — so
|
|
100
|
+
# a stray daemon-side `all_proxy` could crash a request it never
|
|
101
|
+
# applied to (#136).
|
|
102
|
+
async with httpx.AsyncClient(timeout=timeout, trust_env=False,
|
|
103
|
+
proxy=self._proxy) as client:
|
|
101
104
|
return await client.get(url)
|
|
102
105
|
|
|
103
106
|
async def _discover_ws_url(self, base_url: str, timeout: float) -> str:
|
|
@@ -118,7 +121,8 @@ class RealCdpBackend:
|
|
|
118
121
|
resp = await self._get(url, timeout)
|
|
119
122
|
except (httpx.HTTPError, OSError) as e:
|
|
120
123
|
raise Unavailable(
|
|
121
|
-
f"{self.name}: cannot reach {shown}: {e}"
|
|
124
|
+
f"{self.name}: cannot reach {shown}: {e}"
|
|
125
|
+
f"{proxy_hint(self._proxy, base_url)}",
|
|
122
126
|
attempts={self.name: f"GET {shown} -> {type(e).__name__}: {e}"},
|
|
123
127
|
) from e
|
|
124
128
|
if resp.status_code == 404:
|
|
@@ -166,7 +170,7 @@ class CdpBackend(RealCdpBackend):
|
|
|
166
170
|
|
|
167
171
|
| | old | new |
|
|
168
172
|
|---|---|---|
|
|
169
|
-
| apply
|
|
173
|
+
| apply a proxy | `cdp` no, `env` yes | `not _loopback` |
|
|
170
174
|
| DevToolsActivePort 404 fallback | `cdp` yes, `env` never | `_loopback` |
|
|
171
175
|
|
|
172
176
|
That is strictly more correct than the names were: `--attach=http://127.0.0.1:9222`
|
|
@@ -179,6 +183,7 @@ class CdpBackend(RealCdpBackend):
|
|
|
179
183
|
super().__init__(cfg)
|
|
180
184
|
self.port = cfg.backends.cdp.port
|
|
181
185
|
self.endpoint = cfg.backends.cdp.endpoint
|
|
186
|
+
self.proxy = cfg.backends.cdp.proxy
|
|
182
187
|
|
|
183
188
|
# ---- the one predicate the two old backends disagreed about -------------
|
|
184
189
|
|
|
@@ -190,15 +195,9 @@ class CdpBackend(RealCdpBackend):
|
|
|
190
195
|
return is_loopback_host(self.endpoint)
|
|
191
196
|
|
|
192
197
|
@property
|
|
193
|
-
def
|
|
194
|
-
"""
|
|
195
|
-
|
|
196
|
-
Never for a local browser: proxying to your own loopback is never what
|
|
197
|
-
anyone wants, and it trips httpx[socks] import errors under
|
|
198
|
-
`ALL_PROXY=socks5://...`. Always for a remote endpoint, where reaching
|
|
199
|
-
it through the proxy is usually the whole point.
|
|
200
|
-
"""
|
|
201
|
-
return not self._loopback
|
|
198
|
+
def _proxy(self) -> str | None: # overrides RealCdpBackend's class attribute
|
|
199
|
+
"""The proxy the session pinned (#136); never one for a local browser."""
|
|
200
|
+
return None if self._loopback else self.proxy
|
|
202
201
|
|
|
203
202
|
# ---- URL sources --------------------------------------------------------
|
|
204
203
|
|
|
@@ -197,6 +197,10 @@ class CdpConfig:
|
|
|
197
197
|
"""
|
|
198
198
|
port: int = 9222
|
|
199
199
|
endpoint: str | None = None
|
|
200
|
+
#: The proxy that reaches `endpoint`, or None for direct. Per-session only,
|
|
201
|
+
#: like `endpoint`: resolved by the CLI from the environment of whoever
|
|
202
|
+
#: opened the session (#136). The daemon never reads its own proxy env.
|
|
203
|
+
proxy: str | None = None
|
|
200
204
|
|
|
201
205
|
|
|
202
206
|
@dataclass
|
|
@@ -68,6 +68,7 @@ from .. import _ipc
|
|
|
68
68
|
from ..config import (DEFAULT_FACADE_PORT, LOOPBACK_HOST, Config,
|
|
69
69
|
needs_loopback_cobind)
|
|
70
70
|
from .. import __version__
|
|
71
|
+
from .._net import proxy_toward, redact_url
|
|
71
72
|
from ..errors import Unavailable
|
|
72
73
|
from ..resolver import resolve as resolve_upstream
|
|
73
74
|
from ..._executor.protocol import _MAX_FRAME
|
|
@@ -75,7 +76,7 @@ from .daemon import Daemon, UnknownSessionError, UpstreamContext
|
|
|
75
76
|
from .exec_relay import serve_exec_relay
|
|
76
77
|
from .facade_extension import ExtensionFacadeBridge
|
|
77
78
|
from .relay import RelayServer
|
|
78
|
-
from .
|
|
79
|
+
from .ws_liveness import connect_kwargs as liveness_kwargs, keep_alive
|
|
79
80
|
|
|
80
81
|
logger = logging.getLogger(__name__)
|
|
81
82
|
|
|
@@ -636,7 +637,7 @@ class PlaywrightFacade:
|
|
|
636
637
|
return
|
|
637
638
|
|
|
638
639
|
try:
|
|
639
|
-
await self._bridge(conn, ws_url)
|
|
640
|
+
await self._bridge(conn, ws_url, proxy=self._cdp_proxy(ctx))
|
|
640
641
|
except websockets.exceptions.ConnectionClosed:
|
|
641
642
|
pass
|
|
642
643
|
except Exception as e: # noqa: BLE001
|
|
@@ -656,40 +657,50 @@ class PlaywrightFacade:
|
|
|
656
657
|
rr = await resolve_upstream(cfg)
|
|
657
658
|
return rr.ws_url
|
|
658
659
|
|
|
659
|
-
|
|
660
|
+
def _cdp_proxy(self, ctx: UpstreamContext | None = None) -> str | None:
|
|
661
|
+
"""The proxy the session pinned for its endpoint — the same value its
|
|
662
|
+
own upstream connection uses, so the two always agree on the route
|
|
663
|
+
(#136). The daemon-wide Config never carries one."""
|
|
664
|
+
return getattr(ctx.upstream, "session_proxy", None) if ctx is not None else None
|
|
665
|
+
|
|
666
|
+
async def _bridge(self, client: ServerConnection, upstream_url: str,
|
|
667
|
+
proxy: str | None = None) -> None:
|
|
660
668
|
"""Open a raw ws to the upstream Chrome and shuttle frames both ways.
|
|
661
669
|
|
|
662
670
|
Transparent: no id/sessionId rewriting (unlike the agent Router) — a
|
|
663
671
|
Playwright client owns the whole browser-level CDP namespace on its own
|
|
664
672
|
dedicated upstream connection, so Target.*/Browser.* responses and
|
|
665
|
-
events flow back unmodified.
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
)
|
|
673
|
+
events flow back unmodified.
|
|
674
|
+
|
|
675
|
+
The upstream leg's keepalive is `ws_liveness`, not websockets' own:
|
|
676
|
+
on a slow remote link a pong stuck behind one big reply used to close
|
|
677
|
+
this bridge mid-transfer, which the Playwright client can only read as
|
|
678
|
+
its whole browser closing."""
|
|
679
|
+
upstream = await websockets.connect(
|
|
680
|
+
upstream_url,
|
|
681
|
+
max_size=100 * 1024 * 1024,
|
|
682
|
+
compression=None,
|
|
683
|
+
# Exactly the session's proxy, or None for direct — never
|
|
684
|
+
# websockets' default of reading the daemon's http_proxy/all_proxy,
|
|
685
|
+
# which broke LAN / Tailscale / CloakBrowser upstreams (#20). The
|
|
686
|
+
# CLI that opened the session resolved it from its own environment
|
|
687
|
+
# (#136); per-page proxying is still Chrome's own business.
|
|
688
|
+
proxy=proxy_toward(upstream_url, proxy),
|
|
689
|
+
**liveness_kwargs(),
|
|
690
|
+
)
|
|
682
691
|
c2u = asyncio.create_task(self._pump(client, upstream, "c->u"))
|
|
683
692
|
u2c = asyncio.create_task(self._pump(upstream, client, "u->c"))
|
|
693
|
+
alive = asyncio.create_task(
|
|
694
|
+
keep_alive(upstream, label="facade bridge upstream"))
|
|
684
695
|
try:
|
|
685
696
|
await asyncio.wait(
|
|
686
697
|
{c2u, u2c}, return_when=asyncio.FIRST_COMPLETED)
|
|
687
698
|
finally:
|
|
688
|
-
# Cancel + await
|
|
699
|
+
# Cancel + await every task unconditionally. This runs on the normal
|
|
689
700
|
# FIRST_COMPLETED path AND when stop() cancels the handler task while
|
|
690
701
|
# it's suspended in asyncio.wait above (where the `for pending`
|
|
691
702
|
# cleanup would otherwise be skipped, orphaning the pump tasks).
|
|
692
|
-
for t in (c2u, u2c):
|
|
703
|
+
for t in (c2u, u2c, alive):
|
|
693
704
|
t.cancel()
|
|
694
705
|
# CancelledError is a BaseException; suppress it explicitly so a
|
|
695
706
|
# cancelled pump doesn't escape this cleanup.
|
|
@@ -699,6 +710,12 @@ class PlaywrightFacade:
|
|
|
699
710
|
await upstream.close()
|
|
700
711
|
with contextlib.suppress(Exception):
|
|
701
712
|
await client.close()
|
|
713
|
+
# The client sees this as its browser disconnecting, so say which
|
|
714
|
+
# side ended it: nothing else in the daemon log would.
|
|
715
|
+
logger.info(
|
|
716
|
+
"facade: bridge to %s ended (upstream %s, client %s)",
|
|
717
|
+
redact_url(upstream_url), _close_summary(upstream),
|
|
718
|
+
_close_summary(client))
|
|
702
719
|
|
|
703
720
|
@staticmethod
|
|
704
721
|
async def _pump(src, dst, label: str) -> None:
|
|
@@ -713,6 +730,18 @@ class PlaywrightFacade:
|
|
|
713
730
|
return
|
|
714
731
|
|
|
715
732
|
|
|
733
|
+
def _close_summary(conn: Any) -> str:
|
|
734
|
+
"""`sent 1011 'keepalive timeout', received none` for one ws side."""
|
|
735
|
+
def frame(close: Any) -> str:
|
|
736
|
+
if close is None:
|
|
737
|
+
return "none"
|
|
738
|
+
return f"{int(close.code)} {close.reason!r}"
|
|
739
|
+
|
|
740
|
+
protocol = getattr(conn, "protocol", None)
|
|
741
|
+
return (f"sent {frame(getattr(protocol, 'close_sent', None))}, "
|
|
742
|
+
f"received {frame(getattr(protocol, 'close_rcvd', None))}")
|
|
743
|
+
|
|
744
|
+
|
|
716
745
|
#: ADR-0011 name for what this class now is. `PlaywrightFacade` stays as the
|
|
717
746
|
#: class's own name (it is spelled in a lot of call sites and tests), but new
|
|
718
747
|
#: code should read `EndpointServer` — the object serves three surfaces, only
|