@myrialabs/clopen 0.4.33 → 0.4.34
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 +3 -0
- package/backend/auth/rate-limiter.test.ts +115 -0
- package/backend/auth/rate-limiter.ts +25 -12
- package/backend/db-client/connection-manager.ts +13 -0
- package/backend/index.ts +9 -0
- 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/runner.ts +195 -0
- package/backend/ports/scan-host.ts +260 -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/ssh/forwards.ts +31 -0
- package/backend/ws/auth/device.ts +1 -1
- package/backend/ws/auth/login.ts +8 -4
- package/backend/ws/index.ts +2 -0
- 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/dist/assets/{index-BW0DA8NV.js → index-gGTMFUa8.js} +1 -1
- package/dist/assets/{index-BF4buYmI.js → index-vW2RpdHu.js} +1 -1
- package/dist/assets/main-CYCMEbXB.css +1 -0
- package/dist/assets/{main-BbwWLJRN.js → main-ZCojSzvW.js} +409 -406
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/ports/index.ts +5 -0
- package/shared/types/ports/port.ts +192 -0
- package/shared/utils/logger.ts +1 -0
- package/dist/assets/main-CCvRMXlP.css +0 -1
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port manager — one command interface, two transports.
|
|
3
|
+
*
|
|
4
|
+
* Every probe in this feature is a small POSIX/Windows command, and the same
|
|
5
|
+
* probe has to run on the machine hosting Clopen and on a saved SSH host. So
|
|
6
|
+
* the probes are written once against `CommandRunner` and the transport is
|
|
7
|
+
* swapped underneath: `Bun.spawn` locally, an exec channel remotely.
|
|
8
|
+
*
|
|
9
|
+
* Commands are argv arrays rather than shell strings. Locally that removes
|
|
10
|
+
* quoting from the picture entirely; remotely each argument is quoted by
|
|
11
|
+
* `shellQuote` on the way out, so a hostile process name in an argument can
|
|
12
|
+
* never turn into a second command.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { Client as SshClient } from 'ssh2';
|
|
16
|
+
import { runCommandDetailed, shellQuote } from '../ssh/connect';
|
|
17
|
+
import { sshClientPool } from '../ssh/client-pool';
|
|
18
|
+
import { debug } from '$shared/utils/logger';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The directories a probe might live in, prepended for every POSIX command.
|
|
22
|
+
*
|
|
23
|
+
* A non-interactive SSH exec channel gets whatever PATH the account's shell
|
|
24
|
+
* sets up, and on shared hosting that routinely omits `/usr/sbin` and `/sbin`
|
|
25
|
+
* — which is exactly where `ss`, `netstat` and `lsof` are installed. Without
|
|
26
|
+
* this, a host with every tool present still reports having none.
|
|
27
|
+
*/
|
|
28
|
+
const PROBE_PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/homebrew/bin';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Wrap a POSIX command so it runs with a predictable PATH and locale.
|
|
32
|
+
*
|
|
33
|
+
* Going through `env` rather than a shell keeps the no-shell rule intact —
|
|
34
|
+
* arguments stay arguments — while fixing both problems a bare command has on
|
|
35
|
+
* a remote host: a PATH too narrow to find the tool, and a locale that renders
|
|
36
|
+
* dates in words nothing can parse.
|
|
37
|
+
*/
|
|
38
|
+
export function posixArgv(argv: string[]): string[] {
|
|
39
|
+
return ['env', 'LC_ALL=C', `PATH=${PROBE_PATH}`, ...argv];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface RunResult {
|
|
43
|
+
stdout: string;
|
|
44
|
+
stderr: string;
|
|
45
|
+
code: number;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Platforms the probes know how to interrogate. */
|
|
49
|
+
export type ProbePlatform = 'darwin' | 'linux' | 'win32' | 'unknown';
|
|
50
|
+
|
|
51
|
+
export interface CommandRunner {
|
|
52
|
+
/** Human label for logs and errors ("this machine", the host name). */
|
|
53
|
+
readonly label: string;
|
|
54
|
+
run(argv: string[], timeoutMs?: number): Promise<RunResult>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const DEFAULT_TIMEOUT_MS = 15_000;
|
|
58
|
+
|
|
59
|
+
/** Runs probes on the machine Clopen itself is running on. */
|
|
60
|
+
export class LocalCommandRunner implements CommandRunner {
|
|
61
|
+
readonly label = 'this machine';
|
|
62
|
+
|
|
63
|
+
async run(argv: string[], timeoutMs = DEFAULT_TIMEOUT_MS): Promise<RunResult> {
|
|
64
|
+
const child = Bun.spawn(argv, { stdout: 'pipe', stderr: 'pipe', stdin: 'ignore' });
|
|
65
|
+
// A probe that hangs must not wedge the poll loop; kill it and report
|
|
66
|
+
// the timeout as a failed command so the caller can fall back.
|
|
67
|
+
const timer = setTimeout(() => {
|
|
68
|
+
try {
|
|
69
|
+
child.kill();
|
|
70
|
+
} catch {
|
|
71
|
+
// Already gone — nothing to do.
|
|
72
|
+
}
|
|
73
|
+
}, timeoutMs);
|
|
74
|
+
try {
|
|
75
|
+
const [stdout, stderr, code] = await Promise.all([
|
|
76
|
+
new Response(child.stdout).text(),
|
|
77
|
+
new Response(child.stderr).text(),
|
|
78
|
+
child.exited
|
|
79
|
+
]);
|
|
80
|
+
return { stdout, stderr, code };
|
|
81
|
+
} finally {
|
|
82
|
+
clearTimeout(timer);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Runs probes on a saved SSH host over the shared pooled transport, so the
|
|
89
|
+
* scan reuses whatever connection the terminal and file browser already hold
|
|
90
|
+
* open instead of dialing its own.
|
|
91
|
+
*/
|
|
92
|
+
export class SshCommandRunner implements CommandRunner {
|
|
93
|
+
/**
|
|
94
|
+
* Commands run one at a time on a connection.
|
|
95
|
+
*
|
|
96
|
+
* Every `exec` opens an SSH channel, and sshd caps how many a connection may
|
|
97
|
+
* hold at once — `MaxSessions`, ten by default. A scan issues several
|
|
98
|
+
* commands, and the terminal, SFTP browser and forwards share the same
|
|
99
|
+
* transport, so letting them overlap exhausts that budget and the server
|
|
100
|
+
* answers `Unable to exec` before dropping the connection outright. Queueing
|
|
101
|
+
* costs a little latency and removes the ceiling entirely.
|
|
102
|
+
*/
|
|
103
|
+
private queue: Promise<unknown> = Promise.resolve();
|
|
104
|
+
|
|
105
|
+
constructor(
|
|
106
|
+
readonly label: string,
|
|
107
|
+
private readonly client: SshClient
|
|
108
|
+
) {}
|
|
109
|
+
|
|
110
|
+
async run(argv: string[], timeoutMs = DEFAULT_TIMEOUT_MS): Promise<RunResult> {
|
|
111
|
+
const command = argv.map(shellQuote).join(' ');
|
|
112
|
+
// Chained on the tail whether or not the previous command succeeded, so
|
|
113
|
+
// one failure cannot wedge every command behind it.
|
|
114
|
+
const result = this.queue.then(
|
|
115
|
+
() => runCommandDetailed(this.client, command, timeoutMs),
|
|
116
|
+
() => runCommandDetailed(this.client, command, timeoutMs)
|
|
117
|
+
);
|
|
118
|
+
this.queue = result.catch(() => undefined);
|
|
119
|
+
|
|
120
|
+
const { stdout, stderr, code } = await result;
|
|
121
|
+
return { stdout, stderr, code };
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Borrow a pooled SSH transport for the duration of `fn`. */
|
|
126
|
+
export async function withSshRunner<T>(
|
|
127
|
+
connectionId: string,
|
|
128
|
+
label: string,
|
|
129
|
+
fn: (runner: CommandRunner) => Promise<T>
|
|
130
|
+
): Promise<T> {
|
|
131
|
+
const lease = await sshClientPool.acquire(connectionId);
|
|
132
|
+
try {
|
|
133
|
+
return await fn(new SshCommandRunner(label, lease.client));
|
|
134
|
+
} finally {
|
|
135
|
+
lease.release();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Which OS a runner is talking to. Local is known outright; a remote host is
|
|
141
|
+
* asked once and remembered, because `uname` per scan tick is a round-trip
|
|
142
|
+
* spent on an answer that cannot change while the connection lives.
|
|
143
|
+
*/
|
|
144
|
+
const remotePlatforms = new Map<string, ProbePlatform>();
|
|
145
|
+
|
|
146
|
+
export function localPlatform(): ProbePlatform {
|
|
147
|
+
switch (process.platform) {
|
|
148
|
+
case 'darwin':
|
|
149
|
+
return 'darwin';
|
|
150
|
+
case 'linux':
|
|
151
|
+
return 'linux';
|
|
152
|
+
case 'win32':
|
|
153
|
+
return 'win32';
|
|
154
|
+
default:
|
|
155
|
+
// The BSDs answer the Linux probes closely enough to be worth trying.
|
|
156
|
+
return process.platform === 'freebsd' || process.platform === 'openbsd' ? 'linux' : 'unknown';
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export async function detectRemotePlatform(cacheKey: string, runner: CommandRunner): Promise<ProbePlatform> {
|
|
161
|
+
const cached = remotePlatforms.get(cacheKey);
|
|
162
|
+
if (cached) return cached;
|
|
163
|
+
|
|
164
|
+
let platform: ProbePlatform = 'unknown';
|
|
165
|
+
try {
|
|
166
|
+
const result = await runner.run(['uname', '-s'], 5_000);
|
|
167
|
+
const name = result.stdout.trim().toLowerCase();
|
|
168
|
+
if (result.code === 0 && name) {
|
|
169
|
+
if (name.includes('darwin')) platform = 'darwin';
|
|
170
|
+
else if (name.includes('linux')) platform = 'linux';
|
|
171
|
+
else if (name.includes('bsd')) platform = 'linux';
|
|
172
|
+
}
|
|
173
|
+
} catch (error) {
|
|
174
|
+
debug.log('ports', `uname failed on ${runner.label}:`, error);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// No `uname` usually means a Windows host answering over OpenSSH, where the
|
|
178
|
+
// shell is cmd and `ver` is the equivalent question.
|
|
179
|
+
if (platform === 'unknown') {
|
|
180
|
+
try {
|
|
181
|
+
const result = await runner.run(['cmd', '/c', 'ver'], 5_000);
|
|
182
|
+
if (result.code === 0 && /windows/i.test(result.stdout)) platform = 'win32';
|
|
183
|
+
} catch {
|
|
184
|
+
// Leave it unknown — the caller reports that honestly.
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
remotePlatforms.set(cacheKey, platform);
|
|
189
|
+
return platform;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Drop a host's cached platform when its connection goes away. */
|
|
193
|
+
export function forgetRemotePlatform(cacheKey: string): void {
|
|
194
|
+
remotePlatforms.delete(cacheKey);
|
|
195
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Port manager — one host, one scan, one table.
|
|
3
|
+
*
|
|
4
|
+
* Ties the probe, the process table and the attribution together into the rows
|
|
5
|
+
* the panel renders. Held per host so the process cache survives between ticks,
|
|
6
|
+
* which is what makes a one-second poll cost a single cheap probe.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import type {
|
|
11
|
+
PortEntry,
|
|
12
|
+
PortKillBlockedReason,
|
|
13
|
+
PortLimitation,
|
|
14
|
+
PortPeer,
|
|
15
|
+
PortProtocol,
|
|
16
|
+
PortScanResult,
|
|
17
|
+
PortSocket
|
|
18
|
+
} from '$shared/types/ports';
|
|
19
|
+
import { LOCAL_PORT_HOST } from '$shared/types/ports';
|
|
20
|
+
import { scanSockets, type ProbeMemo } from './scan';
|
|
21
|
+
import { ProcessTable } from './processes';
|
|
22
|
+
import { attribute, buildContext, groupListenerPids } from './attribute';
|
|
23
|
+
import { collectExposedPorts } from './registry';
|
|
24
|
+
import { resolveClopenPids } from './ssh-lineage';
|
|
25
|
+
import type { CommandRunner, ProbePlatform } from './runner';
|
|
26
|
+
|
|
27
|
+
/** Who the scan is running as, which decides what it may signal. */
|
|
28
|
+
export interface ScanPrincipal {
|
|
29
|
+
user: string | null;
|
|
30
|
+
isRoot: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function localPrincipal(): ScanPrincipal {
|
|
34
|
+
let user: string | null = null;
|
|
35
|
+
try {
|
|
36
|
+
user = os.userInfo().username;
|
|
37
|
+
} catch {
|
|
38
|
+
// Some container images have no passwd entry for the running uid.
|
|
39
|
+
user = null;
|
|
40
|
+
}
|
|
41
|
+
const uid = typeof process.getuid === 'function' ? process.getuid() : null;
|
|
42
|
+
// Windows has no uid; an Administrator there still fails to signal some
|
|
43
|
+
// processes, so the kill attempt reports the truth rather than predicting it.
|
|
44
|
+
return { user, isRoot: uid === 0 };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const NO_CLOPEN_LINEAGE: PortLimitation = {
|
|
48
|
+
code: 'no-session-attribution',
|
|
49
|
+
message:
|
|
50
|
+
'Nothing listening here belongs to Clopen’s own SSH connection. If you started a server from a Clopen terminal on this host and it is not listed under “Started from Clopen”, this host does not let Clopen read a process’s environment.'
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function protocolPortKey(protocol: PortProtocol, port: number): string {
|
|
54
|
+
return `${protocol}:${port}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Peers connected to each listening port, keyed `protocol:port`. */
|
|
58
|
+
function collectPeers(sockets: PortSocket[]): Map<string, PortPeer[]> {
|
|
59
|
+
const peers = new Map<string, PortPeer[]>();
|
|
60
|
+
|
|
61
|
+
for (const socket of sockets) {
|
|
62
|
+
if (socket.state !== 'established' || socket.peerAddress === null || socket.peerPort === null) continue;
|
|
63
|
+
const key = protocolPortKey(socket.protocol, socket.port);
|
|
64
|
+
const list = peers.get(key);
|
|
65
|
+
const peer: PortPeer = {
|
|
66
|
+
address: socket.peerAddress,
|
|
67
|
+
port: socket.peerPort,
|
|
68
|
+
processName: socket.processName
|
|
69
|
+
};
|
|
70
|
+
if (list) list.push(peer);
|
|
71
|
+
else peers.set(key, [peer]);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return peers;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function unique<T>(values: T[]): T[] {
|
|
78
|
+
return [...new Set(values)];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* One host's scanner. Construct once per watched host so the process table and
|
|
83
|
+
* its cwd cache persist; drop it when the host stops being watched.
|
|
84
|
+
*/
|
|
85
|
+
export class HostPortScanner {
|
|
86
|
+
private readonly processes: ProcessTable;
|
|
87
|
+
/** Which probe answered for this host; the chain is walked once. */
|
|
88
|
+
private readonly probeMemo: ProbeMemo = {};
|
|
89
|
+
/**
|
|
90
|
+
* A scan already running. Callers join it instead of starting a second one:
|
|
91
|
+
* a remote scan can outlast the poll interval, and two in flight would double
|
|
92
|
+
* the SSH channels while writing to the same process cache.
|
|
93
|
+
*/
|
|
94
|
+
private inFlight: Promise<PortScanResult> | null = null;
|
|
95
|
+
|
|
96
|
+
constructor(
|
|
97
|
+
private readonly hostId: string,
|
|
98
|
+
private readonly hostName: string,
|
|
99
|
+
private readonly runner: CommandRunner,
|
|
100
|
+
private readonly platform: ProbePlatform,
|
|
101
|
+
private readonly principal: ScanPrincipal
|
|
102
|
+
) {
|
|
103
|
+
this.processes = new ProcessTable(runner, platform);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private get isLocal(): boolean {
|
|
107
|
+
return this.hostId === LOCAL_PORT_HOST;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Whether this scan could plausibly signal a process. Ownership is the only
|
|
112
|
+
* thing knowable in advance; anything subtler is left to the kill attempt,
|
|
113
|
+
* which reports what the OS actually said.
|
|
114
|
+
*/
|
|
115
|
+
private killability(
|
|
116
|
+
pid: number | null,
|
|
117
|
+
ownerUser: string | null,
|
|
118
|
+
isClopenItself: boolean
|
|
119
|
+
): { canKill: boolean; reason: PortKillBlockedReason | null } {
|
|
120
|
+
if (isClopenItself) return { canKill: false, reason: 'is-clopen-itself' };
|
|
121
|
+
if (pid === null) return { canKill: false, reason: 'unknown-pid' };
|
|
122
|
+
if (pid <= 1) return { canKill: false, reason: 'system-process' };
|
|
123
|
+
if (this.principal.isRoot) return { canKill: true, reason: null };
|
|
124
|
+
if (ownerUser && this.principal.user && ownerUser !== this.principal.user) {
|
|
125
|
+
return { canKill: false, reason: 'not-permitted' };
|
|
126
|
+
}
|
|
127
|
+
return { canKill: true, reason: null };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Read this host, joining a scan already in progress rather than racing it. */
|
|
131
|
+
async scan(now = Date.now()): Promise<PortScanResult> {
|
|
132
|
+
if (this.inFlight) return this.inFlight;
|
|
133
|
+
this.inFlight = this.runScan(now).finally(() => {
|
|
134
|
+
this.inFlight = null;
|
|
135
|
+
});
|
|
136
|
+
return this.inFlight;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private async runScan(now: number): Promise<PortScanResult> {
|
|
140
|
+
const scannedAt = new Date(now).toISOString();
|
|
141
|
+
|
|
142
|
+
let sockets: PortSocket[];
|
|
143
|
+
let limitations: PortLimitation[];
|
|
144
|
+
let probe: string;
|
|
145
|
+
try {
|
|
146
|
+
const result = await scanSockets(this.runner, this.platform, this.probeMemo);
|
|
147
|
+
sockets = result.sockets;
|
|
148
|
+
limitations = result.limitations;
|
|
149
|
+
probe = result.probe;
|
|
150
|
+
} catch (error) {
|
|
151
|
+
return {
|
|
152
|
+
hostId: this.hostId,
|
|
153
|
+
scannedAt,
|
|
154
|
+
platform: this.platform,
|
|
155
|
+
probe: 'none',
|
|
156
|
+
entries: [],
|
|
157
|
+
limitations: [],
|
|
158
|
+
error: error instanceof Error ? error.message : String(error)
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const listening = sockets.filter((socket) => socket.state === 'listen');
|
|
163
|
+
const pids = unique(listening.map((socket) => socket.pid).filter((pid): pid is number => pid !== null));
|
|
164
|
+
|
|
165
|
+
const processes = await this.processes.refresh(pids, now);
|
|
166
|
+
limitations = [...limitations, ...this.processes.getLimitations()];
|
|
167
|
+
|
|
168
|
+
// On an SSH host, a process is Clopen's if it belongs to Clopen's own SSH
|
|
169
|
+
// connection — the same tier as locally, established from the far end.
|
|
170
|
+
const clopenPids = this.isLocal
|
|
171
|
+
? new Set<number>()
|
|
172
|
+
: await resolveClopenPids(this.hostId, this.runner, this.platform, pids, processes);
|
|
173
|
+
if (!this.isLocal && clopenPids.size === 0 && pids.length > 0) {
|
|
174
|
+
limitations.push(NO_CLOPEN_LINEAGE);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const context = buildContext({
|
|
178
|
+
platform: this.platform,
|
|
179
|
+
processes,
|
|
180
|
+
hostId: this.hostId,
|
|
181
|
+
hostName: this.hostName,
|
|
182
|
+
isLocal: this.isLocal,
|
|
183
|
+
clopenPids
|
|
184
|
+
});
|
|
185
|
+
const peers = collectPeers(sockets);
|
|
186
|
+
const exposed = this.isLocal ? collectExposedPorts() : new Map<number, string>();
|
|
187
|
+
|
|
188
|
+
// Bucket the listening sockets per protocol+port first; a port is the
|
|
189
|
+
// unit the user thinks in, and the addresses under it are detail.
|
|
190
|
+
const buckets = new Map<string, PortSocket[]>();
|
|
191
|
+
for (const socket of listening) {
|
|
192
|
+
const key = protocolPortKey(socket.protocol, socket.port);
|
|
193
|
+
const bucket = buckets.get(key);
|
|
194
|
+
if (bucket) bucket.push(socket);
|
|
195
|
+
else buckets.set(key, [socket]);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const entries: PortEntry[] = [];
|
|
199
|
+
|
|
200
|
+
for (const [key, bucket] of buckets) {
|
|
201
|
+
const { protocol, port } = bucket[0];
|
|
202
|
+
const bucketPids = unique(bucket.map((socket) => socket.pid).filter((pid): pid is number => pid !== null));
|
|
203
|
+
const groups = groupListenerPids(bucketPids, processes);
|
|
204
|
+
|
|
205
|
+
// Sockets whose owner the probe could not name still deserve a row —
|
|
206
|
+
// an unexplained open port is exactly what someone opens this for.
|
|
207
|
+
const orphans = bucket.filter((socket) => socket.pid === null);
|
|
208
|
+
if (orphans.length > 0) groups.push({ owner: -1, members: [] });
|
|
209
|
+
|
|
210
|
+
for (const { owner, members } of groups) {
|
|
211
|
+
const memberPids = new Set(members);
|
|
212
|
+
const groupSockets =
|
|
213
|
+
owner === -1
|
|
214
|
+
? orphans
|
|
215
|
+
: bucket.filter((socket) => socket.pid !== null && memberPids.has(socket.pid));
|
|
216
|
+
if (groupSockets.length === 0) continue;
|
|
217
|
+
|
|
218
|
+
const representative = groupSockets[0];
|
|
219
|
+
const pid = owner === -1 ? null : owner;
|
|
220
|
+
const process = pid !== null ? (processes.get(pid) ?? null) : null;
|
|
221
|
+
const origin = attribute({ ...representative, pid }, context);
|
|
222
|
+
const ownerUser = process?.user ?? representative.user;
|
|
223
|
+
const { canKill, reason } = this.killability(
|
|
224
|
+
pid,
|
|
225
|
+
ownerUser,
|
|
226
|
+
origin.ownerFeature === 'server'
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
entries.push({
|
|
230
|
+
key: `${this.hostId}:${key}:${pid ?? 'unknown'}`,
|
|
231
|
+
protocol,
|
|
232
|
+
port,
|
|
233
|
+
addresses: unique(groupSockets.map((socket) => socket.address)).sort(),
|
|
234
|
+
ipVersions: unique(groupSockets.map((socket) => socket.ipVersion)).sort(),
|
|
235
|
+
pid,
|
|
236
|
+
process,
|
|
237
|
+
workerPids: members,
|
|
238
|
+
origin,
|
|
239
|
+
peers: peers.get(key) ?? [],
|
|
240
|
+
peerCount: (peers.get(key) ?? []).length,
|
|
241
|
+
publicUrl: exposed.get(port) ?? null,
|
|
242
|
+
canKill,
|
|
243
|
+
killBlockedReason: reason
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
entries.sort((a, b) => a.port - b.port || a.protocol.localeCompare(b.protocol));
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
hostId: this.hostId,
|
|
252
|
+
scannedAt,
|
|
253
|
+
platform: this.platform,
|
|
254
|
+
probe,
|
|
255
|
+
entries,
|
|
256
|
+
limitations,
|
|
257
|
+
error: null
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
}
|