LunaHook-mirror/LunaHost/LunaHostDll.cpp

186 lines
6.2 KiB
C++
Raw Normal View History

2024-04-30 14:11:06 +08:00
#include "host.h"
#define C_LUNA_API extern "C" __declspec(dllexport)
2024-02-07 20:59:24 +08:00
BOOL APIENTRY DllMain(HMODULE hModule,
2024-04-30 14:11:06 +08:00
DWORD ul_reason_for_call,
LPVOID lpReserved)
2024-02-07 20:59:24 +08:00
{
switch (ul_reason_for_call)
{
2024-04-30 14:11:06 +08:00
case DLL_PROCESS_ATTACH:
2024-02-07 20:59:24 +08:00
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
2024-04-30 14:11:06 +08:00
}
typedef void (*ProcessEvent)(DWORD);
2024-10-06 00:43:46 +08:00
typedef void (*ThreadEvent)(const wchar_t *, const char *, ThreadParam);
typedef bool (*OutputCallback)(const wchar_t *, const char *, ThreadParam, const wchar_t *);
2024-04-30 14:11:06 +08:00
typedef void (*ConsoleHandler)(const wchar_t *);
typedef void (*HookInsertHandler)(uint64_t, const wchar_t *);
typedef void (*EmbedCallback)(const wchar_t *, ThreadParam);
2024-10-06 00:43:46 +08:00
template <typename T>
std::optional<T> checkoption(bool check, T &&t)
{
if (check)
return std::move(t);
return {};
}
2024-10-03 14:53:59 +08:00
C_LUNA_API void Luna_Start(ProcessEvent Connect, ProcessEvent Disconnect, ThreadEvent Create, ThreadEvent Destroy, OutputCallback Output, ConsoleHandler console, HookInsertHandler hookinsert, EmbedCallback embed, ConsoleHandler Warning)
2024-04-30 14:11:06 +08:00
{
2024-02-07 20:59:24 +08:00
Host::StartEx(
2024-10-06 00:43:46 +08:00
checkoption(Connect, std::function<void(DWORD)>(Connect)),
checkoption(Disconnect, std::function<void(DWORD)>(Disconnect)),
checkoption(Create, [=](const TextThread &thread)
{ Create(thread.hp.hookcode, thread.hp.name, thread.tp); }),
checkoption(Destroy, [=](const TextThread &thread)
{ Destroy(thread.hp.hookcode, thread.hp.name, thread.tp); }),
checkoption(Output, [=](const TextThread &thread, std::wstring &output)
{ return Output(thread.hp.hookcode, thread.hp.name, thread.tp, output.c_str()); }),
checkoption(console, [=](const std::wstring &output)
{ console(output.c_str()); }),
checkoption(hookinsert, [=](uint64_t addr, const std::wstring &output)
{ hookinsert(addr, output.c_str()); }),
checkoption(embed, [=](const std::wstring &output, const ThreadParam &tp)
{ embed(output.c_str(), tp); }),
checkoption(Warning, [=](const std::wstring &output)
{ Warning(output.c_str()); }));
2024-02-07 20:59:24 +08:00
}
2024-04-30 14:11:06 +08:00
C_LUNA_API void Luna_Inject(DWORD pid, LPCWSTR basepath)
{
Host::InjectProcess(pid, basepath);
2024-02-07 20:59:24 +08:00
}
2024-04-30 14:11:06 +08:00
C_LUNA_API bool Luna_CreatePipeAndCheck(DWORD pid)
{
2024-02-07 20:59:24 +08:00
return Host::CreatePipeAndCheck(pid);
}
2024-04-30 14:11:06 +08:00
C_LUNA_API void Luna_Detach(DWORD pid)
{
2024-02-07 20:59:24 +08:00
Host::DetachProcess(pid);
}
2024-05-11 02:54:31 +08:00
C_LUNA_API void Luna_Settings(int flushDelay, bool filterRepetition, int defaultCodepage, int maxBufferSize, int maxHistorySize)
2024-04-30 14:11:06 +08:00
{
TextThread::flushDelay = flushDelay;
TextThread::filterRepetition = filterRepetition;
Host::defaultCodepage = defaultCodepage;
TextThread::maxBufferSize = maxBufferSize;
2024-08-19 05:24:37 +08:00
TextThread::maxHistorySize = maxHistorySize;
2024-02-07 20:59:24 +08:00
}
2024-04-30 14:11:06 +08:00
C_LUNA_API bool Luna_InsertHookCode(DWORD pid, LPCWSTR hookcode)
{
2024-02-07 20:59:24 +08:00
auto hp = HookCode::Parse(hookcode);
2024-04-30 14:11:06 +08:00
if (hp)
2024-02-07 20:59:24 +08:00
Host::InsertHook(pid, hp.value());
return hp.has_value();
}
2024-09-17 08:24:31 +08:00
C_LUNA_API void Luna_QueryThreadHistory(ThreadParam tp, void (*callback)(const wchar_t *))
2024-08-19 05:24:37 +08:00
{
auto s = Host::GetThread(tp).storage.Acquire();
2024-09-17 08:24:31 +08:00
callback(s->c_str());
2024-05-11 02:54:31 +08:00
}
2024-04-30 14:11:06 +08:00
C_LUNA_API void Luna_RemoveHook(DWORD pid, uint64_t addr)
{
Host::RemoveHook(pid, addr);
2024-02-07 20:59:24 +08:00
}
2024-04-30 14:11:06 +08:00
struct simplehooks
{
2024-02-07 20:59:24 +08:00
wchar_t hookcode[HOOKCODE_LEN];
wchar_t *text;
2024-08-19 05:24:37 +08:00
simplehooks() : text(0) {};
2024-02-07 20:59:24 +08:00
};
2024-04-30 14:11:06 +08:00
typedef void (*findhookcallback_t)(wchar_t *hookcode, const wchar_t *text);
C_LUNA_API void Luna_FindHooks(DWORD pid, SearchParam sp, findhookcallback_t findhookcallback)
{
Host::FindHooks(pid, sp, [=](HookParam hp, std::wstring text)
{
wchar_t hookcode[HOOKCODE_LEN];
wcscpy_s(hookcode,HOOKCODE_LEN, hp.hookcode);
findhookcallback(hookcode,text.c_str()); });
2024-02-07 20:59:24 +08:00
}
2024-08-19 05:24:37 +08:00
C_LUNA_API void Luna_EmbedSettings(DWORD pid, UINT32 waittime, UINT8 fontCharSet, bool fontCharSetEnabled, wchar_t *fontFamily, UINT32 spaceadjustpolicy, UINT32 keeprawtext, bool fastskipignore)
2024-04-30 14:11:06 +08:00
{
auto sm = Host::GetEmbedSharedMem(pid);
if (!sm)
return;
sm->waittime = waittime;
sm->fontCharSet = fontCharSet;
sm->fontCharSetEnabled = fontCharSetEnabled;
wcscpy_s(sm->fontFamily, 100, fontFamily);
sm->spaceadjustpolicy = spaceadjustpolicy;
sm->keeprawtext = keeprawtext;
sm->fastskipignore = fastskipignore;
2024-02-07 20:59:24 +08:00
}
2024-04-30 14:11:06 +08:00
C_LUNA_API bool Luna_checkisusingembed(DWORD pid, uint64_t address, uint64_t ctx1, uint64_t ctx2)
{
auto sm = Host::GetEmbedSharedMem(pid);
if (!sm)
return false;
for (int i = 0; i < 10; i++)
{
if (sm->use[i])
{
if ((sm->addr[i] == address) && (sm->ctx1[i] == ctx1) && (sm->ctx2[i] == ctx2))
return true;
2024-02-07 20:59:24 +08:00
}
}
return false;
}
2024-04-30 14:11:06 +08:00
C_LUNA_API void Luna_useembed(DWORD pid, uint64_t address, uint64_t ctx1, uint64_t ctx2, bool use)
{
auto sm = Host::GetEmbedSharedMem(pid);
if (!sm)
return;
sm->codepage = Host::defaultCodepage;
for (int i = 0; i < 10; i++)
{
if (sm->use[i])
{
if ((sm->addr[i] == address) && (sm->ctx1[i] == ctx1) && (sm->ctx2[i] == ctx2))
{
if (use == false)
{
sm->addr[i] = sm->ctx1[i] = sm->ctx2[i] = sm->use[i] = 0;
2024-02-07 20:59:24 +08:00
}
}
}
}
2024-04-30 14:11:06 +08:00
if (use)
{
for (int i = 0; i < 10; i++)
{
if (sm->use[i] == 0)
{
sm->use[i] = 1;
sm->addr[i] = address;
sm->ctx1[i] = ctx1;
sm->ctx2[i] = ctx2;
2024-03-25 15:08:31 +08:00
break;
2024-02-07 20:59:24 +08:00
}
}
2024-04-30 14:11:06 +08:00
}
2024-02-07 20:59:24 +08:00
}
2024-04-30 14:11:06 +08:00
inline UINT64 djb2_n2(const unsigned char *str, size_t len, UINT64 hash = 5381)
2024-02-07 20:59:24 +08:00
{
2024-04-30 14:11:06 +08:00
int i = 0;
while (len--)
{
2024-02-07 20:59:24 +08:00
hash = ((hash << 5) + hash) + (*str++); // hash * 33 + c
}
return hash;
}
2024-04-30 14:11:06 +08:00
C_LUNA_API void Luna_embedcallback(DWORD pid, LPCWSTR text, LPCWSTR trans)
{
auto sm = Host::GetEmbedSharedMem(pid);
if (!sm)
return;
wcscpy_s(sm->text, 1000, trans);
2024-02-07 20:59:24 +08:00
char eventname[1000];
2024-04-30 14:11:06 +08:00
sprintf(eventname, LUNA_EMBED_notify_event, pid, djb2_n2((const unsigned char *)(text), wcslen(text) * 2));
win_event event1(eventname);
2024-02-07 20:59:24 +08:00
event1.signal(true);
}