@myrialabs/clopen 0.4.33 → 0.4.35
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.ts +10 -0
- package/backend/auth/rate-limiter.test.ts +115 -0
- package/backend/auth/rate-limiter.ts +25 -12
- package/backend/containers/actions.ts +316 -0
- package/backend/containers/disk-usage.test.ts +68 -0
- package/backend/containers/disk-usage.ts +170 -0
- package/backend/containers/logs.ts +335 -0
- package/backend/containers/monitor.ts +410 -0
- package/backend/containers/parse.test.ts +599 -0
- package/backend/containers/parse.ts +905 -0
- package/backend/containers/port-index.test.ts +91 -0
- package/backend/containers/port-index.ts +198 -0
- package/backend/containers/prune-job.test.ts +148 -0
- package/backend/containers/prune-job.ts +129 -0
- package/backend/containers/pty-backend.ts +337 -0
- package/backend/containers/pty.ts +114 -0
- package/backend/containers/runtime.ts +218 -0
- package/backend/containers/scan.ts +293 -0
- package/backend/db-client/connection-manager.ts +13 -0
- package/backend/files/file-watcher.test.ts +105 -0
- package/backend/files/file-watcher.ts +45 -1
- package/backend/git/git-service.test.ts +33 -0
- package/backend/git/git-service.ts +7 -3
- package/backend/git/nested-repos.test.ts +292 -1
- package/backend/git/nested-repos.ts +330 -17
- package/backend/host/runner.ts +204 -0
- package/backend/index.ts +20 -2
- package/backend/middleware/security.test.ts +95 -0
- package/backend/middleware/security.ts +114 -0
- package/backend/ports/attribute.test.ts +231 -0
- package/backend/ports/attribute.ts +444 -0
- package/backend/ports/kill.ts +203 -0
- package/backend/ports/monitor.ts +365 -0
- package/backend/ports/processes.test.ts +98 -0
- package/backend/ports/processes.ts +374 -0
- package/backend/ports/registry.ts +165 -0
- package/backend/ports/scan-host.ts +329 -0
- package/backend/ports/scan.test.ts +341 -0
- package/backend/ports/scan.ts +687 -0
- package/backend/ports/ssh-lineage.ts +318 -0
- package/backend/preview/browser/browser-video-capture.ts +84 -31
- package/backend/ssh/forwards.ts +31 -0
- package/backend/ws/auth/device.ts +1 -1
- package/backend/ws/auth/login.ts +8 -4
- package/backend/ws/containers/actions.ts +156 -0
- package/backend/ws/containers/index.ts +43 -0
- package/backend/ws/containers/list.ts +146 -0
- package/backend/ws/containers/logs.ts +43 -0
- package/backend/ws/containers/tunnel.ts +101 -0
- package/backend/ws/git/log.ts +3 -1
- package/backend/ws/git/status.ts +37 -27
- package/backend/ws/index.ts +4 -0
- package/backend/ws/message-rate-limiter.test.ts +45 -0
- package/backend/ws/message-rate-limiter.ts +34 -3
- package/backend/ws/ports/index.ts +19 -0
- package/backend/ws/ports/kill.ts +80 -0
- package/backend/ws/ports/list.ts +60 -0
- package/backend/ws/ssh/connections.ts +8 -0
- package/backend/ws/system/device-info.ts +99 -43
- package/dist/assets/{index-BF4buYmI.js → index-B0iIhSO9.js} +1 -1
- package/dist/assets/{index-BW0DA8NV.js → index-BRJqss6F.js} +1 -1
- package/dist/assets/main-Buko4D0t.css +1 -0
- package/dist/assets/{main-BbwWLJRN.js → main-C_iGUZyL.js} +468 -447
- package/dist/index.html +2 -2
- package/dist/manifest.json +9 -2
- package/package.json +1 -1
- package/shared/types/containers/container.ts +366 -0
- package/shared/types/containers/index.ts +5 -0
- package/shared/types/host/index.ts +18 -0
- package/shared/types/ports/index.ts +5 -0
- package/shared/types/ports/port.ts +230 -0
- package/shared/utils/logger.ts +3 -0
- package/shared/utils/ws-client.ts +67 -12
- package/shared/utils/ws-server.ts +64 -13
- package/dist/assets/main-CCvRMXlP.css +0 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Containers — changing something. Admin only, all of it.
|
|
3
|
+
*
|
|
4
|
+
* The routes are listed in ADMIN_ONLY_ROUTES; these handlers still check host
|
|
5
|
+
* access against the caller's real role rather than a hardcoded one, because a
|
|
6
|
+
* gate that trusts its caller is no gate at all.
|
|
7
|
+
*
|
|
8
|
+
* Stopping a container takes down whatever it serves, for everyone — the same
|
|
9
|
+
* reason stopping a port is admin-only — and removing an image or a volume
|
|
10
|
+
* takes down something nobody can bring back by pressing start.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { t } from 'elysia';
|
|
14
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
15
|
+
import { LOCAL_HOST_ID } from '$shared/types/host';
|
|
16
|
+
import { removeResource, runContainerAction } from '../../containers/actions';
|
|
17
|
+
import { dismissPrune, pruneJobFor, startPrune } from '../../containers/prune-job';
|
|
18
|
+
import { requireHostAccess } from './list';
|
|
19
|
+
import { containerMonitor } from '../../containers/monitor';
|
|
20
|
+
import { sshConnectionQueries } from '../../database/queries';
|
|
21
|
+
import { ws } from '../../utils/ws';
|
|
22
|
+
|
|
23
|
+
function requireManageAccess(conn: Parameters<typeof ws.getUserId>[0], hostId: string): void {
|
|
24
|
+
if (hostId === LOCAL_HOST_ID) return;
|
|
25
|
+
sshConnectionQueries.ensureAccess(hostId, ws.getUserId(conn), ws.getRole(conn) === 'admin');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const ACTIONS = [
|
|
29
|
+
t.Literal('start'),
|
|
30
|
+
t.Literal('stop'),
|
|
31
|
+
t.Literal('restart'),
|
|
32
|
+
t.Literal('pause'),
|
|
33
|
+
t.Literal('unpause'),
|
|
34
|
+
t.Literal('remove'),
|
|
35
|
+
t.Literal('force-remove')
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const PRUNE_KINDS = [
|
|
39
|
+
t.Literal('containers'),
|
|
40
|
+
t.Literal('dangling-images'),
|
|
41
|
+
t.Literal('images'),
|
|
42
|
+
t.Literal('volumes'),
|
|
43
|
+
t.Literal('networks'),
|
|
44
|
+
t.Literal('build-cache')
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
export const containersActionHandler = createRouter()
|
|
48
|
+
.http(
|
|
49
|
+
'containers:action',
|
|
50
|
+
{
|
|
51
|
+
data: t.Object({
|
|
52
|
+
hostId: t.String({ minLength: 1 }),
|
|
53
|
+
containerId: t.String({ minLength: 12 }),
|
|
54
|
+
action: t.Union(ACTIONS)
|
|
55
|
+
}),
|
|
56
|
+
response: t.Any()
|
|
57
|
+
},
|
|
58
|
+
async ({ data, conn }) => {
|
|
59
|
+
requireManageAccess(conn, data.hostId);
|
|
60
|
+
|
|
61
|
+
// Re-read before acting rather than trusting the row the client clicked:
|
|
62
|
+
// a listing is a second or two old, and a container can be removed and
|
|
63
|
+
// its name reused in that time.
|
|
64
|
+
const container = await containerMonitor.findContainer(data.hostId, data.containerId);
|
|
65
|
+
if (!container) {
|
|
66
|
+
return { result: { ok: false, error: null }, gone: true };
|
|
67
|
+
}
|
|
68
|
+
if (!container.canManage) {
|
|
69
|
+
return {
|
|
70
|
+
result: { ok: false, error: `${container.name} cannot be changed from here right now.` },
|
|
71
|
+
gone: false
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
result: await runContainerAction(data.hostId, data.containerId, data.action),
|
|
77
|
+
gone: false
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Remove one image, volume or network.
|
|
84
|
+
*
|
|
85
|
+
* Containers go through `containers:action` instead, because removing one is
|
|
86
|
+
* part of its lifecycle and the confirmation has to know whether it is still
|
|
87
|
+
* running. Everything else has no lifecycle — it exists or it does not.
|
|
88
|
+
*/
|
|
89
|
+
.http(
|
|
90
|
+
'containers:remove',
|
|
91
|
+
{
|
|
92
|
+
data: t.Object({
|
|
93
|
+
hostId: t.String({ minLength: 1 }),
|
|
94
|
+
kind: t.Union([t.Literal('image'), t.Literal('volume'), t.Literal('network')]),
|
|
95
|
+
id: t.String({ minLength: 1 }),
|
|
96
|
+
force: t.Optional(t.Boolean())
|
|
97
|
+
}),
|
|
98
|
+
response: t.Any()
|
|
99
|
+
},
|
|
100
|
+
async ({ data, conn }) => {
|
|
101
|
+
requireManageAccess(conn, data.hostId);
|
|
102
|
+
return { result: await removeResource(data.hostId, data.kind, data.id, data.force === true) };
|
|
103
|
+
}
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Clear up whatever is not in use.
|
|
108
|
+
*
|
|
109
|
+
* Takes a list rather than doing everything, because the kinds differ by what
|
|
110
|
+
* they cost to get back: a stopped container is nothing, an unused image is a
|
|
111
|
+
* pull or a build. The caller decides; each kind reports separately.
|
|
112
|
+
*
|
|
113
|
+
* Returns as soon as the sweep has started, not when it ends. A prune runs
|
|
114
|
+
* for minutes and belongs to the host rather than to the dialog that asked
|
|
115
|
+
* for it — the result arrives on `containers:prune-changed`.
|
|
116
|
+
*/
|
|
117
|
+
.http(
|
|
118
|
+
'containers:prune',
|
|
119
|
+
{
|
|
120
|
+
data: t.Object({
|
|
121
|
+
hostId: t.String({ minLength: 1 }),
|
|
122
|
+
kinds: t.Array(t.Union(PRUNE_KINDS), { minItems: 1, maxItems: 6 })
|
|
123
|
+
}),
|
|
124
|
+
response: t.Any()
|
|
125
|
+
},
|
|
126
|
+
async ({ data, conn }) => {
|
|
127
|
+
requireManageAccess(conn, data.hostId);
|
|
128
|
+
return { job: startPrune(data.hostId, data.kinds, ws.getUserId(conn)) };
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The host's current sweep, or the last one nobody has acknowledged.
|
|
134
|
+
*
|
|
135
|
+
* What a dialog reads when it opens, so a sweep started before a refresh —
|
|
136
|
+
* or on another device — is still visible as the thing it is.
|
|
137
|
+
*/
|
|
138
|
+
.http(
|
|
139
|
+
'containers:prune-status',
|
|
140
|
+
{ data: t.Object({ hostId: t.String({ minLength: 1 }) }), response: t.Any() },
|
|
141
|
+
async ({ data, conn }) => {
|
|
142
|
+
requireHostAccess(conn, data.hostId);
|
|
143
|
+
return { job: pruneJobFor(data.hostId, ws.getUserId(conn)) };
|
|
144
|
+
}
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
/** Acknowledge a finished sweep, so its report stops being offered. */
|
|
148
|
+
.http(
|
|
149
|
+
'containers:prune-dismiss',
|
|
150
|
+
{ data: t.Object({ hostId: t.String({ minLength: 1 }) }), response: t.Any() },
|
|
151
|
+
async ({ data, conn }) => {
|
|
152
|
+
requireManageAccess(conn, data.hostId);
|
|
153
|
+
dismissPrune(data.hostId);
|
|
154
|
+
return { ok: true };
|
|
155
|
+
}
|
|
156
|
+
);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Container manager WebSocket router.
|
|
3
|
+
*
|
|
4
|
+
* Reading the list, the detail and the logs is open to any signed-in user who
|
|
5
|
+
* can already reach the host; `containers:action` is registered in
|
|
6
|
+
* ADMIN_ONLY_ROUTES, and opening a shell is gated inside the PtyKit server's
|
|
7
|
+
* own `authorize`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { t } from 'elysia';
|
|
11
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
12
|
+
import { containersListHandler } from './list';
|
|
13
|
+
import { containersActionHandler } from './actions';
|
|
14
|
+
import { containersLogsHandler } from './logs';
|
|
15
|
+
import { containersTunnelHandler } from './tunnel';
|
|
16
|
+
|
|
17
|
+
export const containersRouter = createRouter()
|
|
18
|
+
.merge(containersListHandler)
|
|
19
|
+
.merge(containersActionHandler)
|
|
20
|
+
.merge(containersLogsHandler)
|
|
21
|
+
.merge(containersTunnelHandler)
|
|
22
|
+
// Pushed only when a watched host's list actually changed.
|
|
23
|
+
.emit('containers:changed', t.Object({ result: t.Any() }))
|
|
24
|
+
// The sidebar count: containers running on this machine.
|
|
25
|
+
.emit('containers:badge', t.Object({ count: t.Number() }))
|
|
26
|
+
// A disk reading, pushed to whoever asked when it finally lands. It cannot
|
|
27
|
+
// be a response: `system df` takes as long as the host's disk is large.
|
|
28
|
+
.emit('containers:disk-usage-measured', t.Object({ hostId: t.String(), usage: t.Any() }))
|
|
29
|
+
// A cleanup sweep reaching its end, pushed to everyone attached to it — the
|
|
30
|
+
// dialog that started it may be long closed by then.
|
|
31
|
+
.emit('containers:prune-changed', t.Object({ job: t.Any() }))
|
|
32
|
+
// One coalesced push of container output while a log stream is followed.
|
|
33
|
+
.emit(
|
|
34
|
+
'containers:log-chunk',
|
|
35
|
+
t.Object({
|
|
36
|
+
streamId: t.String(),
|
|
37
|
+
hostId: t.String(),
|
|
38
|
+
containerId: t.String(),
|
|
39
|
+
data: t.String(),
|
|
40
|
+
done: t.Optional(t.Boolean()),
|
|
41
|
+
error: t.Optional(t.Union([t.String(), t.Null()]))
|
|
42
|
+
})
|
|
43
|
+
);
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Containers — reading the list.
|
|
3
|
+
*
|
|
4
|
+
* Watching is per connection: a client asks for a host when it opens that panel
|
|
5
|
+
* and is pushed a fresh listing whenever the host's containers actually change.
|
|
6
|
+
* The watch is torn down when the socket closes, so a browser tab that vanishes
|
|
7
|
+
* cannot leave a host being polled forever.
|
|
8
|
+
*
|
|
9
|
+
* Reading is open to any signed-in user who can already reach the host. Acting
|
|
10
|
+
* on a container is not, and lives in actions.ts.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { t } from 'elysia';
|
|
14
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
15
|
+
import { LOCAL_HOST_ID } from '$shared/types/host';
|
|
16
|
+
import { containerMonitor } from '../../containers/monitor';
|
|
17
|
+
import { inspectContainer, readStats } from '../../containers/actions';
|
|
18
|
+
import { requestDiskUsage } from '../../containers/disk-usage';
|
|
19
|
+
import { stopLogStreamsForUser } from '../../containers/logs';
|
|
20
|
+
import { sshConnectionQueries } from '../../database/queries';
|
|
21
|
+
import { ws } from '../../utils/ws';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A member may only watch an SSH host they can already reach. The local machine
|
|
25
|
+
* is watchable by anyone signed in — it is the machine they are already using
|
|
26
|
+
* Clopen on.
|
|
27
|
+
*/
|
|
28
|
+
export function requireHostAccess(conn: Parameters<typeof ws.getUserId>[0], hostId: string): void {
|
|
29
|
+
if (hostId === LOCAL_HOST_ID) return;
|
|
30
|
+
const userId = ws.getUserId(conn);
|
|
31
|
+
const isAdmin = ws.getRole(conn) === 'admin';
|
|
32
|
+
sshConnectionQueries.ensureAccess(hostId, userId, isAdmin);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const hostSchema = t.Object({ hostId: t.String({ minLength: 1 }) });
|
|
36
|
+
|
|
37
|
+
export const containersListHandler = createRouter()
|
|
38
|
+
.http('containers:watch', { data: hostSchema, response: t.Any() }, async ({ data, conn }) => {
|
|
39
|
+
requireHostAccess(conn, data.hostId);
|
|
40
|
+
const userId = ws.getUserId(conn);
|
|
41
|
+
|
|
42
|
+
const result = await containerMonitor.watch(data.hostId, userId);
|
|
43
|
+
// The watch belongs to this socket; a reload must not leave it running,
|
|
44
|
+
// and neither must the log streams opened from the same panel.
|
|
45
|
+
ws.addCleanup(conn, () => {
|
|
46
|
+
containerMonitor.unwatch(data.hostId, userId);
|
|
47
|
+
stopLogStreamsForUser(userId);
|
|
48
|
+
});
|
|
49
|
+
return { result };
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
.http('containers:unwatch', { data: hostSchema, response: t.Any() }, async ({ data, conn }) => {
|
|
53
|
+
containerMonitor.unwatch(data.hostId, ws.getUserId(conn));
|
|
54
|
+
return { ok: true };
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The sidebar count, on demand.
|
|
59
|
+
*
|
|
60
|
+
* Pushes only carry changes, so a client that has just loaded — or just
|
|
61
|
+
* reconnected — has no way to learn a count that has been steady. Asking for
|
|
62
|
+
* it is how the badge survives a page refresh instead of reading zero until
|
|
63
|
+
* something happens to move it.
|
|
64
|
+
*/
|
|
65
|
+
.http('containers:summary', { data: t.Object({}), response: t.Any() }, async () => ({
|
|
66
|
+
count: await containerMonitor.refreshBadge()
|
|
67
|
+
}))
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Everything `inspect` knows, read only when a detail pane opens.
|
|
71
|
+
*
|
|
72
|
+
* Environment values are stripped for anyone but an admin. A container's env
|
|
73
|
+
* is where its database password lives, and this panel is reachable by every
|
|
74
|
+
* member and through a shared Remote Access link — so the keys are shown,
|
|
75
|
+
* which is what makes the pane useful, and the values are not.
|
|
76
|
+
*/
|
|
77
|
+
.http(
|
|
78
|
+
'containers:inspect',
|
|
79
|
+
{
|
|
80
|
+
data: t.Object({
|
|
81
|
+
hostId: t.String({ minLength: 1 }),
|
|
82
|
+
containerId: t.String({ minLength: 12 })
|
|
83
|
+
}),
|
|
84
|
+
response: t.Any()
|
|
85
|
+
},
|
|
86
|
+
async ({ data, conn }) => {
|
|
87
|
+
requireHostAccess(conn, data.hostId);
|
|
88
|
+
const detail = await inspectContainer(data.hostId, data.containerId);
|
|
89
|
+
if (!detail || ws.getRole(conn) === 'admin') return { detail, envRedacted: false };
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
detail: {
|
|
93
|
+
...detail,
|
|
94
|
+
env: detail.env.map((line) => {
|
|
95
|
+
const split = line.indexOf('=');
|
|
96
|
+
return split <= 0 ? line : `${line.slice(0, split)}=`;
|
|
97
|
+
})
|
|
98
|
+
},
|
|
99
|
+
envRedacted: true
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* What the host is holding and how much of it is reclaimable.
|
|
106
|
+
*
|
|
107
|
+
* Read-only, so any member may ask — it is the same `system df` they would
|
|
108
|
+
* run in a terminal, and knowing the disk is full is not a privilege.
|
|
109
|
+
*
|
|
110
|
+
* Returns whatever was last measured, immediately, and reports whether a
|
|
111
|
+
* fresh reading is on its way; that one arrives as
|
|
112
|
+
* `containers:disk-usage-measured`. The measurement cannot be awaited here
|
|
113
|
+
* because its duration is set by how much disk the host holds — see
|
|
114
|
+
* `backend/containers/disk-usage.ts`.
|
|
115
|
+
*/
|
|
116
|
+
.http(
|
|
117
|
+
'containers:disk-usage',
|
|
118
|
+
{
|
|
119
|
+
data: t.Object({
|
|
120
|
+
hostId: t.String({ minLength: 1 }),
|
|
121
|
+
/** The Re-check button: measure again even if the cache is fresh. */
|
|
122
|
+
force: t.Optional(t.Boolean())
|
|
123
|
+
}),
|
|
124
|
+
response: t.Any()
|
|
125
|
+
},
|
|
126
|
+
async ({ data, conn }) => {
|
|
127
|
+
requireHostAccess(conn, data.hostId);
|
|
128
|
+
return requestDiskUsage(data.hostId, ws.getUserId(conn), data.force ?? false);
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
/** One live sample of what a container is consuming, when a pane asks. */
|
|
133
|
+
.http(
|
|
134
|
+
'containers:stats',
|
|
135
|
+
{
|
|
136
|
+
data: t.Object({
|
|
137
|
+
hostId: t.String({ minLength: 1 }),
|
|
138
|
+
containerId: t.String({ minLength: 12 })
|
|
139
|
+
}),
|
|
140
|
+
response: t.Any()
|
|
141
|
+
},
|
|
142
|
+
async ({ data, conn }) => {
|
|
143
|
+
requireHostAccess(conn, data.hostId);
|
|
144
|
+
return { stats: await readStats(data.hostId, data.containerId) };
|
|
145
|
+
}
|
|
146
|
+
);
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Containers — following a container's output.
|
|
3
|
+
*
|
|
4
|
+
* Open to any member who can reach the host: reading a log changes nothing.
|
|
5
|
+
* The stream belongs to the socket that asked for it, so closing the tab stops
|
|
6
|
+
* the `docker logs -f` behind it rather than leaving a process running on
|
|
7
|
+
* someone's machine with nothing listening.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { t } from 'elysia';
|
|
11
|
+
import { createRouter } from '$shared/utils/ws-server';
|
|
12
|
+
import { startLogStream, stopLogStream } from '../../containers/logs';
|
|
13
|
+
import { requireHostAccess } from './list';
|
|
14
|
+
import { ws } from '../../utils/ws';
|
|
15
|
+
|
|
16
|
+
export const containersLogsHandler = createRouter()
|
|
17
|
+
.http(
|
|
18
|
+
'containers:logs-start',
|
|
19
|
+
{
|
|
20
|
+
data: t.Object({
|
|
21
|
+
hostId: t.String({ minLength: 1 }),
|
|
22
|
+
containerId: t.String({ minLength: 12 })
|
|
23
|
+
}),
|
|
24
|
+
response: t.Any()
|
|
25
|
+
},
|
|
26
|
+
async ({ data, conn }) => {
|
|
27
|
+
requireHostAccess(conn, data.hostId);
|
|
28
|
+
const userId = ws.getUserId(conn);
|
|
29
|
+
|
|
30
|
+
const started = await startLogStream(data.hostId, data.containerId, userId);
|
|
31
|
+
ws.addCleanup(conn, () => stopLogStream(started.streamId, userId));
|
|
32
|
+
return started;
|
|
33
|
+
}
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
.http(
|
|
37
|
+
'containers:logs-stop',
|
|
38
|
+
{ data: t.Object({ streamId: t.String({ minLength: 1 }) }), response: t.Any() },
|
|
39
|
+
async ({ data, conn }) => {
|
|
40
|
+
stopLogStream(data.streamId, ws.getUserId(conn));
|
|
41
|
+
return { ok: true };
|
|
42
|
+
}
|
|
43
|
+
);
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Containers — the shell tunnel.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors backend/ws/ssh/tunnel.ts: PtyKit's wire protocol rides Clopen's
|
|
5
|
+
* app-wide socket on `containers:pty` / `containers:pty-out`, with one embedded
|
|
6
|
+
* PtyKit connection per Clopen connection.
|
|
7
|
+
*
|
|
8
|
+
* As with the SSH tunnel, the spawn target is stamped here rather than taken
|
|
9
|
+
* from the client. The namespace is the only thing the client says about what
|
|
10
|
+
* it wants, `authorize` has already checked that it may have it, and the target
|
|
11
|
+
* is derived from that same namespace — so naming one container in the
|
|
12
|
+
* namespace and another in the payload cannot open a shell anywhere.
|
|
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 { encodeContainerTarget } from '../../containers/pty-backend';
|
|
22
|
+
import { containerPtyKitServer, rememberNamespace, targetFromNamespace } from '../../containers/pty';
|
|
23
|
+
import { debug } from '$shared/utils/logger';
|
|
24
|
+
|
|
25
|
+
/** One embedded PtyKit connection per Clopen ws connection (by stable id). */
|
|
26
|
+
const containerPtyConnections = new Map<string, PtyKitConnection>();
|
|
27
|
+
|
|
28
|
+
function getContainerPtyConnection(conn: WSConnection): PtyKitConnection {
|
|
29
|
+
const connId = ws.getConnectionId(conn);
|
|
30
|
+
if (!connId) {
|
|
31
|
+
throw new Error('Connection not registered');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const existing = containerPtyConnections.get(connId);
|
|
35
|
+
if (existing) return existing;
|
|
36
|
+
|
|
37
|
+
const userId = ws.getUserId(conn);
|
|
38
|
+
const isAdmin = ws.getRole(conn) === 'admin';
|
|
39
|
+
const ptyConn = containerPtyKitServer.createConnection({
|
|
40
|
+
data: { userId, isAdmin, connId },
|
|
41
|
+
send: (frame) => {
|
|
42
|
+
ws.sendToConnectionId(connId, JSON.stringify({ action: 'containers:pty-out', payload: frame }));
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
containerPtyKitServer.handleOpen(ptyConn);
|
|
46
|
+
containerPtyConnections.set(connId, ptyConn);
|
|
47
|
+
|
|
48
|
+
ws.addCleanup(conn, () => {
|
|
49
|
+
containerPtyKitServer.handleClose(ptyConn);
|
|
50
|
+
containerPtyConnections.delete(connId);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
debug.log('containers', `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 target = targetFromNamespace(request.namespace);
|
|
69
|
+
if (!target) return frame;
|
|
70
|
+
rememberNamespace(request.namespace);
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
action: frame.action,
|
|
74
|
+
payload: { ...payload, data: { ...request, shell: encodeContainerTarget(target) } }
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const containersTunnelHandler = createRouter()
|
|
79
|
+
// Client → server: one PtyKit wire frame for a container shell.
|
|
80
|
+
.on(
|
|
81
|
+
'containers:pty',
|
|
82
|
+
{
|
|
83
|
+
data: t.Object({
|
|
84
|
+
action: t.String(),
|
|
85
|
+
payload: t.Any()
|
|
86
|
+
})
|
|
87
|
+
},
|
|
88
|
+
({ conn, data }) => {
|
|
89
|
+
const ptyConn = getContainerPtyConnection(conn);
|
|
90
|
+
void containerPtyKitServer.handleFrame(ptyConn, stampSpawnTarget(data as WireFrame));
|
|
91
|
+
}
|
|
92
|
+
)
|
|
93
|
+
// Server → client: PtyKit wire frames. Declared for the typed WSAPI; actually
|
|
94
|
+
// sent via ws.sendToConnectionId above.
|
|
95
|
+
.emit(
|
|
96
|
+
'containers:pty-out',
|
|
97
|
+
t.Object({
|
|
98
|
+
action: t.String(),
|
|
99
|
+
payload: t.Any()
|
|
100
|
+
})
|
|
101
|
+
);
|
package/backend/ws/git/log.ts
CHANGED
|
@@ -32,6 +32,7 @@ export const logHandler = createRouter()
|
|
|
32
32
|
limit: t.Optional(t.Number()),
|
|
33
33
|
skip: t.Optional(t.Number()),
|
|
34
34
|
branch: t.Optional(t.String()),
|
|
35
|
+
allBranches: t.Optional(t.Boolean()),
|
|
35
36
|
repoPath: t.Optional(t.String())
|
|
36
37
|
}),
|
|
37
38
|
response: t.Object({
|
|
@@ -55,6 +56,7 @@ export const logHandler = createRouter()
|
|
|
55
56
|
cwd,
|
|
56
57
|
data.limit ?? 50,
|
|
57
58
|
data.skip ?? 0,
|
|
58
|
-
data.branch
|
|
59
|
+
data.branch,
|
|
60
|
+
data.allBranches ?? false
|
|
59
61
|
);
|
|
60
62
|
});
|
package/backend/ws/git/status.ts
CHANGED
|
@@ -55,15 +55,18 @@ export const statusHandler = createRouter()
|
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
// Fetch outer status and nested repo list in parallel — the walk does
|
|
59
|
+
// not depend on the status result, and on Windows the walk (readdir +
|
|
60
|
+
// check-ignore spawns) is the dominant cost. Sharing the walk via the
|
|
61
|
+
// findNestedRepoPaths cache + inflight dedup also keeps concurrent
|
|
62
|
+
// callers (status + branches triggered together) from walking twice.
|
|
63
|
+
const [status, nestedRepoPaths] = await Promise.all([
|
|
64
|
+
gitService.getStatus(project.path),
|
|
65
|
+
// Discovery failing must not cost the user the outer repo's status.
|
|
66
|
+
findNestedRepoPaths(project.path).catch(() => [] as string[])
|
|
67
|
+
]);
|
|
59
68
|
|
|
60
|
-
|
|
61
|
-
// project (e.g. a theme extracted into its own repo, listed in the
|
|
62
|
-
// parent's .gitignore). The outer repo's `git status` skips them
|
|
63
|
-
// entirely, so without this aggregation the Changes tab would not
|
|
64
|
-
// show edits the AI made inside the nested repo.
|
|
65
|
-
try {
|
|
66
|
-
const nestedRepoPaths = await findNestedRepoPaths(project.path);
|
|
69
|
+
if (nestedRepoPaths.length > 0) {
|
|
67
70
|
const nestedPrefixes = nestedRepoPaths.map(
|
|
68
71
|
(repoPath) => pathRelative(project.path, repoPath).replace(/\\/g, '/') + '/'
|
|
69
72
|
);
|
|
@@ -84,26 +87,33 @@ export const statusHandler = createRouter()
|
|
|
84
87
|
status.conflicted = status.conflicted.filter((f) => !underNested(f));
|
|
85
88
|
}
|
|
86
89
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
90
|
+
// Fetch all nested statuses in parallel instead of sequentially.
|
|
91
|
+
// Each `git status` is a separate spawn; on Windows Defender scans
|
|
92
|
+
// .git/index per call, so sequential loops multiplied the wait.
|
|
93
|
+
const nestedStatuses = await Promise.all(
|
|
94
|
+
nestedRepoPaths.map(async (repoPath) => {
|
|
95
|
+
try {
|
|
96
|
+
const nestedStatus = await gitService.getStatus(repoPath);
|
|
97
|
+
return { repoPath, nestedStatus };
|
|
98
|
+
} catch {
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
for (const entry of nestedStatuses) {
|
|
105
|
+
if (!entry) continue;
|
|
106
|
+
const prefix = pathRelative(project.path, entry.repoPath).replace(/\\/g, '/') + '/';
|
|
107
|
+
const withPrefix = (f: typeof entry.nestedStatus.staged[number]) => ({
|
|
108
|
+
...f,
|
|
109
|
+
path: prefix + f.path,
|
|
110
|
+
oldPath: f.oldPath ? prefix + f.oldPath : undefined
|
|
111
|
+
});
|
|
112
|
+
status.staged.push(...entry.nestedStatus.staged.map(withPrefix));
|
|
113
|
+
status.unstaged.push(...entry.nestedStatus.unstaged.map(withPrefix));
|
|
114
|
+
status.untracked.push(...entry.nestedStatus.untracked.map(withPrefix));
|
|
115
|
+
status.conflicted.push(...entry.nestedStatus.conflicted.map(withPrefix));
|
|
104
116
|
}
|
|
105
|
-
} catch {
|
|
106
|
-
// If findNestedRepoPaths itself fails, just return outer status
|
|
107
117
|
}
|
|
108
118
|
|
|
109
119
|
return { isRepo: true, ...status };
|
package/backend/ws/index.ts
CHANGED
|
@@ -32,6 +32,8 @@ import { engineRouter } from './engine';
|
|
|
32
32
|
import { stackRouter } from './stack';
|
|
33
33
|
import { dbClientRouter } from './db-client';
|
|
34
34
|
import { sshRouter } from './ssh';
|
|
35
|
+
import { portsRouter } from './ports';
|
|
36
|
+
import { containersRouter } from './containers';
|
|
35
37
|
import { mcpRouter } from './mcp';
|
|
36
38
|
import { skillsRouter } from './skills';
|
|
37
39
|
import { commandsRouter } from './commands';
|
|
@@ -87,6 +89,8 @@ export const wsRouter = createRouter()
|
|
|
87
89
|
|
|
88
90
|
// SSH client (terminal, SFTP, port forwarding)
|
|
89
91
|
.merge(sshRouter)
|
|
92
|
+
.merge(portsRouter)
|
|
93
|
+
.merge(containersRouter)
|
|
90
94
|
|
|
91
95
|
// External MCP server management (install from the official registry)
|
|
92
96
|
.merge(mcpRouter)
|
|
@@ -56,6 +56,51 @@ describe('MessageRateLimiter', () => {
|
|
|
56
56
|
expect(closeCalls.length).toBe(0);
|
|
57
57
|
});
|
|
58
58
|
|
|
59
|
+
test('throttling never drops a request somebody is waiting on', () => {
|
|
60
|
+
const limiter = new MessageRateLimiter();
|
|
61
|
+
const { conn, closeCalls } = createMockConnection();
|
|
62
|
+
|
|
63
|
+
// Push the connection into the throttled state with event traffic — the
|
|
64
|
+
// shape of a preview stream or a terminal, which is what actually fills
|
|
65
|
+
// this window on a busy instance.
|
|
66
|
+
for (let i = 0; i < 99; i++) {
|
|
67
|
+
expect(limiter.checkRateLimit(conn, 'preview:browser-interaction')).toBe(true);
|
|
68
|
+
}
|
|
69
|
+
expect(limiter.checkRateLimit(conn, 'preview:browser-interaction')).toBe(false);
|
|
70
|
+
expect(limiter.getConnectionStats(conn)?.isThrottled).toBe(true);
|
|
71
|
+
|
|
72
|
+
// A request-response call arriving in the same window still gets through.
|
|
73
|
+
// Shedding it used to hang its caller for a full timeout, which the client
|
|
74
|
+
// then "healed" by tearing down the socket — a far bigger burst than the
|
|
75
|
+
// one being throttled, and the reason the overload sustained itself.
|
|
76
|
+
expect(limiter.checkRateLimit(conn, 'files:list-tree', true)).toBe(true);
|
|
77
|
+
expect(limiter.checkRateLimit(conn, 'git:status', true)).toBe(true);
|
|
78
|
+
|
|
79
|
+
// Only the events were counted as dropped.
|
|
80
|
+
expect(limiter.getConnectionStats(conn)?.messagesDropped).toBe(1);
|
|
81
|
+
expect(closeCalls.length).toBe(0);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test('a request is still refused once the connection is closed for abuse', () => {
|
|
85
|
+
const limiter = new MessageRateLimiter({
|
|
86
|
+
warningThreshold: 50,
|
|
87
|
+
throttleThreshold: 100,
|
|
88
|
+
disconnectThreshold: 200,
|
|
89
|
+
windowMs: 1000
|
|
90
|
+
});
|
|
91
|
+
const { conn, closeCalls } = createMockConnection();
|
|
92
|
+
|
|
93
|
+
for (let i = 0; i < 199; i++) {
|
|
94
|
+
limiter.checkRateLimit(conn, 'files:list-tree', true);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Requests are exempt from THROTTLING, not from the abuse ceiling: past the
|
|
98
|
+
// disconnect threshold the socket is gone, so there is nothing to protect.
|
|
99
|
+
expect(limiter.checkRateLimit(conn, 'files:list-tree', true)).toBe(false);
|
|
100
|
+
expect(limiter.getConnectionStats(conn)?.isFlagged).toBe(true);
|
|
101
|
+
expect(closeCalls.length).toBe(1);
|
|
102
|
+
});
|
|
103
|
+
|
|
59
104
|
test('drops and closes connection at disconnect threshold', () => {
|
|
60
105
|
const limiter = new MessageRateLimiter({
|
|
61
106
|
warningThreshold: 50,
|