|
|
|
@@ -81,8 +81,11 @@ function authCheck(req, target, callback) {
|
|
|
|
|
const finish = (result) => {
|
|
|
|
|
if (completed) return;
|
|
|
|
|
completed = true;
|
|
|
|
|
req.removeListener('aborted', abort);
|
|
|
|
|
req.socket.removeListener('close', abort);
|
|
|
|
|
callback(result);
|
|
|
|
|
};
|
|
|
|
|
const abort = () => check.destroy();
|
|
|
|
|
const check = upstreamRequest(target, { method: 'GET', path: '/', headers: {
|
|
|
|
|
cookie: req.headers.cookie ?? '', accept: 'text/html', 'accept-encoding': 'identity',
|
|
|
|
|
} }, (response) => {
|
|
|
|
@@ -91,8 +94,8 @@ function authCheck(req, target, callback) {
|
|
|
|
|
});
|
|
|
|
|
check.setTimeout(timeoutMs, () => check.destroy(new Error('authentication check timeout')));
|
|
|
|
|
check.on('error', () => finish(null));
|
|
|
|
|
req.on('aborted', () => check.destroy());
|
|
|
|
|
req.socket.once('close', () => { if (!completed) check.destroy(); });
|
|
|
|
|
req.once('aborted', abort);
|
|
|
|
|
req.socket.once('close', abort);
|
|
|
|
|
check.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -100,7 +103,7 @@ function reject(socket, status, phrase = 'Forbidden') {
|
|
|
|
|
if (!socket.destroyed) socket.end(`HTTP/1.1 ${status} ${phrase}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function start({ root = resolve('build/web'), upstream = 'http://127.0.0.1:8095', host = '127.0.0.1', port = 8080 } = {}) {
|
|
|
|
|
export async function start({ root = resolve('build/web'), upstream = 'http://127.0.0.1:8095', host = '127.0.0.1', port = 8096 } = {}) {
|
|
|
|
|
const base = await realpath(root);
|
|
|
|
|
const target = new URL(upstream);
|
|
|
|
|
if (!['http:', 'https:'].includes(target.protocol) || target.username || target.password) throw new Error('UPSTREAM must be credential-free HTTP(S)');
|
|
|
|
@@ -157,6 +160,8 @@ export async function start({ root = resolve('build/web'), upstream = 'http://12
|
|
|
|
|
upgrades.add(upstreamSocket);
|
|
|
|
|
upstreamSocket.on('close', () => upgrades.delete(upstreamSocket));
|
|
|
|
|
upstreamSocket.setTimeout(timeoutMs, () => upstreamSocket.destroy(new Error('upgrade timeout')));
|
|
|
|
|
// Bound establishment only. A healthy, idle chat socket stays connected.
|
|
|
|
|
upstreamSocket.once('data', () => upstreamSocket.setTimeout(0));
|
|
|
|
|
upstreamSocket.once(target.protocol === 'https:' ? 'secureConnect' : 'connect', () => {
|
|
|
|
|
const headers = { ...req.headers, host: target.host, origin: target.origin };
|
|
|
|
|
const lines = Object.entries(headers).filter(([, value]) => value != null)
|
|
|
|
@@ -191,7 +196,7 @@ if (process.argv[1] && import.meta.url === new URL(`file://${process.argv[1]}`).
|
|
|
|
|
if (value.startsWith('--')) result.push([value.slice(2), all[index + 1]]);
|
|
|
|
|
return result;
|
|
|
|
|
}, []));
|
|
|
|
|
start({ root: args.root ?? 'build/web', upstream: process.env.UPSTREAM ?? args.upstream ?? 'http://127.0.0.1:8095', host: args.host ?? '127.0.0.1', port: Number(args.port ?? 8080) })
|
|
|
|
|
start({ root: args.root ?? 'build/web', upstream: process.env.UPSTREAM ?? args.upstream ?? 'http://127.0.0.1:8095', host: args.host ?? '127.0.0.1', port: Number(args.port ?? 8096) })
|
|
|
|
|
.then((preview) => console.log(`preview http://${args.host ?? '127.0.0.1'}:${preview.port}`))
|
|
|
|
|
.catch((error) => { console.error(error); process.exitCode = 1; });
|
|
|
|
|
}
|
|
|
|
|