Browse Source

check licence dynamic library

dongjunhong 2 months ago
parent
commit
88c007a7cf

+ 73 - 0
src/tool/checklicence/.gitignore

@@ -0,0 +1,73 @@
+# This file is used to ignore files which are generated
+# ----------------------------------------------------------------------------
+
+*~
+*.autosave
+*.a
+*.core
+*.moc
+*.o
+*.obj
+*.orig
+*.rej
+*.so
+*.so.*
+*_pch.h.cpp
+*_resource.rc
+*.qm
+.#*
+*.*#
+core
+!core/
+tags
+.DS_Store
+.directory
+*.debug
+Makefile*
+*.prl
+*.app
+moc_*.cpp
+ui_*.h
+qrc_*.cpp
+Thumbs.db
+*.res
+*.rc
+/.qmake.cache
+/.qmake.stash
+
+# qtcreator generated files
+*.pro.user*
+
+# xemacs temporary files
+*.flc
+
+# Vim temporary files
+.*.swp
+
+# Visual Studio generated files
+*.ib_pdb_index
+*.idb
+*.ilk
+*.pdb
+*.sln
+*.suo
+*.vcproj
+*vcproj.*.*.user
+*.ncb
+*.sdf
+*.opensdf
+*.vcxproj
+*vcxproj.*
+
+# MinGW generated files
+*.Debug
+*.Release
+
+# Python byte code
+*.pyc
+
+# Binaries
+# --------
+*.dll
+*.exe
+

+ 134 - 0
src/tool/checklicence/checklicence.cpp

@@ -0,0 +1,134 @@
+#include "checklicence.h"
+#include <thread>
+#include <iostream>
+#include <QRegularExpression>
+#include <QCryptographicHash>
+#include <QFile>
+#include <QTextStream>
+#include <QMessageBox>
+Checklicence::Checklicence()
+{
+}
+QString Checklicence::hashString(const QString &str) {
+    QString strippedStr = str;
+    strippedStr.remove(QChar('-'));
+    QByteArray hashData = QCryptographicHash::hash(strippedStr.toUtf8(), QCryptographicHash::Sha256);
+    QString hashedStr = QString::fromLatin1(hashData.toHex());
+    return hashedStr;
+}
+QString Checklicence::caesarCipher(const QString &text, int shift) {
+    QString result;
+    const int alphabetSize = 26;
+    const int digitSize = 10;
+
+    for (const QChar &ch : text) {
+        if (ch.isLetter()) {
+            QChar base = ch.isLower() ? 'a' : 'A';
+            result.append(QChar((ch.toLatin1() - base.toLatin1() + shift + alphabetSize) % alphabetSize + base.toLatin1()));
+        } else if (ch.isDigit()) {
+            result.append(QChar((ch.toLatin1() - '0' + shift + digitSize) % digitSize + '0'));
+        } else {
+            result.append(ch);
+        }
+    }
+
+    return result;
+}
+QString Checklicence::getDiskID()
+{
+    QProcess process;
+    QString command="echo 'nvidia' | sudo -S blkid";
+    process.start("/bin/bash", QStringList() << "-c" << command);
+    process.waitForFinished();
+    QString output=process.readAllStandardOutput();
+    QStringList diskidlist=output.split("/dev/");
+    QString k0p1;
+    for (int i=0;i<diskidlist.size();i++)
+    {
+        if (diskidlist[i].contains("mmcblk0p1:"))
+        {
+                k0p1=diskidlist[i];
+        }
+    }
+    QStringList k0p1list=k0p1.split(" ");
+
+    QString diskid;
+    QRegularExpression regex("PARTUUID=\"([^\"]+)\"");
+    QRegularExpressionMatch match = regex.match(k0p1);
+    if (match.hasMatch()) {
+       QString subStr = match.captured(1);
+       diskid=subStr;
+    } else {
+       qDebug() << "UUID not found.";
+    }
+
+    return diskid;
+}
+
+QString Checklicence::readLicense()
+{
+    QString filePath = "./license";
+
+    QFile file(filePath);
+    if (!QFile::exists(filePath)){
+        if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+           QTextStream out(&file);
+           out << " " << endl;
+           file.close();
+           qDebug() << "File created and written successfully.";
+           return "0";
+        }
+    }
+    else{
+        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+           qDebug() << "Failed to open file.";
+           return "1";
+        }
+        QTextStream in(&file);
+
+        QString fileContent = in.readAll();
+        fileContent=fileContent.remove(QChar('\n'));
+        file.close();
+        return fileContent;
+    }
+
+}
+
+void Checklicence::creatMachFile(const QString &str)
+{
+
+    QString filePath = "./machineID";
+    QFile file(filePath);
+
+    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+        qDebug() << "Failed to open file for writing.";
+        return;
+    }
+    QString strippedStr = str;
+    strippedStr.remove(QChar('-'));
+    QTextStream out(&file);
+    out << strippedStr;
+    file.close();
+
+    qDebug() << "MachID File created and written successfully.";
+}
+int Checklicence::CheckLincese()
+{
+    QString diskid=getDiskID();
+    QString filelicense=readLicense();
+    QString final_license="catarc"+diskid+"shenlanv2pro";
+    QString hashedStr=hashString(final_license);
+    if (hashedStr==filelicense)
+    {
+        return 1;
+    }
+    else{
+        QString writeid=caesarCipher(diskid,7);
+        creatMachFile(writeid);
+        QMessageBox::warning(nullptr, "未注册程序", "请在程序目录下找到machineID文件并联系管理员进行注册", QMessageBox::Ok);
+        return 0;
+    }
+
+    return 1;
+
+}

+ 26 - 0
src/tool/checklicence/checklicence.h

@@ -0,0 +1,26 @@
+#ifndef CHECKLICENCE_H
+#define CHECKLICENCE_H
+
+#include "checklicence_global.h"
+#include <QCoreApplication>
+#include <QProcess>
+#include "QDebug"
+
+#if defined(CHECKLICENCE_LIBRARY)
+#  define CHECKLICENCE_EXPORT Q_DECL_EXPORT
+#else
+#  define CHECKLICENCE_EXPORT Q_DECL_IMPORT
+#endif
+class CHECKLICENCE_EXPORT Checklicence
+{
+public:
+    Checklicence();
+    static int CheckLincese();
+    static QString getDiskID();
+    static QString hashString(const QString &str);
+    static QString caesarCipher(const QString &text, int shift);
+    static QString readLicense();
+    static void creatMachFile(const QString &str);
+};
+
+#endif // CHECKLICENCE_H

+ 32 - 0
src/tool/checklicence/checklicence.pro

@@ -0,0 +1,32 @@
+QT -= gui
+
+TEMPLATE = lib
+DEFINES += CHECKLICENCE_LIBRARY
+
+CONFIG += c++11
+CONFIG += plugin
+QT       += core
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+# The following define makes your compiler emit warnings if you use
+# any Qt feature that has been marked 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 it uses 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
+
+SOURCES += \
+    checklicence.cpp
+
+HEADERS += \
+    checklicence_global.h \
+    checklicence.h
+
+# Default rules for deployment.
+unix {
+    target.path = /usr/lib
+}
+!isEmpty(target.path): INSTALLS += target

+ 12 - 0
src/tool/checklicence/checklicence_global.h

@@ -0,0 +1,12 @@
+#ifndef CHECKLICENCE_GLOBAL_H
+#define CHECKLICENCE_GLOBAL_H
+
+#include <QtCore/qglobal.h>
+
+#if defined(CHECKLICENCE_LIBRARY)
+#  define CHECKLICENCE_EXPORT Q_DECL_EXPORT
+#else
+#  define CHECKLICENCE_EXPORT Q_DECL_IMPORT
+#endif
+
+#endif // CHECKLICENCE_GLOBAL_H