mainwindow.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "mainwindow.h"
  2. #include "aboutwindow.h"
  3. #include "network.h"
  4. #include "ui_mainwindow.h"
  5. #include <string>
  6. #include <map>
  7. #include <set>
  8. #include <vector>
  9. #include <stdexcept>
  10. #include <utility>
  11. #include <QClipboard>
  12. #include <QMutex>
  13. #include <QCoreApplication>
  14. #include <QDir>
  15. #include <QFile>
  16. #include <QMessageBox>
  17. #include <QDebug>
  18. #include <QProcess>
  19. #include <QStringList>
  20. #include <QVBoxLayout>
  21. // Globally visible
  22. ZeroTier::Node::LocalClient *zeroTierClient = (ZeroTier::Node::LocalClient *)0;
  23. // Main window instance for app
  24. static MainWindow *mainWindow = (MainWindow *)0;
  25. static void handleZTMessage(void *arg,unsigned long id,const char *line)
  26. {
  27. static std::map< unsigned long,std::vector<std::string> > ztReplies;
  28. static QMutex ztReplies_m;
  29. ztReplies_m.lock();
  30. if (*line) {
  31. ztReplies[id].push_back(std::string(line));
  32. ztReplies_m.unlock();
  33. } else { // empty lines conclude transmissions
  34. std::map< unsigned long,std::vector<std::string> >::iterator r(ztReplies.find(id));
  35. if (r != ztReplies.end()) {
  36. MainWindow::ZTMessageEvent *event = new MainWindow::ZTMessageEvent(r->second);
  37. ztReplies.erase(r);
  38. ztReplies_m.unlock();
  39. QCoreApplication::postEvent(mainWindow,event); // must post since this may be another thread
  40. } else ztReplies_m.unlock();
  41. }
  42. }
  43. MainWindow::MainWindow(QWidget *parent) :
  44. QMainWindow(parent),
  45. ui(new Ui::MainWindow)
  46. {
  47. ui->setupUi(this);
  48. this->startTimer(1000); // poll service every second
  49. this->setEnabled(false); // gets enabled when updates are received
  50. mainWindow = this;
  51. this->cyclesSinceResponseFromService = 0;
  52. }
  53. MainWindow::~MainWindow()
  54. {
  55. delete ui;
  56. delete zeroTierClient;
  57. zeroTierClient = (ZeroTier::Node::LocalClient *)0;
  58. mainWindow = (MainWindow *)0;
  59. }
  60. void MainWindow::timerEvent(QTimerEvent *event)
  61. {
  62. QMainWindow::timerEvent(event);
  63. if (!zeroTierClient) {
  64. std::string dotAuthFile((QDir::homePath() + QDir::separator() + ".zeroTierOneAuthToken").toStdString());
  65. std::string authToken;
  66. if (!ZeroTier::Utils::readFile(dotAuthFile.c_str(),authToken)) {
  67. #ifdef __APPLE__
  68. // Run the little AppleScript hack that asks for admin credentials and
  69. // then installs the auth token file in the current user's home.
  70. QString authHelperPath(QCoreApplication::applicationDirPath() + "/../Resources/helpers/mac/ZeroTier One (Authenticate).app/Contents/MacOS/applet");
  71. if (!QFile::exists(authHelperPath)) {
  72. // Allow this to also work from the source tree if it's run from there.
  73. // This is for debugging purposes and shouldn't harm the live release
  74. // in any way.
  75. authHelperPath = QCoreApplication::applicationDirPath() + "/../../../../ZeroTierUI/helpers/mac/ZeroTier One (Authenticate).app/Contents/MacOS/applet";
  76. if (!QFile::exists(authHelperPath)) {
  77. QMessageBox::critical(this,"Unable to Locate Helper","Unable to locate authorization helper, cannot obtain authentication token.",QMessageBox::Ok,QMessageBox::NoButton);
  78. QApplication::exit(1);
  79. return;
  80. }
  81. }
  82. QProcess::execute(authHelperPath,QStringList());
  83. #endif
  84. if (!ZeroTier::Utils::readFile(dotAuthFile.c_str(),authToken)) {
  85. 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);
  86. QApplication::exit(1);
  87. return;
  88. }
  89. }
  90. zeroTierClient = new ZeroTier::Node::LocalClient(authToken.c_str(),0,&handleZTMessage,this);
  91. }
  92. // TODO: do something more user-friendly here... or maybe try to restart
  93. // the service?
  94. if (++this->cyclesSinceResponseFromService == 3)
  95. QMessageBox::critical(this,"No Response from Service","The ZeroTier One service does not appear to be running.",QMessageBox::Ok,QMessageBox::NoButton);
  96. zeroTierClient->send("info");
  97. zeroTierClient->send("listnetworks");
  98. zeroTierClient->send("listpeers");
  99. }
  100. void MainWindow::customEvent(QEvent *event)
  101. {
  102. ZTMessageEvent *m = (ZTMessageEvent *)event; // only one custom event type so far
  103. if (m->ztMessage.size() == 0)
  104. return;
  105. std::vector<std::string> hdr(ZeroTier::Node::LocalClient::splitLine(m->ztMessage[0]));
  106. if (hdr.size() < 2)
  107. return;
  108. if (hdr[0] != "200")
  109. return;
  110. this->cyclesSinceResponseFromService = 0;
  111. if (hdr[1] == "info") {
  112. if (hdr.size() >= 3)
  113. this->myAddress = hdr[2].c_str();
  114. if (hdr.size() >= 4)
  115. this->myStatus = hdr[3].c_str();
  116. if (hdr.size() >= 5)
  117. this->myVersion = hdr[4].c_str();
  118. } else if (hdr[1] == "listnetworks") {
  119. std::map< std::string,std::vector<std::string> > byNwid;
  120. for(unsigned long i=1;i<m->ztMessage.size();++i) {
  121. std::vector<std::string> l(ZeroTier::Node::LocalClient::splitLine(m->ztMessage[i]));
  122. // 200 listnetworks <nwid> <name> <status> <config age> <type> <dev> <ips>
  123. if ((l.size() == 9)&&(l[2].length() == 16))
  124. byNwid[l[2]] = l;
  125. }
  126. std::map< std::string,std::pair<int,Network *> > existingByNwid;
  127. for(int r=0;r<ui->networkListWidget->count();++r) {
  128. Network *nw = (Network *)ui->networkListWidget->itemWidget(ui->networkListWidget->item(r));
  129. existingByNwid[nw->networkId()] = std::make_pair(r,nw);
  130. }
  131. for(std::map< std::string,std::pair<int,Network *> >::iterator i(existingByNwid.begin());i!=existingByNwid.end();++i) {
  132. if (byNwid.count(i->first)) {
  133. std::vector<std::string> &l = byNwid[i->first];
  134. i->second.second->setNetworkName(l[3]);
  135. i->second.second->setStatus(l[4],l[5]);
  136. i->second.second->setNetworkType(l[6]);
  137. i->second.second->setNetworkDeviceName(l[7]);
  138. i->second.second->setIps(l[8]);
  139. } else {
  140. delete ui->networkListWidget->takeItem(i->second.first);
  141. }
  142. }
  143. for(std::map< std::string,std::vector<std::string> >::iterator i(byNwid.begin());i!=byNwid.end();++i) {
  144. if (!existingByNwid.count(i->first)) {
  145. std::vector<std::string> &l = i->second;
  146. Network *nw = new Network((QWidget *)0,i->first);
  147. nw->setNetworkName(l[3]);
  148. nw->setStatus(l[4],l[5]);
  149. nw->setNetworkType(l[6]);
  150. nw->setNetworkDeviceName(l[7]);
  151. nw->setIps(l[8]);
  152. QListWidgetItem *item = new QListWidgetItem();
  153. item->setSizeHint(nw->sizeHint());
  154. ui->networkListWidget->addItem(item);
  155. ui->networkListWidget->setItemWidget(item,nw);
  156. }
  157. }
  158. } else if (hdr[1] == "listpeers") {
  159. this->numPeers = 0;
  160. for(unsigned long i=1;i<m->ztMessage.size();++i) {
  161. std::vector<std::string> l(ZeroTier::Node::LocalClient::splitLine(m->ztMessage[i]));
  162. if ((l.size() >= 5)&&((l[3] != "-")||(l[4] != "-")))
  163. ++this->numPeers; // number of direct peers online -- check for active IPv4 and/or IPv6 address
  164. }
  165. }
  166. if (this->myAddress.size()) {
  167. QString st(this->myAddress);
  168. st += " (";
  169. st += this->myStatus;
  170. st += ", v";
  171. st += this->myVersion;
  172. st += ", ";
  173. st += QString::number(this->numPeers);
  174. st += " peers)";
  175. while (st.size() < 45)
  176. st += QChar::Space;
  177. ui->statusAndAddressButton->setText(st);
  178. }
  179. if (this->myStatus == "ONLINE") {
  180. if (!this->isEnabled())
  181. this->setEnabled(true);
  182. } else {
  183. if (this->isEnabled())
  184. this->setEnabled(false);
  185. }
  186. }
  187. void MainWindow::on_joinNetworkButton_clicked()
  188. {
  189. QString toJoin(ui->networkIdLineEdit->text());
  190. ui->networkIdLineEdit->setText(QString());
  191. if (!zeroTierClient) // sanity check
  192. return;
  193. if (toJoin.size() != 16) {
  194. 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);
  195. return;
  196. }
  197. zeroTierClient->send((QString("join ") + toJoin).toStdString());
  198. }
  199. void MainWindow::on_actionAbout_triggered()
  200. {
  201. AboutWindow *about = new AboutWindow(this);
  202. about->show();
  203. }
  204. void MainWindow::on_networkIdLineEdit_textChanged(const QString &text)
  205. {
  206. QString newText;
  207. for(QString::const_iterator i(text.begin());i!=text.end();++i) {
  208. switch(i->toLatin1()) {
  209. case '0': newText.append('0'); break;
  210. case '1': newText.append('1'); break;
  211. case '2': newText.append('2'); break;
  212. case '3': newText.append('3'); break;
  213. case '4': newText.append('4'); break;
  214. case '5': newText.append('5'); break;
  215. case '6': newText.append('6'); break;
  216. case '7': newText.append('7'); break;
  217. case '8': newText.append('8'); break;
  218. case '9': newText.append('9'); break;
  219. case 'a': newText.append('a'); break;
  220. case 'b': newText.append('b'); break;
  221. case 'c': newText.append('c'); break;
  222. case 'd': newText.append('d'); break;
  223. case 'e': newText.append('e'); break;
  224. case 'f': newText.append('f'); break;
  225. case 'A': newText.append('a'); break;
  226. case 'B': newText.append('b'); break;
  227. case 'C': newText.append('c'); break;
  228. case 'D': newText.append('d'); break;
  229. case 'E': newText.append('e'); break;
  230. case 'F': newText.append('f'); break;
  231. default: break;
  232. }
  233. }
  234. if (newText.size() > 16)
  235. newText.truncate(16);
  236. ui->networkIdLineEdit->setText(newText);
  237. }
  238. void MainWindow::on_statusAndAddressButton_clicked()
  239. {
  240. QApplication::clipboard()->setText(this->myAddress);
  241. }