extract http request and refactor == L""/.size() == 0

This commit is contained in:
Akash Mozumdar 2018-10-18 22:52:27 -04:00
parent d71fe0f147
commit 8f60060e37
4 changed files with 43 additions and 59 deletions

View File

@ -28,7 +28,7 @@ void TextThread::Flush()
std::wstring sentence; std::wstring sentence;
{ {
LOCK(ttMutex); LOCK(ttMutex);
if (buffer.size() == 0) return; if (buffer.empty()) return;
if (buffer.size() < maxBufferSize && GetTickCount() - timestamp < flushDelay) return; if (buffer.size() < maxBufferSize && GetTickCount() - timestamp < flushDelay) return;
sentence = buffer; sentence = buffer;
buffer.clear(); buffer.clear();

View File

@ -205,7 +205,7 @@ void MainWindow::on_hookButton_clicked()
void MainWindow::on_unhookButton_clicked() void MainWindow::on_unhookButton_clicked()
{ {
QVector<HookParam> hooks = GetAllHooks(GetSelectedProcessId()); QVector<HookParam> hooks = GetAllHooks(GetSelectedProcessId());
if (hooks.size() == 0) return Host::AddConsoleOutput(L"no hooks detected"); if (hooks.empty()) return Host::AddConsoleOutput(L"no hooks detected");
QStringList hookList; QStringList hookList;
for (auto hook : hooks) for (auto hook : hooks)
hookList.push_back( hookList.push_back(
@ -248,7 +248,7 @@ void MainWindow::on_addExtenButton_clicked()
void MainWindow::on_rmvExtenButton_clicked() void MainWindow::on_rmvExtenButton_clicked()
{ {
if (extenCombo->currentText().size() == 0) return; if (extenCombo->currentText() == "") return;
UnloadExtension(extenCombo->currentText().split(":")[0].toInt()); UnloadExtension(extenCombo->currentText().split(":")[0].toInt());
ReloadExtensions(); ReloadExtensions();
} }

View File

@ -201,7 +201,7 @@ namespace
MEMORY_BASIC_INFORMATION info; MEMORY_BASIC_INFORMATION info;
if (!VirtualQueryEx(processHandle, (LPCVOID)hp.address, &info, sizeof(info))) goto fin; if (!VirtualQueryEx(processHandle, (LPCVOID)hp.address, &info, sizeof(info))) goto fin;
QString moduleName = GetModuleName(processId, (HMODULE)info.AllocationBase); QString moduleName = GetModuleName(processId, (HMODULE)info.AllocationBase);
if (moduleName.size() == 0) goto fin; if (moduleName == "") goto fin;
hp.type |= MODULE_OFFSET; hp.type |= MODULE_OFFSET;
hp.address -= (uint64_t)info.AllocationBase; hp.address -= (uint64_t)info.AllocationBase;
wcscpy_s<MAX_MODULE_SIZE>(hp.module, moduleName.toStdWString().c_str()); wcscpy_s<MAX_MODULE_SIZE>(hp.module, moduleName.toStdWString().c_str());

View File

@ -54,13 +54,46 @@ QStringList languages
"Portuguese: pt" "Portuguese: pt"
}; };
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) // This function detects language and puts it in translateFrom if it's empty
std::wstring Translate(std::wstring text, std::wstring& translateFrom, std::wstring translateTo)
{ {
static HINTERNET internet = NULL; static HINTERNET internet = NULL;
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0); if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
std::wstring translation;
if (internet)
{
std::wstring location = translateFrom.empty()
? L"/tdetect?text=" + text
: L"/ttranslate?from=" + translateFrom + L"&to=" + translateTo + L"&text=" + text;
if (HINTERNET connection = WinHttpConnect(internet, L"www.bing.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
{
if (HINTERNET request = WinHttpOpenRequest(connection, L"POST", location.c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
{
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
{
DWORD bytesRead;
char buffer[10000] = {};
WinHttpReceiveResponse(request, NULL);
WinHttpReadData(request, buffer, 10000, &bytesRead);
wchar_t wbuffer[10000] = {};
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, 10000);
if (translateFrom.empty()) translateFrom = wbuffer;
// Response formatted as JSON: translation starts with :" and ends with "}
if (std::wcmatch results; std::regex_search(wbuffer, results, std::wregex(L":\"(.+)\"\\}"))) translation = results[1];
}
WinHttpCloseHandle(request);
}
WinHttpCloseHandle(connection);
}
}
return translation;
}
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
static std::wstring translateTo; static std::wstring translateTo;
if (translateTo == L"" && QApplication::instance()->thread() == QThread::currentThread()) if (translateTo.empty() && QApplication::instance()->thread() == QThread::currentThread())
{ {
languages.sort(); languages.sort();
bool ok; bool ok;
@ -73,59 +106,10 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
std::wstring translation; std::wstring translation;
std::wstring translateFrom; std::wstring translateFrom;
Translate(sentence, translateFrom, translateTo);
if (internet) translation = Translate(sentence, translateFrom, translateTo);
{ for (auto& c : translation) if (c == L'\\') c = 0x200b;
if (HINTERNET connection = WinHttpConnect(internet, L"www.bing.com", INTERNET_DEFAULT_HTTPS_PORT, 0)) if (translation.empty()) translation = L"Error while translating.";
{
if (HINTERNET request = WinHttpOpenRequest(
connection, L"POST",
(L"/tdetect?text=" + sentence).c_str(),
NULL, NULL, NULL,
WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE
))
{
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
{
DWORD bytesRead;
char buffer[10000] = {};
WinHttpReceiveResponse(request, NULL);
WinHttpReadData(request, buffer, 10000, &bytesRead);
translateFrom = std::wstring(buffer, buffer + bytesRead);
}
WinHttpCloseHandle(request);
}
if (HINTERNET request = WinHttpOpenRequest(
connection,
L"POST",
(L"/ttranslate?from=" + translateFrom + L"&to=" + translateTo + L"&text=" + sentence).c_str(),
NULL, NULL, NULL,
WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE
))
{
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
{
DWORD bytesRead;
char buffer[10000] = {};
WinHttpReceiveResponse(request, NULL);
WinHttpReadData(request, buffer, 10000, &bytesRead);
// Response formatted as JSON: starts with '{'
if (buffer[0] == '{')
{
wchar_t wbuffer[10000] = {};
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, 10000);
if (std::wcmatch results; std::regex_search(wbuffer, results, std::wregex(L":\"(.+)\"\\}"))) translation = results[1];
for (auto& c : translation) if (c == L'\\') c = 0x200b;
}
}
WinHttpCloseHandle(request);
}
WinHttpCloseHandle(connection);
}
}
if (translation == L"") translation = L"Error while translating.";
sentence += L"\r\n" + translation; sentence += L"\r\n" + translation;
return true; return true;
} }