-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost-preflight.service.ts
More file actions
277 lines (238 loc) · 7.1 KB
/
host-preflight.service.ts
File metadata and controls
277 lines (238 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import { createServer } from 'node:net';
import process from 'node:process';
import { getRunningComposePublishedPorts } from './compose-project.service.js';
import type {
HostBindAddress,
HostPortCheckResult,
HostPortDefinition,
HostPortProbeResult
} from '../types/preflight.types.js';
import type { LabEnv, ProjectContext } from '../types/project.types.js';
import { runCommand } from '../utils/process.js';
const CORE_PORT_ENV_KEYS = [
'LAB_HTTPS_PORT',
'GITEA_HTTPS_PORT'
] as const;
const AI_LLM_PORT_ENV_KEYS = [
'OPENWEBUI_HTTPS_PORT',
'OLLAMA_HTTPS_PORT'
] as const;
const WORKBENCH_PORT_ENV_KEYS = [
'POSTGRES_DEV_HOST_PORT',
'NODE_DEV_HTTPS_PORT',
'PYTHON_DEV_HTTPS_PORT'
] as const;
const BIND_ADDRESSES: readonly HostBindAddress[] = ['0.0.0.0', '::'] as const;
/**
* Fails fast when one or more published host ports are already occupied.
*/
export async function assertPublishedPortsAvailable(
context: ProjectContext,
options: {
includeAiLlm: boolean;
includeWorkbench: boolean;
}
): Promise<void> {
const definitions = getConfiguredHostPorts(
context.env,
options.includeWorkbench,
options.includeAiLlm
);
const currentProjectPorts = await getRunningComposePublishedPorts(context);
const results = await Promise.all(
definitions.map((definition) => checkHostPort(definition, currentProjectPorts))
);
const unavailablePorts = results.filter((result) => !result.available);
if (unavailablePorts.length === 0) {
return;
}
const details = unavailablePorts
.map((result) => `${result.port} (${result.envKey}) ${result.detail}`)
.join('; ');
throw new Error(`Host port preflight failed: ${details}`);
}
/**
* Resolves the list of configured public ports that Compose will publish on the host.
*/
function getConfiguredHostPorts(
env: LabEnv,
includeWorkbench: boolean,
includeAiLlm: boolean
): HostPortDefinition[] {
const envKeys = [
...CORE_PORT_ENV_KEYS,
...(includeAiLlm ? [...AI_LLM_PORT_ENV_KEYS] : []),
...(includeWorkbench ? [...WORKBENCH_PORT_ENV_KEYS] : [])
];
return envKeys.map((envKey) => ({
envKey,
port: parseRequiredPort(env, envKey)
}));
}
/**
* Parses a required TCP port from the env and rejects invalid values early.
*/
function parseRequiredPort(env: LabEnv, envKey: string): number {
const rawValue = env[envKey];
const port = Number.parseInt(rawValue ?? '', 10);
if (!Number.isInteger(port) || port < 1 || port > 65_535) {
throw new Error(`Invalid host port configuration for ${envKey}: ${rawValue ?? '<missing>'}`);
}
return port;
}
/**
* Checks whether a TCP port can be bound on the local machine for both IPv4 and IPv6 wildcards.
*/
async function checkHostPort(
definition: HostPortDefinition,
currentProjectPorts: ReadonlySet<number>
): Promise<HostPortCheckResult> {
const probeResults: HostPortProbeResult[] = [];
for (const host of BIND_ADDRESSES) {
probeResults.push(await probeBindAddress(definition.port, host));
}
const blockingProbe = probeResults.find((result) => result.supported && !result.available);
if (blockingProbe) {
if (currentProjectPorts.has(definition.port)) {
return {
...definition,
available: true,
detail: 'already published by the current Atlas Lab stack'
};
}
const detail = await enrichPortFailureDetail(definition.port, blockingProbe.detail);
return {
...definition,
available: false,
detail: `${detail} (bind ${blockingProbe.host})`
};
}
return {
...definition,
available: true,
detail: 'available'
};
}
/**
* Probes a specific bind address so Windows wildcard listeners are detected before Compose starts.
*/
async function probeBindAddress(port: number, host: HostBindAddress): Promise<HostPortProbeResult> {
return new Promise((resolve) => {
const server = createServer();
server.once('error', (error) => {
if (isUnsupportedBindAddress(error)) {
resolve({
host,
available: true,
supported: false,
detail: 'unsupported'
});
return;
}
resolve({
host,
available: false,
supported: true,
detail: formatPortError(error)
});
});
server.listen(
{
host,
port,
exclusive: true
},
() => {
server.close(() => {
resolve({
host,
available: true,
supported: true,
detail: 'available'
});
});
}
);
});
}
/**
* Normalizes socket bind errors into short CLI-friendly details.
*/
function formatPortError(error: unknown): string {
if (isNodeError(error) && error.code === 'EADDRINUSE') {
return 'is already allocated on the host';
}
if (isNodeError(error) && error.code === 'EACCES') {
return 'is unavailable on the host';
}
return error instanceof Error ? error.message : 'is unavailable';
}
/**
* Ignores bind-address families that are not available on the current host.
*/
function isUnsupportedBindAddress(error: unknown): boolean {
return isNodeError(error) && ['EAFNOSUPPORT', 'EINVAL'].includes(error.code ?? '');
}
/**
* Narrows unknown values to Node.js-style system errors.
*/
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
return typeof error === 'object' && error !== null && 'code' in error;
}
/**
* Adds platform-specific detail for port probe failures.
*/
async function enrichPortFailureDetail(port: number, detail: string): Promise<string> {
if (process.platform !== 'win32' || detail !== 'is unavailable on the host') {
return detail;
}
const windowsRange = await findWindowsExcludedPortRange(port);
if (!windowsRange) {
return `${detail} (permission denied)`;
}
return `is reserved by Windows excluded port range ${windowsRange.start}-${windowsRange.end}`;
}
/**
* Queries Windows reserved TCP port ranges and returns the matching range when present.
*/
async function findWindowsExcludedPortRange(
port: number
): Promise<{ start: number; end: number } | null> {
for (const family of ['ipv4', 'ipv6'] as const) {
const result = await runCommand('netsh', ['interface', family, 'show', 'excludedportrange', 'protocol=tcp'], {
allowFailure: true,
captureOutput: true,
scope: 'host'
});
if (result.exitCode !== 0) {
continue;
}
const ranges = parseWindowsExcludedPortRanges(result.stdout);
const matchingRange = ranges.find((range) => port >= range.start && port <= range.end);
if (matchingRange) {
return matchingRange;
}
}
return null;
}
/**
* Parses the `netsh interface ... show excludedportrange` output.
*/
function parseWindowsExcludedPortRanges(output: string): Array<{ start: number; end: number }> {
return output
.replace(/\r\n/gu, '\n')
.split('\n')
.map((line) => line.trim())
.flatMap((line) => {
const match = line.match(/^(\d+)\s+(\d+)(?:\s+\*)?$/u);
if (!match) {
return [];
}
return [
{
start: Number.parseInt(match[1], 10),
end: Number.parseInt(match[2], 10)
}
];
});
}