| 1 | /* -------------------------------------------------------------------------
|
|---|
| 2 | * BKG NTRIP Client
|
|---|
| 3 | * -------------------------------------------------------------------------
|
|---|
| 4 | *
|
|---|
| 5 | * Class: bncNetQueryV2
|
|---|
| 6 | *
|
|---|
| 7 | * Purpose: Blocking Network Requests (NTRIP Version 2)
|
|---|
| 8 | *
|
|---|
| 9 | * Author: L. Mervart
|
|---|
| 10 | *
|
|---|
| 11 | * Created: 27-Dec-2008
|
|---|
| 12 | *
|
|---|
| 13 | * Changes:
|
|---|
| 14 | *
|
|---|
| 15 | * -----------------------------------------------------------------------*/
|
|---|
| 16 |
|
|---|
| 17 | #include <iostream>
|
|---|
| 18 |
|
|---|
| 19 | #include "bncnetqueryv2.h"
|
|---|
| 20 | #include "bncsettings.h"
|
|---|
| 21 | #include "bncversion.h"
|
|---|
| 22 | #include "bncsslconfig.h"
|
|---|
| 23 | #include "bncsettings.h"
|
|---|
| 24 |
|
|---|
| 25 | // Constructor
|
|---|
| 26 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 27 | bncNetQueryV2::bncNetQueryV2(bool secure) {
|
|---|
| 28 | _secure = secure;
|
|---|
| 29 | _manager = new QNetworkAccessManager(this);
|
|---|
| 30 | connect(_manager, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)),
|
|---|
| 31 | this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
|
|---|
| 32 | _reply = 0;
|
|---|
| 33 | _eventLoop = new QEventLoop(this);
|
|---|
| 34 | _firstData = true;
|
|---|
| 35 | _status = init;
|
|---|
| 36 |
|
|---|
| 37 | bncSettings settings;
|
|---|
| 38 | _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked);
|
|---|
| 39 |
|
|---|
| 40 | if (_secure ) {
|
|---|
| 41 | if (!QSslSocket::supportsSsl()) {
|
|---|
| 42 | BNC_CORE->slotMessage("No SSL support, install OpenSSL run-time libraries", true);
|
|---|
| 43 | stop();
|
|---|
| 44 | }
|
|---|
| 45 | // Generate filenames to consider a potential client certificate
|
|---|
| 46 | _crtFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.crt").arg(_url.port());
|
|---|
| 47 | _keyFileName = settings.value("sslClientCertPath").toString() + _url.host() + QString(".%1.key").arg(_url.port());
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | // Destructor
|
|---|
| 53 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 54 | bncNetQueryV2::~bncNetQueryV2() {
|
|---|
| 55 | delete _eventLoop;
|
|---|
| 56 | if (_reply) {
|
|---|
| 57 | _reply->abort();
|
|---|
| 58 | delete _reply;
|
|---|
| 59 | }
|
|---|
| 60 | delete _manager;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | // Stop (quit event loop)
|
|---|
| 64 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 65 | void bncNetQueryV2::stop() {
|
|---|
| 66 | if (_reply) {
|
|---|
| 67 | _reply->abort();
|
|---|
| 68 | delete _reply;
|
|---|
| 69 | _reply = 0;
|
|---|
| 70 | }
|
|---|
| 71 | _eventLoop->quit();
|
|---|
| 72 | _status = finished;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | // End of Request
|
|---|
| 76 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 77 | void bncNetQueryV2::slotFinished() {
|
|---|
| 78 | _eventLoop->quit();
|
|---|
| 79 | if (_reply && _reply->error() != QNetworkReply::NoError) {
|
|---|
| 80 | _status = error;
|
|---|
| 81 | if (!_reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray().isEmpty()) {
|
|---|
| 82 | emit newMessage(_url.path().toLatin1().replace(0,1,"") +
|
|---|
| 83 | ": NetQueryV2: server replied: " +
|
|---|
| 84 | _reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray(),
|
|---|
| 85 | true);
|
|---|
| 86 | } else {
|
|---|
| 87 | emit newMessage(_url.path().toLatin1().replace(0,1,"") +
|
|---|
| 88 | ": NetQueryV2: server replied: " +
|
|---|
| 89 | _reply->errorString().toLatin1(),
|
|---|
| 90 | true);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | else {
|
|---|
| 94 | _status = finished;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | //
|
|---|
| 99 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 100 | void bncNetQueryV2::slotProxyAuthenticationRequired(const QNetworkProxy&,
|
|---|
| 101 | QAuthenticator*) {
|
|---|
| 102 | emit newMessage("slotProxyAuthenticationRequired", true);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | // Start request, block till the next read
|
|---|
| 106 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 107 | void bncNetQueryV2::startRequest(const QUrl& url, const QByteArray& gga) {
|
|---|
| 108 | startRequestPrivate(url, gga, false);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | // Start request, block till the next read
|
|---|
| 112 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 113 | void bncNetQueryV2::keepAliveRequest(const QUrl& url, const QByteArray& gga) {
|
|---|
| 114 | startRequestPrivate(url, gga, false);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | // Start Request (Private Method)
|
|---|
| 118 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 119 | void bncNetQueryV2::startRequestPrivate(const QUrl& url, const QByteArray& gga,
|
|---|
| 120 | bool full) {
|
|---|
| 121 |
|
|---|
| 122 | _status = running;
|
|---|
| 123 |
|
|---|
| 124 | // Default scheme and path
|
|---|
| 125 | // -----------------------
|
|---|
| 126 | _url = url;
|
|---|
| 127 | if (_url.scheme().isEmpty()) {
|
|---|
| 128 | if (_secure) {
|
|---|
| 129 | _url.setPort(443);
|
|---|
| 130 | _url.setScheme("https");
|
|---|
| 131 | }
|
|---|
| 132 | else {
|
|---|
| 133 | _url.setScheme("http");
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 | if (_url.path().isEmpty()) {
|
|---|
| 137 | _url.setPath("/");
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | // Network Request
|
|---|
| 141 | // ---------------
|
|---|
| 142 | bncSslConfig sslConfig = BNC_SSL_CONFIG;
|
|---|
| 143 |
|
|---|
| 144 | if (_secure) {
|
|---|
| 145 | QFile clientCrtFile(_crtFileName);
|
|---|
| 146 | QFile privateKeyFile(_keyFileName);
|
|---|
| 147 | if ( clientCrtFile.exists() && privateKeyFile.exists()) {
|
|---|
| 148 | // set local certificate if available
|
|---|
| 149 | clientCrtFile.open(QIODevice::ReadOnly);
|
|---|
| 150 | QSslCertificate clientCrt(&clientCrtFile);
|
|---|
| 151 | sslConfig.setLocalCertificate(clientCrt);
|
|---|
| 152 | // set private key if available
|
|---|
| 153 | privateKeyFile.open(QIODevice::ReadOnly);
|
|---|
| 154 | QSslKey privateKey(&privateKeyFile, QSsl::Rsa);
|
|---|
| 155 | sslConfig.setPrivateKey(privateKey);
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | QNetworkRequest request;
|
|---|
| 160 | request.setSslConfiguration(sslConfig);
|
|---|
| 161 | request.setUrl(_url);
|
|---|
| 162 | request.setRawHeader("Host" , _url.host().toLatin1());
|
|---|
| 163 | request.setRawHeader("Ntrip-Version", "Ntrip/2.0");
|
|---|
| 164 | request.setRawHeader("User-Agent" , "NTRIP BNC/" BNCVERSION " (" BNC_OS ")");
|
|---|
| 165 | if (!_url.userName().isEmpty()) {
|
|---|
| 166 | QString uName = QUrl::fromPercentEncoding(_url.userName().toLatin1());
|
|---|
| 167 | QString passW = QUrl::fromPercentEncoding(_url.password().toLatin1());
|
|---|
| 168 | request.setRawHeader("Authorization", "Basic " +
|
|---|
| 169 | (uName + ":" + passW).toLatin1().toBase64());
|
|---|
| 170 | }
|
|---|
| 171 | if (!gga.isEmpty()) {
|
|---|
| 172 | request.setRawHeader("Ntrip-GGA", gga);
|
|---|
| 173 | }
|
|---|
| 174 | request.setRawHeader("Connection" , "close");
|
|---|
| 175 |
|
|---|
| 176 | if (_reply) {
|
|---|
| 177 | delete _reply;
|
|---|
| 178 | _reply = 0;
|
|---|
| 179 | }
|
|---|
| 180 | _reply = _manager->get(request);
|
|---|
| 181 |
|
|---|
| 182 | // Connect Signals
|
|---|
| 183 | // ---------------
|
|---|
| 184 | connect(_reply, SIGNAL(finished()), this, SLOT(slotFinished()));
|
|---|
| 185 | connect(_reply, SIGNAL(finished()), _eventLoop, SLOT(quit()));
|
|---|
| 186 | connect(_reply, SIGNAL(sslErrors(QList<QSslError>)),this, SLOT(slotSslErrors(QList<QSslError>)));
|
|---|
| 187 | if (!full) {
|
|---|
| 188 | connect(_reply, SIGNAL(readyRead()), _eventLoop, SLOT(quit()));
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | // Start Request, wait for its completion
|
|---|
| 193 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 194 | void bncNetQueryV2::waitForRequestResult(const QUrl& url, QByteArray& outData) {
|
|---|
| 195 |
|
|---|
| 196 | // Send Request
|
|---|
| 197 | // ------------
|
|---|
| 198 | startRequestPrivate(url, "", true);
|
|---|
| 199 |
|
|---|
| 200 | // Wait Loop
|
|---|
| 201 | // ---------
|
|---|
| 202 | _eventLoop->exec();
|
|---|
| 203 |
|
|---|
| 204 | // Copy Data and Return
|
|---|
| 205 | // --------------------
|
|---|
| 206 | if (_reply) {
|
|---|
| 207 | outData = _reply->readAll();
|
|---|
| 208 | }
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | // Wait for next data
|
|---|
| 212 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 213 | void bncNetQueryV2::waitForReadyRead(QByteArray& outData) {
|
|---|
| 214 |
|
|---|
| 215 | // Wait Loop
|
|---|
| 216 | // ---------
|
|---|
| 217 | if (!_reply->bytesAvailable()) {
|
|---|
| 218 | _eventLoop->exec();
|
|---|
| 219 | }
|
|---|
| 220 | if (!_reply) {
|
|---|
| 221 | return;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | // Check NTRIPv2 error code
|
|---|
| 225 | // ------------------------
|
|---|
| 226 | if (_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
|
|---|
| 227 | _reply->abort();
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | // Append Data
|
|---|
| 231 | // -----------
|
|---|
| 232 | else {
|
|---|
| 233 | outData.append(_reply->readAll());
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | // TSL/SSL
|
|---|
| 238 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 239 | void bncNetQueryV2::slotSslErrors(QList<QSslError> errors) {
|
|---|
| 240 |
|
|---|
| 241 | QString msg = "SSL Error: ";
|
|---|
| 242 | QSslCertificate cert = _reply->sslConfiguration().peerCertificate();
|
|---|
| 243 | if (!cert.isNull() &&
|
|---|
| 244 | cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() &&
|
|---|
| 245 | cert.issuerInfo(QSslCertificate::Organization).count()) {
|
|---|
| 246 |
|
|---|
| 247 | msg += QString("Server Certificate Issued by:\n"
|
|---|
| 248 | "%1\n%2\nCannot be verified\n")
|
|---|
| 249 | #if QT_VERSION >= 0x050000
|
|---|
| 250 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0))
|
|---|
| 251 | .arg(cert.issuerInfo(QSslCertificate::Organization).at(0));
|
|---|
| 252 | #else
|
|---|
| 253 | .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName))
|
|---|
| 254 | .arg(cert.issuerInfo(QSslCertificate::Organization));
|
|---|
| 255 | #endif
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | QListIterator<QSslError> it(errors);
|
|---|
| 259 | while (it.hasNext()) {
|
|---|
| 260 | const QSslError& err = it.next();
|
|---|
| 261 | msg += err.errorString();
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | if (_sslIgnoreErrors) {
|
|---|
| 265 | _reply->ignoreSslErrors();
|
|---|
| 266 | BNC_CORE->slotMessage("BNC ignores SSL errors as configured", true);
|
|---|
| 267 | }
|
|---|
| 268 | else {
|
|---|
| 269 | BNC_CORE->slotMessage(msg.toLatin1(), true);
|
|---|
| 270 | stop();
|
|---|
| 271 | }
|
|---|
| 272 | return;
|
|---|
| 273 | }
|
|---|