@omnixal/openclaw-nats-plugin 0.2.12 → 0.2.14
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/cli/bun-setup.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from './paths';
|
|
8
8
|
import { downloadNatsServer, NATS_VERSION } from './download-nats';
|
|
9
9
|
import { writeNatsConfig } from './nats-config';
|
|
10
|
-
import { generateApiKey, getExistingApiKey, writeEnvVariables } from './env-writer';
|
|
10
|
+
import { generateApiKey, getExistingApiKey, getExistingHookToken, writeEnvVariables } from './env-writer';
|
|
11
11
|
import {
|
|
12
12
|
getServiceManager, generateSystemdUnit, generateLaunchdPlist,
|
|
13
13
|
installSystemdUnit, installLaunchdPlist, startService, stopService,
|
|
@@ -48,6 +48,7 @@ export async function bunSetup(): Promise<void> {
|
|
|
48
48
|
|
|
49
49
|
// 6. Reuse existing API key or generate new one
|
|
50
50
|
const apiKey = getExistingApiKey() ?? generateApiKey();
|
|
51
|
+
const hookToken = getExistingHookToken() ?? generateApiKey();
|
|
51
52
|
|
|
52
53
|
// 7. Write env variables (to OpenClaw .env for hooks, and sidecar .env for the service)
|
|
53
54
|
const envVars: Record<string, string> = {
|
|
@@ -55,6 +56,7 @@ export async function bunSetup(): Promise<void> {
|
|
|
55
56
|
NATS_PLUGIN_API_KEY: apiKey,
|
|
56
57
|
NATS_SERVERS: 'nats://127.0.0.1:4222',
|
|
57
58
|
OPENCLAW_GATEWAY_URL: 'http://127.0.0.1:18789',
|
|
59
|
+
OPENCLAW_HOOK_TOKEN: hookToken,
|
|
58
60
|
};
|
|
59
61
|
writeEnvVariables(envVars);
|
|
60
62
|
|
|
@@ -66,6 +68,7 @@ export async function bunSetup(): Promise<void> {
|
|
|
66
68
|
`NATS_SERVERS=nats://127.0.0.1:4222`,
|
|
67
69
|
`NATS_PLUGIN_API_KEY=${apiKey}`,
|
|
68
70
|
`OPENCLAW_GATEWAY_URL=http://127.0.0.1:18789`,
|
|
71
|
+
`OPENCLAW_HOOK_TOKEN=${hookToken}`,
|
|
69
72
|
].join('\n');
|
|
70
73
|
writeFileSync(join(SIDECAR_DIR, '.env'), sidecarEnv, 'utf-8');
|
|
71
74
|
|
package/cli/docker-setup.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { mkdirSync, cpSync, writeFileSync, existsSync } from 'node:fs';
|
|
|
2
2
|
import { execFileSync } from 'node:child_process';
|
|
3
3
|
import { join, dirname } from 'node:path';
|
|
4
4
|
import { PLUGIN_DIR, DOCKER_DIR, DASHBOARD_DIR, STATE_FILE, type PluginState } from './paths';
|
|
5
|
-
import { generateApiKey, getExistingApiKey, writeEnvVariables } from './env-writer';
|
|
5
|
+
import { generateApiKey, getExistingApiKey, getExistingHookToken, writeEnvVariables } from './env-writer';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Try to copy dashboard dist into a running OpenClaw container's volume.
|
|
@@ -58,9 +58,10 @@ export async function dockerSetup(): Promise<void> {
|
|
|
58
58
|
cpSync(templateDir, DOCKER_DIR, { recursive: true });
|
|
59
59
|
cpSync(sidecarSrc, join(DOCKER_DIR, 'sidecar'), { recursive: true });
|
|
60
60
|
|
|
61
|
-
// 2. Reuse existing API key or generate new
|
|
61
|
+
// 2. Reuse existing API key and hook token or generate new ones
|
|
62
62
|
const apiKey = getExistingApiKey() ?? generateApiKey();
|
|
63
|
-
|
|
63
|
+
const hookToken = getExistingHookToken() ?? generateApiKey();
|
|
64
|
+
writeFileSync(join(DOCKER_DIR, '.env'), `NATS_PLUGIN_API_KEY=${apiKey}\nOPENCLAW_HOOK_TOKEN=${hookToken}\n`);
|
|
64
65
|
|
|
65
66
|
// 3. Build and start
|
|
66
67
|
console.log('Building and starting containers...');
|
|
@@ -71,6 +72,8 @@ export async function dockerSetup(): Promise<void> {
|
|
|
71
72
|
NATS_SIDECAR_URL: 'http://127.0.0.1:3104',
|
|
72
73
|
NATS_PLUGIN_API_KEY: apiKey,
|
|
73
74
|
NATS_SERVERS: 'nats://127.0.0.1:4222',
|
|
75
|
+
OPENCLAW_GATEWAY_URL: 'http://127.0.0.1:18789',
|
|
76
|
+
OPENCLAW_HOOK_TOKEN: hookToken,
|
|
74
77
|
});
|
|
75
78
|
|
|
76
79
|
// 5. Copy dashboard dist into OpenClaw container (host→container bridge)
|
package/cli/env-writer.ts
CHANGED
|
@@ -14,6 +14,13 @@ export function getExistingApiKey(): string | null {
|
|
|
14
14
|
return match?.[1]?.trim() || null;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
export function getExistingHookToken(): string | null {
|
|
18
|
+
if (!existsSync(OPENCLAW_ENV)) return null;
|
|
19
|
+
const content = readFileSync(OPENCLAW_ENV, 'utf-8');
|
|
20
|
+
const match = content.match(/^OPENCLAW_HOOK_TOKEN=(.+)$/m);
|
|
21
|
+
return match?.[1]?.trim() || null;
|
|
22
|
+
}
|
|
23
|
+
|
|
17
24
|
export function mergeEnvContent(
|
|
18
25
|
existingContent: string,
|
|
19
26
|
variables: Record<string, string>,
|
|
@@ -31,6 +31,7 @@ services:
|
|
|
31
31
|
- NATS_SERVERS=nats://nats:4222
|
|
32
32
|
- OPENCLAW_GATEWAY_URL=http://host.docker.internal:18789
|
|
33
33
|
- NATS_PLUGIN_API_KEY=${NATS_PLUGIN_API_KEY}
|
|
34
|
+
- OPENCLAW_HOOK_TOKEN=${OPENCLAW_HOOK_TOKEN}
|
|
34
35
|
volumes:
|
|
35
36
|
- sidecar-data:/app/data
|
|
36
37
|
restart: unless-stopped
|
package/package.json
CHANGED
|
@@ -2,11 +2,13 @@ import fs from 'node:fs/promises';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { existsSync } from 'node:fs';
|
|
4
4
|
import { homedir } from 'node:os';
|
|
5
|
+
import http from 'node:http';
|
|
5
6
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
6
7
|
|
|
7
8
|
const ROUTE_PREFIX = '/nats-dashboard';
|
|
8
9
|
const SIDECAR_URL = process.env.NATS_SIDECAR_URL || 'http://127.0.0.1:3104';
|
|
9
10
|
const API_KEY = process.env.NATS_PLUGIN_API_KEY || 'dev-nats-plugin-key';
|
|
11
|
+
const sidecarParsed = new URL(SIDECAR_URL);
|
|
10
12
|
|
|
11
13
|
// Stable location (copied during setup) takes priority over in-package dist
|
|
12
14
|
const STABLE_DIST = path.join(homedir(), '.openclaw', 'nats-plugin', 'dashboard');
|
|
@@ -52,7 +54,6 @@ async function proxyToSidecar(
|
|
|
52
54
|
res: ServerResponse,
|
|
53
55
|
): Promise<boolean> {
|
|
54
56
|
try {
|
|
55
|
-
const targetUrl = `${SIDECAR_URL}${subPath}${search}`;
|
|
56
57
|
const headers: Record<string, string> = {
|
|
57
58
|
'Authorization': `Bearer ${API_KEY}`,
|
|
58
59
|
};
|
|
@@ -65,27 +66,61 @@ async function proxyToSidecar(
|
|
|
65
66
|
body = await readBody(req);
|
|
66
67
|
}
|
|
67
68
|
|
|
68
|
-
|
|
69
|
+
// Use node:http directly — global fetch() may be intercepted by gateway SSRF guards
|
|
70
|
+
const upstream = await httpRequest({
|
|
71
|
+
hostname: sidecarParsed.hostname,
|
|
72
|
+
port: Number(sidecarParsed.port),
|
|
73
|
+
path: `${subPath}${search}`,
|
|
69
74
|
method: req.method || 'GET',
|
|
70
75
|
headers,
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
res.
|
|
76
|
-
res.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
timeout: 10_000,
|
|
77
|
+
}, body);
|
|
78
|
+
|
|
79
|
+
res.statusCode = upstream.statusCode;
|
|
80
|
+
res.setHeader('content-type', upstream.headers['content-type'] || 'application/json');
|
|
81
|
+
res.end(upstream.body);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
84
|
+
console.error(`[nats-dashboard] Sidecar proxy error: ${message} (url=${SIDECAR_URL}${subPath})`);
|
|
80
85
|
res.statusCode = 502;
|
|
81
86
|
res.setHeader('content-type', 'application/json');
|
|
82
|
-
res.end(JSON.stringify({ error: 'Sidecar unreachable' }));
|
|
87
|
+
res.end(JSON.stringify({ error: 'Sidecar unreachable', detail: message }));
|
|
83
88
|
}
|
|
84
89
|
return true;
|
|
85
90
|
}
|
|
86
91
|
|
|
87
92
|
const MAX_BODY_BYTES = 1_048_576; // 1MB
|
|
88
93
|
|
|
94
|
+
interface HttpResponse {
|
|
95
|
+
statusCode: number;
|
|
96
|
+
headers: Record<string, string | string[] | undefined>;
|
|
97
|
+
body: string;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function httpRequest(
|
|
101
|
+
opts: http.RequestOptions,
|
|
102
|
+
body?: string,
|
|
103
|
+
): Promise<HttpResponse> {
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
const req = http.request(opts, (res) => {
|
|
106
|
+
const chunks: Buffer[] = [];
|
|
107
|
+
res.on('data', (chunk: Buffer) => chunks.push(chunk));
|
|
108
|
+
res.on('end', () => {
|
|
109
|
+
resolve({
|
|
110
|
+
statusCode: res.statusCode || 500,
|
|
111
|
+
headers: res.headers,
|
|
112
|
+
body: Buffer.concat(chunks).toString(),
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
res.on('error', reject);
|
|
116
|
+
});
|
|
117
|
+
req.on('error', reject);
|
|
118
|
+
req.on('timeout', () => { req.destroy(); reject(new Error('Request timeout')); });
|
|
119
|
+
if (body) req.write(body);
|
|
120
|
+
req.end();
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
89
124
|
function readBody(req: IncomingMessage): Promise<string> {
|
|
90
125
|
return new Promise((resolve, reject) => {
|
|
91
126
|
const chunks: Buffer[] = [];
|