我目前正在使用express编写一个Web应用程序,它将为每个会话生成单独的Docker容器并代理通过它们的流量。当我使用express-http-proxy-async
进行测试时,一切工作正常,但是我需要websocket支持juiceshop。这使我开始使用http-proxy-middleware
,它没有异步主机解析支持。我已经创建了更多的中间件来解决此问题,但是我的访问量无法通过代理实现,我也不知道为什么。输出显示已创建代理,但由于我的调用方式,我认为express无法正确使用它。
路线:
app.use(vhost('juiceshop.example.com', async (req, res) => {
let target = await juiceShop_session_handler(req);
return wsproxy({target: target, ws:true});
}));
会话处理程序:
async function juiceShop_session_handler(req){
return new Promise((resolve, reject) => {
if(req.sessionID in instances){
resolve('http://127.0.0.1:' + instances[req.sessionID]['port']);
} else {
instances[req.sessionID] = {};
instances[req.sessionID]['port'] = PORTS.next()['value'];
let instance = spawn('docker', ['run', '--rm', '-p', instances[req.sessionID]['port'] + ':3000', 'bkimminich/juice-shop']);
instance.stdout.on('data', (data) => {
if (data.includes('Server listening on port')) resolve('http://127.0.0.1:' + instances[req.sessionID]['port']);
});
instance.stderr.on('data', (data) => {
if (data.includes('port is already allocated')) {
req.session.error = createError(501, 'Failed to bind to port. Complain to the admin about this.');
reject()
}
});
instances[req.sessionID]['instance'] = instance;
}
});
}
输出:
[+] Listening on 80!
[HPM] Proxy created: / -> http://127.0.0.1:3000
我不知道为什么流量没有通过代理。感谢所有帮助!