DaemonService/DaemonService/worker.cpp
2019-01-08 22:53:09 +08:00

98 lines
3.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <QTcpSocket>
#include <QHostAddress>
#include <QProcess>
#include <QString>
#include <string>
#include "worker.h"
#include "md5.h"
#include "dal.h"
#include "model.h"
#include "ipsechelper.h"
void Worker::run()
{
if (this->m_portList.empty())
return;
this->m_socket = new QTcpSocket();
this->m_socket->setSocketDescriptor(this->m_socketDescriptor);
QString ip = m_socket->peerAddress().toString();
if (!this->m_socket->waitForConnected(5000))
{
qDebug("IP:%s Connect Fail该IP连接失败", ip.toStdString().data());
return;
}
// qDebug("IP:%s Connect Success, Waiting for verification...该IP连接成功等待发送校验信息", ip.data());
if (this->m_socket->waitForReadyRead(1000))
{
QByteArray data = this->m_socket->readAll();
// qDebug("IP:%s send data:'%s' ---- Verifying password...(正在校验中...", ip.data(), data.toStdString().data());
// 校验
// if (data.toStdString()
// == MD5("asdfas35.v;cxv-123"
// + MD5("xck3dy$^@1309uyrew"
// + ip.toStdString()
// + "ioer6719024yoiuew6f178934056").toStr()
// + "sjavlkc907*$!@(.12i.dy1").toStr())
QString password = ip + "asdfas35.v;cxv-123ioer6719024yosjavlkc907*$!@(.12i.dy1iuew6f178934056xck3dy$^@1309uyrew";
if (MD5Check(const_cast<char*>(data.toStdString().data()), const_cast<char*>(password.toStdString().data()), password.toStdString().length()))
{
//qDebug("Verify successful!(校验成功!)");
auto list = DAL::instance().getWhiteList(ip);
for (int port : this->m_portList)
{
bool flag = false;
for (const auto &item : list)
{
if (item.Port == port)
{
flag = true;
break;
}
}
if (!flag)
{
//qDebug("Add to whitelists...正在将该IP添加到白名单...");
IpsecHelper::addItemToWhitelist(ip, port);
}
else
{
//qDebug("Update last login time...检测到该IP已在白名单更新其最后上线时间...");
}
}
if (DAL::instance().updateWhiteList(ip, this->m_portList))
qDebug("IP:%s 已连接", ip.toStdString().data());
else
qWarning("Update failed!(更新失败)");
}
else
{
//qWarning("Verification failed!(校验失败!)");
//qWarning("正在将该IP拉黑...");
// 校验失败,若不是算法问题,则可能是其他人想猜密码
IpsecHelper::addItemToBlackList(ip, 8796);
DAL::instance().addItemToBlackList(ip, 8796);
qDebug("IP:%s 已拉黑", ip.toStdString().data());
}
}
else
{
//qWarning("Check timeout超过指定时间未发送任何消息超时");
// 这个连接连上以后不发任何消息说明不是登录器的socket
// 登录器的socket会在连接后立刻发送校验数据
// 所以将这个IP进行记录当这个IP累计超过一定数量次连接则将其拉黑
//qWarning("正在将该IP拉黑...");
IpsecHelper::addItemToBlackList(ip, 8796);
DAL::instance().addItemToBlackList(ip, 8796);
qDebug("IP:%s 已拉黑", ip.toStdString().data());
}
// qDebug("Close Socket connection关闭Socket连接");
// 关闭socket连接
this->m_socket->close();
this->m_socket->deleteLater();
}