-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNetworkMonitor.cpp
485 lines (474 loc) · 18.3 KB
/
NetworkMonitor.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
#include "NetworkMonitor.h"
#include "ui_NetworkMonitor.h"
#include "Logger.h"
#include "Consts.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QSqlQuery>
#include <QSqlError>
#include <QNetworkInterface>
#include <QDebug>
#include <QtMath>
NetworkMonitor::NetworkMonitor(QWidget *parent) :
QWidget(parent),
ui(new Ui::NetworkMonitor)
{
ui->setupUi(this);
//初始化 网络管理器
netManager=new QNetworkAccessManager();
reply=NULL;
retriedTimes=0;
/*初始化 网络请求*/
//初始化 https策略
QSslConfiguration sslConfig;
sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConfig.setProtocol(QSsl::TlsV1_0OrLater);
//初始化 内网检测地址(vUPC)
vUPC.setUrl(QUrl("http://v.upc.edu.cn"));
vUPC.setRawHeader("Accept","text/html");
vUPC.setHeader(QNetworkRequest::UserAgentHeader,"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
//初始化 网号登录状态地址(getUserInfo)
netIDInfo.setUrl(QUrl("http://121.251.251.207/eportal/InterFace.do?method=getOnlineUserInfo"));
netIDInfo.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
//初始化 外网检测地址(ipHost)
ipHost.setUrl(QUrl("http://ip.jerryzone.cn"));
ipHost.setSslConfiguration(sslConfig);
ipHost.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
//初始化 DNS检测地址(DNSPod)
DNSReader.setUrl(QUrl("https://dnsapi.cn/Record.Info"));
DNSReader.setSslConfiguration(sslConfig);
DNSReader.setHeader(QNetworkRequest::UserAgentHeader,"RaspiFace/2.0([email protected])");
DNSReader.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
//初始化 DNS设定地址(DNSPod)
DNSPoster.setUrl(QUrl("https://dnsapi.cn/Record.Modify"));
DNSPoster.setSslConfiguration(sslConfig);
DNSPoster.setHeader(QNetworkRequest::UserAgentHeader,"RaspiFace/2.0([email protected])");
DNSPoster.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
//初始化 登录页面检查地址
pgCheck.setUrl(QUrl("http://121.251.251.207/eportal/InterFace.do?method=pageInfo"));
pgCheck.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
//初始化 网号登录地址
netLogin.setUrl(QUrl("http://121.251.251.207/eportal/InterFace.do?method=login"));
netLogin.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
//初始化 验证码获取地址
vCodeFetch.setUrl(QUrl("http://121.251.251.207/eportal/validcode"));
//初始化 网号登录必需参数
netLoginQS="";
validCode="";
//直接显示内网IP
showEthIP();
this->netIdDeadline=QDateTime::fromTime_t(0);
//初始化 计时器
heart=new QTimer(this);
//heart->setSingleShot(true);
heart->setInterval(500);
connect(heart,SIGNAL(timeout()),this,SLOT(beat()));
//ui->freshTime->setText(QDateTime::currentDateTime().addSecs(5).toString("HH:mm:ss"));
setTiming(5);
//QTimer::singleShot(0,this,SLOT(beat()));
//初始化 按钮
connect(ui->freshNow,SIGNAL(clicked()),this,SLOT(refreshImmediately()));
}
NetworkMonitor::~NetworkMonitor(){
delete ui;
}
/*开始检测*/
void NetworkMonitor::startCheck(){
ui->freshNow->setDisabled(true);
showEthIP();
ethCKRequest();
}
/*发起 IP获取&内网检测*/
void NetworkMonitor::ethCKRequest(){
qDebug()<<"正在检测校园网连接...";
if(reply) delete reply;
reply=netManager->get(vUPC);
QTimer::singleShot(NET_WAITING_TIME*2,reply,SLOT(abort()));
connect(reply,SIGNAL(finished()),this,SLOT(ethCKRequestRespond()));
}
/* IP获取&内网检测 响应 */
void NetworkMonitor::ethCKRequestRespond(){
reply->blockSignals(true);
//qDebug()<<reply->errorString();
if(reply->error()==QNetworkReply::OperationCanceledError){
reply->disconnect();
checkTerminated("校园网检测超时!",CHECK_TERMINATE_CONTEXT);
return;
}
if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()!=200)
return checkTerminated("校园网连接中断!",CHECK_TERMINATE_CONTEXT);
QString body=QString(reply->readAll());
QRegExp rx("(\\d{1,3}\\.){3}\\d{1,3}");
int pos = 0;
QStringList IPS;
while ((pos = rx.indexIn(body, pos)) != -1){
pos += rx.matchedLength();
QString result = rx.cap(0);
IPS << result;
}
if(IPS.count()<=0)
return checkTerminated("查询公网IP失败!",CHECK_TERMINATE_CONTEXT);
ui->intIP->setText(IPS[IPS.count()-1]);
qInfo()<<"校园网连接正常!";
idInfRequest();
}
/*发起 检查网号登录状态*/
void NetworkMonitor::idInfRequest(){
qDebug()<<"正在拉取网号状态...";
if(reply) delete reply;
reply=netManager->post(netIDInfo,QByteArray("userIndex="));
QTimer::singleShot(NET_WAITING_TIME,reply,SLOT(abort()));
connect(reply,SIGNAL(finished()),this,SLOT(idInfRequestRespond()));
}
/*网号状态拉取 请求响应*/
void NetworkMonitor::idInfRequestRespond(){
reply->blockSignals(true);
if(reply->error()==QNetworkReply::OperationCanceledError){
reply->disconnect();
ui->netId->setText("[拉取超时]");
//ui->netIdDead->setText("[拉取超时]");
checkTerminated("拉取网号状态数据超时!",CHECK_TERMINATE_CONTEXT);
return;
}
if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()!=200){
ui->netId->setText("[通信失败]");
//ui->netIdDead->setText("[通信失败]");
checkTerminated(QString("认证服务器返回异常(%1)!").arg(QString::number(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt())),CHECK_TERMINATE_CONTEXT);
return;
}
QJsonDocument qjd=QJsonDocument::fromJson(reply->readAll());
QJsonObject stat=qjd.object();
QJsonValue userId=stat.value("userId");
QJsonValue userName=stat.value("userName");
QJsonValue ballInfoRaw=stat.value("ballInfo");
if(userId.isNull()){
ui->netId->setText("[未登录网号]");
qWarning()<<"未登网号,尝试登录网号!";
loginPreRequest();
return;
}
currentId=userId.toString("解析失败");
currentName=userName.toString("解析失败");
if(currentId==currentName&¤tId!="解析失败"){
qDebug()<<"重新获取网号信息...";
idInfRequest();
return;
}
qInfo()<<qPrintable(QString("网号:%1(%2)").arg(currentId,currentName));
// QJsonDocument ballInfo=QJsonDocument::fromJson(ballInfoRaw.toString("").toUtf8());
// QJsonArray balls=ballInfo.array();
// if(balls.size()>2){
// QJsonObject flowBall=balls.at(1).toObject();
// QString bytes=flowBall.value("value").toString("0.0").split(".")[0];
// ui->netIdFlow->setText(latterFlow(bytes.toULongLong()));
// }
ui->netId->setText(currentId);
ui->netIdName->setText(currentName);
//ui->netIdDead->setText(QString("%1%2").arg(netIdDeadline.toString("yyyy-MM-dd"),remainDate));
ipHostRequest();
}
/* 发起 转储服务器转储请求*/
void NetworkMonitor::ipHostRequest(){
qDebug()<<"正在更新转储服务器状态...";
if(reply) delete reply;
QString raw("TG=Raspi&IP=%1×tamp=%2&Life=1200");
raw=raw.arg(ui->intIP->text(),QString::number(QDateTime::currentDateTime().toTime_t()));
reply=netManager->post(ipHost,raw.toUtf8());
QTimer::singleShot(NET_WAITING_TIME,reply,SLOT(abort()));
connect(reply,SIGNAL(finished()),this,SLOT(ipHostRequestRespond()));
}
/* 检查网络 请求响应*/
void NetworkMonitor::ipHostRequestRespond(){
reply->blockSignals(true);
if(reply->error()==QNetworkReply::OperationCanceledError){
reply->disconnect();
checkTerminated("更新转储服务器超时!",CHECK_TERMINATE_CONTEXT);
return;
}
if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()!=666){
checkTerminated("公网异常稍后重试!",CHECK_TERMINATE_CONTEXT);
return;
}
qInfo()<<"转储服务器已经更新!";
//dnsCKRequest();
checkFinished();
}
/*发起 DNS检查*/
void NetworkMonitor::dnsCKRequest(){
qDebug()<<"正在检查DNS数据...";
QByteArray data("login_token=114191,82320c0e071bca721cbecc6b98f3e5d8&format=json&domain=jerryhome.tk&record_id=195272416");
if(reply) delete reply;
reply=netManager->post(DNSReader,data);
QTimer::singleShot(NET_WAITING_TIME,reply,SLOT(abort()));
connect(reply,SIGNAL(finished()),this,SLOT(dnsCKRequestRespond()));
}
/*DNS检查 请求响应*/
void NetworkMonitor::dnsCKRequestRespond(){
//disconnect(reply,SIGNAL(finished()),this,SLOT(dnsCKRequestRespond()));
reply->blockSignals(true);
if(reply->error()==QNetworkReply::OperationCanceledError){
reply->disconnect();
checkTerminated("请求DNSPod超时!",CHECK_TERMINATE_CONTEXT);
return;
}
if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)!=200){
return checkTerminated("检查DNS时发生HTTP错误!",CHECK_TERMINATE_CONTEXT);
}
QJsonDocument QJDom=QJsonDocument::fromJson(reply->readAll());
if(QJDom.object().take("status").toObject().take("code").toString().toInt()!=1)
checkTerminated("检查DNS时发生鉴权错误!",CHECK_TERMINATE_CONTEXT);
else if(QJDom.object().take("record").toObject().take("value").toString()!=ui->intIP->text()){
qWarning()<<"DNS信息过期!";
dnsSTRequest();
}else{
qInfo()<<"DNS信息无需更新!";
checkFinished();
}
}
/*发起 DNS设定*/
void NetworkMonitor::dnsSTRequest(){
QString raw="login_token=114191,82320c0e071bca721cbecc6b98f3e5d8&format=json&domain=jerryhome.tk&record_id=195272416&sub_domain=www&record_type=A&record_line=%e9%bb%98%e8%ae%a4&value=%1";
raw=raw.arg(ui->intIP->text());
qDebug()<<"正在更新DNS数据...";
if(reply) delete reply;
reply=netManager->post(DNSPoster,raw.toLatin1());
QTimer::singleShot(NET_WAITING_TIME,reply,SLOT(abort()));
connect(reply,SIGNAL(finished()),this,SLOT(dnsSTRequestRespond()));
}
/*DNS设定 请求响应*/
void NetworkMonitor::dnsSTRequestRespond(){
reply->blockSignals(true);
if(reply->error()==QNetworkReply::OperationCanceledError){
reply->disconnect();
checkTerminated("请求DNSPod超时!",CHECK_TERMINATE_CONTEXT);
return;
}
if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)!=200)
return checkTerminated("设定DNS时发生HTTP错误!",CHECK_TERMINATE_CONTEXT);
QJsonDocument QJDom=QJsonDocument::fromJson(reply->readAll());
if(QJDom.object().take("status").toObject().take("code").toString().toInt()!=1)
return checkTerminated("设定DNS失败!",CHECK_TERMINATE_CONTEXT);
qInfo()<<"DNS更新成功!";
checkFinished();
}
/*发起 登录网号预备*/
void NetworkMonitor::loginPreRequest(){
qDebug()<<"正在检测认证服务器状态...";
if(reply) delete reply;
reply=netManager->get(ipHost);
QTimer::singleShot(NET_WAITING_TIME,reply,SLOT(abort()));
connect(reply,SIGNAL(finished()),this,SLOT(loginPreRequestRespond()));
}
/*登录网号预备 响应*/
void NetworkMonitor::loginPreRequestRespond(){
reply->blockSignals(true);
if(reply->error()==QNetworkReply::OperationCanceledError){
reply->disconnect();
checkTerminated("认证服务器请求超时!",CHECK_TERMINATE_CONTEXT);
return;
}else if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute)==200){
qInfo()<<"疑似网络正常,重拉网号状态!";
idInfRequest();
return;
}else if(!reply->hasRawHeader("Location")){
checkTerminated("认证服务器返回异常!",CHECK_TERMINATE_CONTEXT);
return;
}
qInfo()<<"认证服务器工作正常";
QString newLocation=reply->header(QNetworkRequest::LocationHeader).toString();
netLoginQS=newLocation.split("?").at(1);
loginPgChkRequest();
//loginRequest();
}
/*发起 页面检查请求*/
void NetworkMonitor::loginPgChkRequest(){
qDebug()<<"检查验证码需求...";
QString raw=QString("queryString=%1").arg(netLoginQS);
if(reply) delete reply;
reply=netManager->post(pgCheck,raw.toLatin1());
QTimer::singleShot(NET_WAITING_TIME,reply,SLOT(abort()));
connect(reply,SIGNAL(finished()),this,SLOT(loginPgChkRequestRespond()));
}
/*页面检查 请求响应*/
void NetworkMonitor::loginPgChkRequestRespond(){
//disconnect(reply,SIGNAL(finished()),this,SLOT(loginPgChkRequestRespond()));
reply->blockSignals(true);
if(reply->error()==QNetworkReply::OperationCanceledError){
reply->disconnect();
checkTerminated("检查网络环境超时!",CHECK_TERMINATE_CONTEXT);
return;
}else if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()!=200){
checkTerminated("检查网络环境时发生网络错误!",CHECK_TERMINATE_CONTEXT);
return;
}
QJsonObject data=QJsonDocument::fromJson(reply->readAll()).object();
QString vCodeUrl=data.value("validCodeUrl").toString();
if(!vCodeUrl.isEmpty()){
qWarning()<<"认证系统请求验证码!";
vCodeRequest();
return;
}else{
qInfo()<<"认证系统未请求验证码!";
}
loginRequest();
}
/*发起 登录网号*/
void NetworkMonitor::loginRequest(){
QString raw("operatorPwd=&operatorUserId=&password=%2&queryString=%4&service=%3&userId=%1&validcode=%5");
raw=raw.arg("S18070010","123456Abc","cmcc",QString(netLoginQS.toLocal8Bit().toPercentEncoding()),validCode);
//raw=raw.arg("1407030207","528204","cmcc",QString(netLoginQS.toLocal8Bit().toPercentEncoding()),validCode);
if(reply) delete reply;
reply=netManager->post(netLogin,raw.toLatin1());
QTimer::singleShot(NET_WAITING_TIME,reply,SLOT(abort()));
connect(reply,SIGNAL(finished()),this,SLOT(loginRequestRespond()));
}
/*登网号 请求响应*/
void NetworkMonitor::loginRequestRespond(){
reply->blockSignals(true);
if(reply->error()==QNetworkReply::OperationCanceledError){
reply->disconnect();
checkTerminated("登录网号超时!",CHECK_TERMINATE_CONTEXT);
return;
}else if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt()!=200){
checkTerminated("登录网号时发生网络错误!",CHECK_TERMINATE_CONTEXT);
return;
}
QJsonDocument qjd=QJsonDocument::fromJson(reply->readAll());
QJsonObject stat=qjd.object();
QString result=stat.value("result").toString();
if(result=="success"){
qInfo()<<"登录网号成功!";
validCode="";
idInfRequest();
}else{
QString Reason=stat.value("message").toString();
qCritical("登录失败:%s",qPrintable(Reason));
if(Reason=="验证码错误.")
vCodeRequest();
else
checkTerminated();
}
}
/*更新验证码*/
void NetworkMonitor::vCodeRequest(){
if(reply) delete reply;
qDebug()<<"正在获取验证码...";
reply=netManager->get(vCodeFetch);
QTimer::singleShot(NET_WAITING_TIME,reply,SLOT(abort()));
connect(reply,SIGNAL(finished()),this,SLOT(vCodeRequestRespond()));
}
/*更新验证码 响应槽*/
void NetworkMonitor:: vCodeRequestRespond(){
reply->blockSignals(true);
if(reply->error()==QNetworkReply::OperationCanceledError){
reply->disconnect();
checkTerminated("获取验证码超时!",CHECK_TERMINATE_CONTEXT);
return;
}
QByteArray data=reply->readAll();
QString picMD5=QString(QCryptographicHash::hash(data,QCryptographicHash::Md5).toHex());
QSqlDatabase db;
QSqlQuery sqlHandle(db);
sqlHandle.prepare("Select code from vcode where md5=?");
sqlHandle.bindValue(0,picMD5);
sqlHandle.exec();
if(sqlHandle.next()){
validCode=sqlHandle.value("code").toString();
qInfo()<<qPrintable(QString("验证码:%1").arg(validCode));
loginRequest();
}else{
checkTerminated("验证码识别失败!",CHECK_TERMINATE_CONTEXT);
}
}
void NetworkMonitor::checkTerminated(){
return NetworkMonitor::checkTerminated("",CHECK_TERMINATE_CONTEXT);
}
/*检查错误中断*/
void NetworkMonitor::checkTerminated(QString reason,const char *file, int line, const char *function){
if(reason.count()>0)
QMessageLogger(file, line, function).critical()<<qPrintable(reason);
this->retriedTimes++;
if(this->retriedTimes>=5){
this->retriedTimes=0;
qCritical()<<"重试次数超过5次,5分钟后重试";
setTiming(300);
}else{
setTiming(20);
}
//QTimer::singleShot(0,this,SLOT(beat()));
ui->freshNow->setEnabled(true);
}
/*检查结束*/
void NetworkMonitor::checkFinished(){
qInfo()<<"网络连接正常!";
this->retriedTimes=0;
setTiming(1200);
//QTimer::singleShot(0,this,SLOT(beat()));
ui->freshNow->setEnabled(true);
}
void NetworkMonitor::beat(){
timing=QDateTime::currentDateTime().msecsTo(nextFresh);
ui->countBar->setValue(timing);
//ui->networkTimeCount->setText(QString::number(timing));
if(timing<=0){
heart->stop();
this->startCheck();
}/*else{
QTimer::singleShot(500,this,SLOT(beat()));
}*/
}
/**
* 普通信息获取
*/
/* 获取&显示内网IP*/
void NetworkMonitor::showEthIP(){
QNetworkInterface eth0=QNetworkInterface::interfaceFromName("eth0");
QList<QNetworkAddressEntry> entryList = eth0.addressEntries();
QString EthIP;
QRegExp ip("(\\d{1,3}\\.){3}\\d{1,3}");
if(entryList.count()<=0)
EthIP="---.---.---.---";
else
foreach(QNetworkAddressEntry temp,entryList){
if(ip.exactMatch(temp.ip().toString().replace("\n",""))){
EthIP=temp.ip().toString().replace("\n","");
break;
}
}
ui->ethIP->setText(EthIP);
}
void NetworkMonitor::setTiming(int length){
//timing=length;
//updateTime();
nextFresh=QDateTime::currentDateTime().addSecs(length);
ui->freshTime->setText(nextFresh.toString("HH:mm:ss"));
ui->countBar->setRange(0,length*1000);
ui->countBar->setValue(0);
heart->start();
}
void NetworkMonitor::refreshImmediately(){
nextFresh=QDateTime::currentDateTime();
ui->countBar->setValue(0);
ui->freshTime->setText(nextFresh.toString("HH:mm:ss"));
heart->stop();
this->retriedTimes=0;
this->startCheck();
}
void NetworkMonitor::updateTime(){
ui->freshTime->setText(QDateTime::currentDateTime().addSecs(timing).toString("HH:mm:ss"));
//if(netIdDeadline.toTime_t()!=0)
//ui->netIdDead->setText(QString("%1%2").arg(netIdDeadline.toString("yyyy-MM-dd"),formatTime(netIdDeadline.secsTo(QDateTime::currentDateTime()))));
}
QString NetworkMonitor::formatTime(int t){
int timeVal[]={1,60,3600,86400};
QString timeUnit[]={"秒","分","时","天"};
int i=0;
for(i=0;i<3&&timeVal[i]<t&&timeVal[i+1]<t;i++);
return QString("[%1%2]").arg(QString::number(t/timeVal[i]),timeUnit[i]);
}
QString NetworkMonitor::latterFlow(quint64 f){
int rate=f>0?qFloor(qLn(f)/qLn(1024)):0;
const QString units[]={"B","KB","MB","GB","TB","PB","EB"};
return QString("%1%2").arg(QString::number(f/qPow(1024,rate),'f',1),units[rate]);
}