@myrialabs/clopen 0.4.32 → 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/chat/stream-manager.ts +4 -1
- package/backend/database/migrations/069_create_ssh_client_tables.ts +116 -0
- package/backend/database/migrations/index.ts +7 -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/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 +33 -4
- package/backend/engine/adapters/copilot/stream.ts +1 -1
- package/backend/engine/adapters/cursor/stream.ts +1 -1
- package/backend/engine/adapters/qwen/stream.ts +1 -1
- package/backend/engine/docs/adapter-contract.md +7 -0
- 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/nested-repos.test.ts +108 -5
- package/backend/git/nested-repos.ts +103 -18
- 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 +16 -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/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/db-client/connections.ts +1 -0
- package/backend/ws/files/watch.ts +5 -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-DSvGoxp2.js → index-BF4buYmI.js} +1 -1
- package/dist/assets/{index-hu17G6tX.js → index-BW0DA8NV.js} +1 -1
- package/dist/assets/{main-BccclZsC.js → main-BbwWLJRN.js} +499 -473
- 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/dist/assets/main-Ciuw0wF0.css +0 -1
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ssh-client — connection CRUD, health and host-key trust WS handlers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { t } from 'elysia';
|
|
6
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
7
|
+
import { initializeDatabase } from '../../database';
|
|
8
|
+
import { sshConnectionQueries, sshKnownHostQueries } from '../../database/queries';
|
|
9
|
+
import { sshClientPool } from '../../ssh/client-pool';
|
|
10
|
+
import { sshForwardManager } from '../../ssh/forwards';
|
|
11
|
+
import { sshHealthService } from '../../ssh/health';
|
|
12
|
+
import { knownHosts } from '../../ssh/known-hosts';
|
|
13
|
+
import { killSessionsForConnection } from '../../ssh/ptykit';
|
|
14
|
+
import type { SshConnectionInput } from '$shared/types/ssh';
|
|
15
|
+
import { debug } from '$shared/utils/logger';
|
|
16
|
+
import { getSshPrincipal, requireSshConnection } from './access';
|
|
17
|
+
|
|
18
|
+
const authMethodSchema = t.Union([
|
|
19
|
+
t.Literal('password'),
|
|
20
|
+
t.Literal('key'),
|
|
21
|
+
t.Literal('key-file'),
|
|
22
|
+
t.Literal('agent')
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
const connectionInputSchema = t.Object({
|
|
26
|
+
name: t.String({ minLength: 1 }),
|
|
27
|
+
host: t.String({ minLength: 1 }),
|
|
28
|
+
port: t.Optional(t.Number()),
|
|
29
|
+
username: t.String({ minLength: 1 }),
|
|
30
|
+
authMethod: t.Optional(authMethodSchema),
|
|
31
|
+
password: t.Optional(t.String()),
|
|
32
|
+
privateKey: t.Optional(t.String()),
|
|
33
|
+
privateKeyPath: t.Optional(t.String()),
|
|
34
|
+
passphrase: t.Optional(t.String()),
|
|
35
|
+
agentSocket: t.Optional(t.String()),
|
|
36
|
+
jumpConnectionId: t.Optional(t.Nullable(t.String())),
|
|
37
|
+
initialPath: t.Optional(t.String()),
|
|
38
|
+
keepaliveSeconds: t.Optional(t.Number()),
|
|
39
|
+
strictHostKey: t.Optional(t.Boolean()),
|
|
40
|
+
color: t.Optional(t.String())
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const connectionPatchSchema = t.Object({
|
|
44
|
+
name: t.Optional(t.String({ minLength: 1 })),
|
|
45
|
+
host: t.Optional(t.String({ minLength: 1 })),
|
|
46
|
+
port: t.Optional(t.Number()),
|
|
47
|
+
username: t.Optional(t.String({ minLength: 1 })),
|
|
48
|
+
authMethod: t.Optional(authMethodSchema),
|
|
49
|
+
password: t.Optional(t.String()),
|
|
50
|
+
privateKey: t.Optional(t.String()),
|
|
51
|
+
privateKeyPath: t.Optional(t.String()),
|
|
52
|
+
passphrase: t.Optional(t.String()),
|
|
53
|
+
agentSocket: t.Optional(t.String()),
|
|
54
|
+
jumpConnectionId: t.Optional(t.Nullable(t.String())),
|
|
55
|
+
initialPath: t.Optional(t.String()),
|
|
56
|
+
keepaliveSeconds: t.Optional(t.Number()),
|
|
57
|
+
strictHostKey: t.Optional(t.Boolean()),
|
|
58
|
+
color: t.Optional(t.String())
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const healthSchema = t.Object({
|
|
62
|
+
ok: t.Boolean(),
|
|
63
|
+
latencyMs: t.Nullable(t.Number()),
|
|
64
|
+
serverBanner: t.Nullable(t.String()),
|
|
65
|
+
remoteOs: t.Nullable(t.String()),
|
|
66
|
+
hostKeyFingerprint: t.Nullable(t.String()),
|
|
67
|
+
hostKeyChanged: t.Boolean(),
|
|
68
|
+
suspended: t.Boolean(),
|
|
69
|
+
error: t.Nullable(t.String())
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const connectionTestSchema = t.Union([t.Object({ id: t.String({ minLength: 1 }) }), connectionInputSchema]);
|
|
73
|
+
|
|
74
|
+
function isInput(value: unknown): value is SshConnectionInput {
|
|
75
|
+
return typeof value === 'object' && value !== null && 'host' in (value as Record<string, unknown>);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Changing where or how a host is reached invalidates everything riding the old
|
|
80
|
+
* transport, so shells, forwards and the pooled client all go before the write.
|
|
81
|
+
*/
|
|
82
|
+
async function tearDownConnection(connectionId: string): Promise<void> {
|
|
83
|
+
killSessionsForConnection(connectionId);
|
|
84
|
+
await sshForwardManager.stopForConnection(connectionId);
|
|
85
|
+
sshClientPool.release(connectionId);
|
|
86
|
+
// Editing or deleting is not "disconnect" — a host suspended earlier should
|
|
87
|
+
// be dialable again once its settings change.
|
|
88
|
+
sshClientPool.resume(connectionId);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export const sshConnectionsHandler = createRouter()
|
|
92
|
+
.http('ssh:list', {
|
|
93
|
+
data: t.Object({}),
|
|
94
|
+
response: t.Array(t.Any())
|
|
95
|
+
}, async ({ conn }) => {
|
|
96
|
+
await initializeDatabase();
|
|
97
|
+
const { userId, isAdmin } = getSshPrincipal(conn);
|
|
98
|
+
return sshConnectionQueries.listForUser(userId, isAdmin);
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
.http('ssh:get', {
|
|
102
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
103
|
+
response: t.Any()
|
|
104
|
+
}, async ({ data, conn }) => {
|
|
105
|
+
const { userId, isAdmin } = getSshPrincipal(conn);
|
|
106
|
+
const connection = sshConnectionQueries.getForUser(data.id, userId, isAdmin);
|
|
107
|
+
if (!connection) throw new Error('ssh connection not found');
|
|
108
|
+
return connection;
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
.http('ssh:create', {
|
|
112
|
+
data: connectionInputSchema,
|
|
113
|
+
response: t.Any()
|
|
114
|
+
}, async ({ data, conn }) => {
|
|
115
|
+
await initializeDatabase();
|
|
116
|
+
const { userId } = getSshPrincipal(conn);
|
|
117
|
+
const created = sshConnectionQueries.createForUser(data as SshConnectionInput, userId);
|
|
118
|
+
debug.log('ssh', `created connection ${created.id} (${created.host})`);
|
|
119
|
+
return created;
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
.http('ssh:update', {
|
|
123
|
+
data: t.Object({ id: t.String({ minLength: 1 }), patch: connectionPatchSchema }),
|
|
124
|
+
response: t.Any()
|
|
125
|
+
}, async ({ data, conn }) => {
|
|
126
|
+
const { userId, isAdmin } = getSshPrincipal(conn);
|
|
127
|
+
requireSshConnection(conn, data.id);
|
|
128
|
+
await tearDownConnection(data.id);
|
|
129
|
+
return sshConnectionQueries.updateForUser(
|
|
130
|
+
data.id,
|
|
131
|
+
data.patch as Partial<SshConnectionInput>,
|
|
132
|
+
userId,
|
|
133
|
+
isAdmin
|
|
134
|
+
);
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
.http('ssh:delete', {
|
|
138
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
139
|
+
response: t.Object({ ok: t.Boolean() })
|
|
140
|
+
}, async ({ data, conn }) => {
|
|
141
|
+
const { userId, isAdmin } = getSshPrincipal(conn);
|
|
142
|
+
requireSshConnection(conn, data.id);
|
|
143
|
+
await tearDownConnection(data.id);
|
|
144
|
+
sshConnectionQueries.deleteForUser(data.id, userId, isAdmin);
|
|
145
|
+
return { ok: true };
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
.http('ssh:test', {
|
|
149
|
+
data: connectionTestSchema,
|
|
150
|
+
response: healthSchema
|
|
151
|
+
}, async ({ data, conn }) => {
|
|
152
|
+
await initializeDatabase();
|
|
153
|
+
if (isInput(data)) {
|
|
154
|
+
return sshHealthService.testInput(data as SshConnectionInput);
|
|
155
|
+
}
|
|
156
|
+
const id = (data as { id: string }).id;
|
|
157
|
+
requireSshConnection(conn, id);
|
|
158
|
+
return sshHealthService.testSaved(id);
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Open the host now (and start its auto-start forwards). Called when the user
|
|
163
|
+
* selects a connection, so the sidebar can show a live state before any tab
|
|
164
|
+
* or file listing asks for one.
|
|
165
|
+
*/
|
|
166
|
+
.http('ssh:activate', {
|
|
167
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
168
|
+
response: healthSchema
|
|
169
|
+
}, async ({ data, conn }) => {
|
|
170
|
+
requireSshConnection(conn, data.id);
|
|
171
|
+
sshClientPool.resume(data.id);
|
|
172
|
+
const health = await sshHealthService.testSaved(data.id);
|
|
173
|
+
if (health.ok) {
|
|
174
|
+
await sshForwardManager.startAutoForwards(data.id);
|
|
175
|
+
}
|
|
176
|
+
return health;
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Close a host's transport, its shells and its forwards, and keep it closed.
|
|
181
|
+
* Without the suspend the pool would redial on the next keystroke, which is
|
|
182
|
+
* what made this button look like it did nothing.
|
|
183
|
+
*/
|
|
184
|
+
.http('ssh:disconnect', {
|
|
185
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
186
|
+
response: healthSchema
|
|
187
|
+
}, async ({ data, conn }) => {
|
|
188
|
+
requireSshConnection(conn, data.id);
|
|
189
|
+
killSessionsForConnection(data.id);
|
|
190
|
+
await sshForwardManager.stopForConnection(data.id);
|
|
191
|
+
sshClientPool.suspend(data.id);
|
|
192
|
+
return sshHealthService.testSaved(data.id);
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
.http('ssh:known-hosts', {
|
|
196
|
+
data: t.Object({}),
|
|
197
|
+
response: t.Array(t.Any())
|
|
198
|
+
}, async () => {
|
|
199
|
+
await initializeDatabase();
|
|
200
|
+
return sshKnownHostQueries.list();
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Accept the key a host is presenting now, replacing the trusted one. The
|
|
205
|
+
* fingerprint is re-read from the live handshake rather than taken from the
|
|
206
|
+
* client, so trusting is always trusting what the host actually sent.
|
|
207
|
+
*/
|
|
208
|
+
.http('ssh:trust-host-key', {
|
|
209
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
210
|
+
response: healthSchema
|
|
211
|
+
}, async ({ data, conn }) => {
|
|
212
|
+
const connection = requireSshConnection(conn, data.id);
|
|
213
|
+
knownHosts.forget(connection.host, connection.port);
|
|
214
|
+
await tearDownConnection(data.id);
|
|
215
|
+
return sshHealthService.testSaved(data.id);
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
.http('ssh:forget-host-key', {
|
|
219
|
+
data: t.Object({ host: t.String({ minLength: 1 }), port: t.Number() }),
|
|
220
|
+
response: t.Object({ ok: t.Boolean() })
|
|
221
|
+
}, async ({ data }) => {
|
|
222
|
+
knownHosts.forget(data.host, data.port);
|
|
223
|
+
return { ok: true };
|
|
224
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ssh-client — port forwarding WS handlers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { t } from 'elysia';
|
|
6
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
7
|
+
import { sshPortForwardQueries } from '../../database/queries';
|
|
8
|
+
import { sshForwardManager } from '../../ssh/forwards';
|
|
9
|
+
import type { SshForwardInput } from '$shared/types/ssh';
|
|
10
|
+
import { requireSshConnection, requireSshForward } from './access';
|
|
11
|
+
|
|
12
|
+
const forwardTypeSchema = t.Union([t.Literal('local'), t.Literal('remote'), t.Literal('dynamic')]);
|
|
13
|
+
|
|
14
|
+
const forwardInputSchema = t.Object({
|
|
15
|
+
name: t.String({ minLength: 1 }),
|
|
16
|
+
type: forwardTypeSchema,
|
|
17
|
+
listenHost: t.Optional(t.String()),
|
|
18
|
+
listenPort: t.Number({ minimum: 0, maximum: 65535 }),
|
|
19
|
+
destHost: t.Optional(t.String()),
|
|
20
|
+
destPort: t.Optional(t.Number({ minimum: 0, maximum: 65535 })),
|
|
21
|
+
autoStart: t.Optional(t.Boolean())
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const forwardPatchSchema = t.Object({
|
|
25
|
+
name: t.Optional(t.String({ minLength: 1 })),
|
|
26
|
+
type: t.Optional(forwardTypeSchema),
|
|
27
|
+
listenHost: t.Optional(t.String()),
|
|
28
|
+
listenPort: t.Optional(t.Number({ minimum: 0, maximum: 65535 })),
|
|
29
|
+
destHost: t.Optional(t.String()),
|
|
30
|
+
destPort: t.Optional(t.Number({ minimum: 0, maximum: 65535 })),
|
|
31
|
+
autoStart: t.Optional(t.Boolean())
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/** A non-dynamic forward is meaningless without somewhere to forward to. */
|
|
35
|
+
function requireDestination(type: string, destHost?: string, destPort?: number): void {
|
|
36
|
+
if (type === 'dynamic') return;
|
|
37
|
+
if (!destHost || !destPort) {
|
|
38
|
+
throw new Error('Local and remote forwards need a destination host and port');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const sshForwardsHandler = createRouter()
|
|
43
|
+
.http('ssh:forward-list', {
|
|
44
|
+
data: t.Object({ connectionId: t.String({ minLength: 1 }) }),
|
|
45
|
+
response: t.Any()
|
|
46
|
+
}, async ({ data, conn }) => {
|
|
47
|
+
requireSshConnection(conn, data.connectionId);
|
|
48
|
+
return {
|
|
49
|
+
forwards: sshPortForwardQueries.listForConnection(data.connectionId),
|
|
50
|
+
statuses: sshForwardManager.statusesForConnection(data.connectionId)
|
|
51
|
+
};
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
.http('ssh:forward-create', {
|
|
55
|
+
data: t.Object({
|
|
56
|
+
connectionId: t.String({ minLength: 1 }),
|
|
57
|
+
input: forwardInputSchema
|
|
58
|
+
}),
|
|
59
|
+
response: t.Any()
|
|
60
|
+
}, async ({ data, conn }) => {
|
|
61
|
+
requireSshConnection(conn, data.connectionId);
|
|
62
|
+
const input = data.input as SshForwardInput;
|
|
63
|
+
requireDestination(input.type, input.destHost, input.destPort);
|
|
64
|
+
return sshPortForwardQueries.create(data.connectionId, input);
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
.http('ssh:forward-update', {
|
|
68
|
+
data: t.Object({ id: t.String({ minLength: 1 }), patch: forwardPatchSchema }),
|
|
69
|
+
response: t.Any()
|
|
70
|
+
}, async ({ data, conn }) => {
|
|
71
|
+
const existing = requireSshForward(conn, data.id);
|
|
72
|
+
const patch = data.patch as Partial<SshForwardInput>;
|
|
73
|
+
requireDestination(
|
|
74
|
+
patch.type ?? existing.type,
|
|
75
|
+
patch.destHost ?? existing.destHost ?? undefined,
|
|
76
|
+
patch.destPort ?? existing.destPort ?? undefined
|
|
77
|
+
);
|
|
78
|
+
// Settings only take effect on a fresh listener, so restart-on-edit is the
|
|
79
|
+
// honest behaviour: stop now, and let the user start it again.
|
|
80
|
+
await sshForwardManager.stop(data.id);
|
|
81
|
+
return sshPortForwardQueries.update(data.id, patch);
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
.http('ssh:forward-delete', {
|
|
85
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
86
|
+
response: t.Object({ ok: t.Boolean() })
|
|
87
|
+
}, async ({ data, conn }) => {
|
|
88
|
+
requireSshForward(conn, data.id);
|
|
89
|
+
await sshForwardManager.stop(data.id);
|
|
90
|
+
sshPortForwardQueries.delete(data.id);
|
|
91
|
+
return { ok: true };
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
.http('ssh:forward-start', {
|
|
95
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
96
|
+
response: t.Any()
|
|
97
|
+
}, async ({ data, conn }) => {
|
|
98
|
+
const forward = requireSshForward(conn, data.id);
|
|
99
|
+
return sshForwardManager.start(forward);
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
.http('ssh:forward-stop', {
|
|
103
|
+
data: t.Object({ id: t.String({ minLength: 1 }) }),
|
|
104
|
+
response: t.Any()
|
|
105
|
+
}, async ({ data, conn }) => {
|
|
106
|
+
const forward = requireSshForward(conn, data.id);
|
|
107
|
+
await sshForwardManager.stop(data.id);
|
|
108
|
+
return sshForwardManager.status(forward);
|
|
109
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ssh-client WebSocket router.
|
|
3
|
+
*
|
|
4
|
+
* Terminal sessions run on their own PtyKit manager and are tunneled over this
|
|
5
|
+
* socket (tunnel.ts); everything else is ordinary request/response.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
9
|
+
import { sshConnectionsHandler } from './connections';
|
|
10
|
+
import { sshSftpHandler } from './sftp';
|
|
11
|
+
import { sshForwardsHandler } from './forwards';
|
|
12
|
+
import { sshTunnelHandler } from './tunnel';
|
|
13
|
+
|
|
14
|
+
export const sshRouter = createRouter()
|
|
15
|
+
.merge(sshConnectionsHandler)
|
|
16
|
+
.merge(sshSftpHandler)
|
|
17
|
+
.merge(sshForwardsHandler)
|
|
18
|
+
.merge(sshTunnelHandler);
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ssh-client — SFTP browsing WS handlers.
|
|
3
|
+
*
|
|
4
|
+
* Small operations (listing, rename, chmod, text edits) travel here. Bulk file
|
|
5
|
+
* bytes do not: uploads and downloads use the HTTP routes in
|
|
6
|
+
* backend/http/ssh-sftp.ts, for the same reason the local file browser does —
|
|
7
|
+
* a WebSocket wedges on sustained binary transfer behind the dev proxy.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { t } from 'elysia';
|
|
11
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
12
|
+
import { sftpService } from '../../ssh/sftp';
|
|
13
|
+
import { requireSshConnection } from './access';
|
|
14
|
+
|
|
15
|
+
const connectionAndPath = t.Object({
|
|
16
|
+
connectionId: t.String({ minLength: 1 }),
|
|
17
|
+
path: t.String()
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/** What to do about a name the destination is already using. */
|
|
21
|
+
const conflictStrategy = t.Union([
|
|
22
|
+
t.Literal('skip'),
|
|
23
|
+
t.Literal('overwrite'),
|
|
24
|
+
t.Literal('rename')
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
export const sshSftpHandler = createRouter()
|
|
28
|
+
.http('ssh:sftp-list', {
|
|
29
|
+
data: connectionAndPath,
|
|
30
|
+
response: t.Any()
|
|
31
|
+
}, async ({ data, conn }) => {
|
|
32
|
+
const connection = requireSshConnection(conn, data.connectionId);
|
|
33
|
+
// An empty path means "wherever this connection starts": its configured
|
|
34
|
+
// initial path, else the account's home directory.
|
|
35
|
+
const path = data.path || connection.initialPath || '.';
|
|
36
|
+
return sftpService.list(data.connectionId, path);
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
.http('ssh:sftp-stat', {
|
|
40
|
+
data: connectionAndPath,
|
|
41
|
+
response: t.Any()
|
|
42
|
+
}, async ({ data, conn }) => {
|
|
43
|
+
requireSshConnection(conn, data.connectionId);
|
|
44
|
+
return sftpService.stat(data.connectionId, data.path);
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
.http('ssh:sftp-mkdir', {
|
|
48
|
+
data: connectionAndPath,
|
|
49
|
+
response: t.Object({ ok: t.Boolean() })
|
|
50
|
+
}, async ({ data, conn }) => {
|
|
51
|
+
requireSshConnection(conn, data.connectionId);
|
|
52
|
+
await sftpService.makeDirectory(data.connectionId, data.path);
|
|
53
|
+
return { ok: true };
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
.http('ssh:sftp-create-file', {
|
|
57
|
+
data: connectionAndPath,
|
|
58
|
+
response: t.Object({ ok: t.Boolean() })
|
|
59
|
+
}, async ({ data, conn }) => {
|
|
60
|
+
requireSshConnection(conn, data.connectionId);
|
|
61
|
+
await sftpService.createFile(data.connectionId, data.path);
|
|
62
|
+
return { ok: true };
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
.http('ssh:sftp-rename', {
|
|
66
|
+
data: t.Object({
|
|
67
|
+
connectionId: t.String({ minLength: 1 }),
|
|
68
|
+
fromPath: t.String({ minLength: 1 }),
|
|
69
|
+
toPath: t.String({ minLength: 1 })
|
|
70
|
+
}),
|
|
71
|
+
response: t.Object({ ok: t.Boolean() })
|
|
72
|
+
}, async ({ data, conn }) => {
|
|
73
|
+
requireSshConnection(conn, data.connectionId);
|
|
74
|
+
await sftpService.rename(data.connectionId, data.fromPath, data.toPath);
|
|
75
|
+
return { ok: true };
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
.http('ssh:sftp-chmod', {
|
|
79
|
+
data: t.Object({
|
|
80
|
+
connectionId: t.String({ minLength: 1 }),
|
|
81
|
+
path: t.String({ minLength: 1 }),
|
|
82
|
+
mode: t.Number({ minimum: 0, maximum: 0o7777 })
|
|
83
|
+
}),
|
|
84
|
+
response: t.Object({ ok: t.Boolean() })
|
|
85
|
+
}, async ({ data, conn }) => {
|
|
86
|
+
requireSshConnection(conn, data.connectionId);
|
|
87
|
+
await sftpService.chmod(data.connectionId, data.path, data.mode);
|
|
88
|
+
return { ok: true };
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
.http('ssh:sftp-delete', {
|
|
92
|
+
data: t.Object({
|
|
93
|
+
connectionId: t.String({ minLength: 1 }),
|
|
94
|
+
path: t.String({ minLength: 1 }),
|
|
95
|
+
recursive: t.Optional(t.Boolean())
|
|
96
|
+
}),
|
|
97
|
+
response: t.Object({ ok: t.Boolean() })
|
|
98
|
+
}, async ({ data, conn }) => {
|
|
99
|
+
requireSshConnection(conn, data.connectionId);
|
|
100
|
+
await sftpService.remove(data.connectionId, data.path, data.recursive === true);
|
|
101
|
+
return { ok: true };
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
.http('ssh:sftp-read', {
|
|
105
|
+
data: t.Object({
|
|
106
|
+
connectionId: t.String({ minLength: 1 }),
|
|
107
|
+
path: t.String({ minLength: 1 })
|
|
108
|
+
}),
|
|
109
|
+
response: t.Any()
|
|
110
|
+
}, async ({ data, conn }) => {
|
|
111
|
+
requireSshConnection(conn, data.connectionId);
|
|
112
|
+
return sftpService.readText(data.connectionId, data.path);
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
.http('ssh:sftp-write', {
|
|
116
|
+
data: t.Object({
|
|
117
|
+
connectionId: t.String({ minLength: 1 }),
|
|
118
|
+
path: t.String({ minLength: 1 }),
|
|
119
|
+
text: t.String()
|
|
120
|
+
}),
|
|
121
|
+
response: t.Object({ ok: t.Boolean() })
|
|
122
|
+
}, async ({ data, conn }) => {
|
|
123
|
+
requireSshConnection(conn, data.connectionId);
|
|
124
|
+
await sftpService.writeText(data.connectionId, data.path, data.text);
|
|
125
|
+
return { ok: true };
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
.http('ssh:sftp-delete-many', {
|
|
129
|
+
data: t.Object({
|
|
130
|
+
connectionId: t.String({ minLength: 1 }),
|
|
131
|
+
paths: t.Array(t.String({ minLength: 1 }), { minItems: 1 }),
|
|
132
|
+
recursive: t.Optional(t.Boolean())
|
|
133
|
+
}),
|
|
134
|
+
response: t.Any()
|
|
135
|
+
}, async ({ data, conn }) => {
|
|
136
|
+
requireSshConnection(conn, data.connectionId);
|
|
137
|
+
return sftpService.removeMany(data.connectionId, data.paths, data.recursive !== false);
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
.http('ssh:sftp-check-conflicts', {
|
|
141
|
+
data: t.Object({
|
|
142
|
+
connectionId: t.String({ minLength: 1 }),
|
|
143
|
+
paths: t.Array(t.String({ minLength: 1 }), { minItems: 1 }),
|
|
144
|
+
destinationDirectory: t.String({ minLength: 1 }),
|
|
145
|
+
operation: t.Union([t.Literal('move'), t.Literal('copy')])
|
|
146
|
+
}),
|
|
147
|
+
response: t.Any()
|
|
148
|
+
}, async ({ data, conn }) => {
|
|
149
|
+
requireSshConnection(conn, data.connectionId);
|
|
150
|
+
return sftpService.findConflicts(
|
|
151
|
+
data.connectionId,
|
|
152
|
+
data.paths,
|
|
153
|
+
data.destinationDirectory,
|
|
154
|
+
data.operation
|
|
155
|
+
);
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
.http('ssh:sftp-move', {
|
|
159
|
+
data: t.Object({
|
|
160
|
+
connectionId: t.String({ minLength: 1 }),
|
|
161
|
+
paths: t.Array(t.String({ minLength: 1 }), { minItems: 1 }),
|
|
162
|
+
destinationDirectory: t.String({ minLength: 1 }),
|
|
163
|
+
onConflict: t.Optional(conflictStrategy)
|
|
164
|
+
}),
|
|
165
|
+
response: t.Any()
|
|
166
|
+
}, async ({ data, conn }) => {
|
|
167
|
+
requireSshConnection(conn, data.connectionId);
|
|
168
|
+
return sftpService.move(
|
|
169
|
+
data.connectionId,
|
|
170
|
+
data.paths,
|
|
171
|
+
data.destinationDirectory,
|
|
172
|
+
data.onConflict ?? 'skip'
|
|
173
|
+
);
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
.http('ssh:sftp-copy', {
|
|
177
|
+
data: t.Object({
|
|
178
|
+
connectionId: t.String({ minLength: 1 }),
|
|
179
|
+
paths: t.Array(t.String({ minLength: 1 }), { minItems: 1 }),
|
|
180
|
+
destinationDirectory: t.String({ minLength: 1 }),
|
|
181
|
+
onConflict: t.Optional(conflictStrategy)
|
|
182
|
+
}),
|
|
183
|
+
response: t.Any()
|
|
184
|
+
}, async ({ data, conn }) => {
|
|
185
|
+
requireSshConnection(conn, data.connectionId);
|
|
186
|
+
return sftpService.copy(
|
|
187
|
+
data.connectionId,
|
|
188
|
+
data.paths,
|
|
189
|
+
data.destinationDirectory,
|
|
190
|
+
data.onConflict ?? 'skip'
|
|
191
|
+
);
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
.http('ssh:sftp-compress', {
|
|
195
|
+
data: t.Object({
|
|
196
|
+
connectionId: t.String({ minLength: 1 }),
|
|
197
|
+
paths: t.Array(t.String({ minLength: 1 }), { minItems: 1 }),
|
|
198
|
+
archivePath: t.String({ minLength: 1 }),
|
|
199
|
+
format: t.Union([t.Literal('zip'), t.Literal('tar.gz')]),
|
|
200
|
+
onConflict: t.Optional(conflictStrategy)
|
|
201
|
+
}),
|
|
202
|
+
response: t.Any()
|
|
203
|
+
}, async ({ data, conn }) => {
|
|
204
|
+
requireSshConnection(conn, data.connectionId);
|
|
205
|
+
return sftpService.compress(
|
|
206
|
+
data.connectionId,
|
|
207
|
+
data.paths,
|
|
208
|
+
data.archivePath,
|
|
209
|
+
data.format,
|
|
210
|
+
data.onConflict ?? 'skip'
|
|
211
|
+
);
|
|
212
|
+
})
|
|
213
|
+
|
|
214
|
+
.http('ssh:sftp-inspect-archive', {
|
|
215
|
+
data: t.Object({
|
|
216
|
+
connectionId: t.String({ minLength: 1 }),
|
|
217
|
+
archivePath: t.String({ minLength: 1 }),
|
|
218
|
+
destinationDirectory: t.String({ minLength: 1 })
|
|
219
|
+
}),
|
|
220
|
+
response: t.Any()
|
|
221
|
+
}, async ({ data, conn }) => {
|
|
222
|
+
requireSshConnection(conn, data.connectionId);
|
|
223
|
+
return sftpService.inspectArchive(
|
|
224
|
+
data.connectionId,
|
|
225
|
+
data.archivePath,
|
|
226
|
+
data.destinationDirectory
|
|
227
|
+
);
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
.http('ssh:sftp-extract', {
|
|
231
|
+
data: t.Object({
|
|
232
|
+
connectionId: t.String({ minLength: 1 }),
|
|
233
|
+
archivePath: t.String({ minLength: 1 }),
|
|
234
|
+
destinationDirectory: t.String({ minLength: 1 }),
|
|
235
|
+
mode: t.Optional(t.Union([t.Literal('smart'), t.Literal('here'), t.Literal('folder')])),
|
|
236
|
+
folderName: t.Optional(t.String()),
|
|
237
|
+
onConflict: t.Optional(conflictStrategy)
|
|
238
|
+
}),
|
|
239
|
+
response: t.Any()
|
|
240
|
+
}, async ({ data, conn }) => {
|
|
241
|
+
requireSshConnection(conn, data.connectionId);
|
|
242
|
+
return sftpService.extract(data.connectionId, data.archivePath, data.destinationDirectory, {
|
|
243
|
+
mode: data.mode ?? 'smart',
|
|
244
|
+
folderName: data.folderName,
|
|
245
|
+
onConflict: data.onConflict ?? 'rename'
|
|
246
|
+
});
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
.http('ssh:sftp-disk-usage', {
|
|
250
|
+
data: connectionAndPath,
|
|
251
|
+
response: t.Any()
|
|
252
|
+
}, async ({ data, conn }) => {
|
|
253
|
+
const connection = requireSshConnection(conn, data.connectionId);
|
|
254
|
+
// An empty path means "wherever this account lives". `.` resolves to the
|
|
255
|
+
// home directory in the exec session, which is what `df` needs and what the
|
|
256
|
+
// quota sources ignore — so the host overview can ask without knowing a path.
|
|
257
|
+
const path = data.path || connection.initialPath || '.';
|
|
258
|
+
return sftpService.diskUsage(data.connectionId, path);
|
|
259
|
+
});
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ssh-client — the SSH terminal tunnel.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors backend/ws/terminal/tunnel.ts: PtyKit's wire protocol rides Clopen's
|
|
5
|
+
* app-wide socket on `ssh:pty` / `ssh:pty-out`, with one embedded PtyKit
|
|
6
|
+
* connection per Clopen connection.
|
|
7
|
+
*
|
|
8
|
+
* One thing this tunnel does that the local one does not: it stamps the spawn
|
|
9
|
+
* target onto every `create-session` frame. The client says which host it wants
|
|
10
|
+
* through the namespace, which `authorize` checks ownership of; the connection
|
|
11
|
+
* id the backend actually dials is then derived from that namespace here, so a
|
|
12
|
+
* client cannot name one host in the namespace and open a shell on another.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { t } from 'elysia';
|
|
16
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
17
|
+
import type { WSConnection } from '$shared/utils/ws-server';
|
|
18
|
+
import type { PtyKitConnection } from '@myrialabs/ptykit/server';
|
|
19
|
+
import type { WireFrame } from '@myrialabs/ptykit';
|
|
20
|
+
import { ws } from '$backend/utils/ws';
|
|
21
|
+
import { encodeSshTarget } from '../../ssh/pty-backend';
|
|
22
|
+
import { connectionIdFromNamespace, sshPtyKitServer } from '../../ssh/ptykit';
|
|
23
|
+
import { debug } from '$shared/utils/logger';
|
|
24
|
+
|
|
25
|
+
/** One embedded PtyKit connection per Clopen ws connection (by stable id). */
|
|
26
|
+
const sshPtyConnections = new Map<string, PtyKitConnection>();
|
|
27
|
+
|
|
28
|
+
function getSshPtyConnection(conn: WSConnection): PtyKitConnection {
|
|
29
|
+
const connId = ws.getConnectionId(conn);
|
|
30
|
+
if (!connId) {
|
|
31
|
+
throw new Error('Connection not registered');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const existing = sshPtyConnections.get(connId);
|
|
35
|
+
if (existing) return existing;
|
|
36
|
+
|
|
37
|
+
const userId = ws.getUserId(conn);
|
|
38
|
+
const isAdmin = ws.getRole(conn) === 'admin';
|
|
39
|
+
const ptyConn = sshPtyKitServer.createConnection({
|
|
40
|
+
data: { userId, isAdmin, connId },
|
|
41
|
+
send: (frame) => {
|
|
42
|
+
ws.sendToConnectionId(connId, JSON.stringify({ action: 'ssh:pty-out', payload: frame }));
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
sshPtyKitServer.handleOpen(ptyConn);
|
|
46
|
+
sshPtyConnections.set(connId, ptyConn);
|
|
47
|
+
|
|
48
|
+
ws.addCleanup(conn, () => {
|
|
49
|
+
sshPtyKitServer.handleClose(ptyConn);
|
|
50
|
+
sshPtyConnections.delete(connId);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
debug.log('ssh', `PtyKit connection opened for ${connId}`);
|
|
54
|
+
return ptyConn;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Replace whatever `shell` the client sent with the target derived from the
|
|
59
|
+
* frame's own namespace. Frames that are not `create-session` pass through.
|
|
60
|
+
*/
|
|
61
|
+
function stampSpawnTarget(frame: WireFrame): WireFrame {
|
|
62
|
+
if (frame.action !== 'create-session') return frame;
|
|
63
|
+
|
|
64
|
+
const payload = frame.payload as { requestId?: string; data?: Record<string, unknown> } | null;
|
|
65
|
+
const request = payload?.data;
|
|
66
|
+
if (!request || typeof request.namespace !== 'string') return frame;
|
|
67
|
+
|
|
68
|
+
const connectionId = connectionIdFromNamespace(request.namespace);
|
|
69
|
+
if (!connectionId) return frame;
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
action: frame.action,
|
|
73
|
+
payload: { ...payload, data: { ...request, shell: encodeSshTarget(connectionId) } }
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export const sshTunnelHandler = createRouter()
|
|
78
|
+
// Client → server: one PtyKit wire frame for a remote shell.
|
|
79
|
+
.on('ssh:pty', {
|
|
80
|
+
data: t.Object({
|
|
81
|
+
action: t.String(),
|
|
82
|
+
payload: t.Any()
|
|
83
|
+
})
|
|
84
|
+
}, ({ conn, data }) => {
|
|
85
|
+
const ptyConn = getSshPtyConnection(conn);
|
|
86
|
+
void sshPtyKitServer.handleFrame(ptyConn, stampSpawnTarget(data as WireFrame));
|
|
87
|
+
})
|
|
88
|
+
// Server → client: PtyKit wire frames. Declared for the typed WSAPI; actually
|
|
89
|
+
// sent via ws.sendToConnectionId above.
|
|
90
|
+
.emit('ssh:pty-out', t.Object({
|
|
91
|
+
action: t.String(),
|
|
92
|
+
payload: t.Any()
|
|
93
|
+
}));
|