@myrialabs/clopen 0.4.31 → 0.4.33
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/backend/auth/permissions.test.ts +0 -1
- package/backend/auth/permissions.ts +0 -4
- package/backend/chat/stream-manager.ts +31 -12
- package/backend/database/migrations/068_create_engine_config_revision.test.ts +121 -0
- package/backend/database/migrations/068_create_engine_config_revision.ts +117 -0
- package/backend/database/migrations/069_create_ssh_client_tables.ts +116 -0
- package/backend/database/migrations/index.ts +14 -0
- package/backend/database/queries/db-client-connection-queries.ts +7 -2
- package/backend/database/queries/file-audit-log-queries.ts +1 -0
- package/backend/database/queries/index.ts +3 -0
- package/backend/database/queries/ssh-connection-queries.ts +244 -0
- package/backend/database/queries/ssh-known-host-queries.ts +67 -0
- package/backend/database/queries/ssh-port-forward-queries.ts +113 -0
- package/backend/db-client/connection-manager.ts +8 -1
- package/backend/engine/README.md +16 -0
- package/backend/engine/adapters/claude/stream.ts +89 -49
- package/backend/engine/adapters/cline/stream.ts +62 -24
- package/backend/engine/adapters/codex/message-converter.ts +18 -12
- package/backend/engine/adapters/codex/patch-rollout.test.ts +88 -0
- package/backend/engine/adapters/codex/patch-rollout.ts +120 -109
- package/backend/engine/adapters/codex/stream.ts +78 -22
- package/backend/engine/adapters/copilot/stream.ts +97 -53
- package/backend/engine/adapters/cursor/stream.ts +75 -35
- package/backend/engine/adapters/opencode/server.ts +511 -134
- package/backend/engine/adapters/opencode/stream.ts +131 -79
- package/backend/engine/adapters/pi/stream.ts +63 -24
- package/backend/engine/adapters/qwen/stream.ts +92 -59
- package/backend/engine/adapters/run-registry.test.ts +75 -0
- package/backend/engine/adapters/run-registry.ts +63 -0
- package/backend/engine/config-revision.ts +150 -0
- package/backend/engine/docs/adapter-contract.md +47 -9
- package/backend/engine/docs/adding-an-engine.md +8 -0
- package/backend/engine/docs/frontend-and-chat.md +39 -0
- package/backend/engine/docs/lessons-learned.md +70 -0
- package/backend/engine/docs/quick-reference.md +3 -1
- package/backend/engine/hot-reload.ts +99 -0
- package/backend/engine/index.ts +96 -24
- package/backend/engine/types.ts +17 -5
- package/backend/files/file-watcher.ts +283 -51
- package/backend/files/git-events.test.ts +110 -0
- package/backend/git/git-dirs.test.ts +132 -0
- package/backend/git/git-dirs.ts +0 -0
- package/backend/git/git-executor.ts +22 -3
- package/backend/git/git-service.ts +1 -1
- package/backend/git/nested-repos.test.ts +224 -0
- package/backend/git/nested-repos.ts +386 -0
- package/backend/http/audio.ts +5 -32
- package/backend/http/bearer-auth.ts +47 -0
- package/backend/http/files-download.test.ts +146 -0
- package/backend/http/files-download.ts +109 -0
- package/backend/http/files-upload.ts +3 -29
- package/backend/http/ssh-sftp.ts +133 -0
- package/backend/index.ts +23 -2
- package/backend/mcp/README.md +34 -6
- package/backend/mcp/index.ts +8 -8
- package/backend/mcp/internal/config.ts +69 -19
- package/backend/mcp/internal/project-context.test.ts +40 -0
- package/backend/mcp/internal/project-context.ts +80 -7
- package/backend/mcp/internal/remote-server.test.ts +17 -2
- package/backend/mcp/internal/remote-server.ts +36 -3
- package/backend/mcp/internal/servers/helper.ts +48 -18
- package/backend/preview/browser/browser-video-capture.ts +51 -4
- package/backend/snapshot/gitignore.ts +10 -174
- package/backend/ssh/client-pool.ts +196 -0
- package/backend/ssh/connect.ts +375 -0
- package/backend/ssh/disk-usage.test.ts +134 -0
- package/backend/ssh/disk-usage.ts +214 -0
- package/backend/ssh/forwards.ts +309 -0
- package/backend/ssh/health.ts +141 -0
- package/backend/ssh/known-hosts.ts +96 -0
- package/backend/ssh/pty-backend.ts +215 -0
- package/backend/ssh/ptykit.ts +66 -0
- package/backend/ssh/sftp.test.ts +242 -0
- package/backend/ssh/sftp.ts +967 -0
- package/backend/ssh/socks5.test.ts +167 -0
- package/backend/ssh/socks5.ts +164 -0
- package/backend/ssh/tunnel.ts +87 -0
- package/backend/ws/chat/stream.ts +24 -0
- package/backend/ws/db-client/connections.ts +1 -0
- package/backend/ws/engine/codex/accounts.ts +0 -12
- package/backend/ws/engine/copilot/accounts.ts +0 -10
- package/backend/ws/engine/cursor/accounts.ts +0 -10
- package/backend/ws/engine/index.ts +17 -2
- package/backend/ws/engine/opencode/providers.ts +0 -52
- package/backend/ws/files/read.ts +1 -1
- package/backend/ws/files/watch.ts +5 -1
- package/backend/ws/git/conflict.ts +1 -1
- package/backend/ws/git/diff.ts +1 -1
- package/backend/ws/git/staging.ts +1 -1
- package/backend/ws/git/status.ts +1 -1
- package/backend/ws/index.ts +4 -0
- package/backend/ws/ssh/access.ts +31 -0
- package/backend/ws/ssh/connections.ts +224 -0
- package/backend/ws/ssh/forwards.ts +109 -0
- package/backend/ws/ssh/index.ts +18 -0
- package/backend/ws/ssh/sftp.ts +259 -0
- package/backend/ws/ssh/tunnel.ts +93 -0
- package/dist/assets/{index-DYwQ5bP8.js → index-BF4buYmI.js} +1 -1
- package/dist/assets/{index-DUc5T21e.js → index-BW0DA8NV.js} +1 -1
- package/dist/assets/main-BbwWLJRN.js +1057 -0
- package/dist/assets/main-CCvRMXlP.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/database/schema.ts +49 -0
- package/shared/types/db-client/connection.ts +6 -0
- package/shared/types/ssh/connection.ts +80 -0
- package/shared/types/ssh/forward.ts +46 -0
- package/shared/types/ssh/index.ts +8 -0
- package/shared/types/ssh/known-host.ts +19 -0
- package/shared/types/ssh/sftp.ts +141 -0
- package/shared/utils/logger.ts +1 -0
- package/backend/ws/engine/restart.ts +0 -54
- package/dist/assets/main-VgdjTBLR.css +0 -1
- package/dist/assets/main-r0iv2PDz.js +0 -1044
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import type { Socket } from 'node:net';
|
|
4
|
+
import { readSocksRequest, socksReply, SOCKS_REPLY_SUCCESS } from './socks5';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A stub standing in for the accepted TCP socket. Only the surface the parser
|
|
8
|
+
* touches is implemented: `on`/`removeListener` for inbound data, `write` and
|
|
9
|
+
* `end` for the replies it sends back, and `unshift` for the bytes it hands to
|
|
10
|
+
* the tunnel. Inbound data is delivered by emitting, not by writing, so the
|
|
11
|
+
* captured outbound writes stay separate from what the test feeds in.
|
|
12
|
+
*/
|
|
13
|
+
function fakeSocket(): {
|
|
14
|
+
socket: Socket;
|
|
15
|
+
feed: (bytes: number[]) => void;
|
|
16
|
+
close: () => void;
|
|
17
|
+
written: () => Buffer;
|
|
18
|
+
unshifted: () => Buffer;
|
|
19
|
+
} {
|
|
20
|
+
const emitter = new EventEmitter();
|
|
21
|
+
const writes: Buffer[] = [];
|
|
22
|
+
const unshifts: Buffer[] = [];
|
|
23
|
+
|
|
24
|
+
const stub = emitter as unknown as Socket & {
|
|
25
|
+
unshift: (chunk: Buffer) => void;
|
|
26
|
+
// `writable` is read-only on the real Socket; widened here so the stub can
|
|
27
|
+
// report itself as writable, which the parser checks before replying.
|
|
28
|
+
writable: boolean;
|
|
29
|
+
};
|
|
30
|
+
stub.writable = true;
|
|
31
|
+
stub.write = ((chunk: Buffer) => {
|
|
32
|
+
writes.push(Buffer.from(chunk));
|
|
33
|
+
return true;
|
|
34
|
+
}) as Socket['write'];
|
|
35
|
+
stub.end = ((chunk?: Buffer) => {
|
|
36
|
+
if (chunk) writes.push(Buffer.from(chunk));
|
|
37
|
+
return stub;
|
|
38
|
+
}) as Socket['end'];
|
|
39
|
+
stub.unshift = (chunk: Buffer): void => {
|
|
40
|
+
unshifts.push(Buffer.from(chunk));
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
socket: stub,
|
|
45
|
+
feed: (bytes) => emitter.emit('data', Buffer.from(bytes)),
|
|
46
|
+
close: () => emitter.emit('close'),
|
|
47
|
+
written: () => Buffer.concat(writes),
|
|
48
|
+
unshifted: () => Buffer.concat(unshifts)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const NO_AUTH_GREETING = [0x05, 0x01, 0x00];
|
|
53
|
+
|
|
54
|
+
describe('readSocksRequest', () => {
|
|
55
|
+
test('parses a CONNECT to a domain name', async () => {
|
|
56
|
+
const { socket, feed, written } = fakeSocket();
|
|
57
|
+
const pending = readSocksRequest(socket);
|
|
58
|
+
|
|
59
|
+
feed(NO_AUTH_GREETING);
|
|
60
|
+
feed([0x05, 0x01, 0x00, 0x03, 0x0b, ...Buffer.from('example.com'), 0x01, 0xbb]);
|
|
61
|
+
|
|
62
|
+
await expect(pending).resolves.toEqual({ host: 'example.com', port: 443 });
|
|
63
|
+
// The greeting is answered with "version 5, no authentication".
|
|
64
|
+
expect([...written()]).toEqual([0x05, 0x00]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('parses a CONNECT to an IPv4 address', async () => {
|
|
68
|
+
const { socket, feed } = fakeSocket();
|
|
69
|
+
const pending = readSocksRequest(socket);
|
|
70
|
+
|
|
71
|
+
feed(NO_AUTH_GREETING);
|
|
72
|
+
feed([0x05, 0x01, 0x00, 0x01, 10, 0, 0, 7, 0x22, 0xb8]);
|
|
73
|
+
|
|
74
|
+
await expect(pending).resolves.toEqual({ host: '10.0.0.7', port: 8888 });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('parses a CONNECT to an IPv6 address', async () => {
|
|
78
|
+
const { socket, feed } = fakeSocket();
|
|
79
|
+
const pending = readSocksRequest(socket);
|
|
80
|
+
|
|
81
|
+
feed(NO_AUTH_GREETING);
|
|
82
|
+
feed([
|
|
83
|
+
0x05, 0x01, 0x00, 0x04,
|
|
84
|
+
0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01,
|
|
85
|
+
0x00, 0x50
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
await expect(pending).resolves.toEqual({ host: '2001:db8:0:0:0:0:0:1', port: 80 });
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test('reassembles a request split across packets', async () => {
|
|
92
|
+
const { socket, feed } = fakeSocket();
|
|
93
|
+
const pending = readSocksRequest(socket);
|
|
94
|
+
|
|
95
|
+
feed([0x05]);
|
|
96
|
+
feed([0x01, 0x00]);
|
|
97
|
+
feed([0x05, 0x01, 0x00, 0x03, 0x03]);
|
|
98
|
+
feed([...Buffer.from('abc')]);
|
|
99
|
+
feed([0x00, 0x16]);
|
|
100
|
+
|
|
101
|
+
await expect(pending).resolves.toEqual({ host: 'abc', port: 22 });
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('hands bytes sent after the request back to the socket', async () => {
|
|
105
|
+
const { socket, feed, unshifted } = fakeSocket();
|
|
106
|
+
const pending = readSocksRequest(socket);
|
|
107
|
+
|
|
108
|
+
feed(NO_AUTH_GREETING);
|
|
109
|
+
feed([0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50, 0x47, 0x45, 0x54]);
|
|
110
|
+
|
|
111
|
+
await pending;
|
|
112
|
+
expect(unshifted().toString('utf8')).toBe('GET');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('rejects a command other than CONNECT', async () => {
|
|
116
|
+
const { socket, feed, written } = fakeSocket();
|
|
117
|
+
const pending = readSocksRequest(socket);
|
|
118
|
+
|
|
119
|
+
feed(NO_AUTH_GREETING);
|
|
120
|
+
// 0x02 is BIND, which `ssh -D` does not implement either.
|
|
121
|
+
feed([0x05, 0x02, 0x00, 0x01, 127, 0, 0, 1, 0x00, 0x50]);
|
|
122
|
+
|
|
123
|
+
await expect(pending).rejects.toThrow('CONNECT');
|
|
124
|
+
// The greeting reply, then a failure frame with 0x07 = command not supported.
|
|
125
|
+
expect([...written()]).toEqual([0x05, 0x00, 0x05, 0x07, 0x00, 0x01, 0, 0, 0, 0, 0, 0]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test('rejects a client that will not do no-auth', async () => {
|
|
129
|
+
const { socket, feed, written } = fakeSocket();
|
|
130
|
+
const pending = readSocksRequest(socket);
|
|
131
|
+
|
|
132
|
+
// Offers only username/password (0x02).
|
|
133
|
+
feed([0x05, 0x01, 0x02]);
|
|
134
|
+
|
|
135
|
+
await expect(pending).rejects.toThrow('authentication');
|
|
136
|
+
// 0xff = no acceptable methods, sent as the closing frame.
|
|
137
|
+
expect([...written()]).toEqual([0x05, 0xff]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('rejects a non-SOCKS5 client', async () => {
|
|
141
|
+
const { socket, feed } = fakeSocket();
|
|
142
|
+
const pending = readSocksRequest(socket);
|
|
143
|
+
|
|
144
|
+
// SOCKS4 announces version 0x04.
|
|
145
|
+
feed([0x04, 0x01]);
|
|
146
|
+
|
|
147
|
+
await expect(pending).rejects.toThrow('SOCKS5');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test('rejects a client that disconnects mid-handshake', async () => {
|
|
151
|
+
const { socket, feed, close } = fakeSocket();
|
|
152
|
+
const pending = readSocksRequest(socket);
|
|
153
|
+
|
|
154
|
+
feed(NO_AUTH_GREETING);
|
|
155
|
+
close();
|
|
156
|
+
|
|
157
|
+
await expect(pending).rejects.toThrow('disconnected');
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('socksReply', () => {
|
|
162
|
+
test('reports the bound address as 0.0.0.0:0', () => {
|
|
163
|
+
expect([...socksReply(SOCKS_REPLY_SUCCESS)]).toEqual([
|
|
164
|
+
0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0
|
|
165
|
+
]);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ssh-client — the SOCKS5 half of dynamic port forwarding.
|
|
3
|
+
*
|
|
4
|
+
* `ssh -D` is a SOCKS5 proxy whose CONNECT requests are opened as channels on
|
|
5
|
+
* the SSH transport. This module speaks just enough of RFC 1928 to serve that:
|
|
6
|
+
* the no-auth handshake and the CONNECT command. BIND and UDP ASSOCIATE are
|
|
7
|
+
* rejected — OpenSSH does not implement them for `-D` either.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Socket } from 'node:net';
|
|
11
|
+
|
|
12
|
+
const SOCKS_VERSION = 0x05;
|
|
13
|
+
const NO_AUTHENTICATION = 0x00;
|
|
14
|
+
const NO_ACCEPTABLE_METHODS = 0xff;
|
|
15
|
+
|
|
16
|
+
const COMMAND_CONNECT = 0x01;
|
|
17
|
+
|
|
18
|
+
const ADDRESS_IPV4 = 0x01;
|
|
19
|
+
const ADDRESS_DOMAIN = 0x03;
|
|
20
|
+
const ADDRESS_IPV6 = 0x04;
|
|
21
|
+
|
|
22
|
+
export const SOCKS_REPLY_SUCCESS = 0x00;
|
|
23
|
+
export const SOCKS_REPLY_GENERAL_FAILURE = 0x01;
|
|
24
|
+
export const SOCKS_REPLY_HOST_UNREACHABLE = 0x04;
|
|
25
|
+
export const SOCKS_REPLY_COMMAND_NOT_SUPPORTED = 0x07;
|
|
26
|
+
|
|
27
|
+
export interface SocksRequest {
|
|
28
|
+
host: string;
|
|
29
|
+
port: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The reply frame. The bound address is reported as 0.0.0.0:0 — the client is
|
|
34
|
+
* about to be piped into an SSH channel that has no local address of its own,
|
|
35
|
+
* and every SOCKS client tolerates that.
|
|
36
|
+
*/
|
|
37
|
+
export function socksReply(code: number): Buffer {
|
|
38
|
+
return Buffer.from([SOCKS_VERSION, code, 0x00, ADDRESS_IPV4, 0, 0, 0, 0, 0, 0]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Read the greeting and the CONNECT request from `socket`, answering the
|
|
43
|
+
* greeting along the way. Resolves with where the client wants to go; rejects
|
|
44
|
+
* (after sending the matching failure reply) when the exchange is not a
|
|
45
|
+
* no-auth CONNECT.
|
|
46
|
+
*
|
|
47
|
+
* Data arriving after the request is left unread on the socket, so the caller
|
|
48
|
+
* can pipe it straight into the SSH channel without losing a byte.
|
|
49
|
+
*/
|
|
50
|
+
export function readSocksRequest(socket: Socket): Promise<SocksRequest> {
|
|
51
|
+
return new Promise((resolvePromise, rejectPromise) => {
|
|
52
|
+
let buffered = Buffer.alloc(0);
|
|
53
|
+
let greeted = false;
|
|
54
|
+
let settled = false;
|
|
55
|
+
|
|
56
|
+
const cleanup = (): void => {
|
|
57
|
+
socket.removeListener('data', onData);
|
|
58
|
+
socket.removeListener('error', onError);
|
|
59
|
+
socket.removeListener('close', onClose);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const fail = (message: string, replyCode: number): void => {
|
|
63
|
+
if (settled) return;
|
|
64
|
+
settled = true;
|
|
65
|
+
cleanup();
|
|
66
|
+
if (replyCode !== SOCKS_REPLY_SUCCESS && socket.writable) {
|
|
67
|
+
socket.write(socksReply(replyCode));
|
|
68
|
+
}
|
|
69
|
+
rejectPromise(new Error(message));
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const succeed = (request: SocksRequest, consumed: number): void => {
|
|
73
|
+
if (settled) return;
|
|
74
|
+
settled = true;
|
|
75
|
+
cleanup();
|
|
76
|
+
// Anything the client sent past the request belongs to the tunnel.
|
|
77
|
+
const leftover = buffered.subarray(consumed);
|
|
78
|
+
if (leftover.length > 0) socket.unshift(leftover);
|
|
79
|
+
resolvePromise(request);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
function onData(chunk: Buffer): void {
|
|
83
|
+
buffered = Buffer.concat([buffered, chunk]);
|
|
84
|
+
|
|
85
|
+
if (!greeted) {
|
|
86
|
+
if (buffered.length < 2) return;
|
|
87
|
+
if (buffered[0] !== SOCKS_VERSION) {
|
|
88
|
+
fail('Not a SOCKS5 client', SOCKS_REPLY_GENERAL_FAILURE);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const methodCount = buffered[1];
|
|
92
|
+
const greetingLength = 2 + methodCount;
|
|
93
|
+
if (buffered.length < greetingLength) return;
|
|
94
|
+
|
|
95
|
+
const methods = buffered.subarray(2, greetingLength);
|
|
96
|
+
if (!methods.includes(NO_AUTHENTICATION)) {
|
|
97
|
+
settled = true;
|
|
98
|
+
cleanup();
|
|
99
|
+
socket.end(Buffer.from([SOCKS_VERSION, NO_ACCEPTABLE_METHODS]));
|
|
100
|
+
rejectPromise(new Error('SOCKS client requires authentication'));
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
socket.write(Buffer.from([SOCKS_VERSION, NO_AUTHENTICATION]));
|
|
105
|
+
buffered = buffered.subarray(greetingLength);
|
|
106
|
+
greeted = true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (buffered.length < 4) return;
|
|
110
|
+
if (buffered[0] !== SOCKS_VERSION) {
|
|
111
|
+
fail('Malformed SOCKS5 request', SOCKS_REPLY_GENERAL_FAILURE);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (buffered[1] !== COMMAND_CONNECT) {
|
|
115
|
+
fail('Only the CONNECT command is supported', SOCKS_REPLY_COMMAND_NOT_SUPPORTED);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const addressType = buffered[3];
|
|
120
|
+
let host: string;
|
|
121
|
+
let cursor: number;
|
|
122
|
+
|
|
123
|
+
if (addressType === ADDRESS_IPV4) {
|
|
124
|
+
if (buffered.length < 10) return;
|
|
125
|
+
host = Array.from(buffered.subarray(4, 8)).join('.');
|
|
126
|
+
cursor = 8;
|
|
127
|
+
} else if (addressType === ADDRESS_DOMAIN) {
|
|
128
|
+
const nameLength = buffered[4];
|
|
129
|
+
if (buffered.length < 5 + nameLength + 2) return;
|
|
130
|
+
host = buffered.subarray(5, 5 + nameLength).toString('utf8');
|
|
131
|
+
cursor = 5 + nameLength;
|
|
132
|
+
} else if (addressType === ADDRESS_IPV6) {
|
|
133
|
+
if (buffered.length < 22) return;
|
|
134
|
+
const groups: string[] = [];
|
|
135
|
+
for (let offset = 4; offset < 20; offset += 2) {
|
|
136
|
+
groups.push(buffered.readUInt16BE(offset).toString(16));
|
|
137
|
+
}
|
|
138
|
+
host = groups.join(':');
|
|
139
|
+
cursor = 20;
|
|
140
|
+
} else {
|
|
141
|
+
fail('Unsupported SOCKS5 address type', SOCKS_REPLY_HOST_UNREACHABLE);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const port = buffered.readUInt16BE(cursor);
|
|
146
|
+
succeed({ host, port }, cursor + 2);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function onError(error: Error): void {
|
|
150
|
+
if (settled) return;
|
|
151
|
+
settled = true;
|
|
152
|
+
cleanup();
|
|
153
|
+
rejectPromise(error);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function onClose(): void {
|
|
157
|
+
fail('SOCKS client disconnected during the handshake', SOCKS_REPLY_SUCCESS);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
socket.on('data', onData);
|
|
161
|
+
socket.on('error', onError);
|
|
162
|
+
socket.on('close', onClose);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ssh-client — local port forwards opened on behalf of other features.
|
|
3
|
+
*
|
|
4
|
+
* The database client reaches a database behind a bastion through one of these.
|
|
5
|
+
* Pointing it at a saved SSH connection (rather than at credentials copied into
|
|
6
|
+
* the database connection) means the host, its jump chain and its trusted host
|
|
7
|
+
* key are configured once and shared.
|
|
8
|
+
*
|
|
9
|
+
* The tunnel holds a pool lease for its whole life, so the transport it rides
|
|
10
|
+
* cannot be swept out from under an open database connection.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import net from 'node:net';
|
|
14
|
+
import { sshClientPool, type SshLease } from './client-pool';
|
|
15
|
+
import { debug } from '$shared/utils/logger';
|
|
16
|
+
|
|
17
|
+
export interface SshManagedTunnel {
|
|
18
|
+
localPort: number;
|
|
19
|
+
close(): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Open `127.0.0.1:<ephemeral>` forwarding to `remoteHost:remotePort` through
|
|
24
|
+
* the saved SSH connection `connectionId`.
|
|
25
|
+
*/
|
|
26
|
+
export async function openTunnelViaSavedConnection(
|
|
27
|
+
connectionId: string,
|
|
28
|
+
remoteHost: string,
|
|
29
|
+
remotePort: number
|
|
30
|
+
): Promise<SshManagedTunnel> {
|
|
31
|
+
const lease: SshLease = await sshClientPool.acquire(connectionId);
|
|
32
|
+
|
|
33
|
+
const sockets = new Set<net.Socket>();
|
|
34
|
+
|
|
35
|
+
const server = net.createServer((socket) => {
|
|
36
|
+
sockets.add(socket);
|
|
37
|
+
socket.on('close', () => sockets.delete(socket));
|
|
38
|
+
|
|
39
|
+
lease.client.forwardOut(
|
|
40
|
+
socket.remoteAddress ?? '127.0.0.1',
|
|
41
|
+
socket.remotePort ?? 0,
|
|
42
|
+
remoteHost,
|
|
43
|
+
remotePort,
|
|
44
|
+
(error, stream) => {
|
|
45
|
+
if (error) {
|
|
46
|
+
debug.warn('ssh', `tunnel forwardOut failed: ${error.message}`);
|
|
47
|
+
socket.destroy();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
socket.pipe(stream).pipe(socket);
|
|
51
|
+
stream.on('error', () => socket.destroy());
|
|
52
|
+
socket.on('error', () => stream.destroy());
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
let localPort: number;
|
|
58
|
+
try {
|
|
59
|
+
localPort = await new Promise<number>((resolvePromise, rejectPromise) => {
|
|
60
|
+
server.once('error', rejectPromise);
|
|
61
|
+
server.listen(0, '127.0.0.1', () => {
|
|
62
|
+
server.removeListener('error', rejectPromise);
|
|
63
|
+
const address = server.address();
|
|
64
|
+
if (address && typeof address === 'object') {
|
|
65
|
+
resolvePromise(address.port);
|
|
66
|
+
} else {
|
|
67
|
+
rejectPromise(new Error('Failed to obtain a local port for the SSH tunnel'));
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
} catch (error) {
|
|
72
|
+
lease.release();
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
debug.log('ssh', `tunnel up: 127.0.0.1:${localPort} → ${remoteHost}:${remotePort} via saved host`);
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
localPort,
|
|
80
|
+
async close() {
|
|
81
|
+
for (const socket of sockets) socket.destroy();
|
|
82
|
+
sockets.clear();
|
|
83
|
+
await new Promise<void>((resolvePromise) => server.close(() => resolvePromise()));
|
|
84
|
+
lease.release();
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -253,6 +253,7 @@ export const streamHandler = createRouter()
|
|
|
253
253
|
const stream = streamManager.getStream(streamId);
|
|
254
254
|
if (stream) {
|
|
255
255
|
ws.emit.chatSession(data.chatSessionId, 'chat:connection', {
|
|
256
|
+
chatSessionId: data.chatSessionId,
|
|
256
257
|
processId: stream.processId,
|
|
257
258
|
timestamp: stream.startedAt.toISOString(),
|
|
258
259
|
seq: 1
|
|
@@ -273,6 +274,7 @@ export const streamHandler = createRouter()
|
|
|
273
274
|
switch (event.type) {
|
|
274
275
|
case 'connection':
|
|
275
276
|
ws.emit.chatSession(chatSessionId, 'chat:connection', {
|
|
277
|
+
chatSessionId,
|
|
276
278
|
processId: event.data.processId,
|
|
277
279
|
timestamp: event.data.timestamp,
|
|
278
280
|
seq: event.seq
|
|
@@ -284,6 +286,7 @@ export const streamHandler = createRouter()
|
|
|
284
286
|
|
|
285
287
|
case 'message': {
|
|
286
288
|
ws.emit.chatSession(chatSessionId, 'chat:message', {
|
|
289
|
+
chatSessionId,
|
|
287
290
|
processId: event.processId,
|
|
288
291
|
message: trimSubAgentForWire(event.data.message),
|
|
289
292
|
usage: event.data.usage,
|
|
@@ -302,6 +305,7 @@ export const streamHandler = createRouter()
|
|
|
302
305
|
|
|
303
306
|
case 'partial':
|
|
304
307
|
ws.emit.chatSession(chatSessionId, 'chat:partial', {
|
|
308
|
+
chatSessionId,
|
|
305
309
|
processId: event.processId,
|
|
306
310
|
eventType: event.data.eventType as any,
|
|
307
311
|
partialText: event.data.partialText || '',
|
|
@@ -336,6 +340,7 @@ export const streamHandler = createRouter()
|
|
|
336
340
|
|
|
337
341
|
case 'complete':
|
|
338
342
|
ws.emit.chatSession(chatSessionId, 'chat:complete', {
|
|
343
|
+
chatSessionId,
|
|
339
344
|
processId: event.processId,
|
|
340
345
|
timestamp: event.data.timestamp,
|
|
341
346
|
seq: event.seq
|
|
@@ -347,6 +352,7 @@ export const streamHandler = createRouter()
|
|
|
347
352
|
|
|
348
353
|
case 'error':
|
|
349
354
|
ws.emit.chatSession(chatSessionId, 'chat:error', {
|
|
355
|
+
chatSessionId,
|
|
350
356
|
processId: event.processId,
|
|
351
357
|
error: event.data.error,
|
|
352
358
|
timestamp: event.data.timestamp,
|
|
@@ -358,6 +364,7 @@ export const streamHandler = createRouter()
|
|
|
358
364
|
|
|
359
365
|
case 'cancelled':
|
|
360
366
|
ws.emit.chatSession(chatSessionId, 'chat:error', {
|
|
367
|
+
chatSessionId,
|
|
361
368
|
processId: event.processId,
|
|
362
369
|
error: 'Stream cancelled',
|
|
363
370
|
timestamp: event.data.timestamp,
|
|
@@ -387,6 +394,7 @@ export const streamHandler = createRouter()
|
|
|
387
394
|
debug.error('chat', 'WS chat:stream error:', errorMessage);
|
|
388
395
|
|
|
389
396
|
ws.emit.chatSession(data.chatSessionId, 'chat:error', {
|
|
397
|
+
chatSessionId: data.chatSessionId,
|
|
390
398
|
processId: crypto.randomUUID(),
|
|
391
399
|
error: errorMessage,
|
|
392
400
|
timestamp: new Date().toISOString()
|
|
@@ -420,6 +428,7 @@ export const streamHandler = createRouter()
|
|
|
420
428
|
switch (event.type) {
|
|
421
429
|
case 'connection':
|
|
422
430
|
ws.emit.chatSession(chatSessionId, 'chat:connection', {
|
|
431
|
+
chatSessionId,
|
|
423
432
|
processId: event.data.processId,
|
|
424
433
|
timestamp: event.data.timestamp,
|
|
425
434
|
seq: event.seq
|
|
@@ -428,6 +437,7 @@ export const streamHandler = createRouter()
|
|
|
428
437
|
|
|
429
438
|
case 'message': {
|
|
430
439
|
ws.emit.chatSession(chatSessionId, 'chat:message', {
|
|
440
|
+
chatSessionId,
|
|
431
441
|
processId: event.processId,
|
|
432
442
|
message: trimSubAgentForWire(event.data.message),
|
|
433
443
|
usage: event.data.usage,
|
|
@@ -446,6 +456,7 @@ export const streamHandler = createRouter()
|
|
|
446
456
|
|
|
447
457
|
case 'partial':
|
|
448
458
|
ws.emit.chatSession(chatSessionId, 'chat:partial', {
|
|
459
|
+
chatSessionId,
|
|
449
460
|
processId: event.processId,
|
|
450
461
|
eventType: event.data.eventType as any,
|
|
451
462
|
partialText: event.data.partialText || '',
|
|
@@ -480,6 +491,7 @@ export const streamHandler = createRouter()
|
|
|
480
491
|
|
|
481
492
|
case 'complete':
|
|
482
493
|
ws.emit.chatSession(chatSessionId, 'chat:complete', {
|
|
494
|
+
chatSessionId,
|
|
483
495
|
processId: event.processId,
|
|
484
496
|
timestamp: event.data.timestamp,
|
|
485
497
|
seq: event.seq
|
|
@@ -490,6 +502,7 @@ export const streamHandler = createRouter()
|
|
|
490
502
|
|
|
491
503
|
case 'error':
|
|
492
504
|
ws.emit.chatSession(chatSessionId, 'chat:error', {
|
|
505
|
+
chatSessionId,
|
|
493
506
|
processId: event.processId,
|
|
494
507
|
error: event.data.error,
|
|
495
508
|
timestamp: event.data.timestamp,
|
|
@@ -501,6 +514,7 @@ export const streamHandler = createRouter()
|
|
|
501
514
|
|
|
502
515
|
case 'cancelled':
|
|
503
516
|
ws.emit.chatSession(chatSessionId, 'chat:error', {
|
|
517
|
+
chatSessionId,
|
|
504
518
|
processId: event.processId,
|
|
505
519
|
error: 'Stream cancelled',
|
|
506
520
|
timestamp: event.data.timestamp,
|
|
@@ -523,6 +537,7 @@ export const streamHandler = createRouter()
|
|
|
523
537
|
|
|
524
538
|
// Send current state snapshot to chat session room so frontend can catch up
|
|
525
539
|
ws.emit.chatSession(chatSessionId, 'chat:connection', {
|
|
540
|
+
chatSessionId,
|
|
526
541
|
processId: streamState.processId,
|
|
527
542
|
timestamp: streamState.startedAt.toISOString(),
|
|
528
543
|
seq: streamState.eventSeq
|
|
@@ -657,6 +672,7 @@ export const streamHandler = createRouter()
|
|
|
657
672
|
// Stream not found - could already be completed/cleaned up
|
|
658
673
|
// Send cancellation to chat session room so frontend stops loading
|
|
659
674
|
ws.emit.chatSession(chatSessionId, 'chat:cancelled', {
|
|
675
|
+
chatSessionId,
|
|
660
676
|
status: 'cancelled',
|
|
661
677
|
processId: ''
|
|
662
678
|
});
|
|
@@ -669,6 +685,7 @@ export const streamHandler = createRouter()
|
|
|
669
685
|
await streamManager.cancelStream(streamState.streamId);
|
|
670
686
|
// Always send cancelled to chat session room to clear UI
|
|
671
687
|
ws.emit.chatSession(chatSessionId, 'chat:cancelled', {
|
|
688
|
+
chatSessionId,
|
|
672
689
|
status: 'cancelled',
|
|
673
690
|
processId: streamState.processId
|
|
674
691
|
});
|
|
@@ -679,6 +696,7 @@ export const streamHandler = createRouter()
|
|
|
679
696
|
} catch (error) {
|
|
680
697
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
681
698
|
ws.emit.chatSession(chatSessionId, 'chat:error', {
|
|
699
|
+
chatSessionId,
|
|
682
700
|
processId: '',
|
|
683
701
|
error: errorMessage,
|
|
684
702
|
timestamp: new Date().toISOString()
|
|
@@ -966,12 +984,14 @@ export const streamHandler = createRouter()
|
|
|
966
984
|
}))
|
|
967
985
|
|
|
968
986
|
.emit('chat:connection', t.Object({
|
|
987
|
+
chatSessionId: t.String(),
|
|
969
988
|
processId: t.String(),
|
|
970
989
|
timestamp: t.String(),
|
|
971
990
|
seq: t.Optional(t.Number())
|
|
972
991
|
}))
|
|
973
992
|
|
|
974
993
|
.emit('chat:message', t.Object({
|
|
994
|
+
chatSessionId: t.String(),
|
|
975
995
|
processId: t.String(),
|
|
976
996
|
message: t.Any(), // SDKMessage
|
|
977
997
|
usage: t.Optional(t.Any()),
|
|
@@ -985,6 +1005,7 @@ export const streamHandler = createRouter()
|
|
|
985
1005
|
}))
|
|
986
1006
|
|
|
987
1007
|
.emit('chat:partial', t.Object({
|
|
1008
|
+
chatSessionId: t.String(),
|
|
988
1009
|
processId: t.String(),
|
|
989
1010
|
eventType: t.Union([
|
|
990
1011
|
t.Literal('start'),
|
|
@@ -1030,12 +1051,14 @@ export const streamHandler = createRouter()
|
|
|
1030
1051
|
}))
|
|
1031
1052
|
|
|
1032
1053
|
.emit('chat:complete', t.Object({
|
|
1054
|
+
chatSessionId: t.String(),
|
|
1033
1055
|
processId: t.String(),
|
|
1034
1056
|
timestamp: t.String(),
|
|
1035
1057
|
seq: t.Optional(t.Number())
|
|
1036
1058
|
}))
|
|
1037
1059
|
|
|
1038
1060
|
.emit('chat:error', t.Object({
|
|
1061
|
+
chatSessionId: t.String(),
|
|
1039
1062
|
processId: t.String(),
|
|
1040
1063
|
error: t.String(),
|
|
1041
1064
|
timestamp: t.String(),
|
|
@@ -1043,6 +1066,7 @@ export const streamHandler = createRouter()
|
|
|
1043
1066
|
}))
|
|
1044
1067
|
|
|
1045
1068
|
.emit('chat:cancelled', t.Object({
|
|
1069
|
+
chatSessionId: t.String(),
|
|
1046
1070
|
status: t.Literal('cancelled'),
|
|
1047
1071
|
processId: t.Optional(t.String()),
|
|
1048
1072
|
seq: t.Optional(t.Number())
|
|
@@ -36,6 +36,7 @@ const sshAuthSchema = t.Union([t.Literal('password'), t.Literal('key')]);
|
|
|
36
36
|
|
|
37
37
|
const sshInputSchema = t.Object({
|
|
38
38
|
enabled: t.Optional(t.Boolean()),
|
|
39
|
+
connectionId: t.Optional(t.Nullable(t.String())),
|
|
39
40
|
host: t.Optional(t.String()),
|
|
40
41
|
port: t.Optional(t.Number()),
|
|
41
42
|
username: t.Optional(t.String()),
|
|
@@ -504,18 +504,6 @@ export const codexAccountsHandler = createRouter()
|
|
|
504
504
|
cleanupSetup(data.setupId);
|
|
505
505
|
})
|
|
506
506
|
|
|
507
|
-
// Restart all Codex engine instances. Use after changing the active
|
|
508
|
-
// account so subsequent models:list / chat calls re-initialise with the
|
|
509
|
-
// right credential. (The auth-blob swap on accounts-switch already
|
|
510
|
-
// handles the on-disk file; this just drops cached engine instances.)
|
|
511
|
-
.http('engine:codex-restart', {
|
|
512
|
-
data: t.Object({}),
|
|
513
|
-
response: t.Object({ success: t.Boolean() })
|
|
514
|
-
}, async () => {
|
|
515
|
-
await disposeCodexEngines();
|
|
516
|
-
return { success: true };
|
|
517
|
-
})
|
|
518
|
-
|
|
519
507
|
// ─── Server → client events ───
|
|
520
508
|
|
|
521
509
|
.emit('engine:codex-account-setup-url', t.Object({
|
|
@@ -121,14 +121,4 @@ export const copilotAccountsHandler = createRouter()
|
|
|
121
121
|
const active = engineQueries.getActiveAccountForEngine('copilot');
|
|
122
122
|
if (active?.id === data.id) await disposeCopilotEngines();
|
|
123
123
|
return { success: true };
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
// Restart all Copilot engine instances. Use after changing the active token
|
|
127
|
-
// so subsequent models:list / chat calls re-initialise with fresh credentials.
|
|
128
|
-
.http('engine:copilot-restart', {
|
|
129
|
-
data: t.Object({}),
|
|
130
|
-
response: t.Object({ success: t.Boolean() })
|
|
131
|
-
}, async () => {
|
|
132
|
-
await disposeCopilotEngines();
|
|
133
|
-
return { success: true };
|
|
134
124
|
});
|
|
@@ -150,14 +150,4 @@ export const cursorAccountsHandler = createRouter()
|
|
|
150
150
|
const active = engineQueries.getActiveAccountForEngine('cursor');
|
|
151
151
|
if (active?.id === data.id) await disposeCursorEngines();
|
|
152
152
|
return { success: true };
|
|
153
|
-
})
|
|
154
|
-
|
|
155
|
-
// Restart all Cursor engine instances so subsequent models:list / chat calls
|
|
156
|
-
// re-initialise with fresh credentials.
|
|
157
|
-
.http('engine:cursor-restart', {
|
|
158
|
-
data: t.Object({}),
|
|
159
|
-
response: t.Object({ success: t.Boolean() })
|
|
160
|
-
}, async () => {
|
|
161
|
-
await disposeCursorEngines();
|
|
162
|
-
return { success: true };
|
|
163
153
|
});
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* Merges Claude Code and Open Code engine routers.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
+
import { t } from 'elysia';
|
|
8
9
|
import { createRouter } from '$shared/utils/ws-server';
|
|
9
10
|
import { claudeCodeEngineRouter } from './claude';
|
|
10
11
|
import { openCodeEngineRouter } from './opencode';
|
|
@@ -14,7 +15,6 @@ import { qwenEngineRouter } from './qwen';
|
|
|
14
15
|
import { piEngineRouter } from './pi';
|
|
15
16
|
import { clineEngineRouter } from './cline';
|
|
16
17
|
import { cursorEngineRouter } from './cursor';
|
|
17
|
-
import { engineRestartRouter } from './restart';
|
|
18
18
|
|
|
19
19
|
export const engineRouter = createRouter()
|
|
20
20
|
.merge(claudeCodeEngineRouter)
|
|
@@ -25,4 +25,19 @@ export const engineRouter = createRouter()
|
|
|
25
25
|
.merge(piEngineRouter)
|
|
26
26
|
.merge(clineEngineRouter)
|
|
27
27
|
.merge(cursorEngineRouter)
|
|
28
|
-
|
|
28
|
+
|
|
29
|
+
// ─── Server → client events ───
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Engine-affecting config changed and has been applied.
|
|
33
|
+
*
|
|
34
|
+
* A doorbell, not a delivery: it carries the revision and nothing else, and
|
|
35
|
+
* clients refetch over the routes they already use. This is the one piece of
|
|
36
|
+
* the removed "Restart Server" button that had to survive — the button's
|
|
37
|
+
* success path also refreshed the model list, and without an announcement a
|
|
38
|
+
* newly added provider would sit invisible in the picker until something else
|
|
39
|
+
* happened to trigger a refetch.
|
|
40
|
+
*/
|
|
41
|
+
.emit('engine:config-changed', t.Object({
|
|
42
|
+
revision: t.Number()
|
|
43
|
+
}));
|