You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
import 'dotenv/config';
|
|
import http from 'http';
|
|
import https from 'https';
|
|
|
|
const TARGET = process.env.PROXY_AUTH_TARGET;
|
|
const PORT = process.env.PROXY_AUTH_PORT;
|
|
const LOCAL_ORIGIN = process.env.PROXY_LOCAL_ORIGIN;
|
|
|
|
if (!TARGET) throw new Error('PROXY_AUTH_TARGET environment variable is required');
|
|
if (!PORT) throw new Error('PROXY_AUTH_PORT environment variable is required');
|
|
if (!LOCAL_ORIGIN) throw new Error('PROXY_LOCAL_ORIGIN environment variable is required');
|
|
|
|
const parsedPort = parseInt(PORT, 10);
|
|
|
|
const server = http.createServer((req, res) => {
|
|
// Handle preflight OPTIONS requests
|
|
if (req.method === 'OPTIONS') {
|
|
res.setHeader('Access-Control-Allow-Origin', LOCAL_ORIGIN);
|
|
res.setHeader('Access-Control-Allow-Credentials', 'true');
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, PATCH');
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Timezone, X-Locale');
|
|
res.writeHead(204);
|
|
res.end();
|
|
return;
|
|
}
|
|
|
|
const options = {
|
|
hostname: TARGET,
|
|
port: 443,
|
|
path: req.url,
|
|
method: req.method,
|
|
headers: {
|
|
...req.headers,
|
|
host: TARGET,
|
|
},
|
|
};
|
|
|
|
const proxyReq = https.request(options, (proxyRes) => {
|
|
// Copy response headers but add CORS
|
|
const headers = { ...proxyRes.headers };
|
|
headers['access-control-allow-origin'] = LOCAL_ORIGIN;
|
|
headers['access-control-allow-credentials'] = 'true';
|
|
|
|
res.writeHead(proxyRes.statusCode, headers);
|
|
proxyRes.pipe(res);
|
|
});
|
|
|
|
proxyReq.on('error', (err) => {
|
|
console.error('Proxy error:', err);
|
|
res.writeHead(500);
|
|
res.end('Proxy error');
|
|
});
|
|
|
|
req.pipe(proxyReq);
|
|
});
|
|
|
|
server.listen(parsedPort, () => {
|
|
console.log(`Auth Proxy running: localhost:${parsedPort} -> ${TARGET}`);
|
|
});
|