Changeset 9715 in ntrip for trunk/BNC/src
- Timestamp:
- May 17, 2022, 11:29:08 AM (3 years ago)
- Location:
- trunk/BNC/src/upload
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/BNC/src/upload/bncuploadcaster.cpp
r9714 r9715 21 21 #include "bnctableitem.h" 22 22 #include "bncsettings.h" 23 #include "bncsslconfig.h" 23 24 24 25 using namespace std; … … 26 27 // Constructor 27 28 //////////////////////////////////////////////////////////////////////////// 28 bncUploadCaster::bncUploadCaster(const QString& mountpoint, 29 const QString& outHost, int outPort, 30 const QString& ntripVersion, 31 const QString& userName, const QString& password, 32 int iRow, 33 int rate) { 34 _mountpoint = mountpoint; 29 bncUploadCaster::bncUploadCaster(const QString &mountpoint, 30 const QString &outHost, int outPort, const QString &ntripVersion, 31 const QString &userName, const QString &password, int iRow, int rate) { 32 bncSettings settings; 33 34 _mountpoint = mountpoint; 35 35 _casterOutHost = outHost; 36 36 _casterOutPort = outPort; 37 _ntripVersion 38 _userName 39 _password 40 _outSocket 41 _sOpenTrial 42 _iRow 43 _rate 44 45 if 37 _ntripVersion = ntripVersion; 38 _userName = userName; 39 _password = password; 40 _outSocket = 0; 41 _sOpenTrial = 0; 42 _iRow = iRow; 43 _rate = rate; 44 45 if (_rate < 0) { 46 46 _rate = 0; 47 } 48 else if (_rate > 60) { 47 } else if (_rate > 60) { 49 48 _rate = 60; 50 49 } … … 52 51 53 52 connect(this, SIGNAL(newMessage(QByteArray,bool)), 54 BNC_CORE, SLOT(slotMessage(const QByteArray,bool))); 55 56 if (BNC_CORE->_uploadTableItems.find(_iRow) != BNC_CORE->_uploadTableItems.end()){ 53 BNC_CORE, SLOT(slotMessage(const QByteArray,bool))); 54 55 if (BNC_CORE->_uploadTableItems.find(_iRow) 56 != BNC_CORE->_uploadTableItems.end()) { 57 57 connect(this, SIGNAL(newBytes(QByteArray,double)), 58 BNC_CORE->_uploadTableItems.value(iRow), 59 SLOT(slotNewBytes(const QByteArray,double))); 60 } 61 if (BNC_CORE->_uploadEphTableItems.find(_iRow) != BNC_CORE->_uploadEphTableItems.end()){ 58 BNC_CORE->_uploadTableItems.value(iRow), 59 SLOT(slotNewBytes(const QByteArray,double))); 60 } 61 if (BNC_CORE->_uploadEphTableItems.find(_iRow) 62 != BNC_CORE->_uploadEphTableItems.end()) { 62 63 connect(this, SIGNAL(newBytes(QByteArray,double)), 63 BNC_CORE->_uploadEphTableItems.value(iRow), 64 SLOT(slotNewBytes(const QByteArray,double))); 65 } 66 67 bncSettings settings; 68 _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) == Qt::Checked); 69 _proxyHost = settings.value("proxyHost").toString(); 70 _proxyPort = settings.value("proxyPort").toInt(); 71 (_proxyHost.isEmpty()) ? _proxy = false : _proxy = true; 72 64 BNC_CORE->_uploadEphTableItems.value(iRow), 65 SLOT(slotNewBytes(const QByteArray,double))); 66 } 67 68 _sslIgnoreErrors = (Qt::CheckState(settings.value("sslIgnoreErrors").toInt()) 69 == Qt::Checked); 70 71 _proxyOutHost = settings.value("proxyHost").toString(); 72 _proxyOutPort = settings.value("proxyPort").toInt(); 73 (_proxyOutHost.isEmpty()) ? _proxy = false : _proxy = true; 74 75 _secure = false; 73 76 if (_ntripVersion == "2s") { 74 _secure = true;75 77 if (!QSslSocket::supportsSsl()) { 76 emit(newMessage("For SSL support please install OpenSSL run-time libraries: Ntrip Version 2 is tried", true)); 77 _secure = false; 78 emit(newMessage( 79 "For SSL support please install OpenSSL run-time libraries: Ntrip Version 2 is tried", 80 true)); 78 81 _ntripVersion == "2"; 79 } 80 } else { 81 _secure = false; 82 } 83 84 if (_secure) { 85 _casterOutPort = 443; 86 (_proxy) ? _postExtension = QString("https://%1:%2").arg(_casterOutHost).arg(_casterOutPort) : _postExtension = ""; 87 } 88 else { 89 (_proxy) ? _postExtension = QString("http://%1:%2").arg(_casterOutHost).arg(_casterOutPort) : _postExtension = ""; 90 } 91 82 } else { 83 _secure = true; 84 _casterOutPort = 443; 85 } 86 } 87 88 if (!_secure && _proxy) { 89 _postExtension = QString("http://%1:%2").arg(_casterOutHost).arg(_casterOutPort); 90 } else { 91 _postExtension = ""; 92 } 92 93 } 93 94 … … 115 116 //////////////////////////////////////////////////////////////////////////// 116 117 void bncUploadCaster::slotProxyAuthenticationRequired(const QNetworkProxy&, 117 118 QAuthenticator*) { 118 119 emit newMessage("slotProxyAuthenticationRequired", true); 119 120 } 120 121 121 122 /* TSL/SSL 123 //////////////////////////////////////////////////////////////////////////// 124 void bncUploadCaster::slotSslErrors(QList<QSslError> errors) { 125 126 QString msg = "SSL Error\n"; 127 QSslCertificate cert = _outSocket->sslConfiguration().peerCertificate(); 128 if (!cert.isNull()) { 129 msg += QString("Server Certificate Issued by:\n" 130 "%1\n%2\nCannot be verified\n") 122 // TSL/SSL 123 //////////////////////////////////////////////////////////////////////////// 124 void bncUploadCaster::slotSslErrors(QList<QSslError> errors) { 125 126 QString msg = "SSL Error\n"; 127 QSslCertificate cert = _outSocket->sslConfiguration().peerCertificate(); 128 if (!cert.isNull() && 129 cert.issuerInfo(QSslCertificate::OrganizationalUnitName).count() && 130 cert.issuerInfo(QSslCertificate::Organization).count()) { 131 msg += QString("Server Certificate Issued by:\n" 132 "%1\n%2\nCannot be verified\n") 133 131 134 #if QT_VERSION >= 0x050000 132 133 135 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName).at(0)) 136 .arg(cert.issuerInfo(QSslCertificate::Organization).at(0)); 134 137 #else 135 136 138 .arg(cert.issuerInfo(QSslCertificate::OrganizationalUnitName)) 139 .arg(cert.issuerInfo(QSslCertificate::Organization)); 137 140 #endif 138 141 } 142 139 143 QListIterator<QSslError> it(errors); 140 144 while (it.hasNext()) { … … 147 151 if (_sslIgnoreErrors) { 148 152 _outSocket->ignoreSslErrors(); 149 } 150 else { 153 } else { 151 154 deleteSafely(); 152 155 } 153 156 } 154 */ 157 155 158 156 159 // Endless Loop … … 170 173 _outSocket->write(_outBuffer); 171 174 _outSocket->flush(); 172 } 173 else { 174 QString chunkSize = QString("%1").arg(_outBuffer.size(),0,16,QLatin1Char('0')); 175 QByteArray chunkedData = chunkSize.toLatin1() + "\r\n" + _outBuffer + "\r\n"; 175 } else { 176 QString chunkSize = QString("%1").arg(_outBuffer.size(), 0, 16, 177 QLatin1Char('0')); 178 QByteArray chunkedData = chunkSize.toLatin1() + "\r\n" + _outBuffer 179 + "\r\n"; 176 180 _outSocket->write(chunkedData); 177 181 _outSocket->flush(); … … 186 190 } 187 191 msleep(100); //sleep 0.1 sec 188 } 189 else { 192 } else { 190 193 sleep(_rate); 191 194 } … … 196 199 //////////////////////////////////////////////////////////////////////////// 197 200 void bncUploadCaster::open() { 201 const int timeOut = 5000; // 5 seconds 202 QByteArray msg; 203 bncSslConfig sslConfig; 198 204 199 205 if (_mountpoint.isEmpty()) { … … 204 210 if (_outSocket->state() == QAbstractSocket::ConnectedState) { 205 211 return; 206 } 207 else {208 emit(newMessage("Broadcaster: No connection for " + _mountpoint.toLatin1(), true));209 } 210 } 211 212 delete _outSocket;_outSocket = 0;213 214 double minDt = pow(2.0, _sOpenTrial);212 } else { 213 emit(newMessage( 214 "Broadcaster: No connection for " + _mountpoint.toLatin1(), true)); 215 } 216 } 217 delete _outSocket; 218 _outSocket = 0; 219 220 double minDt = pow(2.0, _sOpenTrial); 215 221 if (++_sOpenTrial > 4) { 216 222 _sOpenTrial = 4; 217 223 } 218 if (_outSocketOpenTime.isValid() &&219 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {224 if (_outSocketOpenTime.isValid() 225 && _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) { 220 226 return; 221 } 222 else { 227 } else { 223 228 _outSocketOpenTime = QDateTime::currentDateTime(); 224 229 } 225 230 226 231 _outSocket = new QSslSocket(); 227 const int timeOut = 5000; // 5 seconds 228 if (_sslIgnoreErrors) { 229 _outSocket->ignoreSslErrors(); 230 } 232 _outSocket->setSslConfiguration(sslConfig); 233 connect(_outSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(slotSslErrors(QList<QSslError>))); 231 234 _outSocket->setProxy(QNetworkProxy::NoProxy); 232 _outHost = _casterOutHost; 233 _outPort = _casterOutPort; 235 234 236 if (_proxy) { 235 _outHost = _proxyHost; 236 _outPort = _proxyPort; 237 connect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), 238 this, SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*))); 239 if (_ntripVersion == "1") { 240 emit(newMessage("No proxy support in Ntrip Version 1 upload!",true)); 241 } 242 } 243 244 if (!_secure || _proxy) { 245 _outSocket->connectToHost(_outHost, _outPort); 246 if (!_outSocket->waitForConnected(timeOut)) { 247 emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1() 248 + " (" + _outHost.toLatin1()+ ":" + QString("%1) ").arg(_outPort).toLatin1() 249 + _outSocket->errorString().toLatin1(), true)); 237 if (_ntripVersion == "1") { 238 emit(newMessage("No proxy support in Ntrip Version 1 upload!", true)); 250 239 delete _outSocket; 251 240 _outSocket = 0; 252 241 return; 253 242 } 254 } 255 else { 256 _outSocket->connectToHostEncrypted(_outHost, _outPort); 257 if (!_outSocket->waitForEncrypted(timeOut)) { 258 emit(newMessage("Broadcaster: Connect timeout for " + _mountpoint.toLatin1() 259 + " (" + _outHost.toLatin1()+ ":" + QString("%1) ").arg(_outPort).toLatin1() 260 + _outSocket->errorString().toLatin1(), true)); 261 delete _outSocket; 262 _outSocket = 0; 263 return; 264 } 265 } 266 267 QByteArray msg; 243 connect(_outSocket, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), 244 this,SLOT(slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*))); 245 246 if (!connectToHost(_proxyOutHost, _proxyOutPort, false)) { 247 return; 248 } 249 250 if (_secure) { 251 msg = "CONNECT " + _casterOutHost.toLatin1() + ":" 252 + QString("%1").arg(_casterOutPort).toLatin1() + " HTTP/1.1\r\n" 253 + "Proxy-Connection: Keep-Alive\r\n" 254 + "Host: " + _casterOutHost.toLatin1() + "\r\n" 255 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n" 256 + "\r\n"; cout << msg.toStdString().c_str(); 257 _outSocket->write(msg); 258 _outSocket->waitForBytesWritten(); 259 _outSocket->waitForReadyRead(); 260 QByteArray ans = _outSocket->readLine(); cout << ans.toStdString().c_str() << endl; 261 if (ans.indexOf("200") == -1) { 262 emit(newMessage( 263 "Proxy: Connection broken for " + _mountpoint.toLatin1() 264 + ": " + ans.left(ans.length() - 2), true)); 265 delete _outSocket; 266 _outSocket = 0; 267 return; 268 } else { 269 emit(newMessage( 270 "Proxy: Connection opened for " + _mountpoint.toLatin1() 271 + ": " + ans.left(ans.length() - 2), true)); 272 _sOpenTrial = 0; 273 _outSocket->setPeerVerifyName(_casterOutHost); 274 _outSocket->startClientEncryption(); 275 if (!_outSocket->waitForEncrypted(timeOut)) { 276 emit(newMessage( 277 "Proxy: Encrypt timeout for " + _mountpoint.toLatin1() + " (" 278 + _casterOutHost.toLatin1() + ":" 279 + QString("%1) ").arg(_casterOutPort).toLatin1() 280 + _outSocket->errorString().toLatin1(), true)); 281 delete _outSocket; 282 _outSocket = 0; 283 return; 284 } else { 285 emit(newMessage("Proxy: SSL handshake completed for " + _mountpoint.toLatin1(), true)); 286 } 287 } 288 } 289 } else { 290 if (!connectToHost(_casterOutHost, _casterOutPort, _secure)) { 291 return; 292 } 293 } 294 268 295 if (_ntripVersion == "1") { 269 msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1() + "\r\n" + 270 "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n"; 271 } 272 else { 273 msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1() + " HTTP/1.1\r\n" + 274 "Host: " + _casterOutHost.toLatin1() + "\r\n" + 275 "Ntrip-Version: Ntrip/2.0\r\n" + 276 "Authorization: Basic " + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n" + 277 "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n" + 278 "Connection: close\r\n" + 279 "Transfer-Encoding: chunked\r\n\r\n"; 296 msg = "SOURCE " + _password.toLatin1() + " /" + _mountpoint.toLatin1() 297 + "\r\n" + "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n"; 298 } else { 299 msg = "POST " + _postExtension.toLatin1() + "/" + _mountpoint.toLatin1() 300 + " HTTP/1.1\r\n" + "Host: " + _casterOutHost.toLatin1() + "\r\n" 301 + "Ntrip-Version: Ntrip/2.0\r\n" + "Authorization: Basic " 302 + (_userName + ":" + _password).toLatin1().toBase64() + "\r\n" 303 + "User-Agent: NTRIP BNC/" BNCVERSION " (" + BNC_OS + ")\r\n" 304 + "Connection: close\r\n" + "Transfer-Encoding: chunked\r\n\r\n"; 280 305 } 281 306 cout << msg.toStdString().c_str(); 282 307 _outSocket->write(msg); 283 308 _outSocket->waitForBytesWritten(); 284 285 309 _outSocket->waitForReadyRead(); 286 QByteArray ans = _outSocket->readLine();cout << ans.toStdString().c_str() << endl; 287 288 if (ans.indexOf("OK") == -1) { 310 311 QByteArray ans = _outSocket->readLine(); cout << ans.toStdString().c_str() << endl; 312 313 if (ans.indexOf("200") == -1) { 289 314 delete _outSocket; 290 315 _outSocket = 0; 291 emit(newMessage("Broadcaster: Connection broken for " + _mountpoint.toLatin1() + ": " + ans.left(ans.length()-2), true)); 292 } 293 else { 294 emit(newMessage("Broadcaster: Connection opened for " + _mountpoint.toLatin1() + ": " + ans.left(ans.length()-2), true)); 316 emit(newMessage( 317 "Broadcaster: Connection broken for " + _mountpoint.toLatin1() + ": " 318 + ans.left(ans.length() - 2), true)); 319 } else { 320 emit(newMessage( 321 "Broadcaster: Connection opened for " + _mountpoint.toLatin1() + ": " 322 + ans.left(ans.length() - 2), true)); 295 323 _sOpenTrial = 0; 296 324 } 297 325 } 298 326 327 // Try connection to NTRIP Caster or Proxy 328 //////////////////////////////////////////////////////////////////////////// 329 bool bncUploadCaster::connectToHost(QString outHost, int outPort, bool encrypted) { 330 const int timeOut = 5000; // 5 seconds 331 if (encrypted) { 332 _outSocket->connectToHostEncrypted(outHost, outPort); 333 if (!_outSocket->waitForEncrypted(timeOut)) { 334 emit(newMessage( 335 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " (" 336 + outHost.toLatin1() + ":" 337 + QString("%1) ").arg(outPort).toLatin1() 338 + _outSocket->errorString().toLatin1(), true)); 339 delete _outSocket; 340 _outSocket = 0; 341 return false; 342 } 343 } else { 344 _outSocket->connectToHost(outHost, outPort); 345 if (!_outSocket->waitForConnected(timeOut)) { 346 emit(newMessage( 347 "Broadcaster: Connect timeout for " + _mountpoint.toLatin1() + " (" 348 + outHost.toLatin1() + ":" 349 + QString("%1) ").arg(outPort).toLatin1() 350 + _outSocket->errorString().toLatin1(), true)); 351 delete _outSocket; 352 _outSocket = 0; 353 return false; 354 } 355 } 356 return true; 357 } 358 359 360 361 362 363 364 -
trunk/BNC/src/upload/bncuploadcaster.h
r9714 r9715 36 36 private slots: 37 37 void slotProxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*); 38 //void slotSslErrors(QList<QSslError>);38 void slotSslErrors(QList<QSslError>); 39 39 40 40 private: 41 41 void open(); 42 bool connectToHost(QString outHost, int outPort, bool encrypted); 42 43 virtual void run(); 43 44 bool _isToBeDeleted; … … 47 48 QString _casterOutHost; 48 49 int _casterOutPort; 49 QString _proxy Host;50 int _proxy Port;50 QString _proxyOutHost; 51 int _proxyOutPort; 51 52 QString _userName; 52 53 QString _password;
Note:
See TracChangeset
for help on using the changeset viewer.