-
Notifications
You must be signed in to change notification settings - Fork 57
/
thundercore.cpp
627 lines (492 loc) · 18.5 KB
/
thundercore.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* CloudClient - A Qt cloud client for lixian.vip.xunlei.com
* Copyright (C) 2012 by Aaron Lewis <[email protected]>
*
* 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 "thundercore.h"
ThunderCore::ThunderCore(QObject *parent) :
QObject(parent),
tmp_cookieIsStored (false),
tc_nam (new QNetworkAccessManager (this))
{
connect (tc_nam, SIGNAL(finished(QNetworkReply*)),
SLOT(slotFinished(QNetworkReply*)));
}
QList<Thunder::Task> ThunderCore::getCloudTasks()
{
return tc_cloudTasks;
}
QList<Thunder::Task> ThunderCore::getGarbagedTasks()
{
return tc_garbagedTasks;
}
QList<Thunder::BatchTask> ThunderCore::getUploadedBatchTasks()
{
return tmp_batchTasks;
}
Thunder::RemoteTask ThunderCore::getSingleRemoteTask()
{
return tmp_singleTask;
}
void ThunderCore::addBatchTaskPre(const QString &urls)
{
post (QUrl("http://dynamic.cloud.vip.xunlei.com/interface/batch_task_check"),
"random=123456&url=" + urls.toUtf8().toPercentEncoding());
}
void ThunderCore::addBatchTaskPost(const QStringList &urls)
{
QByteArray postData = "cid%5B%5D=&class_id=0&"
"batch_old_taskid=0&batch_old_database=0";
foreach (const QString & url, urls)
{
postData.append("&url%5B%5D=").append(url.toUtf8().toPercentEncoding());
}
post (QUrl("http://dynamic.cloud.vip.xunlei.com/interface/batch_task_commit?callback=a"),
postData);
}
void ThunderCore::delayCloudTask(const QStringList &ids)
{
QString id_list;
foreach (const QString & id, ids)
{
id_list.append(id).append("_1,");
}
get (QUrl ("http://dynamic.cloud.vip.xunlei.com/interface/task_delay"
"?taskids=" + id_list +
"&interfrom=task&noCacheIE=1362310408959"));
}
void ThunderCore::cleanupHistory()
{
QUrl url ("http://dynamic.cloud.vip.xunlei.com/interface/history_clear"
"?tcache=1328430359476&flag=6");
url.addQueryItem("uid", tc_session.value("userid"));
}
void ThunderCore::commitBitorrentTask(const QList<Thunder::BTSubTask> &tasks)
{
QString ids, sizes;
foreach (const Thunder::BTSubTask & task, tasks)
{
ids.append(task.id).append("_");
sizes.append(task.size).append("_");
}
post (QUrl("http://dynamic.cloud.vip.xunlei.com/"
"interface/bt_task_commit?callback=a&t=" +
QDateTime::currentDateTime().toString().toAscii().toPercentEncoding()),
"goldbean=0&class_id=0&o_taskid=0&o_page=task&silverbean=0"
"&findex=" + ids.toAscii() +
"&uid=" + tc_session.value("userid").toAscii() +
"&btname=" + tmp_btTask.ftitle.toUtf8().toPercentEncoding() +
"&tsize=" + QByteArray::number(tmp_btTask.btsize) +
"&size=" + sizes.toAscii() +
"&cid=" + tmp_btTask.infoid.toAscii());
}
void ThunderCore::addCloudTaskPost(const Thunder::RemoteTask &task)
{
QUrl url ("http://dynamic.cloud.vip.xunlei.com/interface/"
"task_commit?callback=ret_task&cid=&gcid=&goldbean=0&"
"silverbean=0&type=2&o_page=task&o_taskid=0&ref_url=");
url.addQueryItem("size", task.size);
url.addQueryItem("uid", tc_session["userid"]);
url.addQueryItem("t", task.name);
url.addQueryItem("url", task.url);
get (url);
}
void ThunderCore::loginWithCapcha(const QByteArray &capcha)
{
error (tr("Login .. cookie: %1").arg(QString::fromAscii(capcha)), Info);
post (QUrl("http://login.xunlei.com/sec2login/"),
"login_hour=720&login_enable=0&u=" + tc_userName.toAscii() +
"&verifycode=" + capcha +
"&p=" + Util::getEncryptedPassword(
tc_passwd, QString::fromAscii(capcha), true).toAscii());
}
void ThunderCore::getContentsOfBTFolder(const Thunder::Task &bt_task)
{
get ("http://dynamic.cloud.vip.xunlei.com/interface/fill_bt_list"
"?callback=a&g_net=1&p=1&noCacheIE=1328405858893&infoid=" + bt_task.cid +
"&tid=" + bt_task.id +
"&uid=" + tc_session.value("userid"));
}
void ThunderCore::setCapcha(const QString &code)
{
loginWithCapcha(code.toAscii());
}
void ThunderCore::reloadCloudTasks()
{
get (QUrl("http://dynamic.cloud.vip.xunlei.com/user_task?st=0&userid=" +
tc_session.value("userid")));
}
void ThunderCore::addCloudTaskPre(const QString &url)
{
QUrl link ("http://dynamic.cloud.vip.xunlei.com/interface/"
"task_check?callback=queryCid"
"&random=13271369889801529719.0135479392&tcache=1327136998160");
link.addQueryItem("url", url);
get (link);
}
void ThunderCore::slotFinished(QNetworkReply *reply)
{
const QUrl & url = reply->url();
const QString & urlStr = url.toString();
const QByteArray & data = reply->readAll();
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (httpStatus < 200 || httpStatus > 400)
{
error (tr("Error reading %1, got %2")
.arg(urlStr)
.arg(httpStatus), Notice);
return;
}
/////
if (urlStr.startsWith("http://login.xunlei.com/check"))
{
QByteArray capcha;
foreach (const QNetworkCookie & cookie, tc_nam->cookieJar()->cookiesForUrl(url))
{
if (cookie.name() == "check_result") { capcha = cookie.value(); break; }
}
/* Get rid of heading 0: */
capcha = capcha.remove(0, 2);
if (capcha.isEmpty())
{
error (tr("Capcha required, retrieving .."), Notice);
tc_loginStatus = Failed; emit StatusChanged (LoginChanged);
/* TODO: */
get (QString("http://verify.xunlei.com/image?cachetime=1359365355018"));
return;
}
loginWithCapcha(capcha);
return;
}
if (urlStr.startsWith("http://verify.xunlei.com/image"))
{
tc_capcha = data;
error (tr("Capcha received, size %1").arg(tc_capcha.size()), Notice);
emit StatusChanged(CapchaReady);
return;
}
if (urlStr.startsWith("http://login.xunlei.com/sec2login/"))
{
foreach (const QNetworkCookie & cookie, tc_nam->cookieJar()->cookiesForUrl(url))
{
if (cookie.name() == "usernick")
{
error (tr("User nick: %1").arg(QString::fromAscii(cookie.value())), Info);
}
tc_session.insert(cookie.name(), cookie.value());
}
if (! tc_session.contains("jumpkey"))
{
error (tr("Logon failure"), Warning);
tc_loginStatus = Failed; emit StatusChanged (LoginChanged);
return;
}
get (QUrl("http://dynamic.cloud.vip.xunlei.com/login?"
"cachetime=1327129660280&cachetime=1327129660555&from=0"));
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/login"))
{
reloadCloudTasks();
tc_loginStatus = NoError; emit StatusChanged (LoginChanged);
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/user_task"))
{
/*
* It's best that we handle the page with QtScript!
*/
error (tr("Retrieved user page, analysing .."), Info);
parseCloudPage(data);
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/interface/task_delete"))
{
/*!
* \brief Reload user page!
*/
reloadCloudTasks();
error (tr("Task removed, reloading page .."), Info);
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/interface/task_check"))
{
QStringList fields = Util::parseFunctionFields(data);
if (fields.size() != 10)
{
error (tr("Protocol changed or parser failure, submit this line: %1")
.arg(QString::fromAscii(data)), Warning);
return;
}
tmp_singleTask.name = fields.at(4);
tmp_singleTask.size = Util::toReadableSize(fields.at(2).toULongLong());
emit RemoteTaskChanged(SingleTaskReady);
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/interface/task_commit"))
{
error(tr("Task added, reloading page .."), Notice);
reloadCloudTasks();
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/"
"interface/torrent_upload"))
{
// todo: bt task editing?
QByteArray json = data;
// remove btResult =
json.remove (0, 51);
// chop </script>
json.chop (10);
QJson::Parser parser;
bool ok = false;
QVariant result = parser.parse(json, &ok);
if (! ok)
{
error (tr("JSON parse failure, protocol changed or invalid data."),
Warning);
qDebug() << json;
return;
}
tmp_btTask.ftitle = result.toMap().value("ftitle").toString();
tmp_btTask.infoid = result.toMap().value("infoid").toString();
tmp_btTask.btsize = result.toMap().value("btsize").toULongLong();
foreach (const QVariant & item, result.toMap().value("filelist").toList())
{
QVariantMap map = item.toMap();
Thunder::BTSubTask task;
task.name = map.value("subtitle").toString();
task.format_size = map.value("subformatsize").toString();
task.size = map.value("subsize").toString();
task.id = map.value("id").toString();
tmp_btTask.subtasks.append(task);
}
emit RemoteTaskChanged(BitorrentTaskReady);
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/"
"interface/bt_task_commit"))
{
error(tr("Bitorrent commited, reloading tasks .."), Notice);
reloadCloudTasks();
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/interface/history_clear"))
{
error(tr("History emptied"), Notice);
// ERROR checking?
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/interface/fill_bt_list"))
{
error(tr("BT task page retrieved, parsing .."), Notice);
QByteArray json = data;
// remove a(
json.remove (0, 2);
// chop )
json.chop (1);
QJson::Parser parser;
bool ok = false;
QVariant result = parser.parse(json, &ok);
if (! ok)
{
error (tr("BT task page: JSON parse failure, "
"protocol changed or invalid data."),
Warning);
qDebug() << json;
return;
}
result = result.toMap().value("Result");
Thunder::BitorrentTask bt_task;
const QVariantMap & resultMap = result.toMap();
if (resultMap.size() == 0)
{
error(tr("BT task not finished, skipping sub tasks."), Notice);
return;
}
bt_task.taskid = resultMap.keys().first();
QVariant mainMap = resultMap.value(bt_task.taskid);
foreach (const QVariant & record, mainMap.toList())
{
const QVariantMap & map = record.toMap();
Thunder::BTSubTask subtask;
subtask.id = map.value("id").toString();
subtask.size = Util::toReadableSize(map.value("filesize").toULongLong());
subtask.link = map.value("downurl").toString().replace("\\/", "/");
subtask.name = map.value("title").toString();
bt_task.subtasks.append(subtask);
}
emit BTSubTaskReady (bt_task);
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/interface/batch_task_check"))
{
QByteArray json = data;
// remove <script>document.domain='xunlei.com';parent.begin_task_batch_resp(
json.remove(0, 66);
// chop ,'123456');</script>"
json.chop(20);
QJson::Parser parser;
bool ok = false;
QVariant result = parser.parse(json, &ok);
if (! ok)
{
error (tr("Batch task parser: JSON parse failure, "
"protocol changed or invalid data."),
Warning);
qDebug() << json;
return;
}
tmp_batchTasks.clear();
foreach (const QVariant & item, result.toList())
{
const QVariantMap & itemData = item.toMap();
Thunder::BatchTask batch_task;
batch_task.url = itemData.value("url").toString();
batch_task.name = itemData.value("name").toString();
batch_task.size = itemData.value("filesize").toULongLong();
batch_task.formatsize = itemData.value("formatsize").toString();
tmp_batchTasks.push_back(batch_task);
}
emit RemoteTaskChanged(ThunderCore::BatchTaskReady);
return;
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/interface/batch_task_commit"))
{
reloadCloudTasks();
}
if (urlStr.startsWith("http://dynamic.cloud.vip.xunlei.com/interface/task_delay"))
{
return;
}
qDebug() << "Unhandled reply:" << "\n----";
qDebug() << "URL: " << urlStr;
qDebug() << "Cookie: ";
foreach (const QNetworkCookie & cookie, tc_nam->cookieJar()->cookiesForUrl(url))
{
qDebug() << cookie.name() << cookie.value();
}
qDebug() << "Data: " << data;
}
QByteArray ThunderCore::getCapchaCode()
{
return tc_capcha;
}
Thunder::BitorrentTask ThunderCore::getUploadedBTTasks()
{
return tmp_btTask;
}
void ThunderCore::parseCloudPage(const QByteArray &body)
{
QWebPage page;
page.settings()->setAttribute(QWebSettings::JavaEnabled, false);
page.settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
page.settings()->setAttribute(QWebSettings::AutoLoadImages, false);
page.mainFrame()->setHtml(QString::fromUtf8(body));
// tc_session.clear();
/// FIND gdriveid
foreach (const QWebElement & input, page.mainFrame()->findAllElements("input"))
if (input.attribute("id") == "cok")
{
tc_session.insert("gdriveid", input.attribute("value"));
if (! tmp_cookieIsStored)
{
tmp_cookieIsStored = true;
Util::writeFile(Util::getHomeLocation() + "/.tdcookie",
".vip.xunlei.com\tTRUE\t/\tFALSE\t90147186842\tgdriveid\t" + tc_session.value("gdriveid").toAscii());
}
break;
}
if (! tc_session.contains("gdriveid"))
{
error (tr("Broken page! No gdrive id found, "
"you will not be able to initiate any file transfer! "), Warning);
return;
}
tc_cloudTasks.clear();
/// CACHE TASK IDS for automatic task renewal
QStringList local_taskids;
/// FIND TASKS
foreach (const QWebElement & div, page.mainFrame()->findAllElements("div"))
{
if (! div.attributeNames().contains("taskid")) continue;
Thunder::Task task;
task.id = div.attribute("taskid");
task.type = Thunder::Single;
foreach (const QWebElement & input, div.findAll("input"))
{
const QString & id = input.attribute("id");
if (id.startsWith("f_url")) task.source = input.attribute("value");
if (id.startsWith("dcid")) task.cid = input.attribute("value");
if (id.startsWith("durl")) task.name = input.attribute("value");
if (id.startsWith("dl_url")) task.link = input.attribute("value");
if (id.startsWith("bt_down_url")) task.bt_url = input.attribute("value");
if (id.startsWith("ysfilesize")) task.size = input.attribute("value").toULongLong();
if (id.startsWith("d_status")) task.status = input.attribute("value").toInt();
}
if (! task.isEmpty())
{
if (task.bt_url.startsWith("bt://"))
task.type = Thunder::BT;
tc_cloudTasks.push_back(task);
local_taskids.append(task.id);
}
}
/// MAGIC!
delayCloudTask(local_taskids);
error (tr("%1 task(s) loaded.").arg(tc_cloudTasks.size()), Notice);
emit StatusChanged(TaskChanged);
}
void ThunderCore::removeCloudTasks(const QStringList &ids)
{
post (QUrl("http://dynamic.cloud.vip.xunlei.com/interface/task_delete?type=2&callback=a"),
"databases=0,&old_databaselist=&old_idlist=&taskids=" + ids.join(",").toAscii());
}
void ThunderCore::get(const QUrl &url)
{
tc_nam->get(QNetworkRequest(url));
}
void ThunderCore::uploadBitorrent(const QString &file)
{
tmp_btTask.subtasks.clear();
const QByteArray & torrent = Util::readWholeFile(file);
const QByteArray & boundary = Util::getRandomString(10).toAscii();
QNetworkRequest request (QString("http://dynamic.cloud.vip.xunlei.com/"
"interface/torrent_upload"));
request.setHeader(QNetworkRequest::ContentTypeHeader,
"multipart/form-data; boundary=----" + boundary);
tc_nam->post(request, "------" + boundary + "\n"
"Content-Disposition: form-data; "
"name=\"filepath\"; filename=sample.torrent\n"
"Content-Type: application/x-bitorrent\n\n" + torrent +
"Content-Disposition: form-data; name=\"random\"\n\n" +
"13284335922471757912.3826739355\n"
"------" + boundary + "\n");
}
void ThunderCore::post(const QUrl &url, const QByteArray &body)
{
QNetworkRequest request (url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
tc_nam->post(request, body);
}
void ThunderCore::login(const QString &user, const QString &passwd)
{
tc_userName = user;
tc_passwd = passwd;
error ("Getting capcha code ..", Info);
this->get(QString("http://login.xunlei.com/check?u=%1&cachetime=%2")
.arg(user)
.arg(QString::number(QDateTime::currentMSecsSinceEpoch())));
}