mirror of
https://github.com/Artikash/Textractor.git
synced 2024-12-23 17:04:12 +08:00
refactor
This commit is contained in:
parent
05a14d201e
commit
e4f5f3cfd5
@ -15,7 +15,7 @@
|
|||||||
#include "winmutex/winmutex.h"
|
#include "winmutex/winmutex.h"
|
||||||
#include <atlbase.h>
|
#include <atlbase.h>
|
||||||
|
|
||||||
#define HM_LOCK CriticalSectionLocker locker(hmcs) // Synchronized scope for accessing private data
|
#define HM_LOCK CriticalSectionLocker locker(hmCs) // Synchronized scope for accessing private data
|
||||||
|
|
||||||
HookManager::HookManager() :
|
HookManager::HookManager() :
|
||||||
current(nullptr),
|
current(nullptr),
|
||||||
@ -28,67 +28,58 @@ HookManager::HookManager() :
|
|||||||
textThreadsByParams(),
|
textThreadsByParams(),
|
||||||
processRecordsByIds()
|
processRecordsByIds()
|
||||||
{
|
{
|
||||||
|
InitializeCriticalSection(&hmCs);
|
||||||
|
|
||||||
TextThread* consoleTextThread = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++, splitDelay);
|
TextThread* consoleTextThread = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++, splitDelay);
|
||||||
consoleTextThread->Status() |= USING_UNICODE;
|
consoleTextThread->Status() |= USING_UNICODE;
|
||||||
SetCurrent(consoleTextThread);
|
SetCurrent(consoleTextThread);
|
||||||
|
|
||||||
InitializeCriticalSection(&hmcs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HookManager::~HookManager()
|
HookManager::~HookManager()
|
||||||
{
|
{
|
||||||
DeleteCriticalSection(&hmcs);
|
HM_LOCK;
|
||||||
|
DeleteCriticalSection(&hmCs);
|
||||||
}
|
}
|
||||||
|
|
||||||
TextThread *HookManager::FindSingle(DWORD number)
|
TextThread *HookManager::FindSingle(DWORD number)
|
||||||
{
|
{
|
||||||
|
HM_LOCK;
|
||||||
for (auto i : textThreadsByParams)
|
for (auto i : textThreadsByParams)
|
||||||
{
|
|
||||||
if (i.second->Number() == number)
|
if (i.second->Number() == number)
|
||||||
{
|
|
||||||
return i.second;
|
return i.second;
|
||||||
}
|
|
||||||
}
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HookManager::SetCurrent(TextThread *it)
|
void HookManager::SetCurrent(TextThread *it)
|
||||||
{
|
{
|
||||||
if (current)
|
HM_LOCK;
|
||||||
current->Status() &= ~CURRENT_SELECT;
|
current->Status() &= ~CURRENT_SELECT;
|
||||||
current = it;
|
current = it;
|
||||||
if (it)
|
|
||||||
it->Status() |= CURRENT_SELECT;
|
it->Status() |= CURRENT_SELECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HookManager::SelectCurrent(DWORD num)
|
void HookManager::SelectCurrent(DWORD num)
|
||||||
{
|
{
|
||||||
if (TextThread *st = FindSingle(num)) {
|
HM_LOCK;
|
||||||
|
if (TextThread *st = FindSingle(num))
|
||||||
|
{
|
||||||
SetCurrent(st);
|
SetCurrent(st);
|
||||||
if (reset)
|
if (reset) reset(st);
|
||||||
reset(st);
|
|
||||||
//st->ResetEditText();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HookManager::RemoveSingleHook(DWORD pid, DWORD addr)
|
void HookManager::RemoveSingleHook(DWORD pid, DWORD addr)
|
||||||
{
|
{
|
||||||
HM_LOCK;
|
HM_LOCK;
|
||||||
std::vector<ThreadParameter> removedThreads;
|
std::vector<ThreadParameter> removedThreads;
|
||||||
for (auto i : textThreadsByParams)
|
for (auto i : textThreadsByParams)
|
||||||
{
|
|
||||||
if (i.first.pid == pid && i.first.hook == addr)
|
if (i.first.pid == pid && i.first.hook == addr)
|
||||||
{
|
{
|
||||||
if (remove)
|
if (remove) remove(i.second);
|
||||||
{
|
|
||||||
remove(i.second);
|
|
||||||
}
|
|
||||||
delete i.second;
|
delete i.second;
|
||||||
removedThreads.push_back(i.first);
|
removedThreads.push_back(i.first);
|
||||||
}
|
}
|
||||||
}
|
for (auto i : removedThreads) textThreadsByParams.erase(i);
|
||||||
for (auto i : removedThreads)
|
|
||||||
{
|
|
||||||
textThreadsByParams.erase(i);
|
|
||||||
}
|
|
||||||
SelectCurrent(0);
|
SelectCurrent(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,44 +88,31 @@ void HookManager::RemoveProcessContext(DWORD pid)
|
|||||||
HM_LOCK;
|
HM_LOCK;
|
||||||
std::vector<ThreadParameter> removedThreads;
|
std::vector<ThreadParameter> removedThreads;
|
||||||
for (auto i : textThreadsByParams)
|
for (auto i : textThreadsByParams)
|
||||||
|
if (i.first.pid == pid)
|
||||||
{
|
{
|
||||||
if (i.first.hook == pid)
|
if (remove) remove(i.second);
|
||||||
{
|
|
||||||
if (remove)
|
|
||||||
{
|
|
||||||
remove(i.second);
|
|
||||||
}
|
|
||||||
delete i.second;
|
delete i.second;
|
||||||
removedThreads.push_back(i.first);
|
removedThreads.push_back(i.first);
|
||||||
}
|
}
|
||||||
}
|
for (auto i : removedThreads) textThreadsByParams.erase(i);
|
||||||
for (auto i : removedThreads)
|
|
||||||
{
|
|
||||||
textThreadsByParams.erase(i);
|
|
||||||
}
|
|
||||||
SelectCurrent(0);
|
SelectCurrent(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HookManager::RegisterProcess(DWORD pid, HANDLE hostPipe)
|
void HookManager::RegisterProcess(DWORD pid, HANDLE hostPipe)
|
||||||
{
|
{
|
||||||
HM_LOCK;
|
HM_LOCK;
|
||||||
|
|
||||||
ProcessRecord* record = processRecordsByIds[pid] = new ProcessRecord;
|
ProcessRecord* record = processRecordsByIds[pid] = new ProcessRecord;
|
||||||
record->hostPipe = hostPipe;
|
record->hostPipe = hostPipe;
|
||||||
record->hookman_section = OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(pid)).c_str());
|
record->hookman_section = OpenFileMappingW(FILE_MAP_READ, FALSE, (ITH_SECTION_ + std::to_wstring(pid)).c_str());
|
||||||
record->hookman_map = MapViewOfFile(record->hookman_section, FILE_MAP_READ, 0, 0, HOOK_SECTION_SIZE / 2); // jichi 1/16/2015: Changed to half to hook section size
|
record->hookman_map = MapViewOfFile(record->hookman_section, FILE_MAP_READ, 0, 0, HOOK_SECTION_SIZE / 2); // jichi 1/16/2015: Changed to half to hook section size
|
||||||
record->process_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
|
record->process_handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
|
||||||
record->hookman_mutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(pid)).c_str());
|
record->hookman_mutex = OpenMutexW(MUTEX_ALL_ACCESS, FALSE, (ITH_HOOKMAN_MUTEX_ + std::to_wstring(pid)).c_str());
|
||||||
|
if (attach) attach(pid);
|
||||||
if (attach)
|
|
||||||
attach(pid);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HookManager::UnRegisterProcess(DWORD pid)
|
void HookManager::UnRegisterProcess(DWORD pid)
|
||||||
{
|
{
|
||||||
HM_LOCK;
|
HM_LOCK;
|
||||||
|
|
||||||
ProcessRecord pr = *processRecordsByIds[pid];
|
ProcessRecord pr = *processRecordsByIds[pid];
|
||||||
CloseHandle(pr.hookman_mutex);
|
CloseHandle(pr.hookman_mutex);
|
||||||
UnmapViewOfFile(pr.hookman_map);
|
UnmapViewOfFile(pr.hookman_map);
|
||||||
@ -142,9 +120,7 @@ void HookManager::UnRegisterProcess(DWORD pid)
|
|||||||
CloseHandle(pr.hookman_section);
|
CloseHandle(pr.hookman_section);
|
||||||
processRecordsByIds.erase(pid);
|
processRecordsByIds.erase(pid);
|
||||||
RemoveProcessContext(pid);
|
RemoveProcessContext(pid);
|
||||||
|
if (detach) detach(pid);
|
||||||
if (detach)
|
|
||||||
detach(pid);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HookManager::DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD spl, const BYTE *text, int len)
|
void HookManager::DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD spl, const BYTE *text, int len)
|
||||||
@ -155,19 +131,17 @@ void HookManager::DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD spl, con
|
|||||||
HM_LOCK;
|
HM_LOCK;
|
||||||
ThreadParameter tp = { pid, hook, retn, spl };
|
ThreadParameter tp = { pid, hook, retn, spl };
|
||||||
TextThread *it;
|
TextThread *it;
|
||||||
if (!(it = textThreadsByParams[tp]))
|
if ((it = textThreadsByParams[tp]) == nullptr)
|
||||||
{
|
{
|
||||||
it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++, splitDelay);
|
it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++, splitDelay);
|
||||||
if (create)
|
if (create) create(it);
|
||||||
{
|
|
||||||
create(it);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
it->AddText(text, len);
|
it->AddText(text, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HookManager::AddConsoleOutput(LPCWSTR text)
|
void HookManager::AddConsoleOutput(LPCWSTR text)
|
||||||
{
|
{
|
||||||
|
HM_LOCK;
|
||||||
if (text)
|
if (text)
|
||||||
{
|
{
|
||||||
int len = wcslen(text) * 2;
|
int len = wcslen(text) * 2;
|
||||||
@ -179,11 +153,8 @@ void HookManager::AddConsoleOutput(LPCWSTR text)
|
|||||||
void HookManager::ClearCurrent()
|
void HookManager::ClearCurrent()
|
||||||
{
|
{
|
||||||
HM_LOCK;
|
HM_LOCK;
|
||||||
if (current) {
|
|
||||||
current->Reset();
|
current->Reset();
|
||||||
if (reset)
|
if (reset) reset(current);
|
||||||
reset(current);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ProcessRecord *HookManager::GetProcessRecord(DWORD pid)
|
ProcessRecord *HookManager::GetProcessRecord(DWORD pid)
|
||||||
@ -200,24 +171,25 @@ HANDLE HookManager::GetHostPipe(DWORD pid)
|
|||||||
|
|
||||||
HookParam HookManager::GetHookParam(DWORD pid, DWORD addr)
|
HookParam HookManager::GetHookParam(DWORD pid, DWORD addr)
|
||||||
{
|
{
|
||||||
|
HM_LOCK;
|
||||||
HookParam ret = {};
|
HookParam ret = {};
|
||||||
ProcessRecord* pr = GetProcessRecord(pid);
|
ProcessRecord* pr = GetProcessRecord(pid);
|
||||||
if (pr == nullptr) return ret;
|
if (pr == nullptr) return ret;
|
||||||
WaitForSingleObject(pr->hookman_mutex, 0);
|
MutexLocker locker(pr->hookman_mutex);
|
||||||
const Hook* hooks = (const Hook*)pr->hookman_map;
|
const Hook* hooks = (const Hook*)pr->hookman_map;
|
||||||
for (int i = 0; i < MAX_HOOK; ++i)
|
for (int i = 0; i < MAX_HOOK; ++i)
|
||||||
if (hooks[i].Address() == addr)
|
if (hooks[i].Address() == addr)
|
||||||
ret = hooks[i].hp;
|
ret = hooks[i].hp;
|
||||||
ReleaseMutex(pr->hookman_mutex);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring HookManager::GetHookName(DWORD pid, DWORD addr)
|
std::wstring HookManager::GetHookName(DWORD pid, DWORD addr)
|
||||||
{
|
{
|
||||||
|
HM_LOCK;
|
||||||
std::string buffer;
|
std::string buffer;
|
||||||
ProcessRecord* pr = GetProcessRecord(pid);
|
ProcessRecord* pr = GetProcessRecord(pid);
|
||||||
if (pr == nullptr) return L"";
|
if (pr == nullptr) return L"";
|
||||||
WaitForSingleObject(pr->hookman_mutex, 0);
|
MutexLocker locker(pr->hookman_mutex);
|
||||||
USES_CONVERSION;
|
USES_CONVERSION;
|
||||||
const Hook* hooks = (const Hook*)pr->hookman_map;
|
const Hook* hooks = (const Hook*)pr->hookman_map;
|
||||||
for (int i = 0; i < MAX_HOOK; ++i)
|
for (int i = 0; i < MAX_HOOK; ++i)
|
||||||
@ -228,7 +200,6 @@ std::wstring HookManager::GetHookName(DWORD pid, DWORD addr)
|
|||||||
ReadProcessMemory(pr->process_handle, hooks[i].Name(), &buffer[0], hooks[i].NameLength(), nullptr);
|
ReadProcessMemory(pr->process_handle, hooks[i].Name(), &buffer[0], hooks[i].NameLength(), nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ReleaseMutex(pr->hookman_mutex);
|
|
||||||
return std::wstring(A2W(buffer.c_str()));
|
return std::wstring(A2W(buffer.c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ private:
|
|||||||
std::unordered_map<ThreadParameter, TextThread*, ThreadParameterHasher> textThreadsByParams;
|
std::unordered_map<ThreadParameter, TextThread*, ThreadParameterHasher> textThreadsByParams;
|
||||||
std::unordered_map<DWORD, ProcessRecord*> processRecordsByIds;
|
std::unordered_map<DWORD, ProcessRecord*> processRecordsByIds;
|
||||||
|
|
||||||
CRITICAL_SECTION hmcs;
|
CRITICAL_SECTION hmCs;
|
||||||
|
|
||||||
TextThread *current;
|
TextThread *current;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user