main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2015 ZeroTier Networks
  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 <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "mainwindow.h"
  31. #include "installdialog.h"
  32. #include "licensedialog.h"
  33. #include <QApplication>
  34. #include <QDir>
  35. #include <QString>
  36. #include <QFont>
  37. #include <QMessageBox>
  38. #include "../node/Constants.hpp"
  39. #include "../node/Defaults.hpp"
  40. // Uncomment for testing to disable making sure Windows service is running
  41. #define DISABLE_WINDOWS_SERVICE_MANAGEMENT
  42. #ifdef __WINDOWS__
  43. #include <WinSock2.h>
  44. #include <windows.h>
  45. #include "../windows/ZeroTierOne/ZeroTierOneService.h"
  46. #ifndef DISABLE_WINDOWS_SERVICE_MANAGEMENT
  47. // Returns true if started or already running, false if failed or not installed
  48. static bool startWindowsService()
  49. {
  50. SERVICE_STATUS ssSvcStatus;
  51. SC_HANDLE schSCManager = NULL;
  52. SC_HANDLE schService = NULL;
  53. schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
  54. if (schSCManager == NULL)
  55. return false;
  56. schService = OpenServiceA(schSCManager, ZT_SERVICE_NAME, SERVICE_QUERY_STATUS | SERVICE_START);
  57. if (schService == NULL) {
  58. CloseServiceHandle(schSCManager);
  59. return false;
  60. }
  61. int tries = 0;
  62. bool running = true;
  63. for(;;) {
  64. memset(&ssSvcStatus,0,sizeof(ssSvcStatus));
  65. if ((++tries > 20)||(!QueryServiceStatus(schService,&ssSvcStatus))) {
  66. running = false;
  67. break;
  68. }
  69. if (ssSvcStatus.dwCurrentState == SERVICE_RUNNING) {
  70. break;
  71. } else if (ssSvcStatus.dwCurrentState == SERVICE_START_PENDING) {
  72. Sleep(500);
  73. continue;
  74. }
  75. StartService(schService,0,NULL);
  76. Sleep(500);
  77. }
  78. CloseServiceHandle(schService);
  79. CloseServiceHandle(schSCManager);
  80. return running;
  81. }
  82. #endif // !DISABLE_WINDOWS_SERVICE_MANAGEMENT
  83. #endif // __WINDOWS__
  84. // Globally visible settings for the app
  85. QSettings *settings = (QSettings *)0;
  86. int main(int argc, char *argv[])
  87. {
  88. QApplication a(argc, argv);
  89. #ifdef __WINDOWS__
  90. // Start up Winsock2
  91. {
  92. WSADATA wsaData;
  93. WSAStartup(MAKEWORD(2,2),&wsaData);
  94. }
  95. #endif
  96. {
  97. QFile qss(":css/stylesheet.css");
  98. qss.open(QFile::ReadOnly);
  99. QString style(qss.readAll());
  100. a.setStyleSheet(style);
  101. }
  102. #ifdef __APPLE__
  103. // If service isn't installed, download and install it
  104. if (!QFile::exists("/Library/Application Support/ZeroTier/One/zerotier-one")) {
  105. // InstallDialog is an alternative main window. It will re-launch the app
  106. // when done.
  107. InstallDialog id;
  108. id.setStyleSheet(a.styleSheet());
  109. id.show();
  110. return a.exec();
  111. }
  112. {
  113. // Put QSettings here because this is one of the writable directories allowed
  114. // in Apple's app store sandbox specs. We might end up in app store someday.
  115. QString zt1AppSupport(QDir::homePath() + "/Library/Application Support/ZeroTier/One");
  116. QDir::root().mkpath(zt1AppSupport);
  117. settings = new QSettings(zt1AppSupport + "/ui.ini",QSettings::IniFormat);
  118. }
  119. #else // on non-Apple boxen put it in the standard place using the default format
  120. settings = new QSettings("ZeroTier Networks","ZeroTier One");
  121. #endif
  122. if (!settings->value("acceptedLicenseV1",false).toBool()) {
  123. LicenseDialog ld;
  124. ld.setStyleSheet(a.styleSheet());
  125. ld.exec();
  126. }
  127. #if defined(__WINDOWS__) && !defined(DISABLE_WINDOWS_SERVICE_MANAGEMENT)
  128. {
  129. bool winSvcInstalled = false;
  130. while (!startWindowsService()) {
  131. if (winSvcInstalled) {
  132. // Service was installed and subsequently failed to start again, so
  133. // something is wrong!
  134. QMessageBox::critical((QWidget *)0,"Service Not Available","Unable to locate or start ZeroTier One service. There may be a problem with the installation. Try installing from the .msi file again or e-mail [email protected] if you cannot install. (Error: service failed to start)",QMessageBox::Ok);
  135. return 1;
  136. }
  137. #ifdef _WIN64
  138. BOOL is64Bit = TRUE;
  139. #else
  140. BOOL is64Bit = FALSE;
  141. IsWow64Process(GetCurrentProcess(),&is64Bit);
  142. #endif
  143. std::string exe(ZeroTier::ZT_DEFAULTS.defaultHomePath + "\\zerotier-one_");
  144. exe.append((is64Bit == TRUE) ? "x64.exe" : "x86.exe");
  145. if (QFile::exists(exe.c_str())) {
  146. STARTUPINFOA si;
  147. PROCESS_INFORMATION pi;
  148. memset(&si,0,sizeof(si));
  149. memset(&pi,0,sizeof(pi));
  150. if (CreateProcessA(NULL,const_cast <LPSTR>((exe + " -I").c_str()),NULL,NULL,FALSE,CREATE_NO_WINDOW|CREATE_NEW_PROCESS_GROUP,NULL,NULL,&si,&pi)) {
  151. WaitForSingleObject(pi.hProcess,INFINITE);
  152. CloseHandle(pi.hProcess);
  153. CloseHandle(pi.hThread);
  154. winSvcInstalled = true;
  155. }
  156. }
  157. if (!winSvcInstalled) {
  158. // Service failed to install -- installation problem like missing .exe
  159. QMessageBox::critical((QWidget *)0,"Service Not Available","Unable to locate or start ZeroTier One service. There may be a problem with the installation. Try installing from the .msi file again or e-mail [email protected] if you cannot install. (Error: service not installed)",QMessageBox::Ok);
  160. return 1;
  161. }
  162. }
  163. }
  164. #endif
  165. MainWindow w;
  166. w.show();
  167. return a.exec();
  168. }