Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ Cookies only: local storage and IndexedDB stay on your machine, so sites that ke
Sites that only accept sign-ins from your network or your IP can be reached by linking this computer: your agents' browser connects to them from here. On macOS or Linux, after `gumloop login`:

```bash
gumloop browser proxy link # automatically runs in the background on startup
gumloop browser proxy status
gumloop browser proxy link # automatically runs in the background on startup
gumloop browser proxy link --foreground # stays connected from this terminal until Ctrl-C
gumloop browser proxy status -v
gumloop browser proxy unlink
```

On Linux without a systemd user session, such as a container, `link` can't run it in the background. Run `gumloop browser proxy connect` and keep it open instead.
On Linux without a systemd user session, such as a container, `link` can't run it in the background. Run `gumloop browser proxy link --foreground` and keep it open instead.

The computer only opens connections to public addresses on ports 80 and 443. Its key stays in the OS keychain. `gumloop logout` unlinks it. A network that inspects TLS traffic can't be used, since sites would show the agent's browser certificate errors.

Expand Down
2 changes: 1 addition & 1 deletion src/gumloop/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from __future__ import annotations

__version__ = "0.5.17"
__version__ = "0.5.18"
261 changes: 236 additions & 25 deletions src/gumloop/cli/commands/custom_proxy.py

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/gumloop/cli/custom_proxy_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ class CustomProxyConnectionStatus(BaseModel):
# When this status was recorded: the connect, or the start of the current attempt or failure.
since: datetime
last_error: str | None = None
# Defaults let `status` read the file from a process still running an older CLI.
running_since: datetime | None = None
pid: int | None = None
cli_version: str | None = None
# Connects after this process's first one.
reconnects: int = 0
last_disconnect_at: datetime | None = None
last_disconnect_reason: str | None = None


def backend_host(base_url: str) -> str:
Expand Down
65 changes: 64 additions & 1 deletion src/gumloop/cli/custom_proxy_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tempfile
from collections.abc import Callable
from collections.abc import Sequence
from dataclasses import dataclass
from pathlib import Path
from typing import Protocol

Expand Down Expand Up @@ -42,6 +43,15 @@ class NoBackgroundService(GumloopError):
as a Linux container or SSH session without a systemd user session."""


@dataclass(frozen=True)
class CustomProxyServiceDetails:
kind: str
path: Path
# None when the service isn't installed or its file can't be read.
executable_path: Path | None
log: str


class CustomProxyService(Protocol):
def install(self, executable_path: Path) -> None:
"""Writes the service and (re)starts it."""
Expand All @@ -51,6 +61,8 @@ def remove(self) -> None: ...

def is_installed(self) -> bool: ...

def describe(self) -> CustomProxyServiceDetails: ...


class LaunchAgentService:
def __init__(
Expand All @@ -68,8 +80,12 @@ def __init__(
def plist_path(self) -> Path:
return self._home / "Library" / "LaunchAgents" / f"{LAUNCH_AGENT_LABEL}.plist"

@property
def log_path(self) -> Path:
return self._home / ".gumloop" / "custom-proxy" / "service.log"

def install(self, executable_path: Path) -> None:
log_path = self._home / ".gumloop" / "custom-proxy" / "service.log"
log_path = self.log_path
try:
log_path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
except OSError as error:
Expand Down Expand Up @@ -98,6 +114,18 @@ def remove(self) -> None:
def is_installed(self) -> bool:
return self.plist_path.is_file()

def describe(self) -> CustomProxyServiceDetails:
executable_path = None
try:
arguments = plistlib.loads(self.plist_path.read_bytes()).get("ProgramArguments")
if isinstance(arguments, list) and arguments and isinstance(arguments[0], str):
executable_path = Path(arguments[0])
except (OSError, ValueError, AttributeError):
pass
return CustomProxyServiceDetails(
kind="LaunchAgent", path=self.plist_path, executable_path=executable_path, log=str(self.log_path)
)

def _job_target(self) -> str:
return f"gui/{self._get_uid()}/{LAUNCH_AGENT_LABEL}"

Expand Down Expand Up @@ -140,6 +168,23 @@ def remove(self) -> None:
def is_installed(self) -> bool:
return self.unit_path.is_file()

def describe(self) -> CustomProxyServiceDetails:
executable_path = None
try:
for line in self.unit_path.read_text(encoding="utf-8").splitlines():
if line.startswith("ExecStart="):
executable = _systemd_unquote_first(line.removeprefix("ExecStart="))
executable_path = Path(executable) if executable else None
break
except (OSError, UnicodeDecodeError):
pass
return CustomProxyServiceDetails(
kind="systemd user unit",
path=self.unit_path,
executable_path=executable_path,
log=f"journalctl --user -u {SYSTEMD_UNIT}",
)

def _systemctl(self, *args: str) -> subprocess.CompletedProcess[str]:
return self._run_command(["systemctl", "--user", *args])

Expand Down Expand Up @@ -179,6 +224,24 @@ def _systemd_quote(value: str) -> str:
return f'"{escaped}"'


def _systemd_unquote_first(command: str) -> str | None:
"""The first argument of a command written with ``_systemd_quote``."""
if not command.startswith('"'):
return None
characters: list[str] = []
index = 1
while index < len(command):
character = command[index]
if character == '"':
return "".join(characters).replace("%%", "%").replace("$$", "$")
if character == "\\" and index + 1 < len(command):
index += 1
character = command[index]
characters.append(character)
index += 1
return None


def _write_private(destination: Path, content: bytes) -> None:
try:
destination.parent.mkdir(parents=True, exist_ok=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ gumloop sync --once --non-interactive # one-shot sync; requires GUMLOOP_API_KEY

```bash
gumloop browser proxy link # automatically runs in the background on startup
gumloop browser proxy status
gumloop browser proxy link --foreground # stays connected from this terminal until Ctrl-C
gumloop browser proxy status [-v] [--json]
gumloop browser proxy unlink [--yes]
```

Expand Down
1 change: 1 addition & 0 deletions tests/cli/test_custom_proxy_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def service(monkeypatch) -> FakeService:
monkeypatch.setattr("gumloop.cli.commands.custom_proxy.check_tls_not_inspected", lambda: None)
monkeypatch.setattr("gumloop.cli.commands.custom_proxy.executable_version", lambda executable: __version__)
monkeypatch.setattr("gumloop.cli.commands.custom_proxy.socket", FixedHostname())
monkeypatch.setattr("gumloop.cli.commands.custom_proxy._CONNECT_WAIT_SECONDS", 0)
save_credentials(Credentials(api_key="key", user_id="actor"))
return fake

Expand Down