Qt本地安全Web套接字

时间:2015-01-20 22:04:08

标签: c++ qt ssl websocket qtwebsockets

我正在开发一个Qt应用程序,它通过WebSocket与我的网络浏览器(目前是谷歌浏览器)进行通信。

一切正常,直到我尝试使用安全的websockets ...

我设法解决了我在OpenSSL中遇到的问题,所以现在我应该有一个有效的Qt应用程序,但我没有。

我正在使用VS2013和Qt 5.3。我有以下代码来启动我的服务器:

MyClass::MyClass (quint16 port, QObject *parent) : 
    QWebSocketServer("Press And Listen Server", QWebSocketServer::SecureMode, parent) {

    QSslConfiguration sslConfiguration;
    QFile certFile (QStringLiteral ("localhost.crt"));
    QFile keyFile (QStringLiteral ("localhost.key"));
    certFile.open (QIODevice::ReadOnly);
    keyFile.open (QIODevice::ReadOnly);
    QSslCertificate certificate (&certFile, QSsl::Pem);
    QSslKey sslKey (&keyFile, QSsl::Rsa, QSsl::Pem);
    certFile.close ();
    keyFile.close ();
    sslConfiguration.setPeerVerifyMode (QSslSocket::VerifyNone);
    sslConfiguration.setLocalCertificate (certificate);
    sslConfiguration.setPrivateKey (sslKey);
    sslConfiguration.setProtocol (QSsl::TlsV1SslV3);
    this->setSslConfiguration (sslConfiguration);

    if (!this->listen (QHostAddress::Any, port)) {
        throw ServerNotStartedException () ;
    }
    qDebug () << "Server listenning on: " << port ;
    connect (this, &QWebSocketServer::newConnection, this, &MyClass::onNewConnection);
    connect (this, &QWebSocketServer::closed, this, &MyClass::onClose);
    connect (this, &QWebSocketServer::sslErrors, this, &MyClass::onSslErrors);
}

我使用以下方法创建了证书文件:https://developer.salesforce.com/blogs/developer-relations/2011/05/generating-valid-self-signed-certificates.html

在浏览器方面,我只有:

var websock = websock = new WebSocket('wss://localhost:52132');

websock.onerror = function (error) {
    console.log('Press & Listen, WS Error: ' + error);
};

websock.onopen = function () {
    console.log('Open!');
};

不幸的是,每当我尝试时,我都会收到以下JS消息:

WebSocket connection to 'wss://localhost:52132/' failed: Error in connection establishment: net::ERR_TIMED_OUT

到目前为止:

  • QSslSocket::supportsSsl ()返回true
  • 我没有任何QSslSocket: cannot resolve XXX method条消息
  • 加载OpenSSL DLL,VS2013输出以下消息:

    'MyProject.exe'(Win32):加载'I:\ Sources \ VS2013 \ x64 \ Release \ ssleay32.dll'。无法找到或打开PDB文件。 'PressAndListenQt.exe'(Win32):加载'I:\ Sources \ VS2013 \ x64 \ Release \ libeay32.dll'。无法找到或打开PDB文件。

我不知道如何找到错误,所以我愿意接受任何建议!

编辑:我尝试使用以下内容生成自签名证书(而不是上面的链接):

openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 100 -nodes

现在我又收到了一个错误:

WebSocket connection to 'wss://localhost:52132/' failed: WebSocket opening handshake was canceled

2 个答案:

答案 0 :(得分:2)

几天前似乎发生了同样的情况。 我解决了将自签名证书信任到证书管理单元中的问题 您可以使用MMC控制台打开。 将证书导入到当前用户和本地计算机文件夹的个人,将证书导入到受信任的发布者文件夹中。请记住在测试后删除证书。

对于教程Qt \ Examples \ Qt-5.5 \ websockets \ sslechoserver \的localhost.cert证书也可以使用相同的内容,并查看websockets网页如何与教程提供的wss连接。

当网页中的websockets无法连接相同的错误时,这对我有用。

(对不起,我的英语很低:))

答案 1 :(得分:-1)

尝试将网址从 wss:// localhost:52132 修改为 https:// http:// 。猜测 wss:// 可能被防火墙和服务器拒绝作为自定义协议。