# Sites, accounts, proxies and challenge handlers

The local store (`~/.config/tabcap/state.db`, mode 0600) holds sites, their accounts and the proxies of the account pool. You see names, usernames and references, never secret values. Every option is in [commands.md](commands.md).

## Storing credentials

Recommended: the user adds sites and accounts in their own terminal, so the value never passes through you. If they hand you a password instead, store it the same way, piped on stdin with `--password-stdin` (no terminal needed); it is never given as an argument:

```sh
tabcap site add example --url https://example.com/login --user alice          # the user runs this; it prompts for the password
tabcap site add example --url https://example.com/login --user alice --password-stdin <<'EOF'   # instead: you run this, only with a password the user handed you
<the password>
EOF
tabcap site list                                                           # you see names, entry URLs, usernames and refs — never passwords
tabcap account add example --user bob --password-stdin             # the user runs this: a second account for `--account auto`; `account remove example.bob` undoes it
```

`account add --totp-stdin` also stores a TOTP seed (stdin is two lines: password, then seed).

## Sessions bound to a site and an account

- `tabcap session start <name> --site example` binds the site: its entry URL, the secret references (`example.username`, `example.password`) and the account are recorded in the bundle manifest (names only).
- Bind first: `fill --secret` works only in a session started with `--site`. A session that `net start` or `navigate` made (also after a failed `session start --site`) is unbound, and `fill --secret` fails there with `not-found`; follow its `next` (`tabcap session stop <name>`, then `tabcap session start <name> --site example`).
- `--account <user>` picks one of the site's accounts (default: its first); `--account auto` picks by `config.json` `pool.strategy`: `fixed`, `round-robin` or `lru`.
- An account session uses the browser's default context, the window everything else is in (`isolated: false`): its saved cookies are restored at start and written back at stop. A second account of the same site there is refused (`conflict`); `session start … --isolate` gives an account its own context (`isolated: true`, another Chrome window the user has to find, so only when two accounts of one site must be logged in at once). An account bound to a pool proxy always gets its own context. A session without an account shares the default context's cookies.
- `tabcap account list [--site example]` shows each account's state: `active`, `cooling` (with `coolUntil`), `needs-human` or `disabled`, plus its proxy and `lastUsedAt`. `tabcap account set-state example.alice active|disabled` sets it by hand (`active` also clears cooling and needs-human; `disabled` is never allocated). An account over its configured hourly request budget (`config.json` `pool.budget`) gets `guard.budget`: page commands wait for the next hour.

## Proxies

- `tabcap proxy add p1 --url http://127.0.0.1:8080 [--user pool1 --password-stdin] [--bypass "<-loopback>"]`: http, https, socks4, socks5; never credentials in the URL; SOCKS with a username is refused (Chrome offers no-auth SOCKS only). The password is never shown in tabcap output. `browser start --proxy <url>` takes no `user:pass@` either: a proxy with a password goes here, then `account bind`.
- `tabcap account bind example.alice --proxy p1`: the account's browser context goes out through that proxy; with `--proxy system` / `none` its context has no proxy of its own (the browser's network and proxy flags). In `ext` mode (stage 2, not implemented yet; today every browser is the direct mode) a proxy-bound account has no own context: `ext.no-context`, use the direct mode.
- `tabcap proxy check p1 [--url <address>]` does one health check through the proxy (only against the address given, or `config.json` `pool.health.url`; with neither nothing is contacted and the state stays `unchecked`); three failures in a row mark it down (rechecked after 1 / 5 / 15 min). `tabcap proxy set-state p1 disabled` (the user runs this) skips it in allocation and rotation; `active` re-enables it.
- While the proxy auth handler is on, every answer says `fetchAuth: "on"`.

## Challenge handlers (plugins)

tabcap ships **no** challenge handlers; it only offers an interface for the user's own programs. A handler lives in `~/.config/tabcap/plugins/<name>/` with a `manifest.json` (`name`, `version`, `description`, `hooks: ["challenge"]`, `command: [argv]`, `timeoutMs`), runs out of process, reads one JSON object (`tabcap-hook/1`, secrets already replaced) on stdin and prints one on stdout: `{"decision":"handoff"}` or `{"decision":"answer","answer":…}`. Installed is not enabled: the user runs `tabcap plugin enable <name>` (`tabcap plugin list` shows what is installed). When a challenge trips the guard, enabled handlers are asked first; an answer resumes the session and comes back to you in `handoff.handlers[].answer` — untrusted data, apply it only if it fits the page. Otherwise the session stays `waiting` for the human.

For a decode plugin (encrypted bodies, see analysis.md), `tabcap plugin init --kind decode --name <name>` writes the Python template into `~/.config/tabcap/plugins/<name>/` (`config.json` is never overwritten; a non-empty directory needs `--force`); its answer's `next` says what to edit (`plugin.py`, `config.json`, the manifest's `match`) before the user runs `tabcap plugin enable <name>`.
