add extension dialog

This commit is contained in:
Akash Mozumdar 2018-11-01 10:38:14 -04:00
parent aa2d71a078
commit e48adc14bb
9 changed files with 273 additions and 153 deletions

View File

@ -9,7 +9,7 @@ set(gui_SRCS
main.cpp
mainwindow.cpp
misc.cpp
extensions.cpp
extendialog.cpp
tests.cpp
host/host.cc
host/textthread.cc

117
GUI/extendialog.cpp Normal file
View File

@ -0,0 +1,117 @@
#include "extendialog.h"
#include "ui_extendialog.h"
#include "types.h"
#include <QFileDialog>
ListRearrangeFilter::ListRearrangeFilter(QWidget* parent) : QObject(parent) {}
bool ListRearrangeFilter::eventFilter(QObject*, QEvent* event)
{
if (event->type() == QEvent::ChildRemoved) emit SigRearranged();
return false;
}
ExtenDialog::ExtenDialog(QWidget* parent) :
QDialog(parent),
ui(new Ui::ExtenDialog),
filter(new ListRearrangeFilter(this))
{
ui->setupUi(this);
extenList = findChild<QListWidget*>("extenList");
extenList->installEventFilter(filter);
connect(filter, &ListRearrangeFilter::SigRearranged, this, &ExtenDialog::Rearrange);
if (extensions.empty())
{
extenSaveFile.open(QIODevice::ReadOnly);
for (auto extenName : QString(extenSaveFile.readAll()).split(">")) Load(extenName);
extenSaveFile.close();
}
for (auto extension : extensions)
extenList->addItem(extension.name);
}
ExtenDialog::~ExtenDialog()
{
delete ui;
}
bool ExtenDialog::DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo)
{
bool success = true;
wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t));
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
InfoForExtension miscInfoLinkedList{ "", 0, nullptr };
InfoForExtension* miscInfoTraverser = &miscInfoLinkedList;
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->next = new InfoForExtension{ i.first.c_str(), i.second, nullptr };
std::shared_lock sharedLock(extenMutex);
for (auto extension : extensions)
{
wchar_t* nextBuffer = extension.callback(sentenceBuffer, &miscInfoLinkedList);
if (nextBuffer == nullptr) { success = false; break; }
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
sentenceBuffer = nextBuffer;
}
sentence = std::wstring(sentenceBuffer);
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
return success;
}
void ExtenDialog::Load(QString extenName)
{
// Extension is dll and exports "OnNewSentence"
HMODULE module = GetModuleHandleW(extenName.toStdWString().c_str());
if (!module) module = LoadLibraryW(extenName.toStdWString().c_str());
if (!module) return;
FARPROC callback = GetProcAddress(module, "OnNewSentence");
if (!callback) return;
extensions.push_back({ extenName, (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback });
}
void ExtenDialog::Unload(QString extenName)
{
extensions.erase(std::remove_if(extensions.begin(), extensions.end(), [&](Extension extension) { return extension.name == extenName; }), extensions.end());
FreeLibrary(GetModuleHandleW(extenName.toStdWString().c_str()));
}
void ExtenDialog::on_addButton_clicked()
{
QString extenFileName = QFileDialog::getOpenFileName(this, "Select Extension", "C:\\", "Extensions (*.dll)");
if (!extenFileName.size()) return;
QString extenName = extenFileName.mid(extenFileName.lastIndexOf("/") + 1);
QFile::copy(extenFileName, extenName);
Load(extenName.left(extenName.lastIndexOf(".dll")));
Sync();
}
void ExtenDialog::on_rmvButton_clicked()
{
for (auto extenName : extenList->selectedItems()) Unload(extenName->text());
Sync();
}
void ExtenDialog::Rearrange()
{
QVector<Extension> newExtensions;
for (int i = 0; i < extenList->count(); ++i)
newExtensions.push_back(*std::find_if(extensions.begin(), extensions.end(), [=](Extension extension) { return extension.name == extenList->item(i)->text(); }));
extensions = newExtensions;
Sync();
}
void ExtenDialog::Sync()
{
extenList->clear();
extenSaveFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
for (auto extension : extensions)
{
extenList->addItem(extension.name);
extenSaveFile.write((extension.name + ">").toUtf8());
}
extenSaveFile.close();
}

70
GUI/extendialog.h Normal file
View File

@ -0,0 +1,70 @@
#ifndef EXTENSIONS_H
#define EXTENSIONS_H
#include "qtcommon.h"
#include <shared_mutex>
#include <QDialog>
#include <QListWidget>
#include <QFile>
namespace Ui
{
class ExtenDialog;
}
class ListRearrangeFilter : public QObject
{
Q_OBJECT
public:
explicit ListRearrangeFilter(QWidget* parent = nullptr);
protected:
bool eventFilter(QObject*, QEvent* event);
signals:
void SigRearranged();
};
class ExtenDialog : public QDialog
{
Q_OBJECT
public:
explicit ExtenDialog(QWidget* parent = nullptr);
~ExtenDialog();
static bool DispatchSentenceToExtensions(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo);
private slots:
void on_addButton_clicked();
void on_rmvButton_clicked();
void Rearrange();
private:
struct InfoForExtension
{
const char* name;
int64_t value;
InfoForExtension* next;
~InfoForExtension() { if (next) delete next; };
};
struct Extension
{
QString name;
wchar_t*(*callback)(const wchar_t*, const InfoForExtension*);
};
inline static std::shared_mutex extenMutex;
inline static QVector<Extension> extensions;
static void Load(QString extenName);
static void Unload(QString extenName);
void Sync();
Ui::ExtenDialog* ui;
QFile extenSaveFile = QFile("Extensions.txt");
QListWidget* extenList;
ListRearrangeFilter* filter;
};
#endif // EXTENSIONS_H

78
GUI/extendialog.ui Normal file
View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExtenDialog</class>
<widget class="QDialog" name="ExtenDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Extensions</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QListWidget" name="extenList">
<property name="dragDropMode">
<enum>QAbstractItemView::InternalMove</enum>
</property>
<property name="defaultDropAction">
<enum>Qt::MoveAction</enum>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::MultiSelection</enum>
</property>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="addButton">
<property name="text">
<string>Add</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="rmvButton">
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -1,67 +0,0 @@
#include "extensions.h"
#include "types.h"
static std::optional<Extension> LoadExtension(QString extenName)
{
// Extension is dll and exports "OnNewSentence"
HMODULE module = GetModuleHandleW(extenName.toStdWString().c_str());
if (!module) module = LoadLibraryW(extenName.toStdWString().c_str());
if (!module) return {};
FARPROC callback = GetProcAddress(module, "OnNewSentence");
if (!callback) return {};
return Extension{ extenName, (wchar_t*(*)(const wchar_t*, const InfoForExtension*))callback };
}
void Extension::Load(QString extenName)
{
LOCK(extenMutex);
if (auto extension = LoadExtension(extenName)) extensions.push_back(extension.value());
}
void Extension::SendToBack(QString extenName)
{
LOCK(extenMutex);
Extension* extenIter = std::find_if(extensions.begin(), extensions.end(), [&](Extension extension) { return extension.name == extenName; });
Extension extension = *extenIter;
extensions.erase(extenIter);
extensions.push_back(extension);
}
void Extension::Unload(QString extenName)
{
LOCK(extenMutex);
extensions.erase(std::find_if(extensions.begin(), extensions.end(), [&](Extension extension) { return extension.name == extenName; }));
FreeLibrary(GetModuleHandleW(extenName.toStdWString().c_str()));
}
QVector<QString> Extension::GetNames()
{
std::shared_lock sharedLock(extenMutex);
QVector<QString> ret;
for (auto extension : extensions) ret.push_back(extension.name);
return ret;
}
bool Extension::DispatchSentence(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo)
{
bool success = true;
wchar_t* sentenceBuffer = (wchar_t*)HeapAlloc(GetProcessHeap(), 0, (sentence.size() + 1) * sizeof(wchar_t));
wcscpy_s(sentenceBuffer, sentence.size() + 1, sentence.c_str());
InfoForExtension miscInfoLinkedList{ "", 0, nullptr };
InfoForExtension* miscInfoTraverser = &miscInfoLinkedList;
for (auto& i : miscInfo) miscInfoTraverser = miscInfoTraverser->next = new InfoForExtension{ i.first.c_str(), i.second, nullptr };
std::shared_lock sharedLock(extenMutex);
for (auto extension : extensions)
{
wchar_t* nextBuffer = extension.callback(sentenceBuffer, &miscInfoLinkedList);
if (nextBuffer == nullptr) { success = false; break; }
if (nextBuffer != sentenceBuffer) HeapFree(GetProcessHeap(), 0, sentenceBuffer);
sentenceBuffer = nextBuffer;
}
sentence = std::wstring(sentenceBuffer);
HeapFree(GetProcessHeap(), 0, sentenceBuffer);
return success;
}

View File

@ -1,32 +0,0 @@
#ifndef EXTENSIONS_H
#define EXTENSIONS_H
#include "qtcommon.h"
#include <shared_mutex>
struct InfoForExtension
{
const char* name;
int64_t value;
InfoForExtension* next;
~InfoForExtension() { if (next) delete next; };
};
class Extension
{
public:
static bool DispatchSentence(std::wstring& sentence, std::unordered_map<std::string, int64_t> miscInfo);
static void Load(QString extenName);
static void SendToBack(QString extenName);
static void Unload(QString extenName);
static QVector<QString> GetNames();
QString name;
wchar_t* (*callback)(const wchar_t*, const InfoForExtension*);
private:
inline static std::shared_mutex extenMutex;
inline static QVector<Extension> extensions;
};
#endif // EXTENSIONS_H

View File

@ -1,7 +1,7 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "defs.h"
#include "extensions.h"
#include "extendialog.h"
#include "misc.h"
#include <QCoreApplication>
#include <QInputDialog>
@ -9,7 +9,8 @@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
ui(new Ui::MainWindow),
extenDialog(new ExtenDialog(this))
{
ui->setupUi(this);
@ -18,19 +19,6 @@ MainWindow::MainWindow(QWidget *parent) :
extenCombo = findChild<QComboBox*>("extenCombo");
textOutput = findChild<QPlainTextEdit*>("textOutput");
QFile extenSaveFile("Extensions.txt");
if (extenSaveFile.exists())
{
extenSaveFile.open(QIODevice::ReadOnly);
for (auto extenName : QString(extenSaveFile.readAll()).split(">")) Extension::Load(extenName);
}
else
{
for (auto file : QDir().entryList())
if (file.endsWith(".dll") && file != ITH_DLL) Extension::Load(file.left(file.lastIndexOf(".dll")));
}
ReloadExtensions();
if (settings.contains("Window")) this->setGeometry(settings.value("Window").toRect());
// TODO: add GUI for changing these
if (settings.contains("Flush_Delay")) TextThread::flushDelay = settings.value("Flush_Delay").toInt();
@ -120,7 +108,7 @@ void MainWindow::ThreadOutput(QString threadString, QString output)
bool MainWindow::ProcessThreadOutput(TextThread* thread, std::wstring& output)
{
if (Extension::DispatchSentence(output, GetInfoForExtensions(thread)))
if (ExtenDialog::DispatchSentenceToExtensions(output, GetInfoForExtensions(thread)))
{
output += L"\r\n";
emit SigThreadOutput(TextThreadString(thread), QString::fromStdWString(output));
@ -152,18 +140,6 @@ DWORD MainWindow::GetSelectedProcessId()
return processCombo->currentText().split(":")[0].toULong(nullptr, 16);
}
void MainWindow::ReloadExtensions()
{
extenCombo->clear();
QFile extenSaveFile("Extensions.txt");
extenSaveFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
for (auto extenName : Extension::GetNames())
{
extenSaveFile.write((extenName + ">").toUtf8());
extenCombo->addItem(extenName);
}
}
std::unordered_map<std::string, int64_t> MainWindow::GetInfoForExtensions(TextThread* thread)
{
return
@ -259,25 +235,5 @@ void MainWindow::on_ttCombo_activated(int index)
void MainWindow::on_addExtenButton_clicked()
{
QString extenFileName = QFileDialog::getOpenFileName(this, "Select Extension", "C:\\", "Extensions (*.dll)");
if (!extenFileName.size()) return;
QString extenName = extenFileName.mid(extenFileName.lastIndexOf("/") + 1);
QFile::copy(extenFileName, extenName);
Extension::Load(extenName.left(extenName.lastIndexOf(".dll")));
ReloadExtensions();
}
void MainWindow::on_moveExtenButton_clicked()
{
if (extenCombo->currentText() == "") return;
Extension::SendToBack(extenCombo->currentText());
ReloadExtensions();
Host::AddConsoleOutput(L"extension sent to back");
}
void MainWindow::on_rmvExtenButton_clicked()
{
if (extenCombo->currentText() == "") return;
Extension::Unload(extenCombo->currentText());
ReloadExtensions();
extenDialog->show();
}

View File

@ -43,15 +43,12 @@ private slots:
void on_hookButton_clicked();
void on_saveButton_clicked();
void on_addExtenButton_clicked();
void on_moveExtenButton_clicked();
void on_rmvExtenButton_clicked();
private:
bool ProcessThreadOutput(TextThread* thread, std::wstring& output);
QString TextThreadString(TextThread* thread);
ThreadParam ParseTextThreadString(QString textThreadString);
DWORD GetSelectedProcessId();
void ReloadExtensions();
std::unordered_map<std::string, int64_t> GetInfoForExtensions(TextThread* thread);
QVector<HookParam> GetAllHooks(DWORD processId);
@ -61,6 +58,7 @@ private:
QComboBox* ttCombo;
QComboBox* extenCombo;
QPlainTextEdit* textOutput;
QWidget* extenDialog;
};
#endif // MAINWINDOW_H

View File

@ -1,4 +1,4 @@
#include "extensions.h"
#include "extendialog.h"
#include "misc.h"
static int TESTS = []