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 <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() :
|
||||
current(nullptr),
|
||||
@ -28,68 +28,59 @@ HookManager::HookManager() :
|
||||
textThreadsByParams(),
|
||||
processRecordsByIds()
|
||||
{
|
||||
TextThread* consoleTextThread = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++, splitDelay);
|
||||
consoleTextThread->Status() |= USING_UNICODE;
|
||||
SetCurrent(consoleTextThread);
|
||||
InitializeCriticalSection(&hmCs);
|
||||
|
||||
InitializeCriticalSection(&hmcs);
|
||||
TextThread* consoleTextThread = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }] = new TextThread({ 0, -1UL, -1UL, -1UL }, nextThreadNumber++, splitDelay);
|
||||
consoleTextThread->Status() |= USING_UNICODE;
|
||||
SetCurrent(consoleTextThread);
|
||||
}
|
||||
|
||||
HookManager::~HookManager()
|
||||
{
|
||||
DeleteCriticalSection(&hmcs);
|
||||
HM_LOCK;
|
||||
DeleteCriticalSection(&hmCs);
|
||||
}
|
||||
|
||||
TextThread *HookManager::FindSingle(DWORD number)
|
||||
{
|
||||
{
|
||||
HM_LOCK;
|
||||
for (auto i : textThreadsByParams)
|
||||
{
|
||||
if (i.second->Number() == number)
|
||||
{
|
||||
if (i.second->Number() == number)
|
||||
return i.second;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void HookManager::SetCurrent(TextThread *it)
|
||||
{
|
||||
if (current)
|
||||
current->Status() &= ~CURRENT_SELECT;
|
||||
current = it;
|
||||
if (it)
|
||||
it->Status() |= CURRENT_SELECT;
|
||||
HM_LOCK;
|
||||
current->Status() &= ~CURRENT_SELECT;
|
||||
current = it;
|
||||
it->Status() |= CURRENT_SELECT;
|
||||
}
|
||||
|
||||
void HookManager::SelectCurrent(DWORD num)
|
||||
{
|
||||
if (TextThread *st = FindSingle(num)) {
|
||||
SetCurrent(st);
|
||||
if (reset)
|
||||
reset(st);
|
||||
//st->ResetEditText();
|
||||
}
|
||||
HM_LOCK;
|
||||
if (TextThread *st = FindSingle(num))
|
||||
{
|
||||
SetCurrent(st);
|
||||
if (reset) reset(st);
|
||||
}
|
||||
}
|
||||
|
||||
void HookManager::RemoveSingleHook(DWORD pid, DWORD addr)
|
||||
{
|
||||
HM_LOCK;
|
||||
std::vector<ThreadParameter> removedThreads;
|
||||
for (auto i : textThreadsByParams)
|
||||
{
|
||||
if (i.first.pid == pid && i.first.hook == addr)
|
||||
{
|
||||
if (remove)
|
||||
{
|
||||
remove(i.second);
|
||||
}
|
||||
delete i.second;
|
||||
removedThreads.push_back(i.first);
|
||||
}
|
||||
}
|
||||
for (auto i : removedThreads)
|
||||
{
|
||||
textThreadsByParams.erase(i);
|
||||
}
|
||||
SelectCurrent(0);
|
||||
HM_LOCK;
|
||||
std::vector<ThreadParameter> removedThreads;
|
||||
for (auto i : textThreadsByParams)
|
||||
if (i.first.pid == pid && i.first.hook == addr)
|
||||
{
|
||||
if (remove) remove(i.second);
|
||||
delete i.second;
|
||||
removedThreads.push_back(i.first);
|
||||
}
|
||||
for (auto i : removedThreads) textThreadsByParams.erase(i);
|
||||
SelectCurrent(0);
|
||||
}
|
||||
|
||||
void HookManager::RemoveProcessContext(DWORD pid)
|
||||
@ -97,127 +88,108 @@ void HookManager::RemoveProcessContext(DWORD pid)
|
||||
HM_LOCK;
|
||||
std::vector<ThreadParameter> removedThreads;
|
||||
for (auto i : textThreadsByParams)
|
||||
{
|
||||
if (i.first.hook == pid)
|
||||
if (i.first.pid == pid)
|
||||
{
|
||||
if (remove)
|
||||
{
|
||||
remove(i.second);
|
||||
}
|
||||
if (remove) remove(i.second);
|
||||
delete i.second;
|
||||
removedThreads.push_back(i.first);
|
||||
}
|
||||
}
|
||||
for (auto i : removedThreads)
|
||||
{
|
||||
textThreadsByParams.erase(i);
|
||||
}
|
||||
for (auto i : removedThreads) textThreadsByParams.erase(i);
|
||||
SelectCurrent(0);
|
||||
}
|
||||
|
||||
void HookManager::RegisterProcess(DWORD pid, HANDLE hostPipe)
|
||||
{
|
||||
HM_LOCK;
|
||||
|
||||
ProcessRecord* record = processRecordsByIds[pid] = new ProcessRecord;
|
||||
record->hostPipe = hostPipe;
|
||||
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->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());
|
||||
|
||||
if (attach)
|
||||
attach(pid);
|
||||
|
||||
HM_LOCK;
|
||||
ProcessRecord* record = processRecordsByIds[pid] = new ProcessRecord;
|
||||
record->hostPipe = hostPipe;
|
||||
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->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());
|
||||
if (attach) attach(pid);
|
||||
}
|
||||
|
||||
void HookManager::UnRegisterProcess(DWORD pid)
|
||||
{
|
||||
HM_LOCK;
|
||||
|
||||
ProcessRecord pr = *processRecordsByIds[pid];
|
||||
CloseHandle(pr.hookman_mutex);
|
||||
UnmapViewOfFile(pr.hookman_map);
|
||||
CloseHandle(pr.process_handle);
|
||||
CloseHandle(pr.hookman_section);
|
||||
processRecordsByIds.erase(pid);
|
||||
RemoveProcessContext(pid);
|
||||
|
||||
if (detach)
|
||||
detach(pid);
|
||||
HM_LOCK;
|
||||
ProcessRecord pr = *processRecordsByIds[pid];
|
||||
CloseHandle(pr.hookman_mutex);
|
||||
UnmapViewOfFile(pr.hookman_map);
|
||||
CloseHandle(pr.process_handle);
|
||||
CloseHandle(pr.hookman_section);
|
||||
processRecordsByIds.erase(pid);
|
||||
RemoveProcessContext(pid);
|
||||
if (detach) detach(pid);
|
||||
}
|
||||
|
||||
void HookManager::DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD spl, const BYTE *text, int len)
|
||||
{
|
||||
// jichi 20/27/2013: When PID is zero, the text comes from console, which I don't need
|
||||
if (!text || !pid || len <= 0)
|
||||
return;
|
||||
HM_LOCK;
|
||||
ThreadParameter tp = {pid, hook, retn, spl};
|
||||
TextThread *it;
|
||||
if (!(it = textThreadsByParams[tp]))
|
||||
{
|
||||
it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++, splitDelay);
|
||||
if (create)
|
||||
{
|
||||
create(it);
|
||||
}
|
||||
}
|
||||
it->AddText(text, len);
|
||||
// jichi 20/27/2013: When PID is zero, the text comes from console, which I don't need
|
||||
if (!text || !pid || len <= 0)
|
||||
return;
|
||||
HM_LOCK;
|
||||
ThreadParameter tp = { pid, hook, retn, spl };
|
||||
TextThread *it;
|
||||
if ((it = textThreadsByParams[tp]) == nullptr)
|
||||
{
|
||||
it = textThreadsByParams[tp] = new TextThread(tp, nextThreadNumber++, splitDelay);
|
||||
if (create) create(it);
|
||||
}
|
||||
it->AddText(text, len);
|
||||
}
|
||||
|
||||
void HookManager::AddConsoleOutput(LPCWSTR text)
|
||||
{
|
||||
if (text)
|
||||
{
|
||||
int len = wcslen(text) * 2;
|
||||
TextThread *console = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }];
|
||||
console->AddSentence(std::wstring(text));
|
||||
}
|
||||
HM_LOCK;
|
||||
if (text)
|
||||
{
|
||||
int len = wcslen(text) * 2;
|
||||
TextThread *console = textThreadsByParams[{ 0, -1UL, -1UL, -1UL }];
|
||||
console->AddSentence(std::wstring(text));
|
||||
}
|
||||
}
|
||||
|
||||
void HookManager::ClearCurrent()
|
||||
{
|
||||
HM_LOCK;
|
||||
if (current) {
|
||||
current->Reset();
|
||||
if (reset)
|
||||
reset(current);
|
||||
}
|
||||
HM_LOCK;
|
||||
current->Reset();
|
||||
if (reset) reset(current);
|
||||
}
|
||||
|
||||
ProcessRecord *HookManager::GetProcessRecord(DWORD pid)
|
||||
{
|
||||
HM_LOCK;
|
||||
return processRecordsByIds[pid];
|
||||
HM_LOCK;
|
||||
return processRecordsByIds[pid];
|
||||
}
|
||||
|
||||
HANDLE HookManager::GetHostPipe(DWORD pid)
|
||||
{
|
||||
HM_LOCK;
|
||||
return processRecordsByIds[pid] ? processRecordsByIds[pid]->hostPipe : nullptr;
|
||||
HM_LOCK;
|
||||
return processRecordsByIds[pid] ? processRecordsByIds[pid]->hostPipe : nullptr;
|
||||
}
|
||||
|
||||
HookParam HookManager::GetHookParam(DWORD pid, DWORD addr)
|
||||
{
|
||||
HM_LOCK;
|
||||
HookParam ret = {};
|
||||
ProcessRecord* pr = GetProcessRecord(pid);
|
||||
if (pr == nullptr) return ret;
|
||||
WaitForSingleObject(pr->hookman_mutex, 0);
|
||||
MutexLocker locker(pr->hookman_mutex);
|
||||
const Hook* hooks = (const Hook*)pr->hookman_map;
|
||||
for (int i = 0; i < MAX_HOOK; ++i)
|
||||
if (hooks[i].Address() == addr)
|
||||
ret = hooks[i].hp;
|
||||
ReleaseMutex(pr->hookman_mutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::wstring HookManager::GetHookName(DWORD pid, DWORD addr)
|
||||
{
|
||||
HM_LOCK;
|
||||
std::string buffer;
|
||||
ProcessRecord* pr = GetProcessRecord(pid);
|
||||
if (pr == nullptr) return L"";
|
||||
WaitForSingleObject(pr->hookman_mutex, 0);
|
||||
MutexLocker locker(pr->hookman_mutex);
|
||||
USES_CONVERSION;
|
||||
const Hook* hooks = (const Hook*)pr->hookman_map;
|
||||
for (int i = 0; i < MAX_HOOK; ++i)
|
||||
@ -227,8 +199,7 @@ std::wstring HookManager::GetHookName(DWORD pid, DWORD addr)
|
||||
buffer.resize(hooks[i].NameLength());
|
||||
ReadProcessMemory(pr->process_handle, hooks[i].Name(), &buffer[0], hooks[i].NameLength(), nullptr);
|
||||
}
|
||||
}
|
||||
ReleaseMutex(pr->hookman_mutex);
|
||||
}
|
||||
return std::wstring(A2W(buffer.c_str()));
|
||||
}
|
||||
|
||||
|
@ -10,13 +10,13 @@
|
||||
#include <string>
|
||||
#include "vnrhook/include/types.h"
|
||||
|
||||
struct ProcessRecord
|
||||
struct ProcessRecord
|
||||
{
|
||||
HANDLE process_handle;
|
||||
HANDLE hookman_mutex;
|
||||
HANDLE hookman_section;
|
||||
LPVOID hookman_map;
|
||||
HANDLE hostPipe;
|
||||
HANDLE process_handle;
|
||||
HANDLE hookman_mutex;
|
||||
HANDLE hookman_section;
|
||||
LPVOID hookman_map;
|
||||
HANDLE hostPipe;
|
||||
};
|
||||
|
||||
typedef void(*ProcessEventCallback)(DWORD pid);
|
||||
@ -33,45 +33,45 @@ struct ThreadParameterHasher
|
||||
class __declspec(dllexport) HookManager
|
||||
{
|
||||
public:
|
||||
HookManager();
|
||||
~HookManager();
|
||||
HookManager();
|
||||
~HookManager();
|
||||
|
||||
TextThread *FindSingle(DWORD number);
|
||||
ProcessRecord *GetProcessRecord(DWORD pid);
|
||||
HANDLE GetHostPipe(DWORD pid);
|
||||
void ClearCurrent();
|
||||
void SelectCurrent(DWORD num);
|
||||
void SetCurrent(TextThread *it);
|
||||
void AddConsoleOutput(LPCWSTR text);
|
||||
void DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD split, const BYTE *text, int len);
|
||||
void RemoveProcessContext(DWORD pid); // private
|
||||
void RemoveSingleHook(DWORD pid, DWORD addr);
|
||||
void RegisterProcess(DWORD pid, HANDLE hostPipe);
|
||||
void UnRegisterProcess(DWORD pid);
|
||||
HookParam GetHookParam(DWORD pid, DWORD addr);
|
||||
std::wstring GetHookName(DWORD pid, DWORD addr);
|
||||
TextThread *FindSingle(DWORD number);
|
||||
ProcessRecord *GetProcessRecord(DWORD pid);
|
||||
HANDLE GetHostPipe(DWORD pid);
|
||||
void ClearCurrent();
|
||||
void SelectCurrent(DWORD num);
|
||||
void SetCurrent(TextThread *it);
|
||||
void AddConsoleOutput(LPCWSTR text);
|
||||
void DispatchText(DWORD pid, DWORD hook, DWORD retn, DWORD split, const BYTE *text, int len);
|
||||
void RemoveProcessContext(DWORD pid); // private
|
||||
void RemoveSingleHook(DWORD pid, DWORD addr);
|
||||
void RegisterProcess(DWORD pid, HANDLE hostPipe);
|
||||
void UnRegisterProcess(DWORD pid);
|
||||
HookParam GetHookParam(DWORD pid, DWORD addr);
|
||||
std::wstring GetHookName(DWORD pid, DWORD addr);
|
||||
|
||||
void RegisterThreadCreateCallback(ThreadEventCallback cf) { create = cf; }
|
||||
void RegisterThreadRemoveCallback(ThreadEventCallback cf) { remove = cf; }
|
||||
void RegisterThreadResetCallback(ThreadEventCallback cf) { reset = cf; }
|
||||
void RegisterProcessAttachCallback(ProcessEventCallback cf) { attach = cf; }
|
||||
void RegisterProcessDetachCallback(ProcessEventCallback cf) { detach = cf; }
|
||||
void RegisterThreadCreateCallback(ThreadEventCallback cf) { create = cf; }
|
||||
void RegisterThreadRemoveCallback(ThreadEventCallback cf) { remove = cf; }
|
||||
void RegisterThreadResetCallback(ThreadEventCallback cf) { reset = cf; }
|
||||
void RegisterProcessAttachCallback(ProcessEventCallback cf) { attach = cf; }
|
||||
void RegisterProcessDetachCallback(ProcessEventCallback cf) { detach = cf; }
|
||||
|
||||
void SetSplitInterval(unsigned int splitDelay) { this->splitDelay = splitDelay; }
|
||||
void SetSplitInterval(unsigned int splitDelay) { this->splitDelay = splitDelay; }
|
||||
|
||||
private:
|
||||
std::unordered_map<ThreadParameter, TextThread*, ThreadParameterHasher> textThreadsByParams;
|
||||
std::unordered_map<DWORD, ProcessRecord*> processRecordsByIds;
|
||||
|
||||
CRITICAL_SECTION hmcs;
|
||||
CRITICAL_SECTION hmCs;
|
||||
|
||||
TextThread *current;
|
||||
TextThread *current;
|
||||
|
||||
ThreadEventCallback create, remove, reset;
|
||||
ProcessEventCallback attach, detach;
|
||||
ThreadEventCallback create, remove, reset;
|
||||
ProcessEventCallback attach, detach;
|
||||
|
||||
WORD nextThreadNumber;
|
||||
unsigned int splitDelay;
|
||||
WORD nextThreadNumber;
|
||||
unsigned int splitDelay;
|
||||
};
|
||||
|
||||
// EOF
|
||||
|
Loading…
x
Reference in New Issue
Block a user