【问题标题】:Can't connect to web socket from Electron when using self signed cert使用自签名证书时无法从 Electron 连接到 Web 套接字
【发布时间】: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


    【解决方案1】:

    我遇到了同样的问题,我添加的只是你的行:

    app.commandLine.appendSwitch('ignore-certificate-errors');
    

    我使用 socket.io,但我认为它的原理相同。 但是,我确实连接到 https 协议,而不是直接连接到 wss。

    这是我的连接在页面上的样子:

    socket = io.connect(
            'https://yoursocketserver:yourport', {
                'socketpath',
                secure: false,
                transports: ['websocket']
            });
    

    这似乎成功了。 谢谢你的帮助:)我希望这个答案也能帮助你。

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 2020-09-09
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 2021-08-05
      • 2020-10-28
      相关资源
      最近更新 更多