2019-02-10 21:46:39 -05:00
|
|
|
|
#include "extension.h"
|
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "network.h"
|
2018-11-03 23:26:53 -04:00
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <QInputDialog>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
2019-02-27 11:33:17 -05:00
|
|
|
|
extern const char* SELECT_LANGUAGE;
|
|
|
|
|
extern const wchar_t* TOO_MANY_TRANS_REQUESTS;
|
|
|
|
|
extern const wchar_t* TRANSLATION_ERROR;
|
|
|
|
|
extern const char* GOOGLE_PROMPT;
|
|
|
|
|
|
2018-11-03 23:26:53 -04:00
|
|
|
|
QStringList languages
|
|
|
|
|
{
|
|
|
|
|
"English: en",
|
|
|
|
|
"Afrikaans: af",
|
|
|
|
|
"Arabic: ar",
|
|
|
|
|
"Albanian: sq",
|
|
|
|
|
"Belarusian: be",
|
|
|
|
|
"Bengali: bn",
|
|
|
|
|
"Bosnian: bs",
|
|
|
|
|
"Bulgarian: bg",
|
|
|
|
|
"Catalan: ca",
|
|
|
|
|
"Chinese(Simplified): zh-CH",
|
|
|
|
|
"Chinese(Traditional): zh-TW",
|
|
|
|
|
"Croatian: hr",
|
|
|
|
|
"Czech: cs",
|
|
|
|
|
"Danish: da",
|
|
|
|
|
"Dutch: nl",
|
|
|
|
|
"Esperanto: eo",
|
|
|
|
|
"Estonian: et",
|
|
|
|
|
"Filipino: tl",
|
|
|
|
|
"Finnish: fi",
|
|
|
|
|
"French: fr",
|
|
|
|
|
"Galician: gl",
|
|
|
|
|
"German: de",
|
|
|
|
|
"Greek: el",
|
|
|
|
|
"Hebrew: iw",
|
|
|
|
|
"Hindi: hi",
|
|
|
|
|
"Hungarian: hu",
|
|
|
|
|
"Icelandic: is",
|
|
|
|
|
"Indonesian: id",
|
|
|
|
|
"Irish: ga",
|
|
|
|
|
"Italian: it",
|
|
|
|
|
"Japanese: ja",
|
|
|
|
|
"Klingon: tlh",
|
|
|
|
|
"Korean: ko",
|
|
|
|
|
"Latin: la",
|
|
|
|
|
"Latvian: lv",
|
|
|
|
|
"Lithuanian: lt",
|
|
|
|
|
"Macedonian: mk",
|
|
|
|
|
"Malay: ms",
|
|
|
|
|
"Maltese: mt",
|
|
|
|
|
"Norwegian: no",
|
|
|
|
|
"Persian: fa",
|
|
|
|
|
"Polish: pl",
|
|
|
|
|
"Portuguese: pt",
|
|
|
|
|
"Romanian: ro",
|
|
|
|
|
"Russian: ru",
|
|
|
|
|
"Serbian: sr",
|
|
|
|
|
"Slovak: sk",
|
|
|
|
|
"Slovenian: sl",
|
|
|
|
|
"Somali: so",
|
|
|
|
|
"Spanish: es",
|
|
|
|
|
"Swahili: sw",
|
|
|
|
|
"Swedish: sv",
|
|
|
|
|
"Thai: th",
|
|
|
|
|
"Turkish: tr",
|
|
|
|
|
"Ukranian: uk",
|
|
|
|
|
"Urdu: ur",
|
|
|
|
|
"Vietnamese: vi",
|
|
|
|
|
"Welsh: cy",
|
|
|
|
|
"Yiddish: yi",
|
|
|
|
|
"Zulu: zu"
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-10 21:46:39 -05:00
|
|
|
|
std::wstring translateTo = L"en";
|
2018-11-03 23:26:53 -04:00
|
|
|
|
|
|
|
|
|
BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
|
|
|
|
|
{
|
|
|
|
|
switch (ul_reason_for_call)
|
|
|
|
|
{
|
|
|
|
|
case DLL_PROCESS_ATTACH:
|
|
|
|
|
{
|
|
|
|
|
QTimer::singleShot(0, []
|
|
|
|
|
{
|
2018-11-04 04:31:49 -05:00
|
|
|
|
translateTo = QInputDialog::getItem(
|
|
|
|
|
nullptr,
|
2018-12-18 16:32:28 -05:00
|
|
|
|
SELECT_LANGUAGE,
|
|
|
|
|
GOOGLE_PROMPT,
|
2018-11-04 04:31:49 -05:00
|
|
|
|
languages,
|
|
|
|
|
0, false, nullptr,
|
|
|
|
|
Qt::WindowCloseButtonHint
|
|
|
|
|
).split(" ")[1].toStdWString();
|
2018-11-03 23:26:53 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-10 21:46:39 -05:00
|
|
|
|
std::wstring GetTranslationUri(const std::wstring& text, unsigned TKK)
|
2018-11-03 23:26:53 -04:00
|
|
|
|
{
|
2018-11-08 05:51:20 -05:00
|
|
|
|
// If no TKK available, use this uri. Can't use too much or google will detect unauthorized access
|
2018-11-03 23:26:53 -04:00
|
|
|
|
if (!TKK) return L"/translate_a/single?client=gtx&dt=ld&dt=rm&dt=t&tl=" + translateTo + L"&q=" + text;
|
|
|
|
|
|
|
|
|
|
// Artikash 8/19/2018: reverse engineered from translate.google.com
|
2019-02-10 21:46:39 -05:00
|
|
|
|
std::wstring escapedText;
|
|
|
|
|
unsigned a = _time64(NULL) / 3600, b = a; // <- the first part of TKK
|
|
|
|
|
for (unsigned char ch : WideStringToString(text))
|
2018-11-03 23:26:53 -04:00
|
|
|
|
{
|
2019-02-10 21:46:39 -05:00
|
|
|
|
wchar_t escapedChar[4] = {};
|
|
|
|
|
swprintf_s<4>(escapedChar, L"%%%02X", (int)ch);
|
|
|
|
|
escapedText += escapedChar;
|
|
|
|
|
a += ch;
|
2018-11-03 23:26:53 -04:00
|
|
|
|
a += a << 10;
|
|
|
|
|
a ^= a >> 6;
|
|
|
|
|
}
|
|
|
|
|
a += a << 3;
|
|
|
|
|
a ^= a >> 11;
|
|
|
|
|
a += a << 15;
|
|
|
|
|
a ^= TKK;
|
|
|
|
|
a %= 1000000;
|
|
|
|
|
b ^= a;
|
|
|
|
|
|
2019-02-10 21:46:39 -05:00
|
|
|
|
return L"/translate_a/single?client=t&dt=ld&dt=rm&dt=t&tl=" + translateTo + L"&tk=" + std::to_wstring(a) + L"." + std::to_wstring(b) + L"&q=" + escapedText;
|
2018-11-03 23:26:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
|
|
|
|
|
{
|
2019-02-17 19:14:49 -05:00
|
|
|
|
if (sentenceInfo["text number"] == 0) return false;
|
2018-11-03 23:26:53 -04:00
|
|
|
|
|
2019-01-23 13:41:50 -05:00
|
|
|
|
static std::atomic<HINTERNET> internet = NULL;
|
2018-12-13 08:37:37 -05:00
|
|
|
|
if (!internet) internet = WinHttpOpen(L"Mozilla/5.0 Textractor", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0);
|
2019-01-23 13:41:50 -05:00
|
|
|
|
static std::atomic<unsigned> TKK = 0;
|
2019-02-10 21:46:39 -05:00
|
|
|
|
static RateLimiter rateLimiter(30, 60 * 1000);
|
|
|
|
|
|
2018-11-03 23:26:53 -04:00
|
|
|
|
std::wstring translation;
|
2019-02-10 21:46:39 -05:00
|
|
|
|
if (!(rateLimiter.Request() || sentenceInfo["current select"])) translation = TOO_MANY_TRANS_REQUESTS;
|
|
|
|
|
else if (internet)
|
2018-11-03 23:26:53 -04:00
|
|
|
|
{
|
2018-12-18 14:21:27 -05:00
|
|
|
|
if (!TKK)
|
2019-02-10 21:46:39 -05:00
|
|
|
|
if (InternetHandle connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
|
|
|
|
if (InternetHandle request = WinHttpOpenRequest(connection, L"GET", L"/", NULL, NULL, NULL, WINHTTP_FLAG_SECURE))
|
2018-12-18 14:21:27 -05:00
|
|
|
|
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
2019-02-10 21:46:39 -05:00
|
|
|
|
if (auto response = ReceiveHttpRequest(request))
|
|
|
|
|
if (std::wsmatch results; std::regex_search(response.value(), results, std::wregex(L"(\\d{7,})'"))) TKK = stoll(results[1]);
|
2018-11-03 23:26:53 -04:00
|
|
|
|
|
2019-02-10 21:46:39 -05:00
|
|
|
|
if (InternetHandle connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
|
|
|
|
if (InternetHandle request = WinHttpOpenRequest(connection, L"GET", GetTranslationUri(sentence, TKK).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
|
2018-11-03 23:26:53 -04:00
|
|
|
|
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
2019-02-10 21:46:39 -05:00
|
|
|
|
if (auto response = ReceiveHttpRequest(request))
|
2019-02-16 22:43:31 -05:00
|
|
|
|
// Response formatted as JSON: starts with [[["
|
2019-02-10 21:46:39 -05:00
|
|
|
|
if (response.value()[0] == L'[')
|
2019-02-16 22:43:31 -05:00
|
|
|
|
{
|
2019-02-10 21:46:39 -05:00
|
|
|
|
for (std::wsmatch results; std::regex_search(response.value(), results, std::wregex(L"\\[\"(.*?)\",[n\"]")); response = results.suffix())
|
|
|
|
|
translation += std::wstring(results[1]) + L" ";
|
2019-06-05 20:26:50 -04:00
|
|
|
|
Unescape(translation);
|
2019-02-16 22:43:31 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
translation = TRANSLATION_ERROR + (L" (TKK=" + std::to_wstring(TKK) + L")");
|
|
|
|
|
TKK = 0;
|
|
|
|
|
}
|
2018-11-03 23:26:53 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-20 11:02:03 -05:00
|
|
|
|
if (translation.empty()) translation = TRANSLATION_ERROR;
|
2019-01-10 21:51:20 -05:00
|
|
|
|
sentence += L"\n" + translation;
|
2018-11-03 23:26:53 -04:00
|
|
|
|
return true;
|
2018-12-18 16:32:28 -05:00
|
|
|
|
}
|
2019-02-10 21:46:39 -05:00
|
|
|
|
|
|
|
|
|
TEST(
|
|
|
|
|
{
|
|
|
|
|
std::wstring test = L"こんにちは";
|
2019-02-12 23:54:33 -05:00
|
|
|
|
ProcessSentence(test, { SentenceInfo::DUMMY });
|
2019-02-10 21:46:39 -05:00
|
|
|
|
assert(test.find(L"Hello") != std::wstring::npos);
|
|
|
|
|
}
|
|
|
|
|
);
|