forked from rileytestut/AltServer-Windows
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDeveloperDiskManager.cpp
More file actions
executable file
·359 lines (297 loc) · 10.3 KB
/
Copy pathDeveloperDiskManager.cpp
File metadata and controls
executable file
·359 lines (297 loc) · 10.3 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
// DeveloperDiskManager.cpp
// AltServer-Windows
//
// Created by Riley Testut on 7/1/21.
// Copyright © 2021 Riley Testut. All rights reserved.
//
#include "DeveloperDiskManager.h"
#include "AltServerApp.h"
#include "Archiver.hpp"
#include <WS2tcpip.h>
#include <cpprest/filestream.h>
#include <iostream>
#include <fstream>
#define odslog(msg) { std::stringstream ss; ss << msg << std::endl; OutputDebugStringA(ss.str().c_str()); }
extern std::string StringFromWideString(std::wstring wideString);
extern std::wstring WideStringFromString(std::string string);
extern std::string temporary_directory();
extern std::string make_uuid();
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency::streams;
DeveloperDiskManager::DeveloperDiskManager() :
#if STAGING
_client(U("https://f000.backblazeb2.com"))
#else
_client(U("https://cdn.altstore.io"))
#endif
{
}
DeveloperDiskManager::~DeveloperDiskManager()
{
}
pplx::task<std::pair<std::string, std::string>> DeveloperDiskManager::DownloadDeveloperDisk(std::shared_ptr<Device> device)
{
return pplx::create_task([=] {
auto osName = ALTOperatingSystemNameForDeviceType(device->type());
if (!osName.has_value())
{
throw DeveloperDiskError(DeveloperDiskErrorCode::UnsupportedOperatingSystem);
}
OperatingSystemVersion osVersion = device->osVersion();
osVersion.patchVersion = 0; // Patch is irrelevant for developer disks
fs::path developerDiskOSPath = AltServerApp::instance()->developerDisksDirectoryPath();
developerDiskOSPath.append(*osName);
fs::create_directory(developerDiskOSPath);
fs::path developerDiskDirectoryPath(developerDiskOSPath);
developerDiskDirectoryPath.append(osVersion.stringValue());
fs::create_directory(developerDiskDirectoryPath);
fs::path developerDiskPath(developerDiskDirectoryPath);
developerDiskPath.append("DeveloperDiskImage.dmg");
fs::path developerDiskSignaturePath(developerDiskDirectoryPath);
developerDiskSignaturePath.append("DeveloperDiskImage.dmg.signature");
auto isCachedDiskCompatible = this->IsDeveloperDiskCompatible(device);
if (isCachedDiskCompatible && fs::exists(developerDiskPath) && fs::exists(developerDiskSignaturePath))
{
// The developer disk is cached and we've confirmed it works, so re-use it.
return pplx::create_task([developerDiskPath, developerDiskSignaturePath]() {
return std::make_pair(developerDiskPath.string(), developerDiskSignaturePath.string());
});
}
else
{
return this->FetchDeveloperDiskURLs().then([=](web::json::value json) {
auto allDisks = json[L"disks"];
if (!allDisks.has_object_field(WideStringFromString(osName->c_str())))
{
throw DeveloperDiskError(DeveloperDiskErrorCode::UnknownDownloadURL);
}
auto disks = allDisks[WideStringFromString(osName->c_str())];
if (!disks.has_object_field(WideStringFromString(osVersion.stringValue().c_str())))
{
throw DeveloperDiskError(DeveloperDiskErrorCode::UnknownDownloadURL);
}
auto diskURLs = disks[WideStringFromString(osVersion.stringValue().c_str())];
if (!diskURLs.has_string_field(L"archive") && !(diskURLs.has_string_field(L"disk") && diskURLs.has_string_field(L"signature")))
{
throw DeveloperDiskError(DeveloperDiskErrorCode::UnknownDownloadURL);
}
if (diskURLs.has_string_field(L"archive"))
{
// Download archive then unzip
auto archiveURL = StringFromWideString(diskURLs[L"archive"].as_string());
return this->DownloadDiskArchive(archiveURL);
}
else
{
// Download files directly
auto diskURL = StringFromWideString(diskURLs[L"disk"].as_string());
auto signatureURL = StringFromWideString(diskURLs[L"signature"].as_string());
return this->DownloadDisk(diskURL, signatureURL);
}
})
.then([developerDiskPath, developerDiskSignaturePath](std::pair<std::string, std::string> paths) {
try {
if (fs::exists(developerDiskPath))
{
fs::remove(developerDiskPath);
}
if (fs::exists(developerDiskSignaturePath))
{
fs::remove(developerDiskSignaturePath);
}
fs::rename(fs::path(paths.first), developerDiskPath);
fs::rename(fs::path(paths.second), developerDiskSignaturePath);
}
catch (std::exception& e) {
fs::remove(fs::path(paths.first));
fs::remove(fs::path(paths.second));
throw;
}
return std::make_pair(developerDiskPath.string(), developerDiskSignaturePath.string());
});
}
});
};
bool DeveloperDiskManager::IsDeveloperDiskCompatible(std::shared_ptr<Device> device)
{
auto id = this->DeveloperDiskCompatibilityID(device);
if (!id.has_value())
{
return false;
}
bool compatible = AltServerApp::instance()->boolValueForRegistryKey(*id);
return compatible;
}
void DeveloperDiskManager::SetDeveloperDiskCompatible(bool compatible, std::shared_ptr<Device> device)
{
auto id = this->DeveloperDiskCompatibilityID(device);
if (!id.has_value())
{
return;
}
AltServerApp::instance()->setBoolValueForRegistryKey(compatible, *id);
}
pplx::task<web::json::value> DeveloperDiskManager::FetchDeveloperDiskURLs()
{
#if STAGING
auto encodedURI = web::uri::encode_uri(L"/file/altstore-staging/altserver/developerdisks.json");
#else
auto encodedURI = web::uri::encode_uri(L"/file/altstore/altserver/developerdisks.json");
#endif
uri_builder builder(encodedURI);
http_request request(methods::GET);
request.set_request_uri(builder.to_string());
return this->client().request(request)
.then([=](http_response response)
{
return response.content_ready();
})
.then([=](http_response response)
{
odslog("Received response status code: " << response.status_code());
return response.extract_vector();
})
.then([=](std::vector<unsigned char> decompressedData)
{
std::string decompressedJSON = std::string(decompressedData.begin(), decompressedData.end());
if (decompressedJSON.size() == 0)
{
return json::value::object();
}
utility::stringstream_t s;
s << WideStringFromString(decompressedJSON);
auto json = json::value::parse(s);
return json;
});
}
pplx::task<size_t> DeveloperDiskManager::DownloadFile(std::string downloadURL, fs::path destinationPath)
{
auto outputFile = std::make_shared<ostream>();
// Open stream to output file.
return fstream::open_ostream(WideStringFromString(destinationPath.string()))
.then([=](ostream file)
{
*outputFile = file;
// Decode -> encode handles both already encoded URLs and raw URLs.
auto decodedURI = web::uri::decode(WideStringFromString(downloadURL));
auto encodedURI = web::uri::encode_uri(decodedURI);
uri_builder builder(encodedURI);
http_client client(builder.to_uri());
return client.request(methods::GET);
})
.then([=](http_response response)
{
printf("Received download response status code:%u\n", response.status_code());
// Write response body into the file.
return response.body().read_to_end(outputFile->streambuf());
})
.then([=](size_t size)
{
outputFile->close();
return size;
});
}
pplx::task<std::pair<std::string, std::string>> DeveloperDiskManager::DownloadDiskArchive(std::string archiveURL)
{
fs::path temporaryPath(temporary_directory());
temporaryPath.append(make_uuid());
fs::path archivePath(temporaryPath);
archivePath.append("archive.zip");
return pplx::create_task([=] {
fs::create_directory(temporaryPath);
return this->DownloadFile(archiveURL, archivePath);
})
.then([=](size_t size) {
UnzipArchive(archivePath.string(), temporaryPath.string());
fs::path diskPath;
fs::path signaturePath;
for (const auto& entry : fs::recursive_directory_iterator(temporaryPath))
{
auto extension = entry.path().extension().string();
std::transform(extension.begin(), extension.end(), extension.begin(), [](unsigned char c) {
return std::tolower(c);
});
if (extension == ".dmg")
{
diskPath = entry.path();
}
else if (extension == ".signature")
{
signaturePath = entry.path();
}
}
if (diskPath.empty() || signaturePath.empty())
{
throw DeveloperDiskError(DeveloperDiskErrorCode::DownloadedDiskNotFound);
}
fs::path destinationDiskPath(temporary_directory());
destinationDiskPath.append(make_uuid());
fs::path destinationSignaturePath(temporary_directory());
destinationSignaturePath.append(make_uuid());
try {
fs::rename(diskPath, destinationDiskPath);
fs::rename(signaturePath, destinationSignaturePath);
}
catch (std::exception& e) {
fs::remove(destinationDiskPath);
fs::remove(destinationSignaturePath);
throw;
}
return std::make_pair(destinationDiskPath.string(), destinationSignaturePath.string());
})
.then([=](pplx::task<std::pair<std::string, std::string>> task) {
try { fs::remove(temporaryPath); }
catch (std::exception& e) { /* Ignore */ }
return task.get();
});
}
pplx::task<std::pair<std::string, std::string>> DeveloperDiskManager::DownloadDisk(std::string diskURL, std::string signatureURL)
{
fs::path temporaryPath(temporary_directory());
temporaryPath.append(make_uuid());
fs::path diskPath(temporaryPath);
diskPath.append("DeveloperDiskImage.dmg");
fs::path signaturePath(temporaryPath);
signaturePath.append("DeveloperDiskImage.dmg.signature");
return pplx::create_task([=] {
fs::create_directory(temporaryPath);
return this->DownloadFile(diskURL, diskPath);
})
.then([=](size_t diskSize) {
return this->DownloadFile(signatureURL, signaturePath.string());
})
.then([=](size_t signatureSize) {
return std::make_pair(diskPath.string(), signaturePath.string());
})
.then([=](pplx::task<std::pair<std::string, std::string>> task) {
try {
return task.get();
}
catch (std::exception& e)
{
// Only remove directory if error was thrown.
try { fs::remove(temporaryPath); }
catch (std::exception& e) { /* Ignore */ }
throw;
}
});
}
std::optional<std::string> DeveloperDiskManager::DeveloperDiskCompatibilityID(std::shared_ptr<Device> device)
{
auto osName = ALTOperatingSystemNameForDeviceType(device->type());
if (!osName.has_value())
{
return std::nullopt;
}
OperatingSystemVersion osVersion = device->osVersion();
osVersion.patchVersion = 0; // Patch is irrelevant for developer disks
std::string id("ALTDeveloperDiskCompatible_" + *osName + "_" + device->osVersion().stringValue());
return id;
}
web::http::client::http_client DeveloperDiskManager::client() const
{
return this->_client;
}