diff --git a/src/analthread.cpp b/src/analthread.cpp index cc2ebf12..1e3eb4c3 100644 --- a/src/analthread.cpp +++ b/src/analthread.cpp @@ -1,18 +1,33 @@ #include "analthread.h" +#include "qrcore.h" #include -#include "mainwindow.h" -AnalThread::AnalThread(MainWindow *w, QWidget *parent) : - QThread(parent) +AnalThread::AnalThread(QWidget *parent) : + QThread(parent), + core(nullptr), + level(2) { - // Radare core found in: - this->w = w; - //this->level = 2; +} + +AnalThread::~AnalThread() +{ + if (isRunning()) { + quit(); + wait(); + } +} + +void AnalThread::start(QRCore *core, int level) +{ + this->core = core; + this->level = level; + + QThread::start(); } // run() will be called when a thread starts void AnalThread::run() { //qDebug() << "Anal level: " << this->level; - this->w->core->analyze(this->level); + core->analyze(this->level); } diff --git a/src/analthread.h b/src/analthread.h index 8aa22cef..1c52d924 100644 --- a/src/analthread.h +++ b/src/analthread.h @@ -3,19 +3,25 @@ #include -class MainWindow; +class QRCore; class AnalThread : public QThread { Q_OBJECT public: - explicit AnalThread(MainWindow *w, QWidget *parent = 0); + explicit AnalThread(QWidget *parent = 0); + ~AnalThread(); + + void start(QRCore *core, int level); + +protected: void run(); - int level; + + using QThread::start; private: - - MainWindow *w; + QRCore *core; + int level; }; #endif // ANALTHREAD_H diff --git a/src/createnewdialog.cpp b/src/createnewdialog.cpp index 3043aff4..33dbf579 100644 --- a/src/createnewdialog.cpp +++ b/src/createnewdialog.cpp @@ -11,7 +11,6 @@ createNewDialog::createNewDialog(QWidget *parent) : ui->setupUi(this); setWindowFlags(windowFlags() & (~Qt::WindowContextHelpButtonHint)); w = new MainWindow(nullptr); - w->core = new QRCore (); } createNewDialog::~createNewDialog() diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index eb96af7c..870df4b6 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -60,10 +60,12 @@ static void appendRow(QTreeWidget *tw, const QString &str, const QString &str2=N MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow) + core(new QRCore()), + ui(new Ui::MainWindow), + webserverThread(core, this) { ui->setupUi(this); - this->core = NULL; + doLock = false; // Add custom font @@ -237,18 +239,27 @@ MainWindow::MainWindow(QWidget *parent) : QShortcut* commands_shortcut = new QShortcut(QKeySequence(Qt::Key_Colon), this); connect(commands_shortcut, SIGNAL(activated()), this->omnibar, SLOT(showCommands())); + connect(&webserverThread, SIGNAL(finished()), this, SLOT(webserverThreadFinished())); +} + +MainWindow::~MainWindow() { + delete ui; + delete core; } void MainWindow::start_web_server() { - // To be removed - static WebServerThread thread; // Start web server - thread.core = core; - thread.start(); - QThread::sleep (1); - if (core->core->http_up == R_FALSE) { - eprintf ("FAILED TO LAUNCH\n"); - } + webserverThread.startServer(); +} + +void MainWindow::webserverThreadFinished() +{ + core->core->http_up = webserverThread.isStarted() ? R_TRUE : R_FALSE; + + // this is not true anymore, cause the webserver might have been stopped + //if (core->core->http_up == R_FALSE) { + // eprintf("FAILED TO LAUNCH\n"); + //} } void MainWindow::adjustColumns(QTreeWidget *tw) { @@ -275,6 +286,20 @@ void MainWindow::appendRow(QTreeWidget *tw, const QString &str, const QString &s tw->insertTopLevelItem(0, tempItem); } +void MainWindow::setWebServerState(bool start) +{ + if (start) { + webserverThread.startServer(); + + // Open web interface on default browser + // ballessay: well isn't this possible with =H& + //QString link = "http://localhost:9090/"; + //QDesktopServices::openUrl(QUrl(link)); + } else { + webserverThread.stopServer(); + } +} + void MainWindow::hideDummyColumns() { // UGLY, should be a loop over all treewidgets... this->functionsDock->functionsTreeWidget->setColumnHidden(0, true); @@ -362,10 +387,6 @@ void MainWindow::def_theme() { settings.setValue("dark", false); } -MainWindow::~MainWindow() { - delete ui; -} - /* * Refresh widget functions */ @@ -739,6 +760,7 @@ void MainWindow::on_consoleInputLineEdit_returnPressed() QCompleter *completer = ui->consoleInputLineEdit->completer(); /* * TODO: FIXME: Crashed the fucking app + * ballessay: yes this will crash if no completer is set -> nullptr */ //QStringListModel *completerModel = (QStringListModel*)(completer->model()); //completerModel->setStringList(completerModel->stringList() << input); @@ -840,24 +862,7 @@ void MainWindow::on_consoleExecButton_clicked() void MainWindow::on_actionStart_Web_Server_triggered() { - static WebServerThread thread; - if (ui->actionStart_Web_Server->isChecked()) { - // Start web server - thread.core = core; - thread.start(); - QThread::sleep (1); - if (core->core->http_up==R_FALSE) { - eprintf ("FAILED TO LAUNCH\n"); - } - // Open web interface on default browser - //QString link = "http://localhost:9090/"; - //QDesktopServices::openUrl(QUrl(link)); - } else { - core->core->http_up= R_FALSE; - // call something to kill the webserver!! - thread.exit(0); - // Stop web server - } + setWebServerState(ui->actionStart_Web_Server->isChecked()); } void MainWindow::on_actionConsoleSync_with_core_triggered() @@ -970,7 +975,7 @@ void MainWindow::add_debug_output(QString msg) void MainWindow::on_actionNew_triggered() { - qApp->quit(); + close(); on_actionLoad_triggered(); } @@ -1018,10 +1023,9 @@ void MainWindow::on_actionSDB_browser_triggered() void MainWindow::on_actionLoad_triggered() { - QProcess* process = new QProcess(this); - process->setProgram(qApp->applicationFilePath()); - process->setEnvironment(QProcess::systemEnvironment()); - process->start(); + QProcess process(this); + process.setEnvironment(QProcess::systemEnvironment()); + process.startDetached(qApp->applicationFilePath()); } void MainWindow::on_actionShow_Hide_mainsidebar_triggered() diff --git a/src/mainwindow.h b/src/mainwindow.h index b2f5631b..0589fc5a 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -57,13 +57,14 @@ public: bool responsive; explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + void start_web_server(); void closeEvent(QCloseEvent *event); void readSettings(); void setFilename(QString fn); void setCore(QRCore *core); void seek(const QString& offset, const QString& name=NULL); - ~MainWindow(); void updateFrames(); void refreshFunctions(); void refreshComments(); @@ -76,6 +77,8 @@ public: void appendRow(QTreeWidget *tw, const QString &str, const QString &str2=NULL, const QString &str3=NULL, const QString &str4=NULL, const QString &str5=NULL); + void setWebServerState(bool start); + public slots: void dark(); @@ -186,6 +189,8 @@ private slots: void on_actionReset_settings_triggered(); + void webserverThreadFinished(); + void on_actionQuit_triggered(); private: @@ -215,6 +220,7 @@ private: QLineEdit *gotoEntry; SdbDock *sdbDock; QAction *sidebar_action; + WebServerThread webserverThread; }; #endif // MAINWINDOW_H diff --git a/src/optionsdialog.cpp b/src/optionsdialog.cpp index d9a7cdfe..69b1ff2b 100644 --- a/src/optionsdialog.cpp +++ b/src/optionsdialog.cpp @@ -8,7 +8,8 @@ OptionsDialog::OptionsDialog(QWidget *parent): QDialog(parent), - ui(new Ui::OptionsDialog) + ui(new Ui::OptionsDialog), + analThread(this) { this->core = new QRCore(); this->anal_level = 0; @@ -39,6 +40,8 @@ OptionsDialog::OptionsDialog(QWidget *parent): // Add this so the dialog resizes when widgets are shown/hidden //this->layout()->setSizeConstraint(QLayout::SetFixedSize); + + connect(&analThread, SIGNAL(finished()), this, SLOT(anal_finished())); } OptionsDialog::~OptionsDialog() @@ -169,18 +172,14 @@ void OptionsDialog::on_okButton_clicked() ui->statusLabel->setText("Analysis in progress"); // Threads stuff - // create an instance of MyThread - this->analThread = new AnalThread(w); - // connect signal/slot - connect(analThread, SIGNAL(finished()), this, SLOT(anal_finished())); - //analThread->level = anal_level; + + int level = 0; if (anal_level == true) { - analThread->level = ui->analSlider->value(); - } else { - analThread->level = 0; + level = ui->analSlider->value(); } - analThread->start(); + + analThread.start(core, level); } void OptionsDialog::anal_finished() diff --git a/src/optionsdialog.h b/src/optionsdialog.h index 6be1a0c9..2cda8b23 100644 --- a/src/optionsdialog.h +++ b/src/optionsdialog.h @@ -43,7 +43,7 @@ private: QString filename; QString shortfn; Ui::OptionsDialog *ui; - AnalThread *analThread; + AnalThread analThread; MainWindow *w; }; diff --git a/src/webserverthread.cpp b/src/webserverthread.cpp index 65cb87e1..8be19e3f 100644 --- a/src/webserverthread.cpp +++ b/src/webserverthread.cpp @@ -1,14 +1,78 @@ #include "webserverthread.h" +#include "qrcore.h" +#include -WebServerThread::WebServerThread(QObject *parent) : - QThread(parent) +WebServerThread::WebServerThread(QRCore *core, QObject *parent) : + QThread(parent), + core(core), + started(false) { // MEOW } +WebServerThread::~WebServerThread() +{ + if (isRunning()) { + quit(); + wait(); + } +} + +void WebServerThread::startServer() +{ + assert(nullptr != core); + + if (!isRunning() && !started) { + QThread::start(); + } +} + +void WebServerThread::stopServer() +{ + assert(nullptr != core); + + if (!isRunning() && started) + { + QThread::start(); + } +} + +bool WebServerThread::isStarted() const +{ + QMutexLocker locker(&mutex); + return started; +} + void WebServerThread::run() { - if (core == NULL) + QMutexLocker locker(&mutex); + + if (core == nullptr) return; //eprintf ("Starting webserver!"); - core->cmd ("=h"); + + toggleWebServer(); +} + +void WebServerThread::toggleWebServer() +{ + // access already locked + + // see libr/core/rtr.c + // "=h", " port", "listen for http connections (r2 -qc=H /bin/ls)", + // "=h-", "", "stop background webserver", + // "=h*", "", "restart current webserver", + // "=h&", " port", "start http server in background)", + + if (started) { + // after this the only reaction to this commands is: + // sandbox: connect disabled + // and the webserver is still running + // TODO: find out why + core->cmd("=h-"); + } else { + core->cmd("=h&"); + } + + // cmd has no usefull return value for this commands, so just toogle the state + started = !started; } diff --git a/src/webserverthread.h b/src/webserverthread.h index 1a8dee7b..5df49cf2 100644 --- a/src/webserverthread.h +++ b/src/webserverthread.h @@ -2,20 +2,32 @@ #define WEBSERVERTHREAD_H #include -#include "qrcore.h" +#include + +class QRCore; class WebServerThread : public QThread { Q_OBJECT public: - QRCore *core; - explicit WebServerThread(QObject *parent = 0); -signals: + explicit WebServerThread(QRCore *core, QObject *parent = 0); + ~WebServerThread(); + + void startServer(); + void stopServer(); + + bool isStarted() const; -public slots: private: void run(); + using QThread::start; + + void toggleWebServer(); + + mutable QMutex mutex; + QRCore *core; + bool started; }; #endif // WEBSERVERTHREAD_H diff --git a/src/widgets/sidebar.cpp b/src/widgets/sidebar.cpp index 0ad1fd32..7f7b9111 100644 --- a/src/widgets/sidebar.cpp +++ b/src/widgets/sidebar.cpp @@ -5,15 +5,14 @@ #include "mainwindow.h" -SideBar::SideBar(MainWindow *main, QWidget *parent) : - QWidget(parent), - ui(new Ui::SideBar) +SideBar::SideBar(MainWindow *main) : + QWidget(main), + ui(new Ui::SideBar), + // Radare core found in: + main(main) { ui->setupUi(this); - // Radare core found in: - this->main = main; - QSettings settings("iaito", "iaito"); if (settings.value("responsive").toBool()) { ui->respButton->setChecked(true); @@ -44,24 +43,7 @@ void SideBar::on_consoleButton_clicked() void SideBar::on_webServerButton_clicked() { - static WebServerThread thread; - if (ui->webServerButton->isChecked()) { - // Start web server - thread.core = this->main->core; - thread.start(); - QThread::sleep (1); - if (this->main->core->core->http_up==R_FALSE) { - eprintf ("FAILED TO LAUNCH\n"); - } - // Open web interface on default browser - //QString link = "http://localhost:9090/"; - //QDesktopServices::openUrl(QUrl(link)); - } else { - this->main->core->core->http_up= R_FALSE; - // call something to kill the webserver!! - thread.exit(0); - // Stop web server - } + main->setWebServerState(ui->webServerButton->isChecked()); } void SideBar::on_lockButton_clicked() diff --git a/src/widgets/sidebar.h b/src/widgets/sidebar.h index 3051725c..98fa8676 100644 --- a/src/widgets/sidebar.h +++ b/src/widgets/sidebar.h @@ -14,7 +14,7 @@ class SideBar : public QWidget Q_OBJECT public: - explicit SideBar(MainWindow *main, QWidget *parent = 0); + explicit SideBar(MainWindow *main); ~SideBar(); public slots: @@ -43,7 +43,7 @@ private slots: private: Ui::SideBar *ui; - MainWindow *main; + MainWindow *main; }; #endif // SIDEBAR_H