This repository has been archived by the owner on May 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
psnrequest.cpp
470 lines (392 loc) · 18.1 KB
/
psnrequest.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
* QPSNProxy is an open source software for downloading PSN packages
* on a PC, then transfer them to a game console via a HTTP proxy.
*
* Copyright (C) 2014 codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "psnrequest.h"
#include "json.h"
#include <QDebug>
#include <QDir>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QSettings>
#include <QStandardPaths>
#include <QUrl>
static const QString userAgent("Mozilla/5.0 (X11; Linux x86_64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/35.0.1916.114 Safari/537.36");
static const QString loginUrl("https://auth.api.sonyentertainmentnetwork.com/login.do");
static const QString oauthUrl("https://auth.api.sonyentertainmentnetwork.com/2.0/oauth/"
"authorize?response_type=code&"
"scope=kamaji:commerce_native,kamaji:commerce_container&"
"client_id=f6c7057b-f688-4744-91c0-8179592371d2&"
"prompt=none&redirect_uri=%1");
static const QString storeUrl("https://store.sonyentertainmentnetwork.com/kamaji/api/chihiro/"
"00_09_000/user/session");
static const QString storeQuery("https://store.sonyentertainmentnetwork.com/kamaji/api/chihiro/"
"00_09_000/user/stores");
static const QString userQuery("https://store.sonyentertainmentnetwork.com/kamaji/api/chihiro/"
"00_09_000/user/account/name");
static const QString storeSignInReferer("https://store.sonyentertainmentnetwork.com/shared/html/"
"signinRedirect_SSOAuth.html");
static const QString storeRefreshReferer("https://store.sonyentertainmentnetwork.com/shared/html/"
"refreshRedirect_SSOAuth.html");
static const QString queryUrl("https://store.sonyentertainmentnetwork.com/kamaji/api/chihiro/"
"00_09_000/gateway/store/v1/users/me/internal_entitlements?"
"start=%1&size=%2&fields=game_meta,drm_def");
static const QString requestDownloadUrl("https://store.sonyentertainmentnetwork.com/kamaji/api/chihiro/"
"00_09_000/user/notification/download");
static const QString requestCancelUrl("https://store.sonyentertainmentnetwork.com/kamaji/api/chihiro/"
"00_09_000/user/notification/download/status");
static const QString requestDownloadStatusUrl("https://store.sonyentertainmentnetwork.com/kamaji/api/"
"chihiro/00_09_000/user/notification/download/status/?"
"status=notstarted&status=stopped&status=waitfordownload&"
"platformString=%1&size=%2");
static const QString jsonFilename("listing.json");
PSNRequest::PSNRequest(QObject *parent) :
QObject(parent), m_manager(this), m_reply(NULL), m_error(QNetworkReply::NoError)
{
m_cookieJar.load();
m_manager.setCookieJar(&m_cookieJar);
m_cookieJar.setParent(0);
}
PSNRequest::~PSNRequest()
{
m_cookieJar.save();
}
void PSNRequest::setLastError(QNetworkReply::NetworkError code)
{
qDebug() << "Error in psn network reply: " << code;
m_error = code;
}
void PSNRequest::checkLogin()
{
QNetworkRequest request((QUrl(storeQuery)));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
m_reply = m_manager.get(request);
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), this, SLOT(receiveLoginResponse()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::receiveLoginResponse()
{
if(m_error != QNetworkReply::NoError && m_error != QNetworkReply::AuthenticationRequiredError)
{
qDebug() << "PSNRequest::receiveLoginResponse error: " << m_error;
emit networkErrorReceived(m_reply->errorString());
return;
}
QByteArray data = m_reply->readAll();
QVariantMap json = json_decode(data);
QVariantMap store_header = json["header"].toMap();
QString status_code = store_header["status_code"].toString();
QString message_key = store_header["message_key"].toString();
bool ok = false;
int code = status_code.toInt(&ok, 16);
if(ok && code == 0)
{
QVariantMap store_data = json["data"].toMap();
QString storeRoot = store_data["root_url"].toString();
qDebug() << "Got store root: " << storeRoot;
emit storeRootUrlReceived(storeRoot);
}
qDebug() << "Login status: " << status_code << ", message: " << message_key;
emit loginStatusReceived(code, message_key);
}
void PSNRequest::login(const QString &username, const QString &password)
{
QByteArray postData;
postData.append("j_username=" + username + "&");
postData.append("j_password=" + password + "&");
postData.append("rememberSignIn=on&");
postData.append("request_locale=en_US&service_entity=psn&disableLinks=SENLink&hidePageElements=SENLogo");
m_cookieJar.clear();
QNetworkRequest request((QUrl(loginUrl)));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/x-www-form-urlencoded"));
qDebug() << "Sending initial login POST";
m_reply = m_manager.post(request, postData);
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), this, SLOT(requestOauthLogin()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::loginRefresh()
{
QNetworkRequest request((QUrl(oauthUrl.arg(storeRefreshReferer))));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
qDebug() << "Requesting oAuth refresh GET";
m_reply = m_manager.get(request);
m_storeReferer = storeRefreshReferer;
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(receiveMetadataResponse()));
connect(m_reply, SIGNAL(finished()), this, SLOT(requestStoreLogin()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::requestOauthLogin()
{
if(m_error != QNetworkReply::NoError)
{
qDebug() << "PSNRequest::requestOauthLogin error: " << m_error;
emit networkErrorReceived(m_reply->errorString());
return;
}
QNetworkRequest request((QUrl(oauthUrl.arg(storeSignInReferer))));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
qDebug() << "Requesting oAuth login GET";
m_reply = m_manager.get(request);
m_storeReferer = storeSignInReferer;
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(receiveMetadataResponse()));
connect(m_reply, SIGNAL(finished()), this, SLOT(requestStoreLogin()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::receiveMetadataResponse()
{
QByteArray code = m_reply->rawHeader("X-NP-GRANT-CODE");
if(!code.isEmpty())
{
qDebug() << "Got auth code: " << code.data();
m_authCode = QString(code);
}
else
qDebug() << "Failed to get auth code";
}
void PSNRequest::requestStoreLogin()
{
// ignore auth failed, we only need the auth code
if(m_error != QNetworkReply::NoError && m_error != QNetworkReply::AuthenticationRequiredError)
{
qDebug() << "PSNRequest::requestStoreLogin error: " << m_error;
emit networkErrorReceived(m_reply->errorString());
return;
}
if(m_authCode.isEmpty())
{
qWarning() << "No auth code received";
emit loginFailed();
return;
}
QByteArray postData;
postData.append("code=" + m_authCode);
QNetworkRequest request((QUrl(storeUrl)));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
request.setRawHeader("X-Alt-Referer", m_storeReferer.toUtf8());
request.setRawHeader("X-Requested-By", "Chihiro");
request.setRawHeader("XMLHttpRequest", "XMLHttpRequest");
request.setRawHeader("Referer", "https://store.sonyentertainmentnetwork.com/");
request.setRawHeader("Kamaji-Client", "Chihiro-Web");
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/x-www-form-urlencoded"));
qDebug() << "Sending final login POST";
m_reply = m_manager.post(request, postData);
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), this, SLOT(receiveLoginCompleteResponse()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::receiveLoginCompleteResponse()
{
if(m_error != QNetworkReply::NoError && m_error != QNetworkReply::AuthenticationRequiredError)
{
qDebug() << "PSNRequest::receiveLoginCompleteResponse error: " << m_error;
emit networkErrorReceived(m_reply->errorString());
return;
}
qDebug() << "Login OK";
emit loginSucceeded();
}
void PSNRequest::requestStoreRootUrl()
{
QNetworkRequest request((QUrl(storeQuery)));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
m_reply = m_manager.get(request);
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), this, SLOT(receiveRootUrlReply()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::receiveRootUrlReply()
{
if(m_error != QNetworkReply::NoError && m_error != QNetworkReply::AuthenticationRequiredError)
{
qDebug() << "PSNRequest::receiveRootUrlReply error: " << m_error;
emit networkErrorReceived(m_reply->errorString());
return;
}
QByteArray data = m_reply->readAll();
QVariantMap json = json_decode(data);
QVariantMap store_data = json["data"].toMap();
QString storeRoot = store_data["root_url"].toString();
qDebug() << "Got store root: " << storeRoot;
emit storeRootUrlReceived(storeRoot);
}
void PSNRequest::requestUserInfo()
{
QNetworkRequest request((QUrl(userQuery)));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
m_reply = m_manager.get(request);
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), this, SLOT(receiveUserInfo()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::receiveUserInfo()
{
if(m_error != QNetworkReply::NoError)
{
qDebug() << "PSNRequest::receiveUserInfo error: " << m_error;
emit networkErrorReceived(m_reply->errorString());
return;
}
QByteArray data = m_reply->readAll();
qDebug() << "Received user data, " << data.size() << " bytes";
QVariantMap json = json_decode(data);
QVariantMap header = json["header"].toMap();
bool ok;
int code = header["status_code"].toInt(&ok);
if(ok && code == 0)
{
QVariantMap user_data = json["data"].toMap();
QString onlineId = user_data["onlineId"].toString();
QString signId = user_data["signInId"].toString();
qDebug() << "Got online ID: " << onlineId << ", sign ID: " << signId;
emit userInfoReceived(onlineId, signId);
}
else
{
emit userInfoRequestFail();
}
}
void PSNRequest::requestDownloadList()
{
int size = QSettings().value("maxTitles", 10240).toInt();
QUrl query_url(QString(queryUrl).arg(QString::number(0), QString::number(size)));
QNetworkRequest request(query_url);
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
qDebug() << "Requesting download list GET";
m_reply = m_manager.get(request);
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), this, SLOT(receiveDownloadList()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::receiveDownloadList()
{
if(m_error != QNetworkReply::NoError && m_error != QNetworkReply::AuthenticationRequiredError)
{
qDebug() << "PSNRequest::receiveDownloadList error: " << m_error;
emit networkErrorReceived(m_reply->errorString());
return;
}
QByteArray data = m_reply->readAll();
qDebug() << "Received list data, " << data.size() << " bytes";
emit downloadListReceived(data);
}
void PSNRequest::requestGameDownload(const QString &contentId, const QString &platform)
{
QNetworkRequest request((QUrl(requestDownloadUrl)));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
request.setRawHeader("X-Requested-By", "Chihiro");
request.setRawHeader("XMLHttpRequest", "XMLHttpRequest");
request.setRawHeader("Referer", "https://store.sonyentertainmentnetwork.com/");
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
QVariantList requestList;
QVariantMap requestMap;
requestMap["platformString"] = platform;
requestMap["contentId"] = contentId;
requestList.append(requestMap);
QByteArray postData;
postData.append(json_encode(requestList));
qDebug() << "Sending download request POST";
m_reply = m_manager.post(request, postData);
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), this, SLOT(receiveRequestResponse()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::requestGameCancel(const QString &contentId, const QString &platform)
{
QNetworkRequest request((QUrl(requestCancelUrl)));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
request.setRawHeader("X-Requested-By", "Chihiro");
request.setRawHeader("XMLHttpRequest", "XMLHttpRequest");
request.setRawHeader("Referer", "https://store.sonyentertainmentnetwork.com/");
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
QVariantList requestList;
QVariantMap requestMap;
requestMap["platformString"] = platform;
requestMap["contentId"] = contentId;
requestMap["status"] = QLatin1String("usercancelled");
requestList.append(requestMap);
QByteArray postData;
postData.append(json_encode(requestList));
qDebug() << "Sending download cancel POST";
m_reply = m_manager.post(request, postData);
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), this, SLOT(receiveRequestResponse()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::receiveRequestResponse()
{
if(m_error != QNetworkReply::NoError)
{
qDebug() << "PSNRequest::receiveRequestResponse error: " << m_error;
emit networkErrorReceived(m_reply->errorString());
return;
}
QNetworkReply *reply = reinterpret_cast<QNetworkReply *>(sender());
QByteArray data = reply->readAll();
QVariantMap json = json_decode(data);
QVariantMap store_header = json["header"].toMap();
QString status_code = store_header["status_code"].toString();
QString message_key = store_header["message_key"].toString();
bool ok = false;
int code = status_code.toInt(&ok, 16);
qDebug() << "Request status: " << status_code << ", message: " << message_key;
emit requestStatusReceived(code, message_key);
}
void PSNRequest::requestDownloadStatus(const QString &platform)
{
int maxChecks = QSettings().value("maxChecks", 1000).toInt();
QNetworkRequest request(requestDownloadStatusUrl.arg(platform, QString::number(maxChecks)));
request.setHeader(QNetworkRequest::UserAgentHeader, userAgent);
qDebug() << "Requesting download status GET";
m_reply = m_manager.get(request);
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(setLastError(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), this, SLOT(receiveStatusList()));
connect(m_reply, SIGNAL(finished()), m_reply, SLOT(deleteLater()));
}
void PSNRequest::receiveStatusList()
{
if(m_error != QNetworkReply::NoError)
{
qDebug() << "PSNRequest::receiveStatusList error: " << m_error;
//emit networkErrorReceived(m_reply->errorString());
return;
}
QNetworkReply *reply = reinterpret_cast<QNetworkReply *>(sender());
QByteArray data = reply->readAll();
QVariantMap json = json_decode(data);
QVariantMap status_header = json["header"].toMap();
QString status_code = status_header["status_code"].toString();
QString message_key = status_header["message_key"].toString();
bool ok = false;
int code = status_code.toInt(&ok, 16);
qDebug() << "Request status: " << status_code << ", message: " << message_key;
if(code == 0)
{
QVariantMap status_data = json["data"].toMap();
QVariantList notification_list = status_data["notifications"].toList();
emit statusReceived(notification_list);
}
else
emit requestStatusReceived(code, message_key);
}