|
| 1 | +# shimkit hosts |
| 2 | + |
| 3 | +`/etc/hosts` editor with atomic-write + timestamped backups. Same |
| 4 | +atomic-replace pattern as `adguard.resolv.write_resolv_static`: write |
| 5 | +to a temp file, then `sudo install -m 0644 -o root <tmp> /etc/hosts`. |
| 6 | +Bind-mounted hosts files (typical inside containers) fall through to a |
| 7 | +Python direct-write through the existing inode. |
| 8 | + |
| 9 | +Pure parsing + mutation logic lives in |
| 10 | +`src/shimkit/tools/hosts/editor.py` (no I/O), so the model is |
| 11 | +unit-testable without touching the system hosts file. |
| 12 | + |
| 13 | +## Commands |
| 14 | + |
| 15 | +| Command | Purpose | |
| 16 | +|----------------------------------------|--------------------------------------------------------------------| |
| 17 | +| `shimkit hosts` | Interactive menu (list / rollback). | |
| 18 | +| `shimkit hosts show` | Print every entry. | |
| 19 | +| `shimkit hosts add IP NAME` | Append. Idempotent. MODERATE prompt. | |
| 20 | +| `shimkit hosts remove NAME` | Remove every entry whose hostname matches. MODERATE prompt. | |
| 21 | +| `shimkit hosts block DOMAIN` | Alias for `add 127.0.0.1 DOMAIN`. | |
| 22 | +| `shimkit hosts unblock DOMAIN` | Alias for `remove DOMAIN`. | |
| 23 | +| `shimkit hosts apply-list SOURCE` | Apply a StevenBlack-style list. **SEVERE** — token required. | |
| 24 | +| `shimkit hosts rollback` | Restore the most recent backup. | |
| 25 | + |
| 26 | +`SOURCE` for `apply-list` is either `http(s)://...` (fetched via |
| 27 | +stdlib `urllib.request`, no extra deps) or a local file path. |
| 28 | + |
| 29 | +Universal flags (`--quiet`, `--verbose`, `--log-file`, `--no-color`, |
| 30 | +`--color`, `--no-input`) go before the subcommand. Per-command flags |
| 31 | +(`--dry-run`, `--yes`, `--force`, `--confirm`, `--path`) go after. |
| 32 | + |
| 33 | +## Safety + the SEVERE tier |
| 34 | + |
| 35 | +`apply-list` is the only severe-tier command. It can write thousands |
| 36 | +of entries at once, so the default token is `APPLY-LIST`: |
| 37 | + |
| 38 | +```bash |
| 39 | +shimkit hosts apply-list https://example.com/list.txt --confirm APPLY-LIST |
| 40 | +``` |
| 41 | + |
| 42 | +The size cap (`tools.hosts.max_entries_per_apply`, default `5000`) |
| 43 | +refuses lists bigger than the threshold. Raise it in |
| 44 | +`~/.config/shimkit/shimkit.json` if you really want the full |
| 45 | +StevenBlack-extended list. |
| 46 | + |
| 47 | +`add` / `remove` / `block` / `unblock` are MODERATE-tier — they |
| 48 | +prompt `[y/N]` by default; `--yes` / `--force` skip; `--no-input` |
| 49 | +refuses with exit 1. |
| 50 | + |
| 51 | +## Atomic-write + backup |
| 52 | + |
| 53 | +Every mutator follows the same sequence: |
| 54 | + |
| 55 | +1. Parse the current file. |
| 56 | +2. Compute the new content in-memory. |
| 57 | +3. `sudo cp -a /etc/hosts /etc/hosts.bak-YYYYMMDDHHMMSS`. |
| 58 | +4. Write to a temp file, then `sudo install -m 0644 -o root` over |
| 59 | + the target. |
| 60 | +5. If `install` fails (typical inside container bind-mounts), fall |
| 61 | + back to a Python direct-write through the existing inode — |
| 62 | + requires the process is already root. |
| 63 | + |
| 64 | +`rollback` restores the latest `*.bak-*` file. |
| 65 | + |
| 66 | +## JSON output |
| 67 | + |
| 68 | +```bash |
| 69 | +$ shimkit hosts show --json |
| 70 | +{ |
| 71 | + "ts": "...", |
| 72 | + "tool": "hosts", |
| 73 | + "step": "show", |
| 74 | + "status": "ok", |
| 75 | + "data": { |
| 76 | + "path": "/etc/hosts", |
| 77 | + "entries": [ |
| 78 | + {"ip": "127.0.0.1", "name": "localhost", "comment": null}, |
| 79 | + {"ip": "::1", "name": "localhost", "comment": null} |
| 80 | + ] |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Configuration |
| 86 | + |
| 87 | +```json |
| 88 | +{ |
| 89 | + "tools": { |
| 90 | + "hosts": { |
| 91 | + "hosts_path": "/etc/hosts", |
| 92 | + "apply_list_severe_token": "APPLY-LIST", |
| 93 | + "max_entries_per_apply": 5000, |
| 94 | + "managed_block_marker": "# === shimkit-managed ===" |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +`--path PATH` overrides `hosts_path` for one invocation — useful for |
| 101 | +testing or for editing a chroot's hosts file from outside. |
| 102 | + |
| 103 | +## Exit codes |
| 104 | + |
| 105 | +| Code | Meaning | |
| 106 | +|-----:|------------------------------------------------------| |
| 107 | +| 0 | success / no-op (entry already present / not present)| |
| 108 | +| 1 | generic failure (invalid IP, no backup, prompt cancelled, severe-token missing) | |
| 109 | +| 2 | Typer usage error | |
| 110 | +| 69 | EX_UNAVAILABLE — wrong platform or hosts file missing | |
| 111 | +| 130 | SIGINT | |
| 112 | + |
| 113 | +## Platform support |
| 114 | + |
| 115 | +| Platform | Status | |
| 116 | +|----------|--------| |
| 117 | +| macOS | ✓ — `/etc/hosts` lives in the same place; same atomic-replace path. | |
| 118 | +| Linux | ✓ | |
| 119 | +| WSL | ✓ (Linux path). | |
| 120 | +| Windows | ✗ — out of charter. | |
0 commit comments