Textractor/extensions/googletranslate.cpp

107 lines
3.6 KiB
C++
Raw Normal View History

#include "extensions.h"
2018-08-19 15:26:50 -04:00
#include <winhttp.h>
#include <ctime>
2018-08-29 22:47:00 -04:00
#include <regex>
2018-08-19 15:26:50 -04:00
std::wstring GetTranslationUri(const wchar_t* text, unsigned int TKK)
{
// If no TKK available, use this uri. Can't use too much or google will detect unauthorized access.
if (!TKK) return std::wstring(L"/translate_a/single?client=gtx&dt=ld&dt=rm&dt=tq=") + text;
// Artikash 8/19/2018: reverse engineered from translate.google.com
char* utf8text = new char[wcslen(text) * 4];
WideCharToMultiByte(CP_UTF8, 0, text, -1, utf8text, wcslen(text) * 4, NULL, NULL);
unsigned int a = (unsigned int)(_time64(NULL) / 3600), b = a; // <- the first part of TKK
for (int i = 0; utf8text[i];)
{
a += (unsigned char)utf8text[i++];
a += a << 10;
a ^= a >> 6;
}
a += a << 3;
a ^= a >> 11;
a += a << 15;
a ^= TKK;
a %= 1000000;
b ^= a;
2018-09-29 05:17:35 -04:00
std::wstring encodedText;
for (int i = 0; utf8text[i];)
{
wchar_t utf8char[3] = {};
swprintf_s<3>(utf8char, L"%02X", (int)(unsigned char)utf8text[i++]);
encodedText += L"%" + std::wstring(utf8char);
}
2018-08-22 13:22:37 -04:00
delete[] utf8text;
2018-09-29 05:17:35 -04:00
return std::wstring(L"/translate_a/single?client=t&dt=ld&dt=rm&dt=t&tk=") + std::to_wstring(a) + L"." + std::to_wstring(b) + L"&q=" + std::wstring(encodedText);
2018-08-19 15:26:50 -04:00
}
2018-09-22 15:08:31 -04:00
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{
static HINTERNET internet = NULL;
2018-09-29 16:05:08 -04:00
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
static unsigned int TKK = 0;
2018-08-19 15:26:50 -04:00
2018-09-29 05:17:35 -04:00
std::wstring translation;
2018-08-19 15:26:50 -04:00
2018-09-22 15:08:31 -04:00
if (sentenceInfo["hook address"] == -1) return false;
2018-08-29 22:47:00 -04:00
if (internet)
{
if (!TKK)
2018-08-19 15:26:50 -04:00
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
{
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", L"/", NULL, NULL, NULL, WINHTTP_FLAG_SECURE))
2018-08-19 15:26:50 -04:00
{
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
{
DWORD bytesRead;
char buffer[100000] = {}; // Google Translate page is ~64kb
2018-08-19 15:26:50 -04:00
WinHttpReceiveResponse(request, NULL);
WinHttpReadData(request, buffer, 100000, &bytesRead);
2018-09-20 21:27:13 -04:00
if (strstr(buffer, "a\\x3d")) TKK = strtoll(strstr(buffer, "a\\x3d") + 5, nullptr, 10) + strtoll(strstr(buffer, "b\\x3d") + 5, nullptr, 10);
else TKK = strtoll(strstr(buffer, "TKK") + 12, nullptr, 10);
2018-08-19 15:26:50 -04:00
}
WinHttpCloseHandle(request);
}
WinHttpCloseHandle(connection);
}
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
{
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", GetTranslationUri(sentence.c_str(), TKK).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] = {};
2018-09-17 19:26:44 -04:00
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, 10000);
std::wstring response(wbuffer);
std::wregex translationFinder(L"\\[\"(.*?)\",[n\"]");
std::wsmatch results;
while (std::regex_search(response, results, translationFinder))
{
translation += std::wstring(results[1]) + L" ";
response = results.suffix().str();
}
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;
return true;
}