main.cpp 5.6 KB

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