2018-08-19 12:13:19 +08:00
|
|
|
#include "extensions.h"
|
2018-08-20 03:26:50 +08:00
|
|
|
#include <winhttp.h>
|
|
|
|
#include <ctime>
|
2018-08-30 10:47:00 +08:00
|
|
|
#include <regex>
|
2018-08-20 03:26:50 +08: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 17:17:35 +08: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-23 01:22:37 +08:00
|
|
|
delete[] utf8text;
|
2018-09-29 17:17:35 +08: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-20 03:26:50 +08:00
|
|
|
}
|
2018-08-19 12:13:19 +08:00
|
|
|
|
2018-09-23 03:08:31 +08:00
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
2018-08-19 12:13:19 +08:00
|
|
|
{
|
2018-09-01 16:24:22 +08:00
|
|
|
static HINTERNET internet = NULL;
|
|
|
|
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 NextHooker", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
|
|
|
|
static unsigned int TKK = 0;
|
2018-08-20 03:26:50 +08:00
|
|
|
|
2018-09-29 17:17:35 +08:00
|
|
|
std::wstring translation;
|
2018-08-20 03:26:50 +08:00
|
|
|
|
2018-09-23 03:08:31 +08:00
|
|
|
if (sentenceInfo["hook address"] == -1) return false;
|
2018-08-30 10:47:00 +08:00
|
|
|
|
2018-09-01 16:24:22 +08:00
|
|
|
if (internet)
|
|
|
|
{
|
|
|
|
if (!TKK)
|
2018-08-20 03:26:50 +08:00
|
|
|
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
|
|
|
{
|
2018-09-01 16:24:22 +08:00
|
|
|
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", L"/", NULL, NULL, NULL, WINHTTP_FLAG_SECURE))
|
2018-08-20 03:26:50 +08:00
|
|
|
{
|
|
|
|
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
|
|
|
{
|
|
|
|
DWORD bytesRead;
|
2018-09-01 16:24:22 +08:00
|
|
|
char buffer[100000] = {}; // Google Translate page is ~64kb
|
2018-08-20 03:26:50 +08:00
|
|
|
WinHttpReceiveResponse(request, NULL);
|
2018-09-01 16:24:22 +08:00
|
|
|
WinHttpReadData(request, buffer, 100000, &bytesRead);
|
2018-09-21 09:27:13 +08: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-20 03:26:50 +08:00
|
|
|
}
|
|
|
|
WinHttpCloseHandle(request);
|
|
|
|
}
|
|
|
|
WinHttpCloseHandle(connection);
|
|
|
|
}
|
|
|
|
|
2018-09-01 16:24:22 +08:00
|
|
|
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-18 07:26:44 +08:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, 10000);
|
2018-09-01 16:24:22 +08:00
|
|
|
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);
|
|
|
|
}
|
2018-08-19 12:13:19 +08:00
|
|
|
}
|
2018-09-01 16:24:22 +08:00
|
|
|
|
|
|
|
if (translation == L"") translation = L"Error while translating.";
|
|
|
|
sentence += L"\r\n" + translation;
|
|
|
|
return true;
|
2018-08-19 12:13:19 +08:00
|
|
|
}
|