-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
408 lines (355 loc) · 11.8 KB
/
index.js
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
const fs = require('fs');
const http = require('http');
const net = require('net');
const path = require('path');
const systemdSocket = require('systemd-socket');
const url = require('url');
const { execFileSync } = require('child_process');
const { program } = require('commander');
const defaultUpstreamSocketPath = '/mnt/wsl/podman-sockets/podman-machine-default/podman-root.sock';
const defaultDownstreamSocketPath = '/run/podman/podman.sock';
program
.name('podman-wsl-service')
.option('-l, --log-level <level>', 'Set the log level (debug, info, error)', 'info')
.option('-u, --upstream-socket <path>', 'The path to the upstream podman socket', defaultUpstreamSocketPath)
.option('-d, --downstream-socket <path>', 'The path to the downstream podman socket', defaultDownstreamSocketPath)
.option('-n, --wsl-distro-name <name>', 'The name of the WSL distro (default: autodetect)', '')
.option('-M, --no-mount-distro-root', 'Do not mount the distro root')
.option(
'-t, --shutdown-timeout <timeout>',
'Time in seconds after which the proxy will shut down if no connections are active (-1 to disable, default: disabled)',
'-1'
)
.parse(process.argv);
const options = program.opts();
const logLevel = options.logLevel;
const upstreamSocketPath = options.upstreamSocket;
const downstreamSocketPath = options.downstreamSocket;
const wslDistroName = options.wslDistroName;
const mountDistroRoot = options.mountDistroRoot;
const shutdownTimeout = parseInt(options.shutdownTimeout);
const sharedRoot = getSharedMountpoint(wslDistroName || getWslDistroName());
const systemdSocketFd = systemdSocket();
const logLevels = ['debug', 'info', 'error'];
if (!logLevels.includes(logLevel)) {
console.error(`Invalid log level: ${logLevel}`);
process.exit(1);
}
console.log = (msg) => (logLevel === 'info' || logLevel === 'debug' ? console.info(msg) : () => {});
console.debug = (msg) => (logLevel === 'debug' ? console.info(msg) : () => {});
console.debug('Options:');
console.debug(`- Log level: ${logLevel}`);
console.debug(`- Upstream socket: ${upstreamSocketPath}`);
console.debug(`- Downstream socket: ${systemdSocketFd && systemdSocketFd.fd ? 'systemd' : downstreamSocketPath}`);
console.debug(`- WSL distro name: ${wslDistroName || 'autodetect'}`);
console.debug(`- Mount distro root: ${!mountDistroRoot}`);
console.debug(`- Shutdown timeout: ${shutdownTimeout < 0 ? 'disabled' : `${shutdownTimeout} seconds`}`);
console.debug(`- Shared root: ${sharedRoot}`);
if (mountDistroRoot) {
console.log(`Mounting shared mountpoint: ${sharedRoot}`);
mountSharedMountpoint(sharedRoot);
}
let activeConnections = 0;
let shutdownTimer = null;
function resetShutdownTimer() {
if (shutdownTimer) {
clearTimeout(shutdownTimer);
shutdownTimer = null;
}
if (shutdownTimeout > 0) {
shutdownTimer = setTimeout(() => {
console.log('No active connections, shutting down.');
cleanup();
}, shutdownTimeout * 1000);
}
}
function getWslDistroName() {
const distroName = process.env.WSL_DISTRO_NAME;
if (distroName) {
return distroName;
}
try {
const result = execFileSync('wslpath', ['-am', '/']).toString().trim();
const parts = result.split('/').filter(Boolean);
if (parts.length !== 2) {
// noinspection ExceptionCaughtLocallyJS
throw new Error('Unexpected value from "wslpath -am /"');
}
return parts[1];
} catch (err) {
console.error(`Unable to get the WSL distro name: ${err.message}`);
process.exit(1);
}
}
function getSharedMountpoint(distroName) {
return `/mnt/wsl/distro-roots/${distroName}`;
}
function mountSharedMountpoint(mountPoint) {
if (!fs.existsSync(mountPoint)) {
fs.mkdirSync(mountPoint, { recursive: true });
}
try {
// Check if the mount point is already mounted
let isMounted;
try {
execFileSync('mountpoint', ['-q', mountPoint]);
isMounted = true;
} catch (err) {
isMounted = false;
}
let options = 'rbind,rslave';
if (isMounted) {
options += ',remount';
}
execFileSync('mount', ['--make-shared', '/']);
execFileSync('mount', ['/', mountPoint, '-o', options]);
} catch (err) {
console.error(`Unable to mount the shared mountpoint: ${err.message}`);
process.exit(1);
}
}
function writeError(res, statusCode, message, err) {
res.writeHead(statusCode, { 'Content-Type': 'application/json' });
res.end(
JSON.stringify({
response: statusCode,
message: `podman-wsl-service: ${message}: ${err.message}`,
cause: err.message,
})
);
}
function wslPathToWindowsPath(wslPath) {
return execFileSync('wslpath', ['-aw', wslPath]).toString().trim();
}
function translateHostPath(hostPath) {
if (hostPath.startsWith('/mnt/wsl/')) {
return hostPath;
}
try {
const winPath = wslPathToWindowsPath(hostPath);
if (winPath.startsWith('\\\\wsl.localhost\\')) {
if (!hostPath.startsWith('/')) {
// noinspection ExceptionCaughtLocallyJS
throw new Error(`PODMAN WSL SERVICE BUG: unexpected path format, expected absolute path: '${hostPath}'`);
}
const res = path.join(sharedRoot, hostPath.slice(1));
console.debug(`Translating host path: ${hostPath} -> ${res}`);
return res;
}
return winPath;
} catch (err) {
console.error('Error translating host path:', err);
throw err;
}
}
function patchVolumesLibpod(body) {
const mounts = body.mounts;
if (!Array.isArray(mounts)) {
return;
}
for (let i = 0; i < mounts.length; i++) {
const mount = mounts[i];
const hostPath = mount.source;
try {
mounts[i].source = translateHostPath(hostPath);
} catch (err) {
console.error('Error mangling volumes (libpod):', err);
throw err;
}
}
}
function patchVolumesDocker(body) {
const mounts = body.HostConfig?.Binds;
if (!Array.isArray(mounts)) {
return;
}
for (let i = 0; i < mounts.length; i++) {
const mount = mounts[i].split(':');
const hostPath = mount[0];
try {
mount[0] = translateHostPath(hostPath);
mounts[i] = mount.join(':');
} catch (err) {
console.error('Error mangling volumes (docker):', err);
throw err;
}
}
}
async function forwardRequest(req, res, modifiedBody = null) {
console.log(`${res.statusCode} ${req.method} ${req.url} - intercepted: ${modifiedBody === null ? 'no' : 'yes'}`);
const headers = { ...req.headers };
const options = {
socketPath: upstreamSocketPath,
method: req.method,
headers,
path: req.url,
};
if (modifiedBody) {
options.headers['Content-Length'] = Buffer.byteLength(modifiedBody);
}
const upstreamReq = http.request(options, (upstreamRes) => {
// Set response headers, preserving capitalization
upstreamRes.rawHeaders.forEach((value, index) => {
if (index % 2 === 0) {
const headerName = value;
const headerValue = upstreamRes.rawHeaders[index + 1];
res.setHeader(headerName, headerValue);
}
}); // Write the status code and flush headers immediately
res.writeHead(upstreamRes.statusCode);
res.flushHeaders(); // Handle data manually
upstreamRes.on('data', (chunk) => {
const writeSuccess = res.write(chunk);
if (!writeSuccess) {
upstreamRes.pause();
}
});
res.on('drain', () => {
upstreamRes.resume();
});
upstreamRes.on('end', () => {
res.end();
});
upstreamRes.on('error', (err) => {
console.error(`Error in upstream response: ${err.message}`);
res.end();
});
res.on('close', () => {
upstreamRes.destroy();
});
res.on('error', (err) => {
console.error(`Error in response: ${err.message}`);
upstreamRes.destroy();
});
});
upstreamReq.on('error', (err) => {
console.error(`Error proxying request: ${err.message}`);
var code, message;
if (err.code === 'ENOENT') {
code = 502;
message = 'Upstream server not found - is Podman running?';
} else {
code = 500;
message = 'Error proxying request';
}
if (!res.headersSent) {
writeError(res, code, message, err);
} else {
res.end(message);
}
});
if (modifiedBody) {
upstreamReq.write(modifiedBody);
upstreamReq.end();
} else if (parseInt(req.headers['content-length'] || '0') > 0 || req.headers['transfer-encoding']) {
// If the request has a body, pipe it
req.pipe(upstreamReq);
} else {
// If no body, just end the upstream request
upstreamReq.end();
} // Handle client request errors
req.on('aborted', () => {
console.log(`Client request aborted: ${req.url}`);
upstreamReq.abort();
});
req.on('error', (err) => {
console.error(`Error in client request: ${err.message}`);
upstreamReq.abort();
});
}
// Create an HTTP server that listens on a Unix socket
const server = http.createServer(async (req, res) => {
activeConnections++;
if (shutdownTimer) {
clearTimeout(shutdownTimer);
shutdownTimer = null;
}
res.on('finish', () => {
activeConnections--;
if (activeConnections === 0) {
resetShutdownTimer();
}
});
const parsedUrl = url.parse(req.url);
const pathWithoutVersion = parsedUrl.pathname.replace(/^\/v\d+\.(?:\d\.?)+\//, '/');
if (
req.method === 'POST' &&
(pathWithoutVersion === '/containers/create' || pathWithoutVersion === '/libpod/containers/create')
) {
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', async () => {
try {
const jsonBody = JSON.parse(body);
if (pathWithoutVersion === '/containers/create') {
patchVolumesDocker(jsonBody);
} else if (pathWithoutVersion === '/libpod/containers/create') {
patchVolumesLibpod(jsonBody);
}
await forwardRequest(req, res, JSON.stringify(jsonBody));
} catch (err) {
console.error('Error processing request body:', err);
writeError(res, 500, 'Error processing request body', err);
}
});
} else {
await forwardRequest(req, res);
}
});
server.on('upgrade', (req, socket, head) => {
activeConnections++;
if (shutdownTimer) {
clearTimeout(shutdownTimer);
shutdownTimer = null;
}
socket.on('close', () => {
activeConnections--;
if (activeConnections === 0) {
resetShutdownTimer();
}
});
console.log(`101 ${req.method} ${req.url} - WebSocket upgrade`);
const upstreamSocket = net.connect(upstreamSocketPath, () => {
let headers = `${req.method} ${req.url} HTTP/${req.httpVersion}\r\n`;
for (let i = 0; i < req.rawHeaders.length; i += 2) {
headers += `${req.rawHeaders[i]}: ${req.rawHeaders[i + 1]}\r\n`;
}
headers += '\r\n';
upstreamSocket.write(headers);
upstreamSocket.write(head);
socket.pipe(upstreamSocket).pipe(socket);
});
upstreamSocket.on('error', (err) => {
console.error(` WebSocket error: ${err.message} - ${req.method} ${req.url}`);
socket.destroy();
});
socket.on('error', (err) => {
console.error(` Client WebSocket error: ${err.message} - ${req.method} ${req.url}`);
upstreamSocket.destroy();
});
socket.on('close', () => {
console.debug(` Client WebSocket disconnected - ${req.method} ${req.url}`);
upstreamSocket.destroy();
});
upstreamSocket.on('close', () => {
console.debug(` Upstream WebSocket disconnected - ${req.method} ${req.url}`);
socket.destroy();
});
});
function cleanup() {
console.log('Cleaning up and closing Unix socket.');
if (!systemdSocketFd && fs.existsSync(downstreamSocketPath)) {
fs.unlinkSync(downstreamSocketPath);
console.log('Closed Unix socket.');
}
process.exit();
}
process.on('SIGINT', cleanup);
process.on('SIGTERM', cleanup);
// Listen on a Unix socket
server.listen(systemdSocketFd || downstreamSocketPath, () => {
console.log('Proxy server is listening on Unix socket');
resetShutdownTimer();
});