@claw-link/gateway-host 0.2.8 → 0.2.9
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/bin/cli.js +5 -0
- package/package.json +1 -1
- package/scripts/transport-test.js +41 -0
- package/src/bridge.js +3 -0
- package/src/transport/relay.js +51 -0
package/bin/cli.js
CHANGED
|
@@ -48,6 +48,10 @@ async function main() {
|
|
|
48
48
|
case 'status':
|
|
49
49
|
return require('../scripts/install').status();
|
|
50
50
|
|
|
51
|
+
case 'transport-test':
|
|
52
|
+
case 'transport':
|
|
53
|
+
return require('../scripts/transport-test').run();
|
|
54
|
+
|
|
51
55
|
case 'rotate':
|
|
52
56
|
console.log([
|
|
53
57
|
'To rotate a Host Token (also how you move an agent to a new machine):',
|
|
@@ -72,6 +76,7 @@ async function main() {
|
|
|
72
76
|
' retoken Re-enter a new Host Token for an existing agent (clhost retoken <id>)',
|
|
73
77
|
' agents List mapped agents',
|
|
74
78
|
' status Show bridge, service state, and mapped agents',
|
|
79
|
+
' transport-test Verify content-addressed Capsule relay (host ⇄ central sync + integrity)',
|
|
75
80
|
' start Start the background service',
|
|
76
81
|
' stop Stop the background service',
|
|
77
82
|
' restart Restart the background service (apply config changes)',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@claw-link/gateway-host",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "ClawLink Host Gateway — a secure, outbound-only worker that bridges a local agent CLI (OpenClaw, Hermes, Claude, Codex, Cursor) to your ClawLink agents. No inbound ports; authenticated per-agent by a Host Token.",
|
|
5
5
|
"homepage": "https://claw-link.co",
|
|
6
6
|
"bin": {
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// `clhost transport-test` — proves the AHP content-addressed relay works end to end:
|
|
2
|
+
// generates random bytes, content-addresses them, uploads to central Storage via a signed
|
|
3
|
+
// URL, fetches them back, and verifies the bytes are byte-identical and the cid matches.
|
|
4
|
+
// Isolated from the harness run loop — safe to run any time against a connected agent.
|
|
5
|
+
'use strict';
|
|
6
|
+
|
|
7
|
+
const crypto = require('crypto');
|
|
8
|
+
const config = require('../src/config');
|
|
9
|
+
const { Bridge } = require('../src/bridge');
|
|
10
|
+
const { cidOf, putBlob, getBlob } = require('../src/transport/relay');
|
|
11
|
+
|
|
12
|
+
async function run() {
|
|
13
|
+
const cfg = config.load();
|
|
14
|
+
const agent = (cfg.agents || [])[0];
|
|
15
|
+
if (!agent || !agent.host_token) {
|
|
16
|
+
console.error('No agent configured. Run: clhost add-agent');
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
const bridge = new Bridge(cfg.bridge_url, agent, cfg.instance_id);
|
|
20
|
+
const sizeKB = Number(process.argv[3]) || 64;
|
|
21
|
+
const payload = crypto.randomBytes(sizeKB * 1024);
|
|
22
|
+
const expectCid = cidOf(payload);
|
|
23
|
+
|
|
24
|
+
console.log(`→ ${(payload.length / 1024).toFixed(0)} KB, cid ${expectCid.slice(0, 18)}…`);
|
|
25
|
+
try {
|
|
26
|
+
const t0 = Date.now();
|
|
27
|
+
const put = await putBlob(bridge, 'selftest', payload);
|
|
28
|
+
console.log(` uploaded → ${put.path} (${Date.now() - t0}ms)`);
|
|
29
|
+
const t1 = Date.now();
|
|
30
|
+
const got = await getBlob(bridge, put.path, expectCid);
|
|
31
|
+
const ok = got.length === payload.length && Buffer.compare(got, payload) === 0;
|
|
32
|
+
console.log(` fetched + verified (${Date.now() - t1}ms)`);
|
|
33
|
+
if (!ok) throw new Error('round-trip mismatch');
|
|
34
|
+
console.log('✓ content-addressed relay OK — host ⇄ central sync + integrity verified.');
|
|
35
|
+
} catch (e) {
|
|
36
|
+
console.error(`✗ transport-test failed: ${e.message}`);
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
module.exports = { run };
|
package/src/bridge.js
CHANGED
|
@@ -41,6 +41,9 @@ class Bridge {
|
|
|
41
41
|
stream(jobId, seq, type, content) { return this._post('stream', { job_id: jobId, seq, type, content }); }
|
|
42
42
|
complete(jobId, finalContent, metadata) { return this._post('complete', { job_id: jobId, final_content: finalContent, metadata: metadata || {} }); }
|
|
43
43
|
fail(jobId, error) { return this._post('fail', { job_id: jobId, error: String(error).slice(0, 1000) }); }
|
|
44
|
+
// AHP data plane: request short-lived signed URLs to sync content-addressed Capsule bytes.
|
|
45
|
+
capsulePut(path) { return this._post('capsule_put', { path }); }
|
|
46
|
+
capsuleGet(path) { return this._post('capsule_get', { path }); }
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
// Retry wrapper for transient network errors (not auth errors).
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// AHP data plane — Phase 1 (content-addressed central relay, pure JS).
|
|
2
|
+
//
|
|
3
|
+
// A Capsule's bytes are content-addressed (cid = sha256) and synced to central Storage
|
|
4
|
+
// via short-lived signed URLs requested over the host's existing outbound bridge — no
|
|
5
|
+
// inbound port, no extra dependency (Node built-in crypto + global fetch). Every fetch is
|
|
6
|
+
// re-hashed and verified against the cid, so a tampered/corrupt object is rejected. This is
|
|
7
|
+
// the relay rung of the fetch ladder and the foundation the Rust P2P core later accelerates;
|
|
8
|
+
// if any step fails the caller falls back to the inline path — never worse than today.
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
const crypto = require('crypto');
|
|
12
|
+
|
|
13
|
+
/** Content address: a stable, self-verifying id derived from the bytes. */
|
|
14
|
+
function cidOf(buf) {
|
|
15
|
+
return 'blob_' + crypto.createHash('sha256').update(buf).digest('hex');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Upload bytes to central Storage under <namespace>/<cid>; returns {cid, path, size}. */
|
|
19
|
+
async function putBlob(bridge, namespace, buf) {
|
|
20
|
+
const data = Buffer.isBuffer(buf) ? buf : Buffer.from(buf);
|
|
21
|
+
const cid = cidOf(data);
|
|
22
|
+
const r = await bridge.capsulePut(`${namespace}/${cid}`);
|
|
23
|
+
if (!r || !r.signed_url) throw new Error('capsule_put: no signed url');
|
|
24
|
+
const res = await fetch(r.signed_url, {
|
|
25
|
+
method: 'PUT',
|
|
26
|
+
headers: {
|
|
27
|
+
'content-type': 'application/octet-stream',
|
|
28
|
+
'x-upsert': 'true', // content-addressed → re-uploading the same cid is idempotent
|
|
29
|
+
...(r.token ? { authorization: `Bearer ${r.token}` } : {}),
|
|
30
|
+
},
|
|
31
|
+
body: data,
|
|
32
|
+
});
|
|
33
|
+
if (!res.ok && res.status !== 409) throw new Error(`capsule upload failed (${res.status})`);
|
|
34
|
+
return { cid, path: r.path, size: data.length };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Fetch bytes by storage path and VERIFY them against expectedCid before returning. */
|
|
38
|
+
async function getBlob(bridge, path, expectedCid) {
|
|
39
|
+
const r = await bridge.capsuleGet(path);
|
|
40
|
+
if (!r || !r.url) throw new Error('capsule_get: no signed url');
|
|
41
|
+
const res = await fetch(r.url);
|
|
42
|
+
if (!res.ok) throw new Error(`capsule download failed (${res.status})`);
|
|
43
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
44
|
+
const got = cidOf(buf);
|
|
45
|
+
if (expectedCid && got !== expectedCid) {
|
|
46
|
+
throw new Error(`integrity check failed: ${got} != ${expectedCid}`);
|
|
47
|
+
}
|
|
48
|
+
return buf;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = { cidOf, putBlob, getBlob };
|