我试图用wscat和浏览器连接到带有自签名证书的wss(代理),但是它给了我错误。
我试图确保我的安全服务器正常运行的东西。
wscat -c wss://localhost:8443 --ca cert.pem,它可以工作。
我发现的错误:
wscat -c wss://localhost:8080 --ca cert.pem我得到了wscat -c wss://localhost:8080 --ca cert.pem我认为问题在于我的代理服务器无法获取cert.pem并将其传递给https服务器。我到处寻找,但我找不到如何连接到wss(代理)与自签署的证书。我无法抑制
/服务器
const app = express()
app.use('/', function (req, res) {
res.writeHead(200);
res.end("hello from a secure world\n");
})
export const server = https.createServer({
cert: fs.readFileSync(path.resolve(__dirname, 'cert.pem'), 'utf-8'),
ca: fs.readFileSync(path.resolve(__dirname, 'cert.pem'), 'utf-8'),
key: fs.readFileSync(path.resolve(__dirname, 'server.key'), 'utf-8')
}, app)
const wss = new WebSocket.Server({ server });
wss.on('connection', function connection(ws) {
console.log("connected");
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send('hello from server!, the time is: ' + timestamp());
});
});/Proxy
const wsProxy = createProxyMiddleware('/', {
target: `https://localhost:8443`,
changeOrigin: true,
secure: true,
ws: true,
ssl: {
cert: fs.readFileSync(path.resolve(__dirname, 'cert.pem')),
}
});
const app = express();
app.use(wsProxy);
const proxy = app.listen(8080)
proxy.on('upgrade', wsProxy.upgrade); // <-- subscribe to http 'upgrade'发布于 2021-05-17 13:28:25
好吧,结果发现我错过了一些关键的东西。没有真正的“代理websocket”,我混淆了https代理和websocket代理。一旦我明白了这一点,我的问题就解决了。我必须使用https服务器(使用cert和key)创建websocket,然后使用相同的cert和key连接到wss :)
https://stackoverflow.com/questions/67558836
复制相似问题