installdialog.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "installdialog.h"
  28. #include "mainwindow.h"
  29. #include "ui_installdialog.h"
  30. #include "../node/Constants.hpp"
  31. #include "../node/Defaults.hpp"
  32. #include "../node/SoftwareUpdater.hpp"
  33. #ifdef __UNIX_LIKE__
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <unistd.h>
  37. #include <sys/stat.h>
  38. #include <fcntl.h>
  39. #endif
  40. #include <QMainWindow>
  41. #include <QMessageBox>
  42. #include <QByteArray>
  43. #include <QSslSocket>
  44. #include <QFile>
  45. #include <QDir>
  46. #include <QProcess>
  47. InstallDialog::InstallDialog(QWidget *parent) :
  48. QDialog(parent),
  49. ui(new Ui::InstallDialog),
  50. nam(new QNetworkAccessManager(this)),
  51. phase(FETCHING_NFO)
  52. {
  53. ui->setupUi(this);
  54. QObject::connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(on_networkReply(QNetworkReply*)));
  55. const char *nfoUrl = ZeroTier::ZT_DEFAULTS.updateLatestNfoURL.c_str();
  56. if (!*nfoUrl) {
  57. QMessageBox::critical(this,"Download Failed","Download failed: internal error: no update URL configured in build!",QMessageBox::Ok,QMessageBox::NoButton);
  58. QApplication::exit(1);
  59. return;
  60. }
  61. QNetworkReply *reply = nam->get(QNetworkRequest(QUrl(nfoUrl)));
  62. QObject::connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(on_downloadProgress(qint64,qint64)));
  63. }
  64. InstallDialog::~InstallDialog()
  65. {
  66. delete ui;
  67. }
  68. void InstallDialog::on_networkReply(QNetworkReply *reply)
  69. {
  70. reply->deleteLater();
  71. if (reply->error() != QNetworkReply::NoError) {
  72. QMessageBox::critical(this,"Download Failed",QString("Download failed: ") + reply->errorString() + "\n\nAre you connected to the Internet?",QMessageBox::Ok,QMessageBox::NoButton);
  73. QApplication::exit(1);
  74. } else {
  75. if (reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200) {
  76. QByteArray installerData(reply->readAll());
  77. switch(phase) {
  78. case FETCHING_NFO: {
  79. unsigned int vMajor = 0,vMinor = 0,vRevision = 0;
  80. installerData.append((char)0);
  81. const char *err = ZeroTier::SoftwareUpdater::parseNfo(installerData.data(),vMajor,vMinor,vRevision,signedBy,signature,url);
  82. if (err) {
  83. QMessageBox::critical(this,"Download Failed","Download failed: there is a problem with the software update web site.\nTry agian later. (invalid .nfo file)",QMessageBox::Ok,QMessageBox::NoButton);
  84. QApplication::exit(1);
  85. return;
  86. }
  87. phase = FETCHING_INSTALLER;
  88. reply = nam->get(QNetworkRequest(QUrl(url.c_str())));
  89. QObject::connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(on_downloadProgress(qint64,qint64)));
  90. } break;
  91. case FETCHING_INSTALLER: {
  92. if (!ZeroTier::SoftwareUpdater::validateUpdate(installerData.data(),installerData.length(),signedBy,signature)) {
  93. QMessageBox::critical(this,"Download Failed","Download failed: there is a problem with the software update web site.\nTry agian later. (failed signature check)",QMessageBox::Ok,QMessageBox::NoButton);
  94. QApplication::exit(1);
  95. return;
  96. }
  97. #ifdef __APPLE__
  98. QString zt1Caches(QDir::homePath() + "/Library/Caches/ZeroTier/One");
  99. QDir::root().mkpath(zt1Caches);
  100. QString instPath(zt1Caches+"/ZeroTierOneInstaller");
  101. int outfd = ::open(instPath.toStdString().c_str(),O_CREAT|O_TRUNC|O_WRONLY,0755);
  102. if (outfd <= 0) {
  103. QMessageBox::critical(this,"Download Failed",QString("Installation failed: unable to write to ")+instPath,QMessageBox::Ok,QMessageBox::NoButton);
  104. QApplication::exit(1);
  105. return;
  106. }
  107. if (::write(outfd,installerData.data(),installerData.length()) != installerData.length()) {
  108. QMessageBox::critical(this,"Installation Failed",QString("Installation failed: unable to write to ")+instPath,QMessageBox::Ok,QMessageBox::NoButton);
  109. QApplication::exit(1);
  110. return;
  111. }
  112. ::close(outfd);
  113. ::chmod(instPath.toStdString().c_str(),0755);
  114. QString installHelperPath(QCoreApplication::applicationDirPath() + "/../Resources/helpers/mac/ZeroTier One (Install).app/Contents/MacOS/applet");
  115. if (!QFile::exists(installHelperPath)) {
  116. QMessageBox::critical(this,"Unable to Locate Helper","Unable to locate install helper, cannot install service.",QMessageBox::Ok,QMessageBox::NoButton);
  117. QApplication::exit(1);
  118. return;
  119. }
  120. // Terminate the GUI and execute the install helper instead
  121. ::execl(installHelperPath.toStdString().c_str(),installHelperPath.toStdString().c_str(),(const char *)0);
  122. // We only make it here if execl() failed
  123. QMessageBox::critical(this,"Unable to Locate Helper","Unable to locate install helper, cannot install service.",QMessageBox::Ok,QMessageBox::NoButton);
  124. QApplication::exit(1);
  125. return;
  126. #endif
  127. } break;
  128. }
  129. ui->progressBar->setMinimum(0);
  130. ui->progressBar->setMaximum(100);
  131. ui->progressBar->setValue(0);
  132. } else {
  133. QMessageBox::critical(this,"Download Failed",QString("Download failed: HTTP status code ") + reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString(),QMessageBox::Ok,QMessageBox::NoButton);
  134. QApplication::exit(1);
  135. }
  136. }
  137. }
  138. void InstallDialog::on_InstallDialog_rejected()
  139. {
  140. QApplication::exit();
  141. }
  142. void InstallDialog::on_cancelButton_clicked()
  143. {
  144. QApplication::exit();
  145. }
  146. void InstallDialog::on_downloadProgress(qint64 bytesReceived,qint64 bytesTotal)
  147. {
  148. if (bytesTotal <= 0) {
  149. ui->progressBar->setValue(0);
  150. ui->progressBar->setMinimum(0);
  151. ui->progressBar->setMaximum(0);
  152. } else {
  153. double pct = ((double)bytesReceived / (double)bytesTotal) * 100.0;
  154. if (pct > 100.0)
  155. pct = 100.0;
  156. ui->progressBar->setMinimum(0);
  157. ui->progressBar->setMaximum(100);
  158. ui->progressBar->setValue((int)pct);
  159. }
  160. }