【发布时间】:2019-08-13 10:54:39
【问题描述】:
我有一个 Electron 应用程序,它尝试通过网络套接字连接到设备。连接是加密的(即 wss),但 SSL 证书是自签名的,因此不受信任。
在 Chrome 内部连接没问题,而且可以正常工作。但是在 Electron 内部我遇到了问题。如果没有在 BrowserWindow 或应用程序上放置任何 certificate-error 处理程序,我会在控制台输出中收到以下错误:
与“wss://some_ip:50443/”的 WebSocket 连接失败:连接建立错误:net::ERR_CERT_AUTHORITY_INVALID
然后不久:
- 用户正在关闭 WAMP 连接....无法访问
在我的代码中,为了建立连接,我运行以下命令。
const connection = new autobahn.Connection({
realm: 'some_realm',
url: 'wss://some_ip:50443'
});
connection.onopen = (session, details) => {
console.log('* User is opening WAMP connection....', session, details);
};
connection.onclose = (reason, details) => {
console.log('* User is closing WAMP connection....', reason, details);
return true;
};
connection.open();
// alternatively, this also displays the same error
const socket = new WebSocket(`wss://some_ip:50443`);
socket.onopen = function (event) {
console.log(event);
};
socket.onclose = function (event) {
console.log(event);
};
注意: Autobahn 是一个 Websocket 库,用于使用 WAMP 协议连接到某种套接字服务器。 (在我的例子中,设备)底层协议是wss。在上面的代码下方,正在调用本机 JS new WebSocket()。换句话说:
正如我所提到的,我已经在浏览器窗口中测试了这段代码并且它可以工作。我还构建了一个较小的应用程序来尝试隔离问题。还是没有运气。
我已尝试将以下代码添加到我的 main.js 进程脚本中:
app.commandLine.appendSwitch('ignore-certificate-errors');
和
win.webContents.on('certificate-error', (event, url, error, certificate, callback) => {
// On certificate error we disable default behaviour (stop loading the page)
// and we then say "it is all fine - true" to the callback
event.preventDefault();
callback(true);
});
和
app.on('certificate-error', (event, webContents, link, error, certificate, callback) => {
// On certificate error we disable default behaviour (stop loading the page)
// and we then say "it is all fine - true" to the callback
event.preventDefault();
callback(true);
});
这将错误更改为:
与“wss://some_ip:50443/”的 WebSocket 连接失败:WebSocket 打开握手被取消
我的理解是,上面的“证书错误”处理程序应该避免任何 SSL 证书错误并允许应用程序继续进行。然而,他们不是。
我还尝试将以下内容添加到main.js:
win = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
webSecurity: false
}
});
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = '1';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
使用 Election,我如何正确处理来自不受信任机构的证书?即自签名证书。
任何帮助将不胜感激。
【问题讨论】:
标签: websocket electron wamp-protocol