-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootloader.cpp
212 lines (197 loc) · 7.72 KB
/
bootloader.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
#include "bootloader.h"
#include "ui_bootloader.h"
#include "qDebug.h"
#include <QFile>
#include <QDir>
#include <QTimer>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QHostInfo>
#include "qjson/src/serializer.h"
#include "qjson/src/parser.h"
#include <QHostAddress>
#include <QNetworkInterface>
BootLoader::BootLoader(QSettings* set, QWidget *parent) :
QDialog(parent),
ui(new Ui::BootLoader)
{
ui->setupUi(this);
settings = set;
}
BootLoader::~BootLoader()
{
delete ui;
}
void BootLoader::processExisting()
{
QEventLoop loop;
//QTimer timer;
QString cip = sender()->property("ip").toString();
if (!processed.contains(cip))
{
if (!cip.isEmpty() && httpBuf[cip]->data().contains(settings->value("Global/MasterKeyword").toByteArray()))
{
ui->textBrowser->append(QString("Connected to %1").arg(cip));
qDebug() << "got sgt from " << cip;
QStringList sl = QString(httpBuf[cip]->data()).split("\r\n");
sl.removeAll("");
sl.removeAll(settings->value("Global/MasterKeyword").toString());
QHttp* http;
nbmac++;
ui->nbmac->setText(QVariant(nbmac).toString());
httpBuf[cip]->close();
foreach (QString cur, sl)
{
http = new QHttp();
http->setHost(cip, settings->value("Global/Webserverport").toInt());
QObject::connect(http, SIGNAL(done(bool)), &loop, SLOT(quit()));
nbfil++;
ui->nbfil->setText(QVariant(nbfil).toString());
ui->inProgress->setText(cur);
QFile file;
file.setFileName(settings->value("Global/Database").toString() + "/" + cur);
file.open(QIODevice::WriteOnly);
http->get("/" + cur, &file);
QTimer* timer = new QTimer();
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()), &loop, SLOT(quit()));
//timer.start(settings->value("Global/AutoDelay").toInt());
loop.exec();
timer->stop();
if (file.pos() == 0) //FIXME
{
qDebug() << "empty... redl...";
sl << cur;
}
file.close();
}
}
processed << cip;
}
ui->progressBar->setValue(1+ui->progressBar->value());
if (ui->progressBar->value() == ips.size())
QTimer::singleShot(33, this, SLOT(close()));
}
void BootLoader::process()
{
// QString xtsi;
// {
// QNetworkAccessManager manager;
// QNetworkRequest request(QUrl("http://localhost:9091/transmission/rpc"));
// request.setRawHeader("User-Agent", "User Agent");
// request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
// QEventLoop loop;
// connect(&manager, SIGNAL(finished(QNetworkReply *)), &loop, SLOT(quit()));
// QNetworkReply *reply = manager.post(request, "");
// loop.exec();
// if (reply->error() == QNetworkReply::UnknownContentError)
// {
// QRegExp re("X-Transmission-Session-Id: ([a-zA-Z0-9]*)");
// re.indexIn(reply->readAll());
// xtsi = re.cap(1);
// }
// }
// {
// QNetworkAccessManager* manager = new QNetworkAccessManager();
// QNetworkRequest request(QUrl("http://localhost:9091/transmission/rpc&test=1"));
// request.setRawHeader("User-Agent", "User Agent");
// request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
// request.setRawHeader("X-Transmission-Session-Id", xtsi.toAscii());
// QVariantMap vars;
// vars.insert("method", "session-get");
// //vars.insert("arguments", "version");
// QJson::Serializer serializer;
// QByteArray json = serializer.serialize(vars);
// qDebug() << json;
// QString PostVariable = json;
// QEventLoop loop;
// connect(manager, SIGNAL(finished(QNetworkReply *)), &loop, SLOT(quit()));
// QNetworkReply *reply = manager->post(request, PostVariable.toUtf8());
// loop.exec();
// QString jsonresult;
// if (reply->error() == QNetworkReply::NoError)
// {
// jsonresult = QString(reply->readAll());
// //qDebug() << jsonresult;
// }
// else
// qDebug() << "error" << reply->error() << reply->readAll() << reply->errorString();
// QJson::Parser parser;
// bool ok;
// QVariantMap result2 = parser.parse (jsonresult.toAscii(), &ok).toMap();
// QVariantMap result = result2["arguments"].toMap();
// foreach (QString v, result.keys())
// {
// qDebug() << v << "=>" << result[v];
// }
// loop.exec();
// }
QString uTorrentIPs = settings->value("Global/uTorrentIPs").toString();
if (uTorrentIPs == "auto")
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
if (interface.flags().testFlag(QNetworkInterface::IsRunning))
foreach (QNetworkAddressEntry entry, interface.addressEntries())
if (interface.hardwareAddress() != "00:00:00:00:00:00" && entry.ip().toString().contains("."))
if (entry.ip().toString().contains("192.168") || entry.ip().toString().contains("10."))
uTorrentIPs = QString(entry.ip().toString()).replace(QRegExp("[.][^.]*$"), ".%1");
nbfil = nbmac = 0;
QStringList toremove;
if (settings->value("Global/CleanDatabase").toBool())
{
qDebug() << QCoreApplication::applicationDirPath()+ "/" + settings->value("Global/Database").toString();
QStringList fl = QDir(QCoreApplication::applicationDirPath()+ "/" + settings->value("Global/Database").toString()).entryList();
foreach(QString f, fl)
{
qDebug() << "removing" << f;
QFile::remove(QCoreApplication::applicationDirPath()+ "/" + settings->value("Global/Database").toString() + "/" + f);
}
}
QStringList filters;
filters << (QString("*.") + settings->value("Global/TorrentDescriptionExt").toString()) << "*.png" << "*.torrent";
QStringList fl = QDir(QCoreApplication::applicationDirPath()+ "/" + settings->value("Global/WebserverRoot").toString()).entryList(filters);
QFile dist;
dist.setFileName(settings->value("Global/WebserverRoot").toString() + "/" + settings->value("Global/Distfile").toString());
dist.open(QIODevice::WriteOnly);
dist.write((settings->value("Global/MasterKeyword").toString() + "\r\n").toAscii());
foreach(QString f, fl)
{
qDebug() << "listing" << f;
dist.write((f + "\r\n").toAscii());
}
dist.close();
ui->progressBar->setValue(0);
//ips << settings->value("Global/MasterServers").toStringList();
if (settings->value("Global/AutoMasterServers").toBool())
{
for (int ip = 1; ip < 255; ip++)
{
ips << uTorrentIPs.arg(ip);
}
}
ips.removeDuplicates();
QStringList ips2 = settings->value("Global/MasterServers").toStringList();
foreach (QString ip, ips2)
for (int i = 1; i < settings->value("Global/MasterServersRetry").toInt(); i++)
foreach (QHostAddress qha, QHostInfo::fromName(ip).addresses())
if (qha.toString().contains("."))
ips << qha.toString();
ips.removeDuplicates();
ui->progressBar->setMaximum(ips.size());
foreach (QString cip, ips)
{
QHttp* http = new QHttp();
http->setProperty("ip", cip);
http->setHost(cip, settings->value("Global/Webserverport").toInt());
httpBuf[cip] = new QBuffer();
QTimer* timer = new QTimer();
http->get("/" + settings->value("Global/Distfile").toString(), httpBuf[cip]);
QObject::connect(http, SIGNAL(done(bool)), this, SLOT(processExisting()));
timer->setSingleShot(true);
connect(timer, SIGNAL(timeout()), this, SLOT(processExisting()));
QEventLoop loop;
QTimer::singleShot(settings->value("Global/AutoDelay").toInt(), &loop, SLOT(quit()));
loop.exec();
timer->start(settings->value("Global/AutoTimeout").toInt());
}
}