Przeglądaj źródła

Generate license code through hard disk partition identification code

dongjunhong 5 miesięcy temu
rodzic
commit
97e1c890aa

+ 129 - 0
src/common/common/license_local/adclicense.cpp

@@ -0,0 +1,129 @@
+#include "adclicense.h"
+
+#include "modulecomm.h"
+#include <thread>
+#include <iostream>
+#include <QRegularExpression>
+#include <QCryptographicHash>
+#include <QFile>
+#include <QTextStream>
+#include <QMessageBox>
+
+
+
+
+ADCLicenseServ::ADCLicenseServ()
+{
+
+}
+QString ADCLicenseServ::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 ADCLicenseServ::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 ADCLicenseServ::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 ADCLicenseServ::readLicense()
+{
+    QString filePath = "./license";
+
+    QFile file(filePath);
+    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 ADCLicenseServ::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 ADCLicenseServ::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;
+
+}

+ 22 - 0
src/common/common/license_local/adclicense.h

@@ -0,0 +1,22 @@
+#ifndef ADCLICENSESERV_H
+#define ADCLICENSESERV_H
+#include <QCoreApplication>
+#include <QProcess>
+#include "QDebug"
+
+class ADCLicenseServ
+{
+
+public:
+    ADCLicenseServ();
+
+public:
+    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 // ADCLICENSESERV_H

+ 5 - 0
src/common/common/license_local/adclicense.pri

@@ -0,0 +1,5 @@
+HEADERS += \
+    $$PWD/adclicense.h
+
+SOURCES += \
+    $$PWD/adclicense.cpp

+ 22 - 0
src/common/common/license_local/adclicensewithlocal.cpp

@@ -0,0 +1,22 @@
+
+#include <QMessageBox>
+#include "adclicense.h"
+
+int ADCLicenseCheckWithWin()
+{
+    int nRtn = ADCLicenseServ::CheckLincese();
+    if(nRtn == -1)
+    {
+        QMessageBox::warning(NULL,"Warning","license 错误,请申请license",QMessageBox::YesAll);
+        return nRtn;
+    }
+    else
+    {
+        if(nRtn == -2)
+        {
+            QMessageBox::warning(NULL,"Warning","license服务启动失败",QMessageBox::YesAll);
+            return nRtn;
+        }
+    }
+    return nRtn;
+}

+ 5 - 0
src/common/common/license_local/adclicensewithlocal.h

@@ -0,0 +1,5 @@
+#ifndef ADCLICENSEWITHWIN_H
+#define ADCLICENSEWITHWIN_H
+
+int ADCLicenseCheckWithWin();
+#endif // ADCLICENSEWITHWIN_H