summaryrefslogtreecommitdiff
path: root/ZeroTierUI/mainwindow.cpp
blob: 255b3989bb89d2063e69bd8578c2d07db55a7b39 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "mainwindow.h"
#include "aboutwindow.h"
#include "networkwidget.h"
#include "ui_mainwindow.h"

#include <string>
#include <map>
#include <set>
#include <vector>
#include <stdexcept>
#include <utility>

#include <QClipboard>
#include <QMutex>
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QMessageBox>
#include <QDebug>
#include <QProcess>
#include <QStringList>
#include <QVBoxLayout>
#include <QScrollBar>

// Globally visible
ZeroTier::Node::LocalClient *zeroTierClient = (ZeroTier::Node::LocalClient *)0;

// Main window instance for app
static MainWindow *mainWindow = (MainWindow *)0;

static void handleZTMessage(void *arg,unsigned long id,const char *line)
{
	static std::map< unsigned long,std::vector<std::string> > ztReplies;
	static QMutex ztReplies_m;

	ztReplies_m.lock();
	if (*line) {
		ztReplies[id].push_back(std::string(line));
		ztReplies_m.unlock();
	} else { // empty lines conclude transmissions
		std::map< unsigned long,std::vector<std::string> >::iterator r(ztReplies.find(id));
		if (r != ztReplies.end()) {
			MainWindow::ZTMessageEvent *event = new MainWindow::ZTMessageEvent(r->second);
			ztReplies.erase(r);
			ztReplies_m.unlock();
			QCoreApplication::postEvent(mainWindow,event); // must post since this may be another thread
		} else ztReplies_m.unlock();
	}
}

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	this->startTimer(1000); // poll service every second
	this->setEnabled(false); // gets enabled when updates are received
	mainWindow = this;
	this->cyclesSinceResponseFromService = 0;

	if (ui->networkListWidget->verticalScrollBar())
		ui->networkListWidget->verticalScrollBar()->setSingleStep(8);

	QWidgetList widgets = this->findChildren<QWidget*>();
	foreach(QWidget* widget, widgets)
		widget->setAttribute(Qt::WA_MacShowFocusRect,false);
}

MainWindow::~MainWindow()
{
	delete ui;
	delete zeroTierClient;
	zeroTierClient = (ZeroTier::Node::LocalClient *)0;
	mainWindow = (MainWindow *)0;
}

void MainWindow::timerEvent(QTimerEvent *event)
{
	QMainWindow::timerEvent(event);

	if (!zeroTierClient) {
		std::string dotAuthFile((QDir::homePath() + QDir::separator() + ".zeroTierOneAuthToken").toStdString());
		std::string authToken;
		if (!ZeroTier::Utils::readFile(dotAuthFile.c_str(),authToken)) {
#ifdef __APPLE__
			// Run the little AppleScript hack that asks for admin credentials and
			// then installs the auth token file in the current user's home.
			QString authHelperPath(QCoreApplication::applicationDirPath() + "/../Resources/helpers/mac/ZeroTier One (Authenticate).app/Contents/MacOS/applet");
			if (!QFile::exists(authHelperPath)) {
				// Allow this to also work from the source tree if it's run from there.
				// This is for debugging purposes and shouldn't harm the live release
				// in any way.
				authHelperPath = QCoreApplication::applicationDirPath() + "/../../../../ZeroTierUI/helpers/mac/ZeroTier One (Authenticate).app/Contents/MacOS/applet";
				if (!QFile::exists(authHelperPath)) {
					QMessageBox::critical(this,"Unable to Locate Helper","Unable to locate authorization helper, cannot obtain authentication token.",QMessageBox::Ok,QMessageBox::NoButton);
					QApplication::exit(1);
					return;
				}
			}
			QProcess::execute(authHelperPath,QStringList());
#endif

			if (!ZeroTier::Utils::readFile(dotAuthFile.c_str(),authToken)) {
				QMessageBox::critical(this,"Cannot Authorize","Unable to authorize this user to administrate ZeroTier One.\n\nTo do so manually, copy 'authtoken.secret' from the ZeroTier One home directory to '.zeroTierOneAuthToken' in your home directory and set file modes on this file to only be readable by you (e.g. 0600 on Mac or Linux systems).",QMessageBox::Ok,QMessageBox::NoButton);
				QApplication::exit(1);
				return;
			}
		}

		zeroTierClient = new ZeroTier::Node::LocalClient(authToken.c_str(),0,&handleZTMessage,this);
	}

	// TODO: do something more user-friendly here... or maybe try to restart
	// the service?
	if (++this->cyclesSinceResponseFromService == 3)
		QMessageBox::critical(this,"No Response from Service","The ZeroTier One service does not appear to be running.",QMessageBox::Ok,QMessageBox::NoButton);

	zeroTierClient->send("info");
	zeroTierClient->send("listnetworks");
	zeroTierClient->send("listpeers");
}

void MainWindow::customEvent(QEvent *event)
{
	ZTMessageEvent *m = (ZTMessageEvent *)event; // only one custom event type so far

	if (m->ztMessage.size() == 0)
		return;

	std::vector<std::string> hdr(ZeroTier::Node::LocalClient::splitLine(m->ztMessage[0]));
	if (hdr.size() < 2)
		return;
	if (hdr[0] != "200")
		return;

	this->cyclesSinceResponseFromService = 0;

	if (hdr[1] == "info") {
		if (hdr.size() >= 3)
			this->myAddress = hdr[2].c_str();
		if (hdr.size() >= 4)
			this->myStatus = hdr[3].c_str();
		if (hdr.size() >= 5)
			this->myVersion = hdr[4].c_str();
	} else if (hdr[1] == "listnetworks") {
		std::map< std::string,std::vector<std::string> > byNwid;
		for(unsigned long i=1;i<m->ztMessage.size();++i) {
			std::vector<std::string> l(ZeroTier::Node::LocalClient::splitLine(m->ztMessage[i]));
			// 200 listnetworks <nwid> <name> <status> <config age> <type> <dev> <ips>
			if ((l.size() == 9)&&(l[2].length() == 16))
				byNwid[l[2]] = l;
		}

		std::map< std::string,std::pair<int,NetworkWidget *> > existingByNwid;
		for(int r=0;r<ui->networkListWidget->count();++r) {
			NetworkWidget *nw = (NetworkWidget *)ui->networkListWidget->itemWidget(ui->networkListWidget->item(r));
			existingByNwid[nw->networkId()] = std::make_pair(r,nw);
		}

		for(std::map< std::string,std::pair<int,NetworkWidget *> >::iterator i(existingByNwid.begin());i!=existingByNwid.end();++i) {
			if (byNwid.count(i->first)) {
				std::vector<std::string> &l = byNwid[i->first];
				i->second.second->setNetworkName(l[3]);
				i->second.second->setStatus(l[4],l[5]);
				i->second.second->setNetworkType(l[6]);
				i->second.second->setNetworkDeviceName(l[7]);
				i->second.second->setIps(l[8]);
			} else {
				delete ui->networkListWidget->takeItem(i->second.first);
			}
		}

		for(std::map< std::string,std::vector<std::string> >::iterator i(byNwid.begin());i!=byNwid.end();++i) {
			if (!existingByNwid.count(i->first)) {
				std::vector<std::string> &l = i->second;
				NetworkWidget *nw = new NetworkWidget((QWidget *)0,i->first);
				nw->setNetworkName(l[3]);
				nw->setStatus(l[4],l[5]);
				nw->setNetworkType(l[6]);
				nw->setNetworkDeviceName(l[7]);
				nw->setIps(l[8]);
				QListWidgetItem *item = new QListWidgetItem();
				item->setSizeHint(nw->sizeHint());
				ui->networkListWidget->addItem(item);
				ui->networkListWidget->setItemWidget(item,nw);
			}
		}
	} else if (hdr[1] == "listpeers") {
		this->numPeers = 0;
		for(unsigned long i=1;i<m->ztMessage.size();++i) {
			std::vector<std::string> l(ZeroTier::Node::LocalClient::splitLine(m->ztMessage[i]));
			if ((l.size() >= 5)&&((l[3] != "-")||(l[4] != "-")))
				++this->numPeers; // number of direct peers online -- check for active IPv4 and/or IPv6 address
		}
	}

	if (this->myAddress.size())
		ui->addressButton->setText(this->myAddress);
	else ui->addressButton->setText("??????????");

	QString st(this->myStatus);
	st += ", v";
	st += this->myVersion;
	st += ", ";
	st += QString::number(this->numPeers);
	st += " direct links to peers";
	ui->statusLabel->setText(st);

	if (this->myStatus == "ONLINE") {
		if (!this->isEnabled())
			this->setEnabled(true);
	} else {
		if (this->isEnabled())
			this->setEnabled(false);
	}
}

void MainWindow::on_joinNetworkButton_clicked()
{
	QString toJoin(ui->networkIdLineEdit->text());
	ui->networkIdLineEdit->setText(QString());

	if (!zeroTierClient) // sanity check
		return;

	if (toJoin.size() != 16) {
		QMessageBox::information(this,"Invalid Network ID","The network ID you entered was not valid. Enter a 16-digit hexadecimal network ID, like '8056c2e21c000001'.",QMessageBox::Ok,QMessageBox::NoButton);
		return;
	}

	zeroTierClient->send((QString("join ") + toJoin).toStdString());
}

void MainWindow::on_actionAbout_triggered()
{
	AboutWindow *about = new AboutWindow(this);
	about->show();
}

void MainWindow::on_networkIdLineEdit_textChanged(const QString &text)
{
	QString newText;
	for(QString::const_iterator i(text.begin());i!=text.end();++i) {
		switch(i->toLatin1()) {
			case '0': newText.append('0'); break;
			case '1': newText.append('1'); break;
			case '2': newText.append('2'); break;
			case '3': newText.append('3'); break;
			case '4': newText.append('4'); break;
			case '5': newText.append('5'); break;
			case '6': newText.append('6'); break;
			case '7': newText.append('7'); break;
			case '8': newText.append('8'); break;
			case '9': newText.append('9'); break;
			case 'a': newText.append('a'); break;
			case 'b': newText.append('b'); break;
			case 'c': newText.append('c'); break;
			case 'd': newText.append('d'); break;
			case 'e': newText.append('e'); break;
			case 'f': newText.append('f'); break;
			case 'A': newText.append('a'); break;
			case 'B': newText.append('b'); break;
			case 'C': newText.append('c'); break;
			case 'D': newText.append('d'); break;
			case 'E': newText.append('e'); break;
			case 'F': newText.append('f'); break;
			default: break;
		}
	}
	if (newText.size() > 16)
		newText.truncate(16);
	ui->networkIdLineEdit->setText(newText);
}

void MainWindow::on_addressButton_clicked()
{
	QApplication::clipboard()->setText(this->myAddress);
}