ckforensics 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +126 -0
- package/bin/ckforensics.js +85 -0
- package/package.json +55 -0
- package/scripts/postinstall.js +237 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 phong28zk
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# ckforensics
|
|
2
|
+
|
|
3
|
+
**Forensic CLI for Claude Code sessions — audit, redact, export your transcripts.**
|
|
4
|
+
|
|
5
|
+
[](https://github.com/phong28zk/ckforensics/actions/workflows/ci.yml)
|
|
6
|
+
[](https://www.npmjs.com/package/ckforensics)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
[](https://bun.sh)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Why ckforensics
|
|
13
|
+
|
|
14
|
+
**Pain:**
|
|
15
|
+
- Claude Code transcripts stored as raw JSONL — no way to summarize or audit them
|
|
16
|
+
- Sensitive keys / tokens leak into `.jsonl` files that get committed or shared
|
|
17
|
+
- No visibility into token usage, tool calls, or subagent orchestration patterns
|
|
18
|
+
|
|
19
|
+
**Solution:**
|
|
20
|
+
- Ingest all sessions into a local SQLite DB — queryable, indexed, stays on disk
|
|
21
|
+
- Redact 9 secret patterns (API keys, AWS creds, JWTs, PEM blocks) before any export
|
|
22
|
+
- CLI commands for summary, audit, session diff, and export — no cloud, no telemetry
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm i -g ckforensics
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or download a prebuilt binary from [Releases](https://github.com/phong28zk/ckforensics/releases):
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# Linux x64
|
|
36
|
+
curl -L https://github.com/phong28zk/ckforensics/releases/latest/download/ckforensics-linux-x64 \
|
|
37
|
+
-o /usr/local/bin/ckforensics && chmod +x /usr/local/bin/ckforensics
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Platforms: `linux-x64`, `linux-arm64`, `macos-x64`, `macos-arm64`, `windows-x64`
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Quick Start
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# 1. Ingest all sessions from default Claude Code path
|
|
48
|
+
ckforensics ingest
|
|
49
|
+
|
|
50
|
+
# 2. Summarize recent sessions
|
|
51
|
+
ckforensics summary --last 7d
|
|
52
|
+
|
|
53
|
+
# 3. Audit a session for leaked secrets
|
|
54
|
+
ckforensics audit --session <session-id>
|
|
55
|
+
|
|
56
|
+
# 4. Redact and export a session as Markdown
|
|
57
|
+
ckforensics export --session <session-id> --format markdown --redact
|
|
58
|
+
|
|
59
|
+
# 5. Health check (verify DB integrity + schema version)
|
|
60
|
+
ckforensics doctor
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
> Sessions are stored in `~/.ckforensics/db.sqlite` (mode `0600`). Nothing leaves your machine.
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Screenshots
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
[ terminal recording placeholder — see docs/demo.gif once available ]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
---
|
|
74
|
+
|
|
75
|
+
## Feature Matrix
|
|
76
|
+
|
|
77
|
+
| Feature | ckforensics | ccusage | Native CC |
|
|
78
|
+
|---------|:-----------:|:-------:|:---------:|
|
|
79
|
+
| Local SQLite storage | ✅ | ✅ | ❌ |
|
|
80
|
+
| Token usage analytics | ✅ | ✅ | ❌ |
|
|
81
|
+
| Secret redaction (9 rules) | ✅ | ❌ | ❌ |
|
|
82
|
+
| Session audit trail | ✅ | ❌ | ❌ |
|
|
83
|
+
| Subagent tracking | ✅ | ❌ | ❌ |
|
|
84
|
+
| Markdown / JSON export | ✅ | ❌ | ❌ |
|
|
85
|
+
| Diff between sessions | ✅ | ❌ | ❌ |
|
|
86
|
+
| Offline / no telemetry | ✅ | ✅ | ✅ |
|
|
87
|
+
| Cross-platform binaries | ✅ | ❌ | ✅ |
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Commands
|
|
92
|
+
|
|
93
|
+
| Command | Description |
|
|
94
|
+
|---------|-------------|
|
|
95
|
+
| `ingest` | Parse JSONL files → SQLite |
|
|
96
|
+
| `summary` | Token + cost summary for a time range |
|
|
97
|
+
| `audit` | Detect secrets / anomalies in a session |
|
|
98
|
+
| `export` | Output session as Markdown or JSON |
|
|
99
|
+
| `redact` | Strip secrets from JSONL in-place |
|
|
100
|
+
| `sessions` | List sessions with metadata |
|
|
101
|
+
| `path` | Show DB path and stats |
|
|
102
|
+
| `doctor` | Verify DB integrity and schema version |
|
|
103
|
+
|
|
104
|
+
---
|
|
105
|
+
|
|
106
|
+
## Architecture
|
|
107
|
+
|
|
108
|
+
See [docs/architecture.md](docs/architecture.md) — module overview and data-flow diagram.
|
|
109
|
+
|
|
110
|
+
## Threat Model
|
|
111
|
+
|
|
112
|
+
See [docs/threat-model.md](docs/threat-model.md) — privacy stance, redaction guarantees, and known gaps.
|
|
113
|
+
|
|
114
|
+
## Schema
|
|
115
|
+
|
|
116
|
+
See [docs/jsonl-schema-v1.md](docs/jsonl-schema-v1.md) — reverse-engineered Claude Code JSONL event types.
|
|
117
|
+
|
|
118
|
+
## Contributing
|
|
119
|
+
|
|
120
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) — dev setup, PR checklist, fixture contribution flow.
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## License
|
|
125
|
+
|
|
126
|
+
MIT — [LICENSE](LICENSE) — Copyright (c) 2026 phong28zk
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* bin/ckforensics.js — npm wrapper shim for ckforensics.
|
|
4
|
+
*
|
|
5
|
+
* This file is the npm `bin` entry point. It detects the current platform/arch,
|
|
6
|
+
* locates the matching prebuilt binary inside node_modules/ckforensics/binaries/,
|
|
7
|
+
* and delegates execution via spawnSync (stdio: inherit + exit code propagation).
|
|
8
|
+
*
|
|
9
|
+
* Written as ESM because package.json sets "type": "module".
|
|
10
|
+
*
|
|
11
|
+
* Requirements:
|
|
12
|
+
* - Node 18+ (uses only built-in modules: path, child_process, fs, url)
|
|
13
|
+
* - Binary must have been placed by scripts/postinstall.js (runs on npm install)
|
|
14
|
+
*
|
|
15
|
+
* Error behaviour:
|
|
16
|
+
* - Binary missing → clear message pointing to postinstall, exits 1
|
|
17
|
+
* - spawnSync error → prints OS error, exits 1
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { spawnSync } from "child_process";
|
|
21
|
+
import { existsSync } from "fs";
|
|
22
|
+
import { createRequire } from "module";
|
|
23
|
+
import { fileURLToPath } from "url";
|
|
24
|
+
import path from "path";
|
|
25
|
+
|
|
26
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
27
|
+
const __dirname = path.dirname(__filename);
|
|
28
|
+
|
|
29
|
+
// ── Platform → binary name mapping ───────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
// darwin-x64 (Intel Mac) intentionally omitted — see release.yml comment.
|
|
32
|
+
// Intel Mac users: build from source via `bun build --compile --target=bun-darwin-x64`.
|
|
33
|
+
const PLATFORM_MAP = {
|
|
34
|
+
"linux-x64": "ckforensics-linux-x64",
|
|
35
|
+
"linux-arm64": "ckforensics-linux-arm64",
|
|
36
|
+
"darwin-arm64": "ckforensics-darwin-arm64",
|
|
37
|
+
"win32-x64": "ckforensics-win-x64.exe",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// ── Resolve binary path ───────────────────────────────────────────────────────
|
|
41
|
+
|
|
42
|
+
const platform = process.platform; // "linux" | "darwin" | "win32"
|
|
43
|
+
const arch = process.arch; // "x64" | "arm64"
|
|
44
|
+
const key = `${platform}-${arch}`;
|
|
45
|
+
const binName = PLATFORM_MAP[key];
|
|
46
|
+
|
|
47
|
+
if (!binName) {
|
|
48
|
+
process.stderr.write(
|
|
49
|
+
`[ckforensics] Unsupported platform: ${platform}-${arch}\n` +
|
|
50
|
+
`Supported: ${Object.keys(PLATFORM_MAP).join(", ")}\n`
|
|
51
|
+
);
|
|
52
|
+
process.exit(1);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Binaries live next to this shim's package root: <pkg>/binaries/<name>
|
|
56
|
+
// __dirname is <pkg>/bin/, so go up one level.
|
|
57
|
+
const pkgRoot = path.resolve(__dirname, "..");
|
|
58
|
+
const binaryDir = path.join(pkgRoot, "binaries");
|
|
59
|
+
const binaryPath = path.join(binaryDir, binName);
|
|
60
|
+
|
|
61
|
+
if (!existsSync(binaryPath)) {
|
|
62
|
+
process.stderr.write(
|
|
63
|
+
`[ckforensics] Binary not found: ${binaryPath}\n` +
|
|
64
|
+
`This usually means the postinstall script did not run.\n` +
|
|
65
|
+
`Try: npm install (or re-run: node ${path.join(pkgRoot, "scripts", "postinstall.js")})\n`
|
|
66
|
+
);
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ── Execute binary ────────────────────────────────────────────────────────────
|
|
71
|
+
|
|
72
|
+
const result = spawnSync(binaryPath, process.argv.slice(2), {
|
|
73
|
+
stdio: "inherit",
|
|
74
|
+
// Pass through the full environment so the binary inherits PATH, HOME, etc.
|
|
75
|
+
env: process.env,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (result.error) {
|
|
79
|
+
process.stderr.write(
|
|
80
|
+
`[ckforensics] Failed to launch binary: ${result.error.message}\n`
|
|
81
|
+
);
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
process.exit(result.status ?? 0);
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ckforensics",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"module": "index.ts",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"ckforensics": "./bin/ckforensics.js"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"cli": "bun run src/cli/index.ts",
|
|
11
|
+
"build:all": "./scripts/build-all.sh",
|
|
12
|
+
"test": "bun test",
|
|
13
|
+
"typecheck": "tsc --noEmit",
|
|
14
|
+
"postinstall": "node ./scripts/postinstall.js || true"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"bin/",
|
|
18
|
+
"scripts/postinstall.js",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"bun": ">=1.1.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"commander": "^14.0.3"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/bun": "latest"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"typescript": "^5"
|
|
33
|
+
},
|
|
34
|
+
"description": "Forensic CLI for Claude Code sessions — audit, redact, export your transcripts.",
|
|
35
|
+
"homepage": "https://github.com/phong28zk/ckforensics#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/phong28zk/ckforensics/issues"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/phong28zk/ckforensics.git"
|
|
42
|
+
},
|
|
43
|
+
"author": "phong28zk",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"keywords": [
|
|
46
|
+
"claude",
|
|
47
|
+
"claude-code",
|
|
48
|
+
"anthropic",
|
|
49
|
+
"cli",
|
|
50
|
+
"audit",
|
|
51
|
+
"forensics",
|
|
52
|
+
"sqlite",
|
|
53
|
+
"bun"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* scripts/postinstall.js — npm postinstall binary downloader.
|
|
4
|
+
*
|
|
5
|
+
* Runs automatically on `npm install` (triggered via package.json postinstall).
|
|
6
|
+
* Downloads the correct platform binary from the GitHub Release matching the
|
|
7
|
+
* package version, verifies SHA256 checksum, makes it executable, and saves
|
|
8
|
+
* it to <pkg>/binaries/ where bin/ckforensics.js will find it.
|
|
9
|
+
*
|
|
10
|
+
* Written as ESM because package.json sets "type": "module".
|
|
11
|
+
*
|
|
12
|
+
* Design constraints:
|
|
13
|
+
* - Node 18+ only (uses built-in https, fs, crypto, path, url — NO npm deps)
|
|
14
|
+
* - Skip silently when running inside the source repo (detected via .git)
|
|
15
|
+
* - Skip when CKFORENSICS_SKIP_DOWNLOAD=1 env var is set (CI compile step)
|
|
16
|
+
* - Fail loudly with actionable message if download or checksum fails
|
|
17
|
+
*
|
|
18
|
+
* GitHub Release asset naming (must match scripts/build-all.sh):
|
|
19
|
+
* ckforensics-linux-x64
|
|
20
|
+
* ckforensics-linux-arm64
|
|
21
|
+
* ckforensics-darwin-x64
|
|
22
|
+
* ckforensics-darwin-arm64
|
|
23
|
+
* ckforensics-win-x64.exe
|
|
24
|
+
* checksums.txt
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import https from "https";
|
|
28
|
+
import fs from "fs";
|
|
29
|
+
import path from "path";
|
|
30
|
+
import crypto from "crypto";
|
|
31
|
+
import { fileURLToPath } from "url";
|
|
32
|
+
|
|
33
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
34
|
+
const __dirname = path.dirname(__filename);
|
|
35
|
+
|
|
36
|
+
// ── Configuration ─────────────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
const GITHUB_REPO = "phong28zk/ckforensics";
|
|
39
|
+
const PKG_ROOT = path.resolve(__dirname, "..");
|
|
40
|
+
const PKG_JSON = path.join(PKG_ROOT, "package.json");
|
|
41
|
+
const BINARIES_DIR = path.join(PKG_ROOT, "binaries");
|
|
42
|
+
|
|
43
|
+
// ── Early exit conditions ─────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
// 1. Respect explicit opt-out (useful in CI compile jobs).
|
|
46
|
+
if (process.env.CKFORENSICS_SKIP_DOWNLOAD === "1") {
|
|
47
|
+
console.log("[ckforensics] CKFORENSICS_SKIP_DOWNLOAD=1 — skipping binary download.");
|
|
48
|
+
process.exit(0);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 2. Skip when running in the source repo (developer workflow).
|
|
52
|
+
// Presence of a .git directory at pkg root means we are in a git checkout.
|
|
53
|
+
if (fs.existsSync(path.join(PKG_ROOT, ".git"))) {
|
|
54
|
+
console.log("[ckforensics] Source repo detected (.git present) — skipping binary download.");
|
|
55
|
+
process.exit(0);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ── Platform → binary name mapping ───────────────────────────────────────────
|
|
59
|
+
|
|
60
|
+
// darwin-x64 (Intel Mac) intentionally omitted from prebuilt releases.
|
|
61
|
+
// Intel Mac users: build from source — see README "Build from source".
|
|
62
|
+
const PLATFORM_MAP = {
|
|
63
|
+
"linux-x64": "ckforensics-linux-x64",
|
|
64
|
+
"linux-arm64": "ckforensics-linux-arm64",
|
|
65
|
+
"darwin-arm64": "ckforensics-darwin-arm64",
|
|
66
|
+
"win32-x64": "ckforensics-win-x64.exe",
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const platform = process.platform; // "linux" | "darwin" | "win32"
|
|
70
|
+
const arch = process.arch; // "x64" | "arm64"
|
|
71
|
+
const key = `${platform}-${arch}`;
|
|
72
|
+
const binName = PLATFORM_MAP[key];
|
|
73
|
+
|
|
74
|
+
if (!binName) {
|
|
75
|
+
console.warn(
|
|
76
|
+
`[ckforensics] Unsupported platform ${platform}-${arch} — no binary available.\n` +
|
|
77
|
+
`Supported: ${Object.keys(PLATFORM_MAP).join(", ")}\n` +
|
|
78
|
+
`You can build from source: bun build --compile src/cli/index.ts`
|
|
79
|
+
);
|
|
80
|
+
// Non-fatal: user can still build from source.
|
|
81
|
+
process.exit(0);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ── Resolve package version ───────────────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
let version;
|
|
87
|
+
try {
|
|
88
|
+
const pkg = JSON.parse(fs.readFileSync(PKG_JSON, "utf8"));
|
|
89
|
+
version = pkg.version;
|
|
90
|
+
if (!version) throw new Error("version field missing");
|
|
91
|
+
} catch (e) {
|
|
92
|
+
fatal(`Cannot read package version from ${PKG_JSON}: ${e.message}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const tagName = `v${version}`;
|
|
96
|
+
const releaseBaseUrl = `https://github.com/${GITHUB_REPO}/releases/download/${tagName}`;
|
|
97
|
+
const binaryUrl = `${releaseBaseUrl}/${binName}`;
|
|
98
|
+
const checksumUrl = `${releaseBaseUrl}/checksums.txt`;
|
|
99
|
+
|
|
100
|
+
// ── Utilities ─────────────────────────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
/** Exit with a human-readable error. */
|
|
103
|
+
function fatal(msg) {
|
|
104
|
+
process.stderr.write(`[ckforensics] ERROR: ${msg}\n`);
|
|
105
|
+
process.exit(1);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Follow HTTP redirects and return the final response.
|
|
110
|
+
* Supports up to 10 hops (GitHub Releases redirects to S3).
|
|
111
|
+
*
|
|
112
|
+
* @param {string} url
|
|
113
|
+
* @param {number} hops
|
|
114
|
+
* @returns {Promise<import("http").IncomingMessage>}
|
|
115
|
+
*/
|
|
116
|
+
function httpsGet(url, hops = 0) {
|
|
117
|
+
return new Promise((resolve, reject) => {
|
|
118
|
+
if (hops > 10) return reject(new Error(`Too many redirects for ${url}`));
|
|
119
|
+
|
|
120
|
+
https
|
|
121
|
+
.get(url, { headers: { "User-Agent": "ckforensics-postinstall/1.0" } }, (res) => {
|
|
122
|
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
123
|
+
res.resume(); // discard body
|
|
124
|
+
return resolve(httpsGet(res.headers.location, hops + 1));
|
|
125
|
+
}
|
|
126
|
+
resolve(res);
|
|
127
|
+
})
|
|
128
|
+
.on("error", reject);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Download a URL and buffer the full body.
|
|
134
|
+
*
|
|
135
|
+
* @param {string} url
|
|
136
|
+
* @returns {Promise<{ status: number, buffer: Buffer }>}
|
|
137
|
+
*/
|
|
138
|
+
async function download(url) {
|
|
139
|
+
const res = await httpsGet(url);
|
|
140
|
+
return new Promise((resolve, reject) => {
|
|
141
|
+
const chunks = [];
|
|
142
|
+
res.on("data", (c) => chunks.push(c));
|
|
143
|
+
res.on("end", () => resolve({ status: res.statusCode, buffer: Buffer.concat(chunks) }));
|
|
144
|
+
res.on("error", reject);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Compute SHA256 hex of a Buffer. */
|
|
149
|
+
function sha256hex(buf) {
|
|
150
|
+
return crypto.createHash("sha256").update(buf).digest("hex");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Parse checksums.txt (sha256sum format: "<hash> <filename>\n").
|
|
155
|
+
*
|
|
156
|
+
* @param {string} text
|
|
157
|
+
* @returns {Map<string, string>} filename → lowercase hex hash
|
|
158
|
+
*/
|
|
159
|
+
function parseChecksums(text) {
|
|
160
|
+
const map = new Map();
|
|
161
|
+
for (const line of text.split("\n")) {
|
|
162
|
+
const trimmed = line.trim();
|
|
163
|
+
if (!trimmed) continue;
|
|
164
|
+
// Two-space separator (sha256sum convention) or single space (shasum)
|
|
165
|
+
const match = trimmed.match(/^([0-9a-f]{64})\s{1,2}(.+)$/i);
|
|
166
|
+
if (match) map.set(match[2].trim(), match[1].toLowerCase());
|
|
167
|
+
}
|
|
168
|
+
return map;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
172
|
+
|
|
173
|
+
async function main() {
|
|
174
|
+
console.log(`[ckforensics] Downloading binary for ${platform}-${arch} (${tagName})...`);
|
|
175
|
+
|
|
176
|
+
// 1. Fetch checksums first (fail fast if release doesn't exist).
|
|
177
|
+
console.log(`[ckforensics] Fetching checksums from: ${checksumUrl}`);
|
|
178
|
+
const cksumResult = await download(checksumUrl).catch((e) =>
|
|
179
|
+
fatal(`Failed to fetch checksums: ${e.message}`)
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
if (cksumResult.status !== 200) {
|
|
183
|
+
fatal(
|
|
184
|
+
`Checksums not found (HTTP ${cksumResult.status}). ` +
|
|
185
|
+
`Release ${tagName} may not exist yet at https://github.com/${GITHUB_REPO}/releases`
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const checksums = parseChecksums(cksumResult.buffer.toString("utf8"));
|
|
190
|
+
const expectedHash = checksums.get(binName);
|
|
191
|
+
|
|
192
|
+
if (!expectedHash) {
|
|
193
|
+
fatal(
|
|
194
|
+
`No checksum entry for "${binName}" in checksums.txt. ` +
|
|
195
|
+
`Available entries: ${[...checksums.keys()].join(", ")}`
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// 2. Download binary.
|
|
200
|
+
console.log(`[ckforensics] Downloading binary from: ${binaryUrl}`);
|
|
201
|
+
const binResult = await download(binaryUrl).catch((e) =>
|
|
202
|
+
fatal(`Failed to download binary: ${e.message}`)
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
if (binResult.status !== 200) {
|
|
206
|
+
fatal(`Binary download failed (HTTP ${binResult.status}). URL: ${binaryUrl}`);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// 3. Verify checksum.
|
|
210
|
+
const actualHash = sha256hex(binResult.buffer);
|
|
211
|
+
if (actualHash !== expectedHash) {
|
|
212
|
+
fatal(
|
|
213
|
+
`SHA256 mismatch for ${binName}!\n` +
|
|
214
|
+
` Expected: ${expectedHash}\n` +
|
|
215
|
+
` Got: ${actualHash}\n` +
|
|
216
|
+
`The download may be corrupt or tampered — aborting for safety.`
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
console.log(`[ckforensics] Checksum verified OK (${actualHash.slice(0, 16)}...)`);
|
|
220
|
+
|
|
221
|
+
// 4. Write binary to disk.
|
|
222
|
+
fs.mkdirSync(BINARIES_DIR, { recursive: true });
|
|
223
|
+
const destPath = path.join(BINARIES_DIR, binName);
|
|
224
|
+
fs.writeFileSync(destPath, binResult.buffer);
|
|
225
|
+
|
|
226
|
+
// 5. chmod +x on Unix.
|
|
227
|
+
if (platform !== "win32") {
|
|
228
|
+
fs.chmodSync(destPath, 0o755);
|
|
229
|
+
console.log(`[ckforensics] Set executable bit on ${destPath}`);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const sizeMB = (binResult.buffer.length / 1024 / 1024).toFixed(1);
|
|
233
|
+
console.log(`[ckforensics] Installed ${binName} (${sizeMB} MB) → ${destPath}`);
|
|
234
|
+
console.log(`[ckforensics] Run: ckforensics --help`);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
main().catch((e) => fatal(String(e)));
|