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
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ The complete inventory, including what is deliberately not implemented, is in
|---|---|---|
| Zero-write | `torrent inspect/verify`, `storage probe/map`, `seed discover/plan`, `client status/list`, ordinary `site` reads and `site bonus review`, `reconcile report`, and every `plan`, `status`, `list`, and `inspect` subcommand | None. Reads can still update atime, hydrate offline placeholders, or be recorded by the remote side. |
| Private store | `metafile store init/import`, `storage profile create`, `storage index refresh/refresh-discover`, `reconcile refresh-report`, `site bonus exchange prepare` | The explicit command itself. Owner-only and no-clobber; not encrypted. |
| Tracker-visible | `site metafile fetch` (one GET; on M-Team, a token POST and the signed-link GET, plus at most one redirect hop), `site bonus exchange submit` (at most one POST), `site login` (one POST and one verifying GET) | `--acknowledge-site-effect`, `--acknowledge-bonus-exchange`; `site login` is the command itself, with the cookie saved only to an owner-only file or a pipe |
| Tracker-visible | `site metafile fetch` (one GET; on M-Team, a token POST and the signed-link GET, plus at most one redirect hop), `site bonus exchange submit` (at most one POST), `site login` (TJUPT: one POST and one verifying GET; an API key: one status read) | `--acknowledge-site-effect`, `--acknowledge-bonus-exchange`; `site login` is the command itself, with the credential saved only to an owner-only file or a pipe. A saved credential never stands in for an acknowledgement flag. |
| Target filesystem | `seed materialize run/resume/abandon` | `--acknowledge-filesystem-write`, `--acknowledge-abandon` |
| Downloader | `client adopt/activate/stop/remove run/resume` | `--acknowledge-client-add`, `--acknowledge-client-recheck`, `--acknowledge-client-stop`, `--acknowledge-client-removal`; repeats, re-adds, and observation-only adoption have their own flags |
| Source deletion | `seed retire run/resume`, `seed retire parent-cleanup run/resume` | `--acknowledge-source-deletion`, `--acknowledge-empty-parent-removal` |
Expand Down Expand Up @@ -124,28 +124,32 @@ ptctl site list
ptctl site capabilities tjupt
```

Check a site session. The complete `Cookie` header value is read only from
stdin; interactive TTY input is refused, and the cookie is never persisted:
Sign in to a site once. After that, every site command uses the saved
credential, so no credential flag is needed:

```bash
printf '%s' "$TJUPT_COOKIE" | ptctl site status --cookie-stdin tjupt
ptctl site login tjupt # username, hidden password, and two-step code
ptctl site login mteam # paste the API key (hidden)
ptctl site status tjupt
ptctl site search mteam "Ubuntu"
ptctl site detail mteam REMOTE_ID
```

For TJUPT, `ptctl site login tjupt` can issue that cookie instead: it prompts
for the username, the hidden password, and the two-step code, then saves the
cookie to an owner-only file and prints its path. Scripts and agents use
`--credential-bundle-stdin`; see `ptctl help login` and
[Site reads and bonus exchange](docs/guide/sites.md).

M-Team uses an API key from 控制台 → 实验室 → 建立存取令牌 instead of a cookie,
with the same stdin-only rules:
TJUPT's login sends one sign-in request and saves the session cookie it
issues; the password is never saved. M-Team uses an API key from
控制台 → 实验室 → 建立存取令牌; `site login mteam` checks it with one status read
before saving it. Both are saved in plaintext to an owner-only file under the
user config directory, and `ptctl site logout SITE` deletes it. Scripts and
agents can still pipe a credential for a single command instead, which always
takes precedence over the saved one:

```bash
printf '%s' "$TJUPT_COOKIE" | ptctl site status --cookie-stdin tjupt
printf '%s' "$MTEAM_API_KEY" | ptctl site account --api-key-stdin mteam
printf '%s' "$MTEAM_API_KEY" | ptctl site search --api-key-stdin mteam "Ubuntu"
printf '%s' "$MTEAM_API_KEY" | ptctl site detail --api-key-stdin mteam REMOTE_ID
```

See `ptctl help login` and [Site reads and bonus exchange](docs/guide/sites.md).

From there, the [command guide](docs/guide/README.md) walks through storing
metafiles, proving content, materializing a layout, handing it to a downloader,
and reconciling the result.
Expand All @@ -172,7 +176,9 @@ A private `.torrent` is secret-bearing because its announce URL often contains
a personal passkey. Site cookies, site API keys, and downloader credentials are
also secrets,
while filesystem reads can update atime, hydrate cloud placeholders, or incur
network cost. `ptctl` keeps credentials in memory, rejects secret arguments,
network cost. `ptctl` rejects secret arguments, keeps credentials in memory
except the one site credential `site login` saves owner-only (and `site
logout` deletes), never saves account or downloader passwords,
emits no request bodies, blocks cross-origin/downgrade redirects, never retries
site reads, bounds network and filesystem work, hides private store/object and
discovery/reconciliation absolute paths by default, never exposes materialize
Expand Down
7 changes: 5 additions & 2 deletions docs/ADDING_A_SITE.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ flowchart LR
`internal/site/sites` is the only place the CLI learns which sites exist. For
each command, the CLI looks up the adapter by site ID, checks that the
descriptor declares the capability and the auth method, asserts the typed port,
and only then reads the credential from stdin. Adapters never see filesystem
handles, stores, or downloader state.
and only then reads the credential, from stdin when a flag names it or else
from the file `site login` saved (`internal/cli/saved_credentials.go`). A new
site gets both sources and `site login --cookie-stdin|--api-key-stdin` without
CLI changes. Adapters never see filesystem handles, stores, or downloader
state.

## Step by step

Expand Down
28 changes: 21 additions & 7 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,20 @@ Capabilities are small and explicit:
password and optional one-time code, typed at hidden terminal prompts or read
from one stdin bundle, and returns a verified cookie credential. The CLI saves
it to an owner-only file, by default under the user config directory, or
writes it to a non-terminal stdout, and never reads it back on its own. It
sends one login request and at most one verifying read, never retries, and
reports a refusal only by a fixed stop reason.
writes it to a non-terminal stdout. It sends one login request and at most one
verifying read, never retries, and reports a refusal only by a fixed stop
reason. For any site, `site login --cookie-stdin` or `--api-key-stdin` (and the
interactive default of an API-key site) instead checks a supplied credential
with one status read and saves it only if the read accepts it.

`internal/cli/saved_credentials.go` resolves where each `site` command gets
its credential: an explicit stdin flag wins, otherwise the command reads the
owner-only file `site login` saved for the site's default auth method
(`ptctl/sessions/SITE.cookie` or `SITE.api-key`). The file is located after
every usage check and read at the same point stdin would have been, so the
preflight order is unchanged, and an unsafe or malformed file is refused
without disclosure. `site logout` deletes the files. Adapters still receive
only a `site.Credential` and cannot tell which source supplied it.

Site-specific form or action writes live under narrow namespaced typed ports
and durable action schemas. They are not forced into universal fields. The effectful
Expand Down Expand Up @@ -405,8 +416,10 @@ non-authoritative.
`bonus.exchange.submit_effectful` workflow. A site that confirms each exchange
with the account password, as TJUPT does, implements
`site.PasswordConfirmedBonusExchangeWriter`: `submit` then reads the cookie and
password together from one strict `ptctl.credentials/v1` stdin bundle, refuses
`--cookie-stdin`, and the adapter adds the password to its single POST.
password together from one strict `ptctl.credentials/v1` stdin bundle, or uses
the saved cookie and asks for the password with echo off on the terminal before
its timeout starts. It refuses `--cookie-stdin`, never saves the password, and
the adapter adds the password to its single POST.
`prepare` stores one canonical
private intent containing a random operation ID, the exact reviewed semantic
ID, canonical site/option/origin/route identifiers, and fixed review/submit
Expand Down Expand Up @@ -699,10 +712,11 @@ uninitialized staging entries are not counted as committed artifacts.
metafile artifact:

```text
site metafile fetch (--cookie-stdin | --api-key-stdin) --acknowledge-site-effect
site metafile fetch [--cookie-stdin | --api-key-stdin] --acknowledge-site-effect
--metafile-store DIR SITE REMOTE_ID
-> validate one site, one remote ID, limits, capability, auth method, and store
-> read the credential only after that zero-write preflight succeeds
-> read the credential, from stdin or the saved file, only after that
zero-write preflight succeeds
-> linked routes only: request a site-issued link, bounded by MaxLinkRequests
-> send one bounded metafile GET, with no automatic redirect and no retry
-> strictly validate the complete exact response
Expand Down
48 changes: 36 additions & 12 deletions docs/AUTOMATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ Per-command details are in

## Secrets

`ptctl site login SITE` saves the site credential once: the TJUPT session
cookie it issues, or the M-Team API key after one status read accepts it.
Every later `site` command for that site, including `site metafile fetch` and
`site bonus exchange submit`, then uses the saved credential without a flag.
An agent saves one without a terminal by piping it,
`site login --cookie-stdin SITE` or `site login --api-key-stdin SITE`, and
`ptctl site logout SITE` deletes it. An explicit stdin flag below always takes
precedence over the saved credential, and a saved credential never stands in
for an acknowledgement flag.

| Flag | Used by | Stdin content |
|---|---|---|
| `--cookie-stdin` | `site` reads, `site bonus exchange submit` for cookie-only sites, `site metafile fetch` for cookie sites (TJUPT) | The complete `Cookie` header value |
Expand All @@ -104,26 +114,40 @@ Per-command details are in
| `--credential-bundle-stdin` | `reconcile report` with both a live site observation and a downloader | `{"schema":"ptctl.credentials/v1","site_cookie":"...","downloader_password":"..."}` |
| `--credential-bundle-stdin` | `site bonus exchange submit` for sites that confirm with the account password (TJUPT) | `{"schema":"ptctl.credentials/v1","site_cookie":"...","site_password":"..."}` |
| `--credential-bundle-stdin` | `site login` | `{"schema":"ptctl.credentials/v1","site_username":"...","site_password":"...","site_totp":"123456"}`; `site_totp` is optional |
| `--cookie-stdin`, `--api-key-stdin` | `site login` | The credential to save; one status read checks it first and nothing is saved when it fails |

- An interactive terminal on stdin is refused; the secret must be piped. The
exception is `site login` without `--credential-bundle-stdin`, which is for
people: it prompts on the terminal with the password and code hidden, and
exits `2` when stdin is not a terminal. Agents always pass the bundle.
exception is `site login` without a stdin flag, which is for people: it
prompts on the terminal with the password, code, or API key hidden, and
exits `2` when stdin is not a terminal. Agents pass the bundle,
`--cookie-stdin`, or `--api-key-stdin`. `site bonus exchange submit` with
the saved cookie on a site that confirms with the account password (TJUPT)
asks for that password the same way, before its timeout starts; agents pass
the bundle instead.
- Single secrets are limited to 64 KiB and the bundle to 128 KiB. The bundle is
strict JSON: unknown, duplicate, or missing fields are rejected.
- Site reads, metafile fetch, and reconciliation validate usage, capabilities,
and local inputs before reading stdin.
- Credentials stay in memory. They are never persisted, printed, or forwarded
in error text. The one exception is the session cookie `site login` issues.
With `--cookie-file PATH` it is saved to that owner-only file, and
`--output json` then reports a `site.login` object with `cookie_file` and
`session_expires_at` but not the cookie. With the bundle and no
`--cookie-file`, it is written to stdout, which must be a pipe or an
owner-only file. Without the bundle, it is saved under the user config
directory. `ptctl help login` documents every flag.
- Credentials stay in memory. They are never printed or forwarded in error
text, and the only one persisted is the site credential `site login` saves.
Without the bundle, or with `--cookie-stdin` or `--api-key-stdin`, it goes
to `ptctl/sessions/SITE.cookie` or `SITE.api-key` under the user config
directory, a plaintext file readable only by its owner. With
`--cookie-file PATH` the session cookie is saved to that owner-only file
instead, and `--output json` then reports a `site.login` object with
`cookie_file` and `session_expires_at` but not the cookie; an API key save
reports `api_key_file`. With the bundle and no `--cookie-file`, the cookie
is written to stdout, which must be a pipe or an owner-only file, and is not
saved. Account and downloader passwords are never saved.
`ptctl help login` documents every flag.
- A saved credential is refused, without being shown, when it is not a
regular file, when other users can read it (outside Windows), or when it is
not exactly one valid credential line. Sign in again or run
`ptctl site logout SITE` to clear it.
- `ptctl site capabilities SITE` lists the authentication methods a site
accepts: `cookie_header` takes `--cookie-stdin`, `api_key_header` takes
`--api-key-stdin`. Omitting the flag names the one the site needs.
`--api-key-stdin`. With neither a flag nor a saved credential the command
exits `2` and names both the `site login` command and the flag.
- A failed ordinary site read prints `site CMD failed`. Adapters that opt in
(currently M-Team) append one fixed cause after a colon: the credential has
the wrong format, the site rejected the credential, the site rate limited
Expand Down
11 changes: 9 additions & 2 deletions docs/FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,19 @@ deliberately does not. For step-by-step usage, see the
one verifying read that must name the same account before the new session
cookie is saved to an owner-only file or written to a pipe or owner-only
stdout, with an optional `site.login` JSON report;
- saved site credentials: `site login SITE` keeps the TJUPT session cookie, or
an M-Team API key once one status read accepts it, in an owner-only file
under the user config directory, so later `site` commands, metafile fetch,
and bonus exchange submit need no credential flag; an explicit stdin flag
takes precedence, an unsafe or malformed file is refused without being
shown, acknowledgement flags are still required, account passwords are never
saved, and `site logout SITE` deletes the file;
- an experimental M-Team session check, account snapshot (username, uploaded
and downloaded bytes, ratio, and bonus), first-page torrent search with
seeders, leechers, promotion factors, and category names, and torrent detail
for one remote ID, through one read-only POST per invocation to the official
JSON API, authenticated by a site-issued API key read only from stdin, with
no redirect, proxy, or retry;
JSON API, authenticated by a site-issued API key saved by `site login` or
piped on stdin, with no redirect, proxy, or retry;
- an explicitly acknowledged M-Team metafile fetch for one remote ID: one
token POST for a signed download link, one credential-free GET of that link,
and at most one further GET only for a redirect to an M-Team HTTPS host, with
Expand Down
50 changes: 35 additions & 15 deletions docs/MTEAM_ADAPTER.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,44 @@ leaks.

## Usage

The key is read only from stdin. Interactive terminal input is refused, the key
is never accepted as an argument, and it is never written to disk or printed:
Save the key once. `site login mteam` asks for it with echo off, checks it with
one status read (`POST /api/member/profile`), and saves it only if the API
accepts it:

```bash
printf '%s' "$MTEAM_API_KEY" | ptctl site status --api-key-stdin mteam
printf '%s' "$MTEAM_API_KEY" | ptctl site account --api-key-stdin mteam
printf '%s' "$MTEAM_API_KEY" | ptctl site search --api-key-stdin mteam "Ubuntu"
printf '%s' "$MTEAM_API_KEY" | ptctl site search --api-key-stdin --output json mteam "Ubuntu"
printf '%s' "$MTEAM_API_KEY" | ptctl site detail --api-key-stdin mteam REMOTE_ID
printf '%s' "$MTEAM_API_KEY" | ptctl site metafile fetch --api-key-stdin \
--acknowledge-site-effect --metafile-store "$PTCTL_STORE" mteam REMOTE_ID
ptctl site login mteam
```

To keep the key out of shell history and your environment, pipe it from a
password manager instead, for example `op read op://Private/M-Team/api-key |
ptctl site account --api-key-stdin mteam`.
After that, every M-Team command uses the saved key:

Without `--api-key-stdin`, every M-Team command fails with a usage error before
anything is read; `--cookie-stdin` is refused because the adapter does not
accept cookies.
```bash
ptctl site status mteam
ptctl site account mteam
ptctl site search mteam "Ubuntu"
ptctl site search --output json mteam "Ubuntu"
ptctl site detail mteam REMOTE_ID
ptctl site metafile fetch --acknowledge-site-effect --metafile-store "$PTCTL_STORE" mteam REMOTE_ID
```

The key is saved in plaintext to `<user config dir>/ptctl/sessions/mteam.api-key`
(on macOS `~/Library/Application Support`, on Linux `$XDG_CONFIG_HOME` or
`~/.config`, on Windows `%AppData%`). The file is owner-only, in an owner-only
directory, and ptctl refuses to use it if group or other users can read it or
if it is a symlink. `ptctl site logout mteam` deletes it; revoke the key on the
M-Team page as well if it may have leaked. The key is never accepted as an
argument and never printed.

Scripts and agents can save the key without a terminal, or skip saving and pipe
it to one command. A piped key always takes precedence over the saved one:

```bash
ptctl site login --api-key-stdin --output json mteam < "$API_KEY_FILE"
op read op://Private/M-Team/api-key | ptctl site account --api-key-stdin mteam
```

Without a saved key and without `--api-key-stdin`, an M-Team command fails with
a usage error that names `ptctl site login mteam`, before anything is read or
sent. `--cookie-stdin` is refused because the adapter does not accept cookies.

## Requests

Expand Down Expand Up @@ -151,6 +169,8 @@ These lines name the cause instead:
|---|---|---|
| `site CMD failed: the credential read from stdin does not have the format this site uses` | The key contains spaces, non-ASCII characters, or is too long. | Copy the token again. Leading and trailing whitespace is already trimmed. |
| `site CMD failed: the site rejected the credential` | The API refused the key (`account` and `search`; `status` reports `unauthenticated` instead). | Check that the token is current, or create a new one. |
| `site login stopped: login_credentials_rejected` | `site login mteam` checked the key and the API did not accept it. Nothing was saved. | Copy the token again, or create a new one. |
| `refusing to use saved API key PATH because other users can access it; ...` | The saved key file is readable by group or other users. | Run `chmod 600 PATH`, or sign in again with `ptctl site login mteam`. |
| `site CMD failed: the site rate limited the request; ...` | HTTP 429 or API code `4`. | Wait before trying again. ptctl never retries. |
| `site CMD failed: the site host resolved only to private or reserved addresses, ...` | DNS returned only blocked addresses, typically from a proxy using fake-IP DNS (`198.18.0.0/15`, as in Clash or Mihomo fake-IP mode). | Exclude `api2.m-team.cc` from fake-IP (for example with `fake-ip-filter`) or use a resolver that returns real addresses. ptctl never uses a proxy. |
| `site CMD timed out: context deadline exceeded` | The 45-second deadline passed. | Check connectivity. |
Expand Down
Loading
Loading