diff --git a/DaemonService.pro b/DaemonService.pro new file mode 100644 index 0000000..9ca2324 --- /dev/null +++ b/DaemonService.pro @@ -0,0 +1,60 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2018-12-27T16:26:24 +# +#------------------------------------------------- + +QT += core gui network sql +RESOURCES += res.qrc +RC_ICONS = Daemon.ico + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = DaemonService +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + daemonservice.cpp \ + worker.cpp \ + md5.cpp \ + dal.cpp \ + log.cpp \ + ipsechelper.cpp + +HEADERS += \ + mainwindow.h \ + daemonservice.h \ + worker.h \ + md5.h \ + dal.h \ + model.h \ + log.h \ + ipsechelper.h + +FORMS += \ + mainwindow.ui + +RC_FILE = uac.rc + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + res.qrc diff --git a/DaemonService.pro.user b/DaemonService.pro.user new file mode 100644 index 0000000..b8fea51 --- /dev/null +++ b/DaemonService.pro.user @@ -0,0 +1,328 @@ + + + + + + EnvironmentId + {6a2adf44-d351-4499-8d66-24024cfa2d9d} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + -fno-delayed-template-parsing + + true + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.6.2 MinGW 32bit + Desktop Qt 5.6.2 MinGW 32bit + qt.56.win32_mingw49_kit + 1 + 0 + 0 + + F:/Qt/build-DaemonService-Desktop_Qt_5_6_2_MinGW_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + F:/Qt/build-DaemonService-Desktop_Qt_5_6_2_MinGW_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + F:/Qt/build-DaemonService-Desktop_Qt_5_6_2_MinGW_32bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + 閮ㄧ讲 + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy Configuration + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + DaemonService + + Qt4ProjectManager.Qt4RunConfiguration:F:/Qt/DaemonService/DaemonService.pro + DaemonService.pro + + 3768 + false + true + true + false + false + true + + F:/Qt/build-DaemonService-Desktop_Qt_5_6_2_MinGW_32bit-Release + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 20 + + + Version + 20 + + diff --git a/DaemonService.pro.user.2d03fc4.4.8-pre1 b/DaemonService.pro.user.2d03fc4.4.8-pre1 new file mode 100644 index 0000000..0451085 --- /dev/null +++ b/DaemonService.pro.user.2d03fc4.4.8-pre1 @@ -0,0 +1,322 @@ + + + + + + EnvironmentId + {2d03fc4c-541a-4fa5-8f5c-ae92a1998dce} + + + ProjectExplorer.Project.ActiveTarget + 0 + + + ProjectExplorer.Project.EditorSettings + + true + false + true + + Cpp + + CppGlobal + + + + QmlJS + + QmlJSGlobal + + + 2 + UTF-8 + false + 4 + false + 80 + true + true + 1 + true + false + 0 + true + true + 0 + 8 + true + 1 + true + true + true + false + + + + ProjectExplorer.Project.PluginSettings + + + -fno-delayed-template-parsing + + true + + + + ProjectExplorer.Project.Target.0 + + Desktop Qt 5.11.2 MinGW 32bit + Desktop Qt 5.11.2 MinGW 32bit + qt.qt5.5112.win32_mingw53_kit + 0 + 0 + 0 + + E:/C++/Qt/build-DaemonService-Desktop_Qt_5_11_2_MinGW_32bit-Debug + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + false + false + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Debug + Debug + Qt4ProjectManager.Qt4BuildConfiguration + 2 + true + + + E:/C++/Qt/build-DaemonService-Desktop_Qt_5_11_2_MinGW_32bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + E:/C++/Qt/build-DaemonService-Desktop_Qt_5_11_2_MinGW_32bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 + + + 0 + 閮ㄧ讲 + + ProjectExplorer.BuildSteps.Deploy + + 1 + Deploy Configuration + + ProjectExplorer.DefaultDeployConfiguration + + 1 + + + false + false + 1000 + + true + + false + false + false + false + true + 0.01 + 10 + true + 1 + 25 + + 1 + true + false + true + valgrind + + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + + 2 + + DaemonService + + Qt4ProjectManager.Qt4RunConfiguration:E:/C++/Qt/DaemonService/DaemonService.pro + true + + DaemonService.pro + + E:/C++/Qt/build-DaemonService-Desktop_Qt_5_11_2_MinGW_32bit-Debug + 3768 + false + true + false + false + true + + 1 + + + + ProjectExplorer.Project.TargetCount + 1 + + + ProjectExplorer.Project.Updater.FileVersion + 18 + + + Version + 18 + + diff --git a/DaemonServiceInitializer.bat b/DaemonServiceInitializer.bat new file mode 100644 index 0000000..6c8a73e --- /dev/null +++ b/DaemonServiceInitializer.bat @@ -0,0 +1,21 @@ +@echo off +echo Daemon Service Initialize Start... + +netsh ipsec static add policy name=DaemonService +netsh ipsec static add filterlist name=whitelist +netsh ipsec static add filterlist name=blacklist +netsh ipsec static add filter filterlist=whitelist srcaddr=192.169.0.100 dstaddr=me protocol=ICMP +netsh ipsec static add filter filterlist=blacklist srcaddr=192.169.0.100 dstaddr=me protocol=ICMP +netsh ipsec static add filteraction name=permit action=permit +netsh ipsec static add filteraction name=block action=block +netsh ipsec static add rule name=permitRule policy=DaemonService filterlist=whitelist filteraction=permit +netsh ipsec static add rule name=blockRule policy=DaemonService filterlist=blacklist filteraction=block +netsh ipsec static delete filter filterlist=whitelist srcaddr=192.169.0.100 dstaddr=me protocol=ICMP +netsh ipsec static delete filter filterlist=blacklist srcaddr=192.169.0.100 dstaddr=me protocol=ICMP +netsh ipsec static set policy name=DaemonService assign=y + +rem 禁止任何人通过Tcp访问指定端口,请使用下面语句,并把dstport改成对应端口 +rem netsh ipsec static add filter filterlist=blacklist srcaddr=any dstaddr=me protocol=TCP dstport=9876 + +echo End of initialization +pause \ No newline at end of file diff --git a/UAC.manifest b/UAC.manifest new file mode 100644 index 0000000..b9b1f7f --- /dev/null +++ b/UAC.manifest @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/daemonservice.cpp b/daemonservice.cpp new file mode 100644 index 0000000..13cd81e --- /dev/null +++ b/daemonservice.cpp @@ -0,0 +1,25 @@ +#include "daemonservice.h" +#include +#include +#include +#include +#include "worker.h" + +//DaemonService::DaemonService() +//{ +// if (this->listen(QHostAddress::AnyIPv4, 9876)) +// qDebug("The server started successfully!"); +// else +// qCritical("Server failed to start锛丒rror message锛%s", this->errorString().toStdString().data()); //閿欒淇℃伅 +//} + +/** + * @brief 褰撴湁鏂拌繛鎺ヨ繘鍏ユ椂 + */ +void DaemonService::incomingConnection(qintptr socketDescriptor) +{ + // qDebug("new connect is connect %d锛堟湁鏂扮殑杩炴帴杩涘叆锛侊級", socketDescriptor); + Worker *worker = new Worker(socketDescriptor, this->m_portList); + QThreadPool::globalInstance()->start(worker); +} + diff --git a/daemonservice.h b/daemonservice.h new file mode 100644 index 0000000..976661c --- /dev/null +++ b/daemonservice.h @@ -0,0 +1,45 @@ +#ifndef DAEMONSERVICE_H +#define DAEMONSERVICE_H + +#include +#include +#include +#include +#include + +/** + * @brief 瀹堟姢鏈嶅姟绫伙紝鍗曚緥瀹炵幇 + */ +class DaemonService : public QTcpServer +{ + Q_OBJECT +public: + /** + * @brief 鍗曚緥瀵硅薄 + * @return 鍞竴瀵硅薄 + */ + static DaemonService& instance() + { + static DaemonService obj; + return obj; + } + + void setPortList(QList portList) + { + this->m_portList = portList; + } + +private: + explicit DaemonService() = default; + DaemonService(const DaemonService&) = delete; + DaemonService& operator=(const DaemonService&) = delete; +protected: + /** + * @brief 褰撴湁鏂拌繛鎺ヨ繘鍏ユ椂 + */ + virtual void incomingConnection(qintptr socketDescriptor); +private: + QList m_portList; +}; + +#endif // DAEMONSERVICE_H diff --git a/dal.cpp b/dal.cpp new file mode 100644 index 0000000..b5ff252 --- /dev/null +++ b/dal.cpp @@ -0,0 +1,258 @@ +#include "dal.h" +#include +#include +#include +#include +#include +#include "model.h" + +bool DAL::updateWhiteList(QString ip, QList portList) +{ + if (portList.empty()) + return false; + // 鍏堟壘鎵剧湅鎸囧畾椤瑰瓨涓嶅瓨鍦 + QString sql = QString("SELECT * FROM whitelist WHERE IP=? AND Port in (%1").arg(portList[0]); + for (int i = 1; i < portList.size(); ++i) + sql += QString(",%1").arg(portList[i]); + sql += ");"; + QSqlQuery query; + query.prepare(sql); + query.addBindValue(ip); + if(!query.exec()) + { + qCritical()< whitelist; + while(query.next()) + whitelist.append(WhiteListItem(query.value(0).toInt(), query.value(1).toString(), query.value(2).toInt(), query.value(3).toDateTime())); + + // 寮濮嬩簨鍔 + QSqlDatabase database = QSqlDatabase::database(QSqlDatabase::defaultConnection); + database.transaction(); + + // 妫鏌ユ暟鎹簱涓殑鐧藉悕鍗 + for (int i = 0; i < whitelist.size(); ++i) + { + // 濡傛灉瑕佹洿鏂扮殑宸茬粡瀛樺湪锛屽垯update + if (portList.contains(whitelist[i].Port)) + { + query.prepare("UPDATE whitelist SET LastUpdateTime = datetime(CURRENT_TIMESTAMP,'localtime') WHERE IP=? AND Port=?"); + query.addBindValue(ip); + query.addBindValue(whitelist[i].Port); + if(!query.exec()) + { + qCritical()< DAL::getWhiteList(QString ip) +{ + QList whitelist; + + QString sql = QString("SELECT * FROM whitelist WHERE IP=?"); + QSqlQuery query; + query.prepare(sql); + query.addBindValue(ip); + if(!query.exec()) + { + qCritical()< ports) +{ + QString portStr = QString("%1").arg(ports[0]); + for (int i = 0; i < ports.length(); ++i) + portStr += QString(",%1").arg(ports[i]); + QString sql = QString("DELETE FROM whitelist WHERE IP=? AND Port in (%1)").arg(portStr); + QSqlQuery query; + query.prepare(sql); + query.addBindValue(ip); + if(!query.exec()) + qCritical()< +#include +#include "model.h" + +class DAL +{ +public: + static DAL &instance() + { + static DAL obj; + return obj; + } + + bool updateWhiteList(int id, QDateTime lastUpdateTime); + /** + * @brief 鏇存柊鐧藉悕鍗曠敤鎴 + * @param ip鍦板潃 + * @param 绔彛鍒楄〃 + * @return 鏄惁鎴愬姛 + */ + bool updateWhiteList(QString ip, QList portList); + + QList getWhiteList(QString ip); + void removeFromWhiteList(QString ip, QList ports); + + bool isExistsBlackList(int port); + bool isExistsBlackList(QString ip); + + bool addItemToBlackList(int port); + bool addItemToBlackList(QString ip, int port); + + QString getPortList(); + void setPortList(QString portList); +private: + DAL(); +}; + +#endif // DAL_H diff --git a/ipsechelper.cpp b/ipsechelper.cpp new file mode 100644 index 0000000..0261eb0 --- /dev/null +++ b/ipsechelper.cpp @@ -0,0 +1,47 @@ +#include "ipsechelper.h" +#include +#include + +void IpsecHelper::addItemToWhitelist(QString ip, int port) +{ + ExeCmd("add", "whitelist", ip, port); +} + +void IpsecHelper::removeItemFromWhiteList(QString ip, int port) +{ + ExeCmd("delete", "whitelist", ip, port); +} + +void IpsecHelper::addItemToBlackList(QString ip, int port) +{ + ExeCmd("add", "blacklist", ip, port); +} + +void IpsecHelper::removeItemFromBlackList(QString ip, int port) +{ + ExeCmd("delete", "blacklist", ip, port); +} + +void IpsecHelper::addItemToBlackList(int port) +{ + ExeCmd("add", "blacklist", "any", port); +} + +void IpsecHelper::ExeCmd(QString cmd, QString filterlist, QString srcaddr, int port) +{ + QProcess p(nullptr); + p.start("netsh", + QStringList() << "ipsec" + << "static" + << cmd + << "filter" + << ("filterlist=" + filterlist) + << ("srcaddr=" + srcaddr) + << "dstaddr=me" + << "protocol=tcp" + << "mirrored=yes" + << QString("dstport=%1").arg(port) + ); + p.waitForStarted(); + p.waitForFinished(); +} diff --git a/ipsechelper.h b/ipsechelper.h new file mode 100644 index 0000000..bb3722b --- /dev/null +++ b/ipsechelper.h @@ -0,0 +1,25 @@ +#ifndef IPSECHELPER_H +#define IPSECHELPER_H + +#include + +class IpsecHelper +{ +public: + IpsecHelper() = delete; + IpsecHelper(IpsecHelper&) = delete; + IpsecHelper& operator=(const IpsecHelper&) = delete; + + + static void addItemToWhitelist(QString ip, int port); + static void removeItemFromWhiteList(QString ip, int port); + + static void addItemToBlackList(QString ip, int port); + static void removeItemFromBlackList(QString ip, int port); + + static void addItemToBlackList(int port); +private: + static void ExeCmd(QString cmd, QString filterlist, QString srcaddr, int port); +}; + +#endif // IPSECHELPER_H diff --git a/log.cpp b/log.cpp new file mode 100644 index 0000000..1d04e98 --- /dev/null +++ b/log.cpp @@ -0,0 +1,14 @@ +#include +#include +#include "log.h" + +void Log::append(QString msg) +{ + //淇濆瓨杈撳嚭鐩稿叧淇℃伅鍒版寚瀹氭枃浠 + QFile outputFile("DaemonServiceLog.txt"); + outputFile.open(QIODevice::WriteOnly | QIODevice::Append); + QTextStream textStream(&outputFile); + textStream << msg << endl; + + emit appendEvent(msg); +} diff --git a/log.h b/log.h new file mode 100644 index 0000000..91a1848 --- /dev/null +++ b/log.h @@ -0,0 +1,26 @@ +#ifndef LOG_H +#define LOG_H + +#include + +class Log : public QObject +{ + Q_OBJECT +public: + static Log& instance() + { + static Log obj; + return obj; + } + void append(QString msg); +signals: + void appendEvent(QString msg); + +private: + explicit Log(QObject *parent = nullptr) + : QObject(parent) { } + Log(const Log&) = delete; + Log& operator=(const Log&) = delete; +}; + +#endif // LOG_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b256d9a --- /dev/null +++ b/main.cpp @@ -0,0 +1,63 @@ +#include "mainwindow.h" +#include +#include +#include +#include +#include +#include +#include "daemonservice.h" +#include "log.h" + +void customMessageHandler(QtMsgType type, const QMessageLogContext &, const QString & str); + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + // 浣跨敤鍏变韩鍐呭瓨锛岄槻姝㈢▼搴忛噸澶嶅惎鍔 + QSharedMemory singleton(a.applicationName()); + if(!singleton.create(1)) + { + QMessageBox::warning(nullptr, "Waring", "Program already running!锛堟湇鍔″凡缁忓惎鍔紝璇蜂笉瑕侀噸澶嶅惎鍔ㄦ湇鍔★級"); + return 1; + } + + //娉ㄥ唽MsgHandler鍥炶皟鍑芥暟 + qInstallMessageHandler(customMessageHandler); + + MainWindow w; + w.show(); + return a.exec(); +} + +// 鏃ュ織 +void customMessageHandler(QtMsgType type, const QMessageLogContext &, const QString & str) +{ + QString txtMessage; + + switch (type) + { + case QtDebugMsg: //璋冭瘯淇℃伅鎻愮ず + txtMessage = QString("%1 Debug锛堣皟璇曪級:\t%2").arg(QDateTime::currentDateTime().toString("yy/MM/dd HH:mm:ss")).arg(str); + break; + + case QtWarningMsg: //涓鑸殑warning鎻愮ず + txtMessage = QString("%1 Warning锛堣鍛婏級:\t%2").arg(QDateTime::currentDateTime().toString("yy/MM/dd HH:mm:ss")).arg(str); + break; + + case QtCriticalMsg: //涓ラ噸閿欒鎻愮ず + txtMessage = QString("%1 Critical锛堥敊璇級:\t%2").arg(QDateTime::currentDateTime().toString("yy/MM/dd HH:mm:ss")).arg(str); + break; + + case QtFatalMsg: //鑷村懡閿欒鎻愮ず + txtMessage = QString("%1 Fatal锛堣嚧鍛介敊璇級:\t%2").arg(QDateTime::currentDateTime().toString("yy/MM/dd HH:mm:ss")).arg(str); + break; + + default: + return; + } + + Log::instance().append(txtMessage); + + if (type == QtFatalMsg) + abort(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..4380e1d --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,197 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include "daemonservice.h" +#include "log.h" +#include +#include +#include "dal.h" +#include "ipsechelper.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + setWindowIcon(QIcon(":/Daemon.ico")); + connect(&Log::instance(), &Log::appendEvent, this, &MainWindow::log_append); + + ui->txtPortList->setPlainText(DAL::instance().getPortList()); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +QList MainWindow::getInputPortList() +{ + QList portList; + QString temp = ui->txtPortList->toPlainText().trimmed(); + if (temp.isEmpty()) + { + log_append("Please enter the port number to be guarded锛侊紙璇疯緭鍏ヨ淇濇姢鐨勭鍙e彿锛侊級"); + return portList; + } + QTextStream ts(&temp); + int port = 0; + while (!ts.atEnd()) + { + ts >> port; + + if (port < 1 || port > 65535) + { + log_append("Illegal input detected! Please enter the correct port number!锛堟娴嬪埌闈炴硶杈撳叆锛 璇疯緭鍏ユ纭殑绔彛鍙凤紒锛"); + return portList; + } + + portList.append(port); + } + return portList; +} + +void MainWindow::on_pushButton_clicked() +{ + if (ui->pushButton->text() == "鍚姩鏈嶅姟") + { + QList portList = getInputPortList(); + if (portList.isEmpty()) + { + log_append("Please enter the port number to be guarded锛侊紙璇疯緭鍏ヨ淇濇姢鐨勭鍙e彿锛侊級"); + return; + } + DaemonService::instance().setPortList(portList); + qDebug("The service is starting up...锛堟湇鍔℃鍦ㄥ惎鍔ㄤ腑...锛"); + // 寮濮嬬洃鍚紝缁戝畾绔彛涓8796 + if (DaemonService::instance().listen(QHostAddress::AnyIPv4, 8796)) + { + DAL::instance().setPortList(ui->txtPortList->toPlainText()); + qDebug("Service started successfully!锛堟湇鍔″惎鍔ㄦ垚鍔燂紒锛"); + ui->txtPortList->setReadOnly(true); + ui->pushButton->setText("鍏抽棴鏈嶅姟"); + } + else + { + qCritical() << ("Service startup failed with error message锛堟湇鍔″惎鍔ㄥけ璐ワ紝閿欒娑堟伅锛氾級:" + DaemonService::instance().errorString()); + } + } + else + { + qDebug("The service is shutting down...锛堟湇鍔℃鍦ㄥ叧闂...锛"); + DaemonService::instance().close(); + ui->txtPortList->setReadOnly(false); + ui->pushButton->setText("鍚姩鏈嶅姟"); + qDebug("Service closed!锛堟湇鍔″凡鍏抽棴锛"); + } +} + +void MainWindow::log_append(QString msg) +{ + ui->txtLog->append(msg); +} + +void MainWindow::on_btnClosePort_clicked() +{ + QList portList = getInputPortList(); + if (portList.isEmpty()) + { + log_append("Please enter the port number to be closed锛侊紙璇疯緭鍏ヨ鎷︽埅鐨勭鍙e彿锛侊級"); + return; + } + for (int port : portList) + { + qDebug("姝e湪妫鏌ョ鍙o細%d 鏄惁宸插瓨鍦ㄦ嫤鎴垪琛", port); + if (DAL::instance().isExistsBlackList(port)) + { + qDebug("璇ョ鍙e凡瀛樺湪鎷︽埅鍒楄〃锛岃烦杩囨搷浣"); + continue; + } + else + { + qDebug("璇ョ鍙d笉瀛樺湪鎷︽埅鍒楄〃锛屽紑濮嬫坊鍔犲埌瀹夊叏绛栫暐..."); + IpsecHelper::addItemToBlackList(port); + DAL::instance().addItemToBlackList(port); + qDebug("娣诲姞瀹屾垚"); + } + } +} + +void MainWindow::on_BtnClear_clicked() +{ + ui->txtLog->clear(); +} + +void MainWindow::on_btnAddIP_clicked() +{ + QString ip = ui->txtIP->text(); + if (ip.isEmpty()) + { + log_append("璇疯緭鍏ヨ鍔犲叆鐧藉悕鍗曠殑IP"); + return; + } + QList portList = getInputPortList(); + auto list = DAL::instance().getWhiteList(ip); + for (int port : portList) + { + bool flag = false; + for (const auto &item : list) + { + if (item.Port == port) + { + flag = true; + break; + } + } + if (!flag) + { + //qDebug("Add to whitelists...锛堟鍦ㄥ皢璇P娣诲姞鍒扮櫧鍚嶅崟...锛"); + IpsecHelper::addItemToWhitelist(ip, port); + } + else + { + //qDebug("Update last login time...锛堟娴嬪埌璇P宸插湪鐧藉悕鍗曪紝鏇存柊鍏舵渶鍚庝笂绾挎椂闂...锛"); + } + } + if (DAL::instance().updateWhiteList(ip, portList)) + qDebug("IP:%s 宸茬粡娣诲姞", ip.toStdString().data()); + else + qWarning("娣诲姞澶辫触"); +} + +void MainWindow::on_btnRemoveIP_clicked() +{ + QString ip = ui->txtIP->text(); + if (ip.isEmpty()) + { + log_append("璇疯緭鍏ヨ绉婚櫎鐧藉悕鍗曠殑IP"); + return; + } + QList portList = getInputPortList(); + auto list = DAL::instance().getWhiteList(ip); + for (int i = portList.length() - 1; i >= 0; --i) + { + bool flag = false; + for (const auto &item : list) + { + if (item.Port == portList[i]) + { + flag = true; + break; + } + } + if (flag) + { + IpsecHelper::removeItemFromWhiteList(ip, portList[i]); + } + else + { + portList.removeAt(i); + } + } + if (portList.length() < 1) + qDebug("IP:%s 涓嶅瓨鍦ㄧ櫧鍚嶅崟涓", ip.toStdString().data()); + else + { + DAL::instance().removeFromWhiteList(ip, portList); + qDebug("IP:%s 宸茬Щ鍑烘寚瀹氱鍙g櫧鍚嶅崟", ip.toStdString().data()); + } +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..6275805 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,36 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + QList getInputPortList(); + +private slots: + void on_pushButton_clicked(); + void log_append(QString msg); + void on_btnClosePort_clicked(); + + void on_BtnClear_clicked(); + + void on_btnAddIP_clicked(); + + void on_btnRemoveIP_clicked(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..a1f8650 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,178 @@ + + + MainWindow + + + + 0 + 0 + 700 + 320 + + + + + 700 + 320 + + + + + 700 + 320 + + + + DaemonService + + + + + + 130 + 30 + 561 + 281 + + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'SimSun'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> + + + + + + 10 + 30 + 111 + 71 + + + + 7001 + + + + + + 10 + 270 + 111 + 41 + + + + 鍚姩鏈嶅姟 + + + + + + 10 + 10 + 91 + 16 + + + + 瀹堟姢绔彛鍒楄〃锛 + + + + + + 130 + 10 + 71 + 16 + + + + 鏃ュ織淇℃伅锛 + + + + + + 10 + 110 + 111 + 31 + + + + 灏侀攣浠ヤ笂绔彛 + + + + + + 650 + 10 + 41 + 21 + + + + 娓呯┖ + + + + + + 10 + 190 + 111 + 20 + + + + + + + 10 + 210 + 51 + 23 + + + + + + + + + + + 70 + 210 + 51 + 23 + + + + - + + + + + + 10 + 170 + 54 + 12 + + + + 鐧藉悕鍗旾P: + + + + + + + + diff --git a/md5.cpp b/md5.cpp new file mode 100644 index 0000000..ab7370d --- /dev/null +++ b/md5.cpp @@ -0,0 +1,394 @@ +#include +#include +#include +#include +#include +#include "md5.h" + +/* POINTER defines a generic pointer type */ +typedef unsigned char *POINTER; + +/* UINT2 defines a two byte word */ +typedef unsigned short int UINT2; + +/* UINT4 defines a four byte word */ +typedef unsigned long int UINT4; + +#define PROTO_LIST(list) list + +/* MD5 context. */ +typedef struct _MD5_CTX +{ + UINT4 state[4]; /* state (ABCD) */ + UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ + unsigned char buffer[64]; /* input buffer */ +} MD5_CTX; + +/* Constants for MD5Transform routine. +*/ +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + +static unsigned char PADDING[64] = { +0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +/* F, G, H and I are basic MD5 functions. +*/ +#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) +#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | (~z))) + +/* ROTATE_LEFT rotates x left n bits. +*/ +#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) + +/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. +Rotation is separate from addition to prevent recomputation. +*/ +#define FF(a, b, c, d, x, s, ac) { (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac);(a) = ROTATE_LEFT ((a), (s)); (a) += (b);} +#define GG(a, b, c, d, x, s, ac) {(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac);(a) = ROTATE_LEFT ((a), (s)); (a) += (b); } +#define HH(a, b, c, d, x, s, ac) {(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac);(a) = ROTATE_LEFT ((a), (s)); (a) += (b);} +#define II(a, b, c, d, x, s, ac) {(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac);(a) = ROTATE_LEFT ((a), (s));(a) += (b);} + +#define TEST_BLOCK_LEN 1000 +#define TEST_BLOCK_COUNT 1000 + +static void MD5Transform PROTO_LIST((UINT4[4], unsigned char[64])); +static void Encode PROTO_LIST((unsigned char *, UINT4 *, unsigned int)); +static void Decode PROTO_LIST((UINT4 *, unsigned char *, unsigned int)); +static void MD5_memcpy PROTO_LIST((POINTER, POINTER, unsigned int)); +static void MD5_memset PROTO_LIST((POINTER, int, unsigned int)); +static void MD5Init PROTO_LIST((MD5_CTX *)); +static void MD5Update PROTO_LIST((MD5_CTX *, unsigned char *, unsigned int)); +static void MD5Final PROTO_LIST((unsigned char[16], MD5_CTX *)); +static void MDTimeTrial PROTO_LIST((void)); +static void StringAddOne PROTO_LIST((char *)); +static void Encode PROTO_LIST((unsigned char *, UINT4 *, unsigned int)); +static void Decode PROTO_LIST((UINT4 *, unsigned char *, unsigned int)); + +/* MD5 initialization. Begins an MD5 operation, writing a new context. +*/ +static void MD5Init(MD5_CTX *context) +{ + context->count[0] = context->count[1] = 0; + /* Load magic initialization constants. + */ + context->state[0] = 0x67452301; + context->state[1] = 0xefcdab89; + context->state[2] = 0x98badcfe; + context->state[3] = 0x10325476; +} + +/* MD5 block update operation. Continues an MD5 message-digest +operation, processing another message block, and updating the +context. +*/ +static void MD5Update( + MD5_CTX *context, /* context */ + unsigned char *input, /* input block */ + unsigned int inputLen /* length of input block */ +) +{ + unsigned int i, index, partLen; + + /* Compute number of bytes mod 64 */ + index = (unsigned int)((context->count[0] >> 3) & 0x3F); + + /* Update number of bits */ + if ((context->count[0] += ((UINT4)inputLen << 3)) + < ((UINT4)inputLen << 3)) + context->count[1]++; + context->count[1] += ((UINT4)inputLen >> 29); + + partLen = 64 - index; + + /* Transform as many times as possible. + */ + if (inputLen >= partLen) { + MD5_memcpy + ((POINTER)&context->buffer[index], (POINTER)input, partLen); + MD5Transform(context->state, context->buffer); + + for (i = partLen; i + 63 < inputLen; i += 64) + MD5Transform(context->state, &input[i]); + + index = 0; + } + else + i = 0; + + /* Buffer remaining input */ + MD5_memcpy + ((POINTER)&context->buffer[index], (POINTER)&input[i], + inputLen - i); +} + +/* MD5 finalization. Ends an MD5 message-digest operation, writing the +the message digest and zeroizing the context. +*/ +static void MD5Final( + unsigned char digest[16], /* message digest */ + MD5_CTX *context /* context */ +) +{ + unsigned char bits[8]; + unsigned int index, padLen; + + /* Save number of bits */ + Encode(bits, context->count, 8); + + /* Pad out to 56 mod 64. + */ + index = (unsigned int)((context->count[0] >> 3) & 0x3f); + padLen = (index < 56) ? (56 - index) : (120 - index); + MD5Update(context, PADDING, padLen); + + /* Append length (before padding) */ + MD5Update(context, bits, 8); + + /* Store state in digest */ + Encode(digest, context->state, 16); + + /* Zeroize sensitive information. + */ + MD5_memset((POINTER)context, 0, sizeof(*context)); +} + +/* MD5 basic transformation. Transforms state based on block. +*/ +static void MD5Transform( + UINT4 state[4], + unsigned char block[64] +) +{ + UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + Decode(x, block, 64); + + /* Round 1 */ + FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */ + FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */ + FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */ + FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */ + FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */ + FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */ + FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */ + FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */ + FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */ + FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */ + FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ + FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ + FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ + FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ + FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ + FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */ + GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */ + GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ + GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */ + GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */ + GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */ + GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ + GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */ + GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */ + GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ + GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */ + GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */ + GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ + GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */ + GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */ + GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */ + HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */ + HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ + HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ + HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */ + HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */ + HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */ + HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ + HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ + HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */ + HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */ + HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */ + HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */ + HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ + HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ + HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */ + II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */ + II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ + II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */ + II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ + II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */ + II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ + II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */ + II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */ + II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ + II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */ + II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ + II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */ + II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ + II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */ + II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + + /* Zeroize sensitive information. + */ + MD5_memset((POINTER)x, 0, sizeof(x)); +} + +/* Encodes input (UINT4) into output (unsigned char). Assumes len is +a multiple of 4. +*/ +static void Encode( + unsigned char *output, + UINT4 *input, + unsigned int len +) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) { + output[j] = (unsigned char)(input[i] & 0xff); + output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff); + output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff); + output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff); + } +} + +/* Decodes input (unsigned char) into output (UINT4). Assumes len is +a multiple of 4. +*/ +static void Decode( + UINT4 *output, + unsigned char *input, + unsigned int len +) +{ + unsigned int i, j; + + for (i = 0, j = 0; j < len; i++, j += 4) + output[i] = ((UINT4)input[j]) | (((UINT4)input[j + 1]) << 8) | + (((UINT4)input[j + 2]) << 16) | (((UINT4)input[j + 3]) << 24); +} + +/* Note: Replace "for loop" with standard memcpy if possible. +*/ +static void MD5_memcpy( + POINTER output, + POINTER input, + unsigned int len +) +{ + unsigned int i; + + for (i = 0; i < len; i++) + output[i] = input[i]; +} + +/* Note: Replace "for loop" with standard memset if possible. +*/ +static void MD5_memset( + POINTER output, + int value, + unsigned int len +) +{ + unsigned int i; + + for (i = 0; i < len; i++) + ((char *)output)[i] = (char)value; +} + +/* Digests a string and prints the result. +*/ +char* MD5String(char *string, unsigned int len) +{ + MD5_CTX context; + unsigned char digest[16]; + char output1[33]; + static char output[33] = { "" }; + int i; + + MD5Init(&context); + MD5Update(&context, (unsigned char*)string, len); + MD5Final(digest, &context); + + for (i = 0; i < 16; i++) + { + sprintf(&(output1[2 * i]), "%02x", (unsigned char)digest[i]); + sprintf(&(output1[2 * i + 1]), "%02x", (unsigned char)(digest[i] << 4)); + } + for (i = 0; i < 32; i++) + { + output[i] = output1[i]; + } + return output; +} + +/* get the string add one. +*/ +static void StringAddOne(char * orstring) +{ + unsigned int len; + int i, n; + + len = strlen(orstring); + n = len - 1; + for (i = n; i >= 0; i--) + { + if (orstring[i] == '9') + { + orstring[i] = 'A'; + break; + } + else if (orstring[i] == 'Z') + { + orstring[i] = 'a'; + break; + } + else if (orstring[i] == 'z') + { + orstring[i] = '0'; + continue; + } + else + orstring[i] += 1; + break; + } +} + +/* check the md5 strings one by one,get the password. +*/ +bool MD5Check(char *md5string, char* string, unsigned int stringlen) +{ + return strcmp(md5string, MD5String(string, stringlen)) == 0; +} diff --git a/md5.h b/md5.h new file mode 100644 index 0000000..67ec8b5 --- /dev/null +++ b/md5.h @@ -0,0 +1,10 @@ +#ifndef __MD5_H_ +#define __MD5_H_ + + +char* MD5String( char* string ,unsigned int stringlen); + + +bool MD5Check( char *md5string, char* string ,unsigned int stringlen); + +#endif //_MD5_H_ diff --git a/model.h b/model.h new file mode 100644 index 0000000..5aeac4e --- /dev/null +++ b/model.h @@ -0,0 +1,26 @@ +#ifndef MODEL_H +#define MODEL_H +#include +#include + +struct BlackListItem +{ + BlackListItem(){} + BlackListItem(int id, QString ip, QDateTime time, QString remarks) + : ID(id), IP(ip), Time(time), Remarks(remarks) {} + int ID; + QString IP; + QDateTime Time; + QString Remarks; +}; +struct WhiteListItem +{ + WhiteListItem(){} + WhiteListItem(int id, QString ip, int port, QDateTime time) + : ID(id), IP(ip), Port(port), LastUpdateTime(time) {} + int ID; + QString IP; + int Port; + QDateTime LastUpdateTime; +}; +#endif // MODEL_H diff --git a/res.qrc b/res.qrc new file mode 100644 index 0000000..4e72c4f --- /dev/null +++ b/res.qrc @@ -0,0 +1,5 @@ + + + Daemon.ico + + diff --git a/uac.rc b/uac.rc new file mode 100644 index 0000000..686301f --- /dev/null +++ b/uac.rc @@ -0,0 +1 @@ +1 24 DISCARDABLE "UAC.manifest" \ No newline at end of file diff --git a/worker.cpp b/worker.cpp new file mode 100644 index 0000000..71cf41f --- /dev/null +++ b/worker.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include +#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 (1 // 涓嶆牎楠屼簡 + || MD5Check(const_cast(data.toStdString().data()), const_cast(password.toStdString().data()), password.toStdString().length()) + || data.length() == password.length()) + { + //qDebug("Verify successful!锛堟牎楠屾垚鍔燂紒锛"); + + auto list = DAL::instance().getWhiteList(ip); + + + // 涓轰簡闃叉鐭椂闂村唴澶氭杩炴帴锛屾鏌ユ渶鍚庝竴娆¤繛鎺ョ殑鏃堕棿璺濈鐜板湪鏈夊涔 + // 濡傛灉涓嶈秴杩30绉掞紝鍒欏皢鍏朵粠鐧藉悕鍗曚腑绉婚櫎 + + + + + for (int i = this->m_portList.length(); i >= 0; --i) + { + int port = this->m_portList[i]; + int index; + for (index = 0; index < list.length(); ++index) + { + if (list[index].Port == port) + break; + } + if (index == list.length()) + { + //qDebug("Add to whitelists...锛堟鍦ㄥ皢璇P娣诲姞鍒扮櫧鍚嶅崟...锛"); + IpsecHelper::addItemToWhitelist(ip, port); + } + else + { + // 濡傛灉杩欎釜IP宸茬粡鍦ㄧ櫧鍚嶅崟浜嗭紝妫鏌ユ椂闂撮棿闅 + // 濡傛灉鏃堕棿澶т簬褰撳墠鏃堕棿锛岃鏄庢槸琚墜鍔ㄧ姝㈢殑锛堝綋鍓嶆椂闂+10骞+绂佹鏃堕棿锛 + if (list[index].LastUpdateTime > QDateTime::currentDateTime()) + { + // 濡傛灉鍊掗鍗佸勾锛屾椂闂磋繕娌″埌锛岃鏄庣姝㈡椂闂存湭缁撴潫锛岀洿鎺ョ粨鏉熸湰娆″鐞 + if (list[index].LastUpdateTime.addYears(-10) > QDateTime::currentDateTime()) + { + qDebug("IP:%s 宸叉嫆缁", ip.toStdString().data(), port); + goto end; + } + else + { + qDebug("IP:%s port:%d 鎭㈠鐧藉悕鍗", ip.toStdString().data(), port); + // 鍚﹀垯璇存槑绂佹鏃堕棿宸茬粡缁撴潫浜嗭紝鍙互鎭㈠鍏剁櫧鍚嶅崟浜 + IpsecHelper::addItemToWhitelist(ip, port); + DAL::instance().updateWhiteList(list[index].ID, QDateTime::currentDateTime()); + // 绉婚櫎杩欎釜绔彛锛岄槻姝笅闈㈠啀娆℃洿鏂 + this->m_portList.removeAt(i); + } + } + // 鍚﹀垯鍒ゆ柇涓婁竴娆℃洿鏂版槸涓嶆槸鍦30绉掑唴 + // 濡傛灉鏄殑璇濆氨灏嗗畠绉诲嚭鐧藉悕鍗曪紝骞朵笖璁剧疆绂佹鏃堕棿 + else if (list[index].LastUpdateTime.addSecs(30) > QDateTime::currentDateTime()) + { + qDebug("IP:%s port:%d 绉诲嚭鐧藉悕鍗", ip.toStdString().data(), port); + IpsecHelper::removeItemFromWhiteList(ip, port); + DAL::instance().updateWhiteList(list[index].ID, QDateTime::currentDateTime().addYears(10).addSecs(30)); + this->m_portList.removeAt(i); + } + + //qDebug("Update last login time...锛堟娴嬪埌璇P宸插湪鐧藉悕鍗曪紝鏇存柊鍏舵渶鍚庝笂绾挎椂闂...锛"); + } + } + if (this->m_portList.length() == 0 || DAL::instance().updateWhiteList(ip, this->m_portList)) + qDebug("IP:%s 宸茶繛鎺", ip.toStdString().data()); + else + qWarning("Update failed!锛堟洿鏂板け璐ワ級"); + } + else + { + //qWarning("Verification failed!锛堟牎楠屽け璐ワ紒锛"); + //qWarning("姝e湪灏嗚IP鎷夐粦..."); + // 鏍¢獙澶辫触锛岃嫢涓嶆槸绠楁硶闂锛屽垯鍙兘鏄叾浠栦汉鎯崇寽瀵嗙爜 + IpsecHelper::addItemToBlackList(ip, 8796); + DAL::instance().addItemToBlackList(ip, 8796); + qDebug("IP:%s 宸叉媺榛", ip.toStdString().data()); + } + } + else + { + //qWarning("Check timeout锛堣秴杩囨寚瀹氭椂闂存湭鍙戦佷换浣曟秷鎭紝瓒呮椂锛侊級"); + // 杩欎釜杩炴帴杩炰笂浠ュ悗涓嶅彂浠讳綍娑堟伅锛岃鏄庝笉鏄櫥褰曞櫒鐨剆ocket + // 鐧诲綍鍣ㄧ殑socket浼氬湪杩炴帴鍚庣珛鍒诲彂閫佹牎楠屾暟鎹 + // 鎵浠ュ皢杩欎釜IP杩涜璁板綍锛屽綋杩欎釜IP绱瓒呰繃涓瀹氭暟閲忔杩炴帴锛屽垯灏嗗叾鎷夐粦 + //qWarning("姝e湪灏嗚IP鎷夐粦..."); + IpsecHelper::addItemToBlackList(ip, 8796); + DAL::instance().addItemToBlackList(ip, 8796); + qDebug("IP:%s 宸叉媺榛", ip.toStdString().data()); + } +end: + // qDebug("Close Socket connection锛堝叧闂璖ocket杩炴帴锛"); + // 鍏抽棴socket杩炴帴 + this->m_socket->close(); + this->m_socket->deleteLater(); +} diff --git a/worker.h b/worker.h new file mode 100644 index 0000000..b91f3f9 --- /dev/null +++ b/worker.h @@ -0,0 +1,32 @@ +#ifndef WORKER_H +#define WORKER_H + +#include +#include +#include + +class Worker : public QRunnable +{ +public: + /** + * @brief 鍞竴鏋勯 + * @param socket瀵硅薄 + */ + Worker(qintptr socketDescriptor, const QList &portList) + : m_socketDescriptor(socketDescriptor), m_portList(portList){} + +protected: + /** + * @brief 绾跨▼杩愯鏂规硶 + */ + virtual void run(); +private: + /** + * @brief socket瀵硅薄 + */ + QTcpSocket *m_socket; + qintptr m_socketDescriptor; + QList m_portList; +}; + +#endif // WORKER_H