From 386d82b100b1564bfd0bea8e6a62760c879722f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E5=82=91?= <840465812@qq.com> Date: Sun, 30 Dec 2018 16:26:59 +0800 Subject: [PATCH] qt --- DaemonService/DaemonService.pro | 53 ++++ DaemonService/DaemonService.pro.user | 322 +++++++++++++++++++++ DaemonService/DaemonServiceInitializer.bat | 21 ++ DaemonService/UAC.manifest | 10 + DaemonService/daemonservice.cpp | 25 ++ DaemonService/daemonservice.h | 45 +++ DaemonService/dal.cpp | 160 ++++++++++ DaemonService/dal.h | 31 ++ DaemonService/log.cpp | 14 + DaemonService/log.h | 26 ++ DaemonService/main.cpp | 63 ++++ DaemonService/mainwindow.cpp | 67 +++++ DaemonService/mainwindow.h | 25 ++ DaemonService/mainwindow.ui | 105 +++++++ DaemonService/md5.cpp | 273 +++++++++++++++++ DaemonService/md5.h | 136 +++++++++ DaemonService/model.h | 26 ++ DaemonService/uac.rc | 1 + DaemonService/worker.cpp | 102 +++++++ DaemonService/worker.h | 32 ++ 20 files changed, 1537 insertions(+) create mode 100644 DaemonService/DaemonService.pro create mode 100644 DaemonService/DaemonService.pro.user create mode 100644 DaemonService/DaemonServiceInitializer.bat create mode 100644 DaemonService/UAC.manifest create mode 100644 DaemonService/daemonservice.cpp create mode 100644 DaemonService/daemonservice.h create mode 100644 DaemonService/dal.cpp create mode 100644 DaemonService/dal.h create mode 100644 DaemonService/log.cpp create mode 100644 DaemonService/log.h create mode 100644 DaemonService/main.cpp create mode 100644 DaemonService/mainwindow.cpp create mode 100644 DaemonService/mainwindow.h create mode 100644 DaemonService/mainwindow.ui create mode 100644 DaemonService/md5.cpp create mode 100644 DaemonService/md5.h create mode 100644 DaemonService/model.h create mode 100644 DaemonService/uac.rc create mode 100644 DaemonService/worker.cpp create mode 100644 DaemonService/worker.h diff --git a/DaemonService/DaemonService.pro b/DaemonService/DaemonService.pro new file mode 100644 index 0000000..6bef36e --- /dev/null +++ b/DaemonService/DaemonService.pro @@ -0,0 +1,53 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2018-12-27T16:26:24 +# +#------------------------------------------------- + +QT += core gui network sql + +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 + +HEADERS += \ + mainwindow.h \ + daemonservice.h \ + worker.h \ + md5.h \ + dal.h \ + model.h \ + log.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 diff --git a/DaemonService/DaemonService.pro.user b/DaemonService/DaemonService.pro.user new file mode 100644 index 0000000..0451085 --- /dev/null +++ b/DaemonService/DaemonService.pro.user @@ -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/DaemonService/DaemonServiceInitializer.bat b/DaemonService/DaemonServiceInitializer.bat new file mode 100644 index 0000000..6c8a73e --- /dev/null +++ b/DaemonService/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/DaemonService/UAC.manifest b/DaemonService/UAC.manifest new file mode 100644 index 0000000..b9b1f7f --- /dev/null +++ b/DaemonService/UAC.manifest @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/DaemonService/daemonservice.cpp b/DaemonService/daemonservice.cpp new file mode 100644 index 0000000..cb90711 --- /dev/null +++ b/DaemonService/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/daemonservice.h b/DaemonService/daemonservice.h new file mode 100644 index 0000000..976661c --- /dev/null +++ b/DaemonService/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/DaemonService/dal.cpp b/DaemonService/dal.cpp new file mode 100644 index 0000000..f6e0202 --- /dev/null +++ b/DaemonService/dal.cpp @@ -0,0 +1,160 @@ +#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()< +#include +#include "model.h" + +class DAL +{ +public: + static DAL &instance() + { + static DAL obj; + return obj; + } + + /** + * @brief 鏇存柊鐧藉悕鍗曠敤鎴 + * @param ip鍦板潃 + * @param 绔彛鍒楄〃 + * @return 鏄惁鎴愬姛 + */ + bool updateWhiteList(QString ip, QList portList); + + QList getWhiteList(QString ip); + +private: + DAL(); +}; + +#endif // DAL_H diff --git a/DaemonService/log.cpp b/DaemonService/log.cpp new file mode 100644 index 0000000..1d04e98 --- /dev/null +++ b/DaemonService/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/DaemonService/log.h b/DaemonService/log.h new file mode 100644 index 0000000..91a1848 --- /dev/null +++ b/DaemonService/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/DaemonService/main.cpp b/DaemonService/main.cpp new file mode 100644 index 0000000..2c14351 --- /dev/null +++ b/DaemonService/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.zzz")).arg(str); + break; + + case QtWarningMsg: //涓鑸殑warning鎻愮ず + txtMessage = QString("%1 Warning:\t%2").arg(QDateTime::currentDateTime().toString("yy/MM/dd HH:mm:ss.zzz")).arg(str); + break; + + case QtCriticalMsg: //涓ラ噸閿欒鎻愮ず + txtMessage = QString("%1 Critical:\t%2").arg(QDateTime::currentDateTime().toString("yy/MM/dd HH:mm:ss.zzz")).arg(str); + break; + + case QtFatalMsg: //鑷村懡閿欒鎻愮ず + txtMessage = QString("%1 Fatal:\t%2").arg(QDateTime::currentDateTime().toString("yy/MM/dd HH:mm:ss.zzz")).arg(str); + break; + + default: + return; + } + + Log::instance().append(txtMessage); + + if (type == QtFatalMsg) + abort(); +} diff --git a/DaemonService/mainwindow.cpp b/DaemonService/mainwindow.cpp new file mode 100644 index 0000000..fbbc2cc --- /dev/null +++ b/DaemonService/mainwindow.cpp @@ -0,0 +1,67 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include "daemonservice.h" +#include "log.h" +#include +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + connect(&Log::instance(), &Log::appendEvent, this, &MainWindow::log_append); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +void MainWindow::on_pushButton_clicked() +{ + QString temp = ui->txtPortList->toPlainText().trimmed(); + if (temp.isEmpty()) + { + log_append("Please enter the port number to be guarded锛"); + return; + } + QTextStream ts(&temp); + QList portList; + 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.append(port); + } + if (portList.isEmpty()) + { + log_append("Please enter the port number to be guarded锛"); + return; + } + DaemonService::instance().setPortList(portList); + qDebug("The service is starting up..."); + // 寮濮嬬洃鍚紝缁戝畾绔彛涓8796 + if (DaemonService::instance().listen(QHostAddress::AnyIPv4, 8796)) + { + qDebug("Service started successfully!"); + ui->txtPortList->setReadOnly(true); + ui->pushButton->setEnabled(false); + } + else + { + qCritical() << ("Service startup failed with error message:" + DaemonService::instance().errorString()); + } +} + +void MainWindow::log_append(QString msg) +{ + ui->txtLog->append(msg); +} diff --git a/DaemonService/mainwindow.h b/DaemonService/mainwindow.h new file mode 100644 index 0000000..f3b24c8 --- /dev/null +++ b/DaemonService/mainwindow.h @@ -0,0 +1,25 @@ +#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 slots: + void on_pushButton_clicked(); + void log_append(QString msg); +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/DaemonService/mainwindow.ui b/DaemonService/mainwindow.ui new file mode 100644 index 0000000..b04ad43 --- /dev/null +++ b/DaemonService/mainwindow.ui @@ -0,0 +1,105 @@ + + + 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 + + + + 9876 +9877 + + + + + + + 10 + 110 + 111 + 41 + + + + 鍚姩鏈嶅姟 + + + + + + 10 + 10 + 91 + 16 + + + + 瀹堟姢绔彛鍒楄〃锛 + + + + + + 130 + 10 + 71 + 16 + + + + 鏃ュ織淇℃伅锛 + + + + + + + + diff --git a/DaemonService/md5.cpp b/DaemonService/md5.cpp new file mode 100644 index 0000000..d9884de --- /dev/null +++ b/DaemonService/md5.cpp @@ -0,0 +1,273 @@ +/** + * @file md5.cpp + * @The implement of md5. + * @author Jiewei Wei + * @mail weijieweijerry@163.com + * @github https://github.com/JieweiWei + * @data Oct 19 2014 + * + */ + + +#include "md5.h" + +/* Define the static member of MD5. */ +const byte MD5::PADDING[64] = { 0x80 }; +const char MD5::HEX_NUMBERS[16] = { + '0', '1', '2', '3', + '4', '5', '6', '7', + '8', '9', 'a', 'b', + 'c', 'd', 'e', 'f' +}; + +/** + * @Construct a MD5 object with a string. + * + * @param {message} the message will be transformed. + * + */ +MD5::MD5(const string& message) { + finished = false; + /* Reset number of bits. */ + count[0] = count[1] = 0; + /* Initialization constants. */ + state[0] = 0x67452301; + state[1] = 0xefcdab89; + state[2] = 0x98badcfe; + state[3] = 0x10325476; + + /* Initialization the object according to message. */ + init(reinterpret_cast(message.c_str()), message.length()); +} + +/** + * @Generate md5 digest. + * + * @return the message-digest. + * + */ +const byte* MD5::getDigest() { + if (!finished) { + finished = true; + + byte bits[8]; + bit32 oldState[4]; + bit32 oldCount[2]; + bit32 index, padLen; + + /* Save current state and count. */ + memcpy(oldState, state, 16); + memcpy(oldCount, count, 8); + + /* Save number of bits */ + encode(count, bits, 8); + + /* Pad out to 56 mod 64. */ + index = static_cast((count[0] >> 3) & 0x3f); + padLen = (index < 56) ? (56 - index) : (120 - index); + init(PADDING, padLen); + + /* Append length (before padding) */ + init(bits, 8); + + /* Store state in digest */ + encode(state, digest, 16); + + /* Restore current state and count. */ + memcpy(state, oldState, 16); + memcpy(count, oldCount, 8); + } + return digest; +} + +/** + * @Initialization the md5 object, processing another message block, + * and updating the context. + * + * @param {input} the input message. + * + * @param {len} the number btye of message. + * + */ +void MD5::init(const byte* input, size_t len) { + + bit32 i, index, partLen; + + finished = false; + + /* Compute number of bytes mod 64 */ + index = static_cast((count[0] >> 3) & 0x3f); + + /* update number of bits */ + if ((count[0] += (static_cast(len) << 3)) < (static_cast(len) << 3)) { + ++count[1]; + } + count[1] += (static_cast(len) >> 29); + + partLen = 64 - index; + + /* transform as many times as possible. */ + if (len >= partLen) { + + memcpy(&buffer[index], input, partLen); + transform(buffer); + + for (i = partLen; i + 63 < len; i += 64) { + transform(&input[i]); + } + index = 0; + + } else { + i = 0; + } + + /* Buffer remaining input */ + memcpy(&buffer[index], &input[i], len - i); +} + +/** + * @MD5 basic transformation. Transforms state based on block. + * + * @param {block} the message block. + */ +void MD5::transform(const byte block[64]) { + + bit32 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; + + decode(block, x, 64); + + /* Round 1 */ + FF (a, b, c, d, x[ 0], s11, 0xd76aa478); + FF (d, a, b, c, x[ 1], s12, 0xe8c7b756); + FF (c, d, a, b, x[ 2], s13, 0x242070db); + FF (b, c, d, a, x[ 3], s14, 0xc1bdceee); + FF (a, b, c, d, x[ 4], s11, 0xf57c0faf); + FF (d, a, b, c, x[ 5], s12, 0x4787c62a); + FF (c, d, a, b, x[ 6], s13, 0xa8304613); + FF (b, c, d, a, x[ 7], s14, 0xfd469501); + FF (a, b, c, d, x[ 8], s11, 0x698098d8); + FF (d, a, b, c, x[ 9], s12, 0x8b44f7af); + FF (c, d, a, b, x[10], s13, 0xffff5bb1); + FF (b, c, d, a, x[11], s14, 0x895cd7be); + FF (a, b, c, d, x[12], s11, 0x6b901122); + FF (d, a, b, c, x[13], s12, 0xfd987193); + FF (c, d, a, b, x[14], s13, 0xa679438e); + FF (b, c, d, a, x[15], s14, 0x49b40821); + + /* Round 2 */ + GG (a, b, c, d, x[ 1], s21, 0xf61e2562); + GG (d, a, b, c, x[ 6], s22, 0xc040b340); + GG (c, d, a, b, x[11], s23, 0x265e5a51); + GG (b, c, d, a, x[ 0], s24, 0xe9b6c7aa); + GG (a, b, c, d, x[ 5], s21, 0xd62f105d); + GG (d, a, b, c, x[10], s22, 0x2441453); + GG (c, d, a, b, x[15], s23, 0xd8a1e681); + GG (b, c, d, a, x[ 4], s24, 0xe7d3fbc8); + GG (a, b, c, d, x[ 9], s21, 0x21e1cde6); + GG (d, a, b, c, x[14], s22, 0xc33707d6); + GG (c, d, a, b, x[ 3], s23, 0xf4d50d87); + GG (b, c, d, a, x[ 8], s24, 0x455a14ed); + GG (a, b, c, d, x[13], s21, 0xa9e3e905); + GG (d, a, b, c, x[ 2], s22, 0xfcefa3f8); + GG (c, d, a, b, x[ 7], s23, 0x676f02d9); + GG (b, c, d, a, x[12], s24, 0x8d2a4c8a); + + /* Round 3 */ + HH (a, b, c, d, x[ 5], s31, 0xfffa3942); + HH (d, a, b, c, x[ 8], s32, 0x8771f681); + HH (c, d, a, b, x[11], s33, 0x6d9d6122); + HH (b, c, d, a, x[14], s34, 0xfde5380c); + HH (a, b, c, d, x[ 1], s31, 0xa4beea44); + HH (d, a, b, c, x[ 4], s32, 0x4bdecfa9); + HH (c, d, a, b, x[ 7], s33, 0xf6bb4b60); + HH (b, c, d, a, x[10], s34, 0xbebfbc70); + HH (a, b, c, d, x[13], s31, 0x289b7ec6); + HH (d, a, b, c, x[ 0], s32, 0xeaa127fa); + HH (c, d, a, b, x[ 3], s33, 0xd4ef3085); + HH (b, c, d, a, x[ 6], s34, 0x4881d05); + HH (a, b, c, d, x[ 9], s31, 0xd9d4d039); + HH (d, a, b, c, x[12], s32, 0xe6db99e5); + HH (c, d, a, b, x[15], s33, 0x1fa27cf8); + HH (b, c, d, a, x[ 2], s34, 0xc4ac5665); + + /* Round 4 */ + II (a, b, c, d, x[ 0], s41, 0xf4292244); + II (d, a, b, c, x[ 7], s42, 0x432aff97); + II (c, d, a, b, x[14], s43, 0xab9423a7); + II (b, c, d, a, x[ 5], s44, 0xfc93a039); + II (a, b, c, d, x[12], s41, 0x655b59c3); + II (d, a, b, c, x[ 3], s42, 0x8f0ccc92); + II (c, d, a, b, x[10], s43, 0xffeff47d); + II (b, c, d, a, x[ 1], s44, 0x85845dd1); + II (a, b, c, d, x[ 8], s41, 0x6fa87e4f); + II (d, a, b, c, x[15], s42, 0xfe2ce6e0); + II (c, d, a, b, x[ 6], s43, 0xa3014314); + II (b, c, d, a, x[13], s44, 0x4e0811a1); + II (a, b, c, d, x[ 4], s41, 0xf7537e82); + II (d, a, b, c, x[11], s42, 0xbd3af235); + II (c, d, a, b, x[ 2], s43, 0x2ad7d2bb); + II (b, c, d, a, x[ 9], s44, 0xeb86d391); + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; +} + +/** +* @Encodes input (unsigned long) into output static_cast. +* +* @param {input} usigned long. +* +* @param {output} byte. +* +* @param {length} the length of input. +* +*/ +void MD5::encode(const bit32* input, byte* output, size_t length) { + + for (size_t i = 0, j = 0; j < length; ++i, j += 4) { + output[j]= static_cast(input[i] & 0xff); + output[j + 1] = static_cast((input[i] >> 8) & 0xff); + output[j + 2] = static_cast((input[i] >> 16) & 0xff); + output[j + 3] = static_cast((input[i] >> 24) & 0xff); + } +} + +/** + * @Decodes input static_cast into output (usigned long). + * + * @param {input} bytes. + * + * @param {output} unsigned long. + * + * @param {length} the length of input. + * + */ +void MD5::decode(const byte* input, bit32* output, size_t length) { + for (size_t i = 0, j = 0; j < length; ++i, j += 4) { + output[i] = (static_cast(input[j])) | ((static_cast(input[j + 1])) << 8) | + ((static_cast(input[j + 2])) << 16) | ((static_cast(input[j + 3])) << 24); + } +} + + +/** + * @Convert digest to string value. + * + * @return the hex string of digest. + * + */ +string MD5::toStr() { + const byte* digest_ = getDigest(); + string str; + str.reserve(16 << 1); + for (size_t i = 0; i < 16; ++i) { + int t = digest_[i]; + int a = t / 16; + int b = t % 16; + str.append(1, HEX_NUMBERS[a]); + str.append(1, HEX_NUMBERS[b]); + } + return str; +} diff --git a/DaemonService/md5.h b/DaemonService/md5.h new file mode 100644 index 0000000..958d839 --- /dev/null +++ b/DaemonService/md5.h @@ -0,0 +1,136 @@ +/** + * @file md5.h + * @The header file of md5. + * @author Jiewei Wei + * @mail weijieweijerry@163.com + * @github https://github.com/JieweiWei + * @data Oct 19 2014 + * + */ + + +#ifndef MD5_H +#define MD5_H + +/* Parameters of MD5. */ +#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 + +/** + * @Basic MD5 functions. + * + * @param there bit32. + * + * @return one bit32. + */ +#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. + * + * @param {num} the raw number. + * + * @param {n} rotate left n. + * + * @return the number after rotated left. + */ +#define ROTATELEFT(num, n) (((num) << (n)) | ((num) >> (32-(n)))) + +/** + * @Transformations for rounds 1, 2, 3, and 4. + */ +#define FF(a, b, c, d, x, s, ac) { \ + (a) += F ((b), (c), (d)) + (x) + ac; \ + (a) = ROTATELEFT ((a), (s)); \ + (a) += (b); \ +} +#define GG(a, b, c, d, x, s, ac) { \ + (a) += G ((b), (c), (d)) + (x) + ac; \ + (a) = ROTATELEFT ((a), (s)); \ + (a) += (b); \ +} +#define HH(a, b, c, d, x, s, ac) { \ + (a) += H ((b), (c), (d)) + (x) + ac; \ + (a) = ROTATELEFT ((a), (s)); \ + (a) += (b); \ +} +#define II(a, b, c, d, x, s, ac) { \ + (a) += I ((b), (c), (d)) + (x) + ac; \ + (a) = ROTATELEFT ((a), (s)); \ + (a) += (b); \ +} + +#include + +using std::string; + +/* Define of btye.*/ +typedef unsigned char byte; +/* Define of byte. */ +typedef unsigned int bit32; + +class MD5 { +public: + /* Construct a MD5 object with a string. */ + MD5(const string& message); + + /* Generate md5 digest. */ + const byte* getDigest(); + + /* Convert digest to string value */ + string toStr(); + +private: + /* Initialization the md5 object, processing another message block, + * and updating the context.*/ + void init(const byte* input, size_t len); + + /* MD5 basic transformation. Transforms state based on block. */ + void transform(const byte block[64]); + + /* Encodes input (usigned long) into output (byte). */ + void encode(const bit32* input, byte* output, size_t length); + + /* Decodes input (byte) into output (usigned long). */ + void decode(const byte* input, bit32* output, size_t length); + +private: + /* Flag for mark whether calculate finished. */ + bool finished; + + /* state (ABCD). */ + bit32 state[4]; + + /* number of bits, low-order word first. */ + bit32 count[2]; + + /* input buffer. */ + byte buffer[64]; + + /* message digest. */ + byte digest[16]; + + /* padding for calculate. */ + static const byte PADDING[64]; + + /* Hex numbers. */ + static const char HEX_NUMBERS[16]; +}; +#endif // MD5_H diff --git a/DaemonService/model.h b/DaemonService/model.h new file mode 100644 index 0000000..5aeac4e --- /dev/null +++ b/DaemonService/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/DaemonService/uac.rc b/DaemonService/uac.rc new file mode 100644 index 0000000..686301f --- /dev/null +++ b/DaemonService/uac.rc @@ -0,0 +1 @@ +1 24 DISCARDABLE "UAC.manifest" \ No newline at end of file diff --git a/DaemonService/worker.cpp b/DaemonService/worker.cpp new file mode 100644 index 0000000..9971243 --- /dev/null +++ b/DaemonService/worker.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include "worker.h" +#include "md5.h" +#include "dal.h" +#include "model.h" + +void Worker::run() +{ + if (this->m_portList.empty()) + return; + this->m_socket = new QTcpSocket(); + this->m_socket->setSocketDescriptor(this->m_socketDescriptor); + if (!this->m_socket->waitForConnected(100000)) + { + qDebug("IP:%s Connect Fail", m_socket->peerAddress().toString().toStdString().data()); + return; + } + std::string ip = m_socket->peerAddress().toString().toStdString(); + qDebug("IP:%s Connect Success, Waiting for verification...", ip.data()); + + if (this->m_socket->waitForReadyRead(3000)) + { + QByteArray data = this->m_socket->readAll(); + qDebug("IP:%s send data:'%s' ---- Verifying password...", ip.data(), data.toStdString().data()); + + QString ip = this->m_socket->peerAddress().toString(); + // 鏍¢獙 +// if (data.toStdString() +// == MD5("asdfas35.v;cxv-123" +// + MD5("xck3dy$^@1309uyrew" +// + ip.toStdString() +// + "ioer6719024yoiuew6f178934056").toStr() +// + "sjavlkc907*$!@(.12i.dy1").toStr()) + if (data.toStdString() == "0d87e84deb68ae435735e0f8ae4668b0") + { + 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..."); + // 娣诲姞鍒扮櫧鍚嶅崟涓 + QProcess p(nullptr); + p.start("netsh", + QStringList() << "ipsec" + << "static" + << "add" + << "filter" + << "filterlist=whitelist" + << ("srcaddr=" + ip) + << "dstaddr=me" + << "protocol=tcp" + << "mirrored=yes" + << QString("dstport=%1").arg(port) + ); + p.waitForStarted(); + p.waitForFinished(); + } + else + { + qDebug("Update last login time..."); + } + } + if (DAL::instance().updateWhiteList(ip, this->m_portList)) + qDebug("Update completed锛"); + else + qWarning("Update failed!"); + } + else + { + qWarning("Verification failed"); + // 鏍¢獙澶辫触锛岃嫢涓嶆槸绠楁硶闂锛屽垯鍙兘鏄叾浠栦汉鎯崇寽瀵嗙爜 + } + } + else + { + qDebug("Check timeout"); + // 杩欎釜杩炴帴杩炰笂浠ュ悗涓嶅彂浠讳綍娑堟伅锛岃鏄庝笉鏄櫥褰曞櫒鐨剆ocket + // 鐧诲綍鍣ㄧ殑socket浼氬湪杩炴帴鍚庣珛鍒诲彂閫佹牎楠屾暟鎹 + // 鎵浠ュ皢杩欎釜IP杩涜璁板綍锛屽綋杩欎釜IP绱瓒呰繃涓瀹氭暟閲忔杩炴帴锛屽垯灏嗗叾鎷夐粦 + // TODO:鎷夐粦 + } + + qDebug("Close Socket connection"); + // 鍏抽棴socket杩炴帴 + this->m_socket->close(); + this->m_socket->deleteLater(); +} diff --git a/DaemonService/worker.h b/DaemonService/worker.h new file mode 100644 index 0000000..b91f3f9 --- /dev/null +++ b/DaemonService/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