websocket - 使用自签名证书时无法从 Electron 连接到 Web 套接字

标签 websocket electron wamp-protocol

我有一个 Electron 尝试通过网络套接字连接到设备的应用程序。连接是加密的(即 wss),但 SSL 证书是自签名的,因此不受信任。

在 Chrome 中连接是可以的,并且可以正常工作。但是在 Electron 内部我遇到了问题。不放任何certificate-error BrowserWindow 上的处理程序或在应用程序上,我在控制台输出中收到以下错误:

WebSocket connection to 'wss://some_ip:50443/' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID



然后不久之后:

  • User is closing WAMP connection.... unreachable


在我的代码中,为了建立连接,我运行以下命令。

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 协议(protocol)连接到某种套接字服务器。 (在我的例子中,设备)底层协议(protocol)是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);
});

这将错误更改为:

WebSocket connection to 'wss://some_ip:50443/' failed: WebSocket opening handshake was canceled



我的理解是,上面的“证书错误”处理程序应该避免任何 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,我如何正确处理来自不受信任机构的证书?即自签名证书。

任何帮助将非常感激。

最佳答案

我有同样的问题,我添加的只是你的行:

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

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

这是我的连接在页面上的样子:
socket = io.connect(
        'https://yoursocketserver:yourport', {
            'socketpath',
            secure: false,
            transports: ['websocket']
        });

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

关于websocket - 使用自签名证书时无法从 Electron 连接到 Web 套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57476284/

相关文章:

javascript - PHP Pusher 事件触发不起作用

python - Websockets,异步处理用户输入。获取连接关闭错误 1006

javascript - 使用 jQuery 的 .clone() 保留复选框 onclick 函数

php - 如何设置从 Thruway 客户端到 Crossbar 路由器的 WAMP 身份验证?

PHP Ratchet Wamp 在发布事件中向订阅者广播

javascript - 使用 WebSocket 握手无法跨域传递 cookie 吗?

php - 使用 Ratchet 创建/管理私有(private)房间?

javascript - 使用jQuery(和 Bootstrap )启用/禁用提交按钮

windows - 检测任务栏按钮的大小

python - 在同一端口上运行 Flask 和 Autobahn WAMP 服务器