#include <iostream>
#include <thread>
#include "Windows.h"
uintptr_t dwSAMP = 0xFF;
void* SetCallHook(uintptr_t HookAddress, void* DetourFunction)
{
uintptr_t OriginalFunction = *reinterpret_cast<uintptr_t*>(HookAddress + 1) + HookAddress + 5;
DWORD oldProt;
VirtualProtect(reinterpret_cast<void*>(HookAddress + 1), sizeof(uintptr_t), PAGE_READWRITE, &oldProt);
*reinterpret_cast<uintptr_t*>(HookAddress + 1) = reinterpret_cast<uintptr_t>(DetourFunction) - HookAddress - 5;
VirtualProtect(reinterpret_cast<void*>(HookAddress + 1), sizeof(uintptr_t), oldProt, &oldProt);
return reinterpret_cast<void*>(OriginalFunction);
}
using SendConnectionRequest = bool(__fastcall*)(void*, void*, const char*, unsigned short, char*, int);
SendConnectionRequest pSendConnectionRequest = nullptr;
int __fastcall ConnectionHook(void* EDX, void* thats, const char* host, unsigned short remotePort, char* passwordData, int passwordDataLength)
{
if (!strncmp(host, "127.0.0.1", 9))
return 0;
return pSendConnectionRequest(EDX, thats, host, remotePort, passwordData, passwordDataLength);
}
void Hooking()
{
pSendConnectionRequest = reinterpret_cast<SendConnectionRequest>(SetCallHook(dwSAMP + 0x3CB16, &ConnectionHook));
}
BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
dwSAMP = reinterpret_cast<uintptr_t>(GetModuleHandleA("samp.dll"));
std::thread(Hooking).detach();
break;
}
}
return TRUE;
}