109 lines
3.8 KiB
JavaScript
Executable File
109 lines
3.8 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
const http = require('http');
|
|
const net = require('net');
|
|
|
|
const expectedHostname = (process.env.TPROXY_HOSTNAME || '').toLowerCase();
|
|
const upstreamHost = '127.0.0.1';
|
|
const upstreamPort = 8080;
|
|
const listenPort = 8088;
|
|
|
|
const page = '<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>Proxy Edge</title><style>body{margin:0;background:#0b1220;color:#dce7f7;font:16px system-ui,sans-serif}main{max-width:720px;margin:16vh auto;padding:32px}h1{font-size:42px;margin:0 0 12px;color:#fff}p{line-height:1.6;color:#9eb2cc}.badge{display:inline-block;padding:6px 10px;border:1px solid #274262;border-radius:999px;color:#86b7ea;font-size:13px}</style></head><body><main><span class="badge">Proxy Edge</span><h1>Private edge transport</h1><p>This endpoint provides resilient transport and availability services for authorized clients.</p></main></body></html>';
|
|
|
|
function requestHostname(req) {
|
|
return (req.headers.host || '').split(':', 1)[0].toLowerCase();
|
|
}
|
|
|
|
function acceptHost(req) {
|
|
return expectedHostname !== '' && requestHostname(req) === expectedHostname;
|
|
}
|
|
|
|
function failMisdirected(response) {
|
|
response.writeHead(421, {
|
|
'content-type': 'text/plain; charset=utf-8',
|
|
'x-content-type-options': 'nosniff',
|
|
'referrer-policy': 'no-referrer',
|
|
});
|
|
response.end('misdirected request\n');
|
|
}
|
|
|
|
const server = http.createServer((req, res) => {
|
|
if (!acceptHost(req)) {
|
|
failMisdirected(res);
|
|
return;
|
|
}
|
|
|
|
const requestUrl = new URL(req.url, 'http://localhost');
|
|
const staticPath =
|
|
requestUrl.pathname === '/' ||
|
|
requestUrl.pathname === '/about' ||
|
|
requestUrl.pathname === '/privacy';
|
|
|
|
if (req.method === 'GET' && staticPath && requestUrl.search === '') {
|
|
res.writeHead(200, {
|
|
'content-type': 'text/html; charset=utf-8',
|
|
'cache-control': 'public, max-age=300',
|
|
'x-content-type-options': 'nosniff',
|
|
'referrer-policy': 'no-referrer',
|
|
});
|
|
res.end(page);
|
|
return;
|
|
}
|
|
|
|
const upstream = http.request(
|
|
{
|
|
host: upstreamHost,
|
|
port: upstreamPort,
|
|
method: req.method,
|
|
path: req.url,
|
|
headers: req.headers,
|
|
},
|
|
(upstreamResponse) => {
|
|
const headers = { ...upstreamResponse.headers };
|
|
headers['x-content-type-options'] = 'nosniff';
|
|
headers['referrer-policy'] = 'no-referrer';
|
|
res.writeHead(upstreamResponse.statusCode || 502, headers);
|
|
upstreamResponse.pipe(res);
|
|
},
|
|
);
|
|
|
|
upstream.on('error', (error) => {
|
|
if (!res.headersSent) {
|
|
res.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' });
|
|
}
|
|
res.end('upstream unavailable\n');
|
|
console.error(`relay error: ${error.message}`);
|
|
});
|
|
req.pipe(upstream);
|
|
});
|
|
|
|
server.on('upgrade', (req, clientSocket, head) => {
|
|
if (!acceptHost(req)) {
|
|
clientSocket.end(
|
|
'HTTP/1.1 421 Misdirected Request\r\nConnection: close\r\n\r\n',
|
|
);
|
|
return;
|
|
}
|
|
|
|
const upstreamSocket = net.connect(upstreamPort, upstreamHost, () => {
|
|
let requestHead = `${req.method} ${req.url} HTTP/${req.httpVersion}\r\n`;
|
|
for (let index = 0; index < req.rawHeaders.length; index += 2) {
|
|
requestHead += `${req.rawHeaders[index]}: ${req.rawHeaders[index + 1]}\r\n`;
|
|
}
|
|
upstreamSocket.write(`${requestHead}\r\n`);
|
|
if (head.length > 0) upstreamSocket.write(head);
|
|
upstreamSocket.pipe(clientSocket);
|
|
clientSocket.pipe(upstreamSocket);
|
|
});
|
|
|
|
upstreamSocket.on('error', () => clientSocket.destroy());
|
|
clientSocket.on('error', () => upstreamSocket.destroy());
|
|
});
|
|
|
|
server.headersTimeout = 10_000;
|
|
server.keepAliveTimeout = 90_000;
|
|
server.listen(listenPort, '0.0.0.0', () => {
|
|
console.log(`ingress listening on :${listenPort} for ${expectedHostname}`);
|
|
});
|