Ret2Console is a Python terminal client for the Ret2Shell CTF platform. It provides an interactive REPL, one-shot commands, script execution, a read-only API-backed virtual filesystem, challenge instance tunnels, downloads, and flag submission. SPEC.md is the design authority; update it when intended behavior changes or when implementation and specification disagree.
ret2console,r2c, orpython -m ret2consoleentersret2console.cli:main.src/ret2console/cli.pyregisters command families, builds the centralApp, and selects REPL,-c, or script mode.src/ret2console/repl.pyparses;/&&, recognizes pasted flags, resolvescommands.REGISTRY, and awaits a command handler.- Handlers in
src/ret2console/commands/read or updateApp/Context, then call the API, VFS, tunnel, subprocess, or rendering layer. Handlers return shell-like integer statuses: normally0success,1expected failure, and127unknown/unavailable command. src/ret2console/api/client.pyowns the lazyhttpx.AsyncClient, bearer token renewal/invalidation, error normalization, and pagination.src/ret2console/vfs_builder.pybinds lazy API loaders toVfsnodes. Navigation updatesContext.vfs_path,game_id, andchallenge_id; completion reuses populated caches.src/ret2console/wsrx.pyowns loopback challenge tunnels for theApplifetime. CLI shutdown closes tunnels and HTTP resources infinally.
State is centralized in the App dataclass (src/ret2console/state.py) and passed to handlers rather than hidden behind service globals. The command registry and shared Rich consoles are the notable globals. Profile switching resets transport, VFS context, and caches as one state transition.
src/ret2console/: application package and terminal/UI helpers.src/ret2console/api/: async Ret2Shell HTTP client and proof-of-work support.src/ret2console/commands/: command handlers grouped by domain (auth.py,challenge.py,submit.py,admin.py, etc.).tests/: flat pytest suite organized by feature; no separate unit/integration tree.scripts/: native, Windows, and containerized Nuitka one-file builds..github/workflows/: multi-platform Nuitka build and binary smoke checks.
uv sync # create/update the development environment
uv run r2c # interactive client
uv run r2c -c "pwd; status" # one-shot command sequence
uv run pytest # full test suite
uv run pytest tests/test_vfs.py::test_resolve_nested # focused testBuild commands:
scripts/build.sh # native Linux/macOS; dependencies preinstalled
scripts/build-in-container.sh # Ubuntu 22.04 Docker build
scripts/build-in-container.sh --cleanup # recreate the builder container
powershell -ExecutionPolicy Bypass -File scripts/build.ps1 # Windows
scripts\build.bat # Windows cmd wrapperNative outputs are under build/{linux,macos,win}/; the container build emits dist/ret2console. The repository defines no lint, formatter, static-type-check, or coverage command. Do not invent one.
- Use
snake_casefor modules/functions,PascalCasefor dataclasses/types,_leading_underscorefor private helpers, andUPPER_CASEfor constants. - Command handlers are async functions named
cmd_<domain>_<action>and returnint. Each domain exposesregister_<domain>(); add it to_register_all()incli.py. - Compound commands use explicit subcommand-to-coroutine dictionaries, not class hierarchies.
- Keep I/O async: HTTP, prompts, VFS loaders/readers, subprocesses, polling, and tunnel lifecycle are awaited. Use synchronous helpers for parsing, formatting, and local state changes. Use
asyncio.create_taskonly for genuine overlap with explicit lifecycle handling. - Inject operational dependencies through
App. Follow existing concrete owners (ApiClient,Vfs,Wsrx) rather than adding a second dependency-injection scheme. - Normalize inconsistent backend payloads at the API or feature boundary. API payloads are commonly
dict[str, Any]; do not spread response-shape branching through UI code. - Raise
ApiErrorfor transport/backend failures andVfsErrorfor virtual-path failures. Expected command errors are rendered for the user (messages are generally Chinese) and return1; do not terminate the REPL. - Preserve cleanup invariants: API and tunnel owners close on every exit path, and partially created tunnel sets roll back on failure.
- Preserve privacy behavior in
src/ret2console/flag.pyandFilteredFileHistory: flag-shaped input must not enter history. - Persistence belongs under
~/.ret2console/; session files require mode0600. Tests redirect this state to.test-home/.
SPEC.md: sole design/behavior reference, including commands, persistence, API behavior, and known gaps.pyproject.toml: package metadata, Python floor, dependencies, entry points, Hatchling and pytest configuration.uv.lock: committed reproducible dependency lock; keep it synchronized with dependency changes.src/ret2console/cli.py: composition root and lifecycle.src/ret2console/state.py: central mutable application/context model.src/ret2console/repl.py: input dispatch, conditional chaining, flag auto-submit, and interactive loop.src/ret2console/commands/__init__.py:Commandcontract and global registry.src/ret2console/api/client.py: HTTP boundary, authentication, token rotation, and pagination.src/ret2console/vfs.py/vfs_builder.py: lazy virtual filesystem and API bindings.src/ret2console/config.py: profiles, sessions, migration, and persistent paths.src/ret2console/wsrx.py: in-process challenge tunnel ownership.tests/conftest.py: isolated HOME/XDG state, command registration, event loop, and autouse cleanup.
- Runtime: CPython
>=3.11. Native binary scripts require Python 3.12+ and platform compiler/development tools. - Package/environment manager:
uv; use the committeduv.lock. Windows build scripts allow pip only as a fallback. - Packaging: Hatchling with a
src/layout. Development execution should useuv run ...rather than manipulatingPYTHONPATH. - Distribution: Nuitka one-file binaries. Linux native builds require
patchelf; macOS usesinstall_name_tool; Windows uses Nuitka MinGW64. - Nix packages/apps support only
x86_64-linuxandaarch64-linux. - Runtime libraries have distinct roles: prompt-toolkit for input/completion, Rich for output, httpx for async HTTP, Pillow for terminal images, and libwsrx for local tunnels.
- Keep each commit focused on one coherent change. Do not mix unrelated user work into the commit.
- Before committing, inspect the working tree and stage only files belonging to the current task. Never discard, overwrite, or reformat unrelated changes.
- Use Gitmoji for every commit subject, formatted as
<code> <concise imperative subject>(for example,:memo: Document Gitmoji commit convention). Choose the single Gitmoji code from https://gitmoji.dev/ that best represents the change; use its:shortcode:form, not the Unicode emoji. Add a body only when the rationale or risk is not evident from the diff. - Run the relevant verification before committing and report both the commit hash and verification result.
- Frameworks: pytest and pytest-asyncio. Async tests use
@pytest.mark.asyncio; tables use@pytest.mark.parametrize. - Name files
tests/test_<feature>.pyand teststest_<behavior>. Keep feature-specific helpers and fake backends local and underscore-prefixed. - Prefer
httpx.MockTransportfor API contracts,monkeypatchfor environment/process/time boundaries,tmp_pathfor generated files, andcapsysfor terminal output. - Assert observable contracts: return codes, state transitions, HTTP method/path/body, output, exact files/bytes, subprocess argv/env, and cleanup behavior. Avoid source-text and implementation-plumbing assertions.
tests/test_integration.pycovers in-process login-to-submission and persisted-session flows.tests/test_wsrx.pycontains the real loopback TCP/WebSocket lifecycle test. Tests must not require live Ret2Shell services.- The suite writes isolated state beneath
.test-home/; do not bypasstests/conftest.pypersistence cleanup. - CI currently builds and smoke-tests binaries but does not run pytest or enforce coverage. Run
uv run pytestlocally for code changes and add focused tests for new observable behavior.