summaryrefslogtreecommitdiff
path: root/ZeroTierUI/main.cpp
blob: e3d24939e4e03b673716cdec1eca96d3f6fc086b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * ZeroTier One - Network Virtualization Everywhere
 * Copyright (C) 2011-2015  ZeroTier, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --
 *
 * ZeroTier may be used and distributed under the terms of the GPLv3, which
 * are available at: http://www.gnu.org/licenses/gpl-3.0.html
 *
 * If you would like to embed ZeroTier into a commercial application or
 * redistribute it in a modified binary form, please contact ZeroTier Networks
 * LLC. Start here: http://www.zerotier.com/
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mainwindow.h"
#include "installdialog.h"
#include "licensedialog.h"

#include <QApplication>
#include <QDir>
#include <QString>
#include <QFont>
#include <QMessageBox>

#include "../node/Constants.hpp"
#include "../node/Defaults.hpp"

// Uncomment for testing to disable making sure Windows service is running
#define DISABLE_WINDOWS_SERVICE_MANAGEMENT

#ifdef __WINDOWS__
#include <WinSock2.h>
#include <windows.h>
#include "../windows/ZeroTierOne/ZeroTierOneService.h"

#ifndef DISABLE_WINDOWS_SERVICE_MANAGEMENT
// Returns true if started or already running, false if failed or not installed
static bool startWindowsService()
{
	SERVICE_STATUS ssSvcStatus;
	SC_HANDLE schSCManager = NULL;
	SC_HANDLE schService = NULL;

	schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
	if (schSCManager == NULL)
		return false;

	schService = OpenServiceA(schSCManager, ZT_SERVICE_NAME, SERVICE_QUERY_STATUS | SERVICE_START);
	if (schService == NULL) {
		CloseServiceHandle(schSCManager);
		return false;
	}

	int tries = 0;
	bool running = true;

	for(;;) {
		memset(&ssSvcStatus,0,sizeof(ssSvcStatus));
		if ((++tries > 20)||(!QueryServiceStatus(schService,&ssSvcStatus))) {
			running = false;
			break;
		}

		if (ssSvcStatus.dwCurrentState == SERVICE_RUNNING) {
			break;
		} else if (ssSvcStatus.dwCurrentState == SERVICE_START_PENDING) {
			Sleep(500);
			continue;
		}

		StartService(schService,0,NULL);
		Sleep(500);
	}

	CloseServiceHandle(schService);
	CloseServiceHandle(schSCManager);
	return running;
}
#endif // !DISABLE_WINDOWS_SERVICE_MANAGEMENT
#endif // __WINDOWS__

// Globally visible settings for the app
QSettings *settings = (QSettings *)0;

int main(int argc, char *argv[])
{
	QApplication a(argc, argv);

#ifdef __WINDOWS__
	// Start up Winsock2
	{
		WSADATA wsaData;
		WSAStartup(MAKEWORD(2,2),&wsaData);
	}
#endif

	{
		QFile qss(":css/stylesheet.css");
		qss.open(QFile::ReadOnly);
		QString style(qss.readAll());
		a.setStyleSheet(style);
	}

#ifdef __APPLE__
	// If service isn't installed, download and install it
	if (!QFile::exists("/Library/Application Support/ZeroTier/One/zerotier-one")) {
		// InstallDialog is an alternative main window. It will re-launch the app
		// when done.
		InstallDialog id;
		id.setStyleSheet(a.styleSheet());
		id.show();
		return a.exec();
	}

	{
		// Put QSettings here because this is one of the writable directories allowed
		// in Apple's app store sandbox specs. We might end up in app store someday.
		QString zt1AppSupport(QDir::homePath() + "/Library/Application Support/ZeroTier/One");
		QDir::root().mkpath(zt1AppSupport);
		settings = new QSettings(zt1AppSupport + "/ui.ini",QSettings::IniFormat);
	}
#else // on non-Apple boxen put it in the standard place using the default format
	settings = new QSettings("ZeroTier Networks","ZeroTier One");
#endif

	if (!settings->value("acceptedLicenseV1",false).toBool()) {
		LicenseDialog ld;
		ld.setStyleSheet(a.styleSheet());
		ld.exec();
	}

#if defined(__WINDOWS__) && !defined(DISABLE_WINDOWS_SERVICE_MANAGEMENT)
	{
		bool winSvcInstalled = false;
		while (!startWindowsService()) {
			if (winSvcInstalled) {
				// Service was installed and subsequently failed to start again, so
				// something is wrong!
				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 contact@zerotier.com if you cannot install. (Error: service failed to start)",QMessageBox::Ok);
				return 1;
			}

#ifdef _WIN64
			BOOL is64Bit = TRUE;
#else
			BOOL is64Bit = FALSE;
			IsWow64Process(GetCurrentProcess(),&is64Bit);
#endif
			std::string exe(ZeroTier::ZT_DEFAULTS.defaultHomePath + "\\zerotier-one_");
			exe.append((is64Bit == TRUE) ? "x64.exe" : "x86.exe");

			if (QFile::exists(exe.c_str())) {
				STARTUPINFOA si;
				PROCESS_INFORMATION pi;
				memset(&si,0,sizeof(si));
				memset(&pi,0,sizeof(pi));
				if (CreateProcessA(NULL,const_cast <LPSTR>((exe + " -I").c_str()),NULL,NULL,FALSE,CREATE_NO_WINDOW|CREATE_NEW_PROCESS_GROUP,NULL,NULL,&si,&pi)) {
					WaitForSingleObject(pi.hProcess,INFINITE);
					CloseHandle(pi.hProcess);
					CloseHandle(pi.hThread);
					winSvcInstalled = true;
				}
			}

			if (!winSvcInstalled) {
				// Service failed to install -- installation problem like missing .exe
				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 contact@zerotier.com if you cannot install. (Error: service not installed)",QMessageBox::Ok);
				return 1;
			}
		}
	}
#endif

	MainWindow w;
	w.show();
	return a.exec();
}