2013-11-13 21:50:49 +00:00
# include "mainwindow.h"
2013-11-15 22:04:32 +00:00
# include "aboutwindow.h"
2013-12-12 23:47:00 +00:00
# include "networkwidget.h"
2013-11-13 21:50:49 +00:00
# include "ui_mainwindow.h"
2013-11-18 20:06:05 +00:00
# include <string>
# include <map>
2013-11-20 23:29:02 +00:00
# include <set>
2013-11-18 20:06:05 +00:00
# include <vector>
2013-11-19 20:05:14 +00:00
# include <stdexcept>
2013-11-21 18:45:44 +00:00
# include <utility>
2013-11-18 20:06:05 +00:00
2013-11-15 22:04:32 +00:00
# include <QClipboard>
2013-11-18 20:06:05 +00:00
# include <QMutex>
2013-11-19 20:05:14 +00:00
# include <QCoreApplication>
# include <QDir>
# include <QFile>
# include <QMessageBox>
# include <QDebug>
2013-11-20 17:19:37 +00:00
# include <QProcess>
# include <QStringList>
2013-11-20 23:29:02 +00:00
# include <QVBoxLayout>
2013-12-20 00:23:41 +00:00
# include <QScrollBar>
2013-12-21 00:07:20 +00:00
# include <QEventLoop>
2013-11-18 20:06:05 +00:00
2014-01-03 23:39:09 +00:00
QNetworkAccessManager * nam ;
2013-11-20 19:10:33 +00:00
// Globally visible
ZeroTier : : Node : : LocalClient * zeroTierClient = ( ZeroTier : : Node : : LocalClient * ) 0 ;
// Main window instance for app
static MainWindow * mainWindow = ( MainWindow * ) 0 ;
2013-11-18 20:06:05 +00:00
static void handleZTMessage ( void * arg , unsigned long id , const char * line )
{
2013-11-20 19:10:33 +00:00
static std : : map < unsigned long , std : : vector < std : : string > > ztReplies ;
static QMutex ztReplies_m ;
2013-11-18 20:06:05 +00:00
ztReplies_m . lock ( ) ;
if ( * line ) {
ztReplies [ id ] . push_back ( std : : string ( line ) ) ;
ztReplies_m . unlock ( ) ;
2013-11-19 20:05:14 +00:00
} else { // empty lines conclude transmissions
2013-11-20 19:10:33 +00:00
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 ( ) ;
2013-11-18 20:06:05 +00:00
}
}
2013-11-13 21:50:49 +00:00
MainWindow : : MainWindow ( QWidget * parent ) :
QMainWindow ( parent ) ,
2014-01-03 23:39:09 +00:00
ui ( new Ui : : MainWindow ) ,
nam ( new QNetworkAccessManager ( this ) )
2013-11-13 21:50:49 +00:00
{
ui - > setupUi ( this ) ;
2013-11-21 20:11:22 +00:00
this - > startTimer ( 1000 ) ; // poll service every second
2013-11-20 17:19:37 +00:00
this - > setEnabled ( false ) ; // gets enabled when updates are received
2013-11-20 19:10:33 +00:00
mainWindow = this ;
2013-11-21 20:11:22 +00:00
this - > cyclesSinceResponseFromService = 0 ;
2013-12-19 22:59:52 +00:00
2014-01-03 23:39:09 +00:00
QObject : : connect ( nam , SIGNAL ( finished ( QNetworkReply * ) ) , this , SLOT ( on_networkReply ( QNetworkReply * ) ) ) ;
2013-12-20 00:23:41 +00:00
if ( ui - > networkListWidget - > verticalScrollBar ( ) )
ui - > networkListWidget - > verticalScrollBar ( ) - > setSingleStep ( 8 ) ;
2013-12-19 22:59:52 +00:00
QWidgetList widgets = this - > findChildren < QWidget * > ( ) ;
foreach ( QWidget * widget , widgets )
widget - > setAttribute ( Qt : : WA_MacShowFocusRect , false ) ;
2013-11-13 21:50:49 +00:00
}
MainWindow : : ~ MainWindow ( )
{
delete ui ;
2013-11-20 17:19:37 +00:00
delete zeroTierClient ;
zeroTierClient = ( ZeroTier : : Node : : LocalClient * ) 0 ;
2013-11-20 19:10:33 +00:00
mainWindow = ( MainWindow * ) 0 ;
2013-11-13 21:50:49 +00:00
}
2013-11-15 22:04:32 +00:00
2013-11-19 20:05:14 +00:00
void MainWindow : : timerEvent ( QTimerEvent * event )
{
2013-12-21 00:07:20 +00:00
event - > accept ( ) ;
2013-11-19 20:05:14 +00:00
if ( ! zeroTierClient ) {
std : : string authToken ;
2014-01-03 23:39:09 +00:00
if ( ! ZeroTier : : Utils : : readFile ( ZeroTier : : Node : : LocalClient : : authTokenDefaultUserPath ( ) . c_str ( ) , authToken ) ) {
2013-11-19 20:05:14 +00:00
# ifdef __APPLE__
2014-01-03 23:39:09 +00:00
if ( QFile : : exists ( " /Library/Application Support/ZeroTier/One/zerotier-one " ) ) {
// 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 " ) ;
2013-11-19 20:05:14 +00:00
if ( ! QFile : : exists ( authHelperPath ) ) {
2014-01-03 23:39:09 +00:00
// 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 ( ) ) ;
} else {
// Download the latest version and install it
this - > setEnabled ( false ) ;
// Run the little AppleScript hack that asks for admin credentials and
// then installs the auth token file in the current user's home.
QString installHelperPath ( QCoreApplication : : applicationDirPath ( ) + " /../Resources/helpers/mac/ZeroTier One (Install).app/Contents/MacOS/applet " ) ;
if ( ! QFile : : exists ( installHelperPath ) ) {
QMessageBox : : critical ( this , " Unable to Locate Helper " , " Unable to locate install helper, cannot install service. " , QMessageBox : : Ok , QMessageBox : : NoButton ) ;
2013-11-19 20:05:14 +00:00
QApplication : : exit ( 1 ) ;
2013-11-20 17:19:37 +00:00
return ;
2013-11-19 20:05:14 +00:00
}
2014-01-03 23:39:09 +00:00
QProcess : : execute ( installHelperPath , QStringList ( ) ) ;
2013-11-19 20:05:14 +00:00
}
# endif
2013-11-20 17:19:37 +00:00
if ( ! ZeroTier : : Utils : : readFile ( dotAuthFile . c_str ( ) , authToken ) ) {
QMessageBox : : critical ( this , " Cannot Authorize " , " Unable to authorize this user to administrate ZeroTier One. \n \n To 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 ;
}
2013-11-19 20:05:14 +00:00
}
2013-11-20 17:19:37 +00:00
zeroTierClient = new ZeroTier : : Node : : LocalClient ( authToken . c_str ( ) , 0 , & handleZTMessage , this ) ;
2013-11-19 20:05:14 +00:00
}
2013-11-20 19:10:33 +00:00
2013-11-21 20:11:22 +00:00
// 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 ) ;
2013-11-20 19:10:33 +00:00
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 ;
2013-11-21 20:11:22 +00:00
this - > cyclesSinceResponseFromService = 0 ;
2013-11-20 19:10:33 +00:00
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 " ) {
2013-12-21 00:07:20 +00:00
std : : map < std : : string , std : : vector < std : : string > > newNetworks ;
2013-11-20 21:16:30 +00:00
for ( unsigned long i = 1 ; i < m - > ztMessage . size ( ) ; + + i ) {
std : : vector < std : : string > l ( ZeroTier : : Node : : LocalClient : : splitLine ( m - > ztMessage [ i ] ) ) ;
2013-11-21 20:11:22 +00:00
// 200 listnetworks <nwid> <name> <status> <config age> <type> <dev> <ips>
if ( ( l . size ( ) = = 9 ) & & ( l [ 2 ] . length ( ) = = 16 ) )
2013-12-21 00:07:20 +00:00
newNetworks [ l [ 2 ] ] = l ;
2013-11-21 18:45:44 +00:00
}
2013-12-21 00:07:20 +00:00
if ( newNetworks ! = networks ) {
networks = newNetworks ;
for ( bool removed = true ; removed ; ) {
removed = false ;
for ( int r = 0 ; r < ui - > networkListWidget - > count ( ) ; + + r ) {
NetworkWidget * nw = ( NetworkWidget * ) ui - > networkListWidget - > itemWidget ( ui - > networkListWidget - > item ( r ) ) ;
if ( ! networks . count ( nw - > networkId ( ) ) ) {
ui - > networkListWidget - > setVisible ( false ) ; // HACK to prevent an occasional crash here, discovered through hours of shotgun debugging... :P
delete ui - > networkListWidget - > takeItem ( r ) ;
removed = true ;
break ;
}
}
2013-11-21 18:45:44 +00:00
}
2013-12-21 00:07:20 +00:00
ui - > networkListWidget - > setVisible ( true ) ;
std : : set < std : : string > alreadyDisplayed ;
for ( int r = 0 ; r < ui - > networkListWidget - > count ( ) ; + + r ) {
NetworkWidget * nw = ( NetworkWidget * ) ui - > networkListWidget - > itemWidget ( ui - > networkListWidget - > item ( r ) ) ;
if ( networks . count ( nw - > networkId ( ) ) > 0 ) {
alreadyDisplayed . insert ( nw - > networkId ( ) ) ;
std : : vector < std : : string > & l = networks [ nw - > networkId ( ) ] ;
nw - > setNetworkName ( l [ 3 ] ) ;
nw - > setStatus ( l [ 4 ] , l [ 5 ] ) ;
nw - > setNetworkType ( l [ 6 ] ) ;
nw - > setNetworkDeviceName ( l [ 7 ] ) ;
nw - > setIps ( l [ 8 ] ) ;
}
}
for ( std : : map < std : : string , std : : vector < std : : string > > : : iterator nwdata ( networks . begin ( ) ) ; nwdata ! = networks . end ( ) ; + + nwdata ) {
if ( alreadyDisplayed . count ( nwdata - > first ) = = 0 ) {
std : : vector < std : : string > & l = nwdata - > second ;
NetworkWidget * nw = new NetworkWidget ( ( QWidget * ) 0 , nwdata - > 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 ) ;
}
2013-11-20 23:29:02 +00:00
}
2013-11-20 21:16:30 +00:00
}
2013-11-20 19:10:33 +00:00
} 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
}
}
2013-12-19 22:59:52 +00:00
if ( this - > myAddress . size ( ) )
ui - > addressButton - > setText ( this - > myAddress ) ;
2013-12-21 00:07:20 +00:00
else ui - > addressButton - > setText ( " " ) ;
2013-12-19 22:59:52 +00:00
QString st ( this - > myStatus ) ;
st + = " , v " ;
st + = this - > myVersion ;
st + = " , " ;
st + = QString : : number ( this - > numPeers ) ;
st + = " direct links to peers " ;
ui - > statusLabel - > setText ( st ) ;
2013-11-21 20:11:22 +00:00
if ( this - > myStatus = = " ONLINE " ) {
if ( ! this - > isEnabled ( ) )
this - > setEnabled ( true ) ;
} else {
if ( this - > isEnabled ( ) )
this - > setEnabled ( false ) ;
}
2013-11-19 20:05:14 +00:00
}
2013-11-15 22:04:32 +00:00
void MainWindow : : on_joinNetworkButton_clicked ( )
{
2013-11-20 21:16:30 +00:00
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 ( ) ) ;
2013-11-15 22:04:32 +00:00
}
void MainWindow : : on_actionAbout_triggered ( )
{
AboutWindow * about = new AboutWindow ( this ) ;
about - > show ( ) ;
}
void MainWindow : : on_networkIdLineEdit_textChanged ( const QString & text )
{
2013-11-18 17:01:33 +00:00
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 ;
}
}
2013-11-20 23:29:02 +00:00
if ( newText . size ( ) > 16 )
newText . truncate ( 16 ) ;
2013-11-18 17:01:33 +00:00
ui - > networkIdLineEdit - > setText ( newText ) ;
2013-11-15 22:04:32 +00:00
}
2013-12-19 22:59:52 +00:00
void MainWindow : : on_addressButton_clicked ( )
2013-11-15 22:04:32 +00:00
{
2013-11-20 19:10:33 +00:00
QApplication : : clipboard ( ) - > setText ( this - > myAddress ) ;
2013-11-15 22:04:32 +00:00
}
2014-01-03 23:39:09 +00:00
void MainWindow : : on_networkReply ( QNetworkReply * reply )
{
}