-
Notifications
You must be signed in to change notification settings - Fork 13
/
googleapkdownloadtask.cpp
278 lines (266 loc) · 10.9 KB
/
googleapkdownloadtask.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "googleapkdownloadtask.h"
#include "googleplayapi.h"
#include "googleloginhelper.h"
#include <QStandardPaths>
#include <QDir>
#ifdef GOOGLEPLAYDOWNLOADER_USEQT
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtConcurrent>
#endif
#include <unistd.h>
GoogleApkDownloadTask::GoogleApkDownloadTask(QObject *parent) : QObject(parent), m_active(false) {
#ifdef GOOGLEPLAYDOWNLOADER_USEQT
connect(this, &GoogleApkDownloadTask::queueDownload, this, &GoogleApkDownloadTask::startDownload);
#endif
}
void GoogleApkDownloadTask::setPlayApi(GooglePlayApi *value) {
Q_ASSERT(m_playApi == nullptr);
m_playApi = value;
}
QStringList GoogleApkDownloadTask::filePaths() {
QMutexLocker l (&fileMutex);
QStringList list;
for(auto&& file : files) {
list.append(file->fileName());
}
return list;
}
void GoogleApkDownloadTask::start(bool skipMainApk) {
m_active.store(true);
emit activeChanged();
m_playApi->getApi()->delivery(m_packageName.toStdString(), m_versionCode, std::string())->call([this, skipMainApk](playapi::proto::finsky::response::ResponseWrapper&& resp) {
auto dd = resp.payload().deliveryresponse().appdeliverydata();
auto apkUrl = dd.has_gzippeddownloadurl() ? dd.gzippeddownloadurl() : dd.downloadurl();
if(apkUrl == "") {
throw std::runtime_error(QObject::tr("Cannot find <a href=\"https://play.google.com/store/apps/details?id=%1\">%1</a> with version %2 on Google Play,<br/><a href=\"https://play.google.com/apps/testing/%1\">Beta Versions requires sign up</a>%3").arg(m_packageName).arg(m_versionCode).arg(m_playApi->getLogin()->isChromeOS() ? QObject::tr(",<br/>you might want to try disabling ChromeOS mode to fix this") : "").toStdString());
}
#ifdef GOOGLEPLAYDOWNLOADER_USEQT
emit queueDownload(dd, skipMainApk);
#else
startDownload(dd, skipMainApk);
#endif
}, [this](std::exception_ptr e) {
try {
std::rethrow_exception(e);
} catch(std::exception& e) {
emit error(e.what());
}
m_active.store(false);
emit activeChanged();
});
}
bool GoogleApkDownloadTask::curlDoZlibInflate(z_stream &zs, int file, char *data, size_t len, int flags) {
char buf[4096];
int ret;
zs.avail_in = (uInt) len;
zs.next_in = (unsigned char*) data;
zs.avail_out = 0;
while (zs.avail_out == 0) {
zs.avail_out = 4096;
zs.next_out = (unsigned char*) buf;
ret = inflate(&zs, flags);
if (ret == Z_STREAM_ERROR)
return false;
if (write(file, buf, sizeof(buf) - zs.avail_out) != sizeof(buf) - zs.avail_out)
return false;
}
return true;
}
template<class T, class U> void GoogleApkDownloadTask::downloadFile(T const&dd, U cookie, std::function<void()> success, std::function<void()> _error, std::shared_ptr<DownloadProgress> _progress, std::string componentName, size_t id) {
auto apkUrl = dd.has_gzippeddownloadurl() ? dd.gzippeddownloadurl() : dd.downloadurl();
emit downloadInfo(QString::fromStdString(apkUrl));
if(m_dryrun) {
std::thread(success).detach();
return;
}
auto apksdir = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)).filePath("mcpelauncher/apks");
QDir().mkpath(apksdir);
auto file = std::make_shared<QTemporaryFile>(QDir(apksdir).filePath(m_keepApks ? (m_packageName.toStdString() + "-" + componentName + "-XXXXXX.apk").data() : "temp-XXXXXX.apk"));
if(m_keepApks) {
file->setAutoRemove(false);
}
#ifdef GOOGLEPLAYDOWNLOADER_USEQT
file->open();
auto url = dd.downloadurl();
{
if(_progress->downloadsize != -1) {
auto size = dd.downloadsize();
if(size > 0) {
_progress->downloadsize += size;
_progress->progress[id] = 0;
} else {
_progress->downloadsize = -1;
}
}
}
QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QNetworkRequest request;
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
request.setUrl(QUrl(QString::fromStdString(url)));
connect(manager, &QNetworkAccessManager::finished, [file, this, manager, success, _error, url](QNetworkReply* reply) {
file->flush();
file->close();
if(!reply->error()) {
{
QMutexLocker l (&fileMutex);
files.push_back(file);
}
success();
} else {
emit error(QObject::tr("Downloading file failed %1").arg(QString::fromStdString(url)));
_error();
}
reply->deleteLater();
manager->deleteLater();
});
auto reply = manager->get(request);
connect(reply, &QNetworkReply::readyRead, [reply, file]() {
file->write(reply->readAll());
});
connect(reply, &QNetworkReply::downloadProgress, [_progress, id, this](qint64 dlnow, qint64 total) {
if(_progress->downloadsize > 0) {
_progress->progress[id] = dlnow;
emit progress((float) std::accumulate(_progress->progress.begin(), _progress->progress.end(), 0) / _progress->downloadsize);
}
});
#else
bool isGzipped = dd.has_gzippeddownloadurl();
playapi::http_request req(isGzipped ? dd.gzippeddownloadurl() : dd.downloadurl());
// The http method has been undefined behavior
req.set_method(playapi::http_method::GET);
if(_progress->downloadsize != -1) {
auto size = isGzipped ? dd.gzippeddownloadsize() : dd.downloadsize();
if(size > 0) {
_progress->downloadsize += size;
_progress->progress[id] = 0;
} else {
_progress->downloadsize = -1;
}
}
if (isGzipped)
req.set_encoding("gzip,deflate");
req.add_header("Accept-Encoding", "identity");
req.add_header("Cookie", cookie.name() + "=" + cookie.value());
auto& device = m_playApi->getLogin()->getDevice();
req.set_user_agent("AndroidDownloadManager/" + device.build_version_string + " (Linux; U; Android " +
device.build_version_string + "; " + device.build_model + " Build/" + device.build_id + ")");
req.set_follow_location(true);
req.set_timeout(0L);
if (!file->open())
throw std::runtime_error("Failed to open file");
int fd = file->handle();
std::shared_ptr<z_stream> zs;
if (isGzipped) {
zs = std::make_shared<z_stream>();
zs->zalloc = Z_NULL;
zs->zfree = Z_NULL;
zs->opaque = Z_NULL;
int ret = inflateInit2(zs.get(), 31);
if (ret != Z_OK)
throw std::runtime_error("Failed to init zlib");
req.set_custom_output_func([fd, zs](char* data, size_t size) {
if (!curlDoZlibInflate(*zs, fd, data, size, Z_NO_FLUSH))
return (size_t) 0;
return size;
});
} else {
req.set_custom_output_func([fd](char* data, size_t size) {
return write(fd, data, size);
});
}
req.set_progress_callback([this, _progress, id](curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
std::lock_guard<std::mutex> guard(_progress->mtx);
if(_progress->downloadsize > 0) {
_progress->progress[id] = dlnow;
emit progress((float) std::accumulate(_progress->progress.begin(), _progress->progress.end(), 0) / _progress->downloadsize);
}
});
emit progress(0.f);
req.perform([this, file, zs, fd, isGzipped, success, _error](playapi::http_response resp) {
if (isGzipped) {
curlDoZlibInflate(*zs, fd, Z_NULL, 0, Z_FINISH);
inflateEnd(zs.get());
}
file->close();
if (resp) {
if(resp.get_status_code() == 200) {
{
QMutexLocker l (&fileMutex);
files.push_back(file);
}
success();
} else {
emit error(QObject::tr("Downloading file failed: Status[%1] '%2'").arg(QString::fromStdString(std::to_string(resp.get_status_code()))).arg(QString::fromStdString(resp.get_body())));
_error();
}
}
else {
emit error(QObject::tr("CURL Network error: %1").arg(QObject::tr("Unknown error")));
_error();
}
}, [this, file, zs, fd, isGzipped, _error](std::exception_ptr e) {
if (isGzipped) {
curlDoZlibInflate(*zs, fd, Z_NULL, 0, Z_FINISH);
inflateEnd(zs.get());
}
file->close();
try {
std::rethrow_exception(e);
} catch(std::exception& e) {
emit error(e.what());
}
_error();
});
#endif
}
void GoogleApkDownloadTask::startDownload(playapi::proto::finsky::download::AndroidAppDeliveryData const &dd, bool skipMainApk) {
if(m_dryrun) {
QString allUrls;
if (!skipMainApk || dd.splitdeliverydata().empty() /* Old Minecraft versions < 1.15.y needs a full download */) {
//allUrls += (dd.has_gzippeddownloadurl() ? dd.gzippeddownloadurl() : dd.downloadurl()) + "<br/>";
auto url = /*dd.has_gzippeddownloadurl() ? dd.gzippeddownloadurl() :*/ dd.downloadurl();
allUrls += QObject::tr("<a href=\"%2\">%1</a>: %2<br/>").arg("main").arg(QString::fromStdString(url));
}
for(auto && data : dd.splitdeliverydata()) {
auto url = /*data.has_gzippeddownloadurl() ? data.gzippeddownloadurl() :*/ data.downloadurl();
allUrls += QObject::tr("<a href=\"%2\">%1</a>: %2<br/>").arg(QString::fromStdString(data.id())).arg(QString::fromStdString(url));
}
emit downloadInfo(allUrls);
return;
}
auto cookie = dd.downloadauthcookie(0);
auto progress = std::make_shared<DownloadProgress>();
std::lock_guard<std::mutex> guard(progress->mtx);
progress->downloads = 1 + dd.splitdeliverydata().Capacity();
progress->progress.resize(progress->downloads);
progress->downloadsize = 0;
auto cleanup = [this, progress]() {
std::lock_guard<std::mutex> guard(progress->mtx);
if(!--progress->downloads) {
m_active.store(false);
emit activeChanged();
}
};
auto success = [this, cleanup, dd, cookie, progress]() {
std::lock_guard<std::mutex> guard(progress->mtx);
if(!--progress->downloads) {
m_active.store(false);
emit activeChanged();
emit finished();
}
};
{
QMutexLocker l (&fileMutex);
files.clear();
}
size_t id = 0;
if (!skipMainApk || dd.splitdeliverydata().empty() /* Old Minecraft versions < 1.15.y needs a full download */) {
downloadFile(dd, cookie, success, cleanup, progress, "main", id++);
}
for(auto && data : dd.splitdeliverydata()) {
downloadFile(data, cookie, success, cleanup, progress, data.id(), id++);
}
progress->downloads = id;
}