问题描述
我正在尝试使用 wscat 和浏览器通过自签名证书连接到 wss(代理),但它给了我错误。
- 使用证书 cert.pem 在 8443 上运行的 https
- 代理在 8080 上运行,安全真实
我试图确保我的安全服务器正常运行的事情。
- 我可以访问 https://localhost:8443 并收到“来自安全世界的问候”
- 我可以使用 wscat
wscat -c wss://localhost:8443 --ca cert.pem
连接到 wss://localhost:8443 并且可以正常工作
我得到的错误:
- 我无法从浏览器访问代理 https://localhost:8080。我收到此站点无法提供安全连接和 500 状态代码
- 我无法使用
wscat -c wss://localhost:8080 --ca cert.pem
连接到 wss://localhost:8080 我得到error: write EPROTO 140266887743360:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:332:
我认为问题在于我的代理服务器无法获取 cert.pem
并将其传递给 https 服务器。我到处找,但找不到如何使用自签名证书连接到 wss(proxy)。我无法抑制
/服务器
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,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());
});
});
/代理
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'
解决方法
好吧,结果我遗漏了一些关键的东西。没有真正的“代理 websocket” 我将 https 代理与 websocket 代理混淆了。一旦我明白了它就解决了我的问题。我必须使用 https 服务器(带有证书和密钥)创建一个 websocket,然后我可以使用相同的证书和密钥连接到 wss :)