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-08-23 01:22:37 +08:00
|
|
|
delete[] utf8text;
|
2018-08-20 03:26:50 +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(text);
|
|
|
|
}
|
2018-08-19 12:13:19 +08:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Param sentence: pointer to sentence received by NextHooker (UTF-16).
|
|
|
|
* You should not modify this sentence. If you want NextHooker to receive a modified sentence, copy it into your own buffer and return that.
|
|
|
|
* Param miscInfo: pointer to start of singly linked list containing misc info about the sentence.
|
|
|
|
* Return value: pointer to sentence NextHooker takes for future processing and display.
|
|
|
|
* Return 'sentence' unless you created a new sentence/buffer as mentioned above.
|
|
|
|
* NextHooker will display the sentence after all extensions have had a chance to process and/or modify it.
|
|
|
|
* THIS FUNCTION MAY BE RUN SEVERAL TIMES CONCURRENTLY: PLEASE ENSURE THAT IT IS THREAD SAFE!
|
|
|
|
*/
|
|
|
|
__declspec(dllexport) const wchar_t* OnNewSentence(const wchar_t* sentence, const InfoForExtension* miscInfo)
|
|
|
|
{
|
2018-08-20 03:26:50 +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;
|
|
|
|
|
|
|
|
wchar_t error[] = L"Error while translating.";
|
2018-08-30 10:47:00 +08:00
|
|
|
std::wstring translation(L"");
|
|
|
|
const wchar_t* message = error;
|
2018-08-20 03:26:50 +08:00
|
|
|
|
2018-08-22 10:43:30 +08:00
|
|
|
if (wcslen(sentence) > 2000 || GetProperty("hook address", miscInfo) == -1) return sentence;
|
2018-08-20 03:26:50 +08:00
|
|
|
|
|
|
|
if (internet)
|
|
|
|
{
|
2018-08-30 10:47:00 +08:00
|
|
|
if (!TKK)
|
|
|
|
if (HINTERNET connection = WinHttpConnect(internet, L"translate.google.com", INTERNET_DEFAULT_HTTPS_PORT, 0))
|
2018-08-20 03:26:50 +08:00
|
|
|
{
|
2018-08-30 10:47:00 +08:00
|
|
|
if (HINTERNET request = WinHttpOpenRequest(connection, L"GET", L"/", NULL, NULL, NULL, WINHTTP_FLAG_SECURE))
|
2018-08-20 03:26:50 +08:00
|
|
|
{
|
2018-08-30 10:47:00 +08:00
|
|
|
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
|
|
|
{
|
|
|
|
DWORD bytesRead;
|
|
|
|
char buffer[100000] = {}; // Google Translate page is ~64kb
|
|
|
|
WinHttpReceiveResponse(request, NULL);
|
|
|
|
WinHttpReadData(request, buffer, 100000, &bytesRead);
|
|
|
|
TKK = strtoll(strstr(buffer, "a\\x3d") + 5, nullptr, 10) + strtoll(strstr(buffer, "b\\x3d") + 5, nullptr, 10);
|
|
|
|
}
|
|
|
|
WinHttpCloseHandle(request);
|
2018-08-20 03:26:50 +08:00
|
|
|
}
|
2018-08-30 10:47:00 +08:00
|
|
|
WinHttpCloseHandle(connection);
|
2018-08-20 03:26:50 +08:00
|
|
|
}
|
2018-08-30 10:47:00 +08:00
|
|
|
|
2018-08-20 03:26:50 +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, TKK).c_str(), NULL, NULL, NULL, WINHTTP_FLAG_ESCAPE_DISABLE | WINHTTP_FLAG_SECURE))
|
|
|
|
{
|
|
|
|
if (WinHttpSendRequest(request, NULL, 0, NULL, 0, 0, NULL))
|
|
|
|
{
|
|
|
|
DWORD bytesRead;
|
2018-08-20 05:55:50 +08:00
|
|
|
char buffer[10000] = {};
|
2018-08-20 03:26:50 +08:00
|
|
|
WinHttpReceiveResponse(request, NULL);
|
2018-08-20 05:55:50 +08:00
|
|
|
WinHttpReadData(request, buffer, 10000, &bytesRead);
|
2018-08-20 03:26:50 +08:00
|
|
|
// Response formatted as JSON: starts with '[[["'
|
2018-08-22 11:41:30 +08:00
|
|
|
if (buffer[0] == '[')
|
|
|
|
{
|
2018-08-30 10:47:00 +08:00
|
|
|
wchar_t wbuffer[10000] = {};
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, (char*)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;
|
|
|
|
message = translation.c_str();
|
2018-08-22 11:41:30 +08:00
|
|
|
}
|
2018-08-20 03:26:50 +08:00
|
|
|
}
|
|
|
|
WinHttpCloseHandle(request);
|
|
|
|
}
|
|
|
|
WinHttpCloseHandle(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t* newSentence = (wchar_t*)malloc((wcslen(sentence) + 3 + wcslen(message)) * sizeof(wchar_t));
|
|
|
|
swprintf(newSentence, wcslen(sentence) + 3 + wcslen(message), L"%s%s%s", sentence, L"\r\n", message);
|
|
|
|
return newSentence;
|
2018-08-19 12:13:19 +08:00
|
|
|
}
|
|
|
|
}
|