RetroShare/retroshare-gui/src/TorControl/TorControlWindow.cpp

231 lines
6.5 KiB
C++
Raw Normal View History

2017-12-21 18:16:32 +01:00
#include <unistd.h>
#include <set>
2017-12-21 18:16:32 +01:00
2017-12-21 15:32:42 +01:00
#include <QTimer>
#include <QFile>
2017-12-21 18:16:32 +01:00
#include <QTcpServer>
#include <QGraphicsDropShadowEffect>
2017-12-21 18:16:32 +01:00
2017-12-21 15:32:42 +01:00
#include <iostream>
#include "util/rstime.h"
2017-12-21 15:32:42 +01:00
#include "TorControlWindow.h"
2017-12-21 18:16:32 +01:00
#include "TorManager.h"
2017-12-21 15:32:42 +01:00
#include "TorControl.h"
#include "HiddenService.h"
TorControlDialog::TorControlDialog(Tor::TorManager *tm,QWidget *parent)
: mTorManager(tm)
{
setupUi(this) ;
QObject::connect(tm->control(),SIGNAL(statusChanged(int,int)),this,SLOT(statusChanged())) ;
QObject::connect(tm->control(),SIGNAL(connected()),this,SLOT(statusChanged()));
QObject::connect(tm->control(),SIGNAL(disconnected()),this,SLOT(statusChanged()));
QObject::connect(tm->control(),SIGNAL(bootstrapStatusChanged()),this,SLOT(statusChanged()));
QObject::connect(tm->control(),SIGNAL(connectivityChanged()),this,SLOT(statusChanged()));
QObject::connect(tm ,SIGNAL(errorChanged()),this,SLOT(statusChanged()));
2017-12-21 15:32:42 +01:00
2017-12-21 18:16:32 +01:00
//QTimer::singleShot(2000,this,SLOT(checkForHiddenService())) ;
mIncomingServer = new QTcpServer(this) ;
mHiddenService = NULL ;
mHiddenServiceStatus = HIDDEN_SERVICE_STATUS_UNKNOWN;
//mBootstrapPhaseFinished = false ;
2017-12-21 18:16:32 +01:00
connect(mIncomingServer, SIGNAL(QTcpServer::newConnection()), this, SLOT(onIncomingConnection()));
QTimer *timer = new QTimer ;
QObject::connect(timer,SIGNAL(timeout()),this,SLOT(showLog())) ;
timer->start(500) ;
// Hide some debug output for the released version
setWindowFlags( Qt::Dialog | Qt::FramelessWindowHint );
adjustSize();
2017-12-21 18:16:32 +01:00
}
void TorControlDialog::onIncomingConnection()
{
std::cerr << "Incoming connection !!" << std::endl;
2017-12-21 15:32:42 +01:00
}
void TorControlDialog::statusChanged()
{
int tor_control_status = mTorManager->control()->status();
2017-12-21 15:32:42 +01:00
int torstatus = mTorManager->control()->torStatus();
QString tor_control_status_str,torstatus_str ;
2017-12-21 15:32:42 +01:00
if(mTorManager->hasError())
mErrorMsg = mTorManager->errorMessage() ;
switch(tor_control_status)
2017-12-21 15:32:42 +01:00
{
default:
case Tor::TorControl::Error : tor_control_status_str = "Error" ; break ;
case Tor::TorControl::NotConnected: tor_control_status_str = "Not connected" ; break ;
case Tor::TorControl::Connecting: tor_control_status_str = "Connecting" ; break ;
case Tor::TorControl::Authenticating: tor_control_status_str = "Authenticating" ; break ;
case Tor::TorControl::Connected: tor_control_status_str = "Connected" ; break ;
2017-12-21 15:32:42 +01:00
}
switch(torstatus)
{
default:
case Tor::TorControl::TorUnknown: torstatus_str = "Unknown" ; break ;
case Tor::TorControl::TorOffline: torstatus_str = "Tor offline" ; break ;
2017-12-21 18:16:32 +01:00
case Tor::TorControl::TorReady: torstatus_str = "Tor ready" ; break ;
2017-12-21 15:32:42 +01:00
}
torStatus_LB->setText(torstatus_str) ;
2018-01-20 17:10:47 +01:00
if(torstatus == Tor::TorControl::TorUnknown)
torStatus_LB->setToolTip(tr("Check that Tor is accessible in your executable path")) ;
else
torStatus_LB->setToolTip("") ;
2017-12-21 15:32:42 +01:00
QVariantMap qvm = mTorManager->control()->bootstrapStatus();
QString bootstrapstatus_str ;
std::cerr << "Tor control status: " << tor_control_status_str.toStdString() << std::endl;
std::cerr << "Tor status: " << torstatus_str.toStdString() << std::endl;
std::cerr << "Bootstrap status map: " << std::endl;
2017-12-21 15:32:42 +01:00
for(auto it(qvm.begin());it!=qvm.end();++it)
std::cerr << " " << it.key().toStdString() << " : " << it.value().toString().toStdString() << std::endl;
2017-12-21 15:32:42 +01:00
if(!qvm["progress"].toString().isNull())
torBootstrapStatus_LB->setText(qvm["progress"].toString() + " % (" + qvm["summary"].toString() + ")") ;
else
torBootstrapStatus_LB->setText(tr("[Waiting for Tor...]")) ;
2017-12-21 15:32:42 +01:00
QString service_id ;
QString onion_address ;
QHostAddress service_target_address ;
uint16_t service_port ;
uint16_t target_port ;
2017-12-21 15:32:42 +01:00
if(mTorManager->getHiddenServiceInfo(service_id,onion_address,service_port, service_target_address,target_port))
{
hiddenServiceAddress_LB->setText(QString::number(service_port) + ":" + service_target_address.toString() + ":" + QString::number(target_port));
onionAddress_LB->setText(onion_address);
}
2017-12-21 15:32:42 +01:00
else
{
hiddenServiceAddress_LB->setText(QString("[Not ready]")) ;
onionAddress_LB->setText(QString("[Not ready]")) ;
2017-12-21 15:32:42 +01:00
}
showLog();
adjustSize();
QCoreApplication::processEvents(); // forces update
2017-12-21 15:32:42 +01:00
}
void TorControlDialog::showLog()
{
static std::set<QString> already_seen ;
2017-12-21 15:32:42 +01:00
QString s ;
QStringList logmsgs = mTorManager->logMessages() ;
bool can_print = false ;
2017-12-21 15:32:42 +01:00
for(QStringList::const_iterator it(logmsgs.begin());it!=logmsgs.end();++it)
{
2017-12-21 15:32:42 +01:00
s += *it + "\n" ;
if(already_seen.find(*it) == already_seen.end())
{
can_print = true ;
already_seen.insert(*it);
}
if(can_print)
std::cerr << "[TOR DEBUG LOG] " << (*it).toStdString() << std::endl;
}
// torLog_TB->setText(s) ;:
std::cerr << "Connexion Proxy: " << mTorManager->control()->socksAddress().toString().toStdString() << ":" << mTorManager->control()->socksPort() << std::endl;
2017-12-21 15:32:42 +01:00
}
2017-12-21 18:16:32 +01:00
TorControlDialog::TorStatus TorControlDialog::checkForTor(QString& error_msg)
2017-12-21 18:16:32 +01:00
{
if(!mErrorMsg.isNull())
{
error_msg = mErrorMsg ;
return TorControlDialog::TOR_STATUS_FAIL ;
}
switch(mTorManager->control()->torStatus())
2017-12-21 18:16:32 +01:00
{
case Tor::TorControl::TorReady: rstime::rs_usleep(1*1000*1000);return TOR_STATUS_OK ;
2017-12-21 18:16:32 +01:00
default:
return TOR_STATUS_UNKNOWN ;
}
}
TorControlDialog::HiddenServiceStatus TorControlDialog::checkForHiddenService()
{
std::cerr << "Checking for hidden services:" ;
2017-12-21 18:16:32 +01:00
switch(mHiddenServiceStatus)
{
default:
case HIDDEN_SERVICE_STATUS_UNKNOWN: {
2017-12-21 18:16:32 +01:00
std::cerr << " trying to setup. " ;
if(!mTorManager->setupHiddenService())
{
mHiddenServiceStatus = HIDDEN_SERVICE_STATUS_FAIL ;
std::cerr << "Failed." << std::endl;
return mHiddenServiceStatus ;
}
std::cerr << "Done." << std::endl;
mHiddenServiceStatus = HIDDEN_SERVICE_STATUS_REQUESTED ;
return mHiddenServiceStatus ;
}
case HIDDEN_SERVICE_STATUS_REQUESTED: {
QList<Tor::HiddenService*> hidden_services = mTorManager->control()->hiddenServices();
if(hidden_services.empty())
{
std::cerr << "Not ready yet." << std::endl;
return mHiddenServiceStatus ;
}
else
{
if(mHiddenService == NULL)
mHiddenService = *(hidden_services.begin()) ;
2017-12-21 18:16:32 +01:00
Tor::HiddenService::Status hss = mHiddenService->status();
std::cerr << "New service acquired. Status is " << hss ;
if(hss == Tor::HiddenService::Online)
{
mHiddenServiceStatus = HIDDEN_SERVICE_STATUS_OK ;
std::cerr << ": published and running!" << std::endl;
return mHiddenServiceStatus ;
}
else
{
std::cerr << ": not ready yet." << std::endl;
return mHiddenServiceStatus ;
}
}
}
case HIDDEN_SERVICE_STATUS_OK :
std::cerr << "New service acquired." << std::endl;
return mHiddenServiceStatus ;
}
}
2017-12-21 18:16:32 +01:00