Browse Source

change fwupdate.

yuchuli 1 year ago
parent
commit
fe0572ead7

+ 6 - 3
src/tool/fwupdate_admin/fwupdate_admin.pro

@@ -30,7 +30,8 @@ SOURCES += \
     ../../include/msgtype/commonrpc.pb.cc \
     ../../include/msgtype/fwlogin.pb.cc \
     ../server_fwupdate/commonrpc.grpc.pb.cc \
-    grpcfwclient.cpp
+    grpcfwclient.cpp \
+    mainwindowadminuser.cpp
 
 HEADERS += \
         mainwindow.h \
@@ -38,11 +39,13 @@ HEADERS += \
     ../../include/msgtype/commonrpc.pb.h \
     ../../include/msgtype/fwlogin.pb.h \
     ../server_fwupdate/commonrpc.grpc.pb.h \
-    grpcfwclient.h
+    grpcfwclient.h \
+    mainwindowadminuser.h
 
 FORMS += \
         mainwindow.ui \
-    mainwindowlogin.ui
+    mainwindowlogin.ui \
+    mainwindowadminuser.ui
 
 RESOURCES += \
     res.qrc

+ 2 - 0
src/tool/fwupdate_admin/grpcfwclient.cpp

@@ -69,9 +69,11 @@ void grpcfwclient::threadrpc()
 
         ClientContext context ;
         context.set_deadline(timespec);
+        std::cout<<" before. reply size: "<<xreply.ByteSize()<<std::endl;
 
         Status status = stub_->rpcquery(&context, xreq, &xreply);
         if (status.ok()) {
+            std::cout<<std::chrono::system_clock::now().time_since_epoch().count()/1000<<" reply size: "<<xreply.ByteSize()<<std::endl;
             mmutexReply.lock();
             mReply.CopyFrom(xreply);
             mbReplyUpdate = true;

+ 5 - 0
src/tool/fwupdate_admin/main.cpp

@@ -4,9 +4,11 @@
 #include "grpcfwclient.h"
 
 #include "mainwindowlogin.h"
+#include "mainwindowadminuser.h"
 
 MainWindow * gwmain;
 MainWindowLogin * gwlogin;
+MainWindowAdminUser * gwadminuser;
 
 grpcfwclient * gpgrpcfwclient;
 
@@ -17,11 +19,14 @@ int main(int argc, char *argv[])
     grpcfwclient fwclient;
     gpgrpcfwclient = &fwclient;
 
+    MainWindowAdminUser wadminuser;
+
     MainWindowLogin wlogin;
     wlogin.show();
 
     MainWindow w;
 //    w.show();
+    gwadminuser = &wadminuser;
     gwlogin = &wlogin;
     gwmain = &w;
 

+ 16 - 0
src/tool/fwupdate_admin/mainwindowadminuser.cpp

@@ -0,0 +1,16 @@
+#include "mainwindowadminuser.h"
+#include "ui_mainwindowadminuser.h"
+
+MainWindowAdminUser::MainWindowAdminUser(QWidget *parent) :
+    QMainWindow(parent),
+    ui(new Ui::MainWindowAdminUser)
+{
+    ui->setupUi(this);
+
+    setWindowTitle("Admin User");
+}
+
+MainWindowAdminUser::~MainWindowAdminUser()
+{
+    delete ui;
+}

+ 22 - 0
src/tool/fwupdate_admin/mainwindowadminuser.h

@@ -0,0 +1,22 @@
+#ifndef MAINWINDOWADMINUSER_H
+#define MAINWINDOWADMINUSER_H
+
+#include <QMainWindow>
+
+namespace Ui {
+class MainWindowAdminUser;
+}
+
+class MainWindowAdminUser : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    explicit MainWindowAdminUser(QWidget *parent = 0);
+    ~MainWindowAdminUser();
+
+private:
+    Ui::MainWindowAdminUser *ui;
+};
+
+#endif // MAINWINDOWADMINUSER_H

+ 24 - 0
src/tool/fwupdate_admin/mainwindowadminuser.ui

@@ -0,0 +1,24 @@
+<ui version="4.0">
+ <author/>
+ <comment/>
+ <exportmacro/>
+ <class>MainWindowAdminUser</class>
+ <widget class="QMainWindow" name="MainWindowAdminUser">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>800</width>
+    <height>600</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>MainWindow</string>
+  </property>
+  <widget class="QMenuBar" name="menubar"/>
+  <widget class="QWidget" name="centralwidget"/>
+  <widget class="QStatusBar" name="statusbar"/>
+ </widget>
+ <pixmapfunction/>
+ <connections/>
+</ui>

+ 20 - 5
src/tool/fwupdate_admin/mainwindowlogin.cpp

@@ -3,16 +3,20 @@
 
 #include <QMessageBox>
 
+#include "fwlogin.pb.h"
+
 #include "grpcfwclient.h"
+
 #include "mainwindow.h"
 #include "mainwindowlogin.h"
-
-#include "fwlogin.pb.h"
+#include "mainwindowadminuser.h"
 
 extern MainWindow * gwmain;
 extern MainWindowLogin * gwlogin;
+extern MainWindowAdminUser * gwadminuser;
 extern grpcfwclient * gpgrpcfwclient;
 
+
 MainWindowLogin::MainWindowLogin(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindowLogin)
@@ -61,6 +65,7 @@ void MainWindowLogin::on_pushButton_Login_clicked()
     iv::fw::loginreq xlogin;
     xlogin.set_username(ui->lineEdit_username->text().toStdString());
     xlogin.set_password(ui->lineEdit_password->text().toStdString());
+    mstrloginuser = xlogin.username();
 
     iv::queryrpcReq xreq;
     xreq.set_ntype(iv::CommonRPCType::LOGIN);
@@ -102,8 +107,16 @@ void MainWindowLogin::onloginstate(int nstate,  const char * strerr)
 {
     if(nstate == 1)
     {
-        this->hide();
-        gwmain->show();
+        if(mstrloginuser == "admin")
+        {
+            this->hide();
+            gwadminuser->show();
+        }
+        else
+        {
+            this->hide();
+            gwmain->show();
+        }
     }
     else
     {
@@ -147,7 +160,9 @@ void MainWindowLogin::threadlogin(iv::queryrpcReq  xreq)
                 {
                     if(xLoginReply.nauth() == 0)
                     {
-                        emit loginstate(0,xLoginReply.errstr().data());
+                        std::cout<<"error code: "<<xLoginReply.errstr()<<std::endl;
+                        strncpy(mstrlasterr,xLoginReply.errstr().data(),256);
+                        emit loginstate(0,mstrlasterr);
                     }
                     else
                     {

+ 4 - 0
src/tool/fwupdate_admin/mainwindowlogin.h

@@ -39,6 +39,10 @@ private:
     QCheckBox * mpcheckdisplay;
 
     std::thread * mpthread;
+
+    char mstrlasterr[256];
+
+    std::string mstrloginuser;
 };
 
 #endif // MAINWINDOWLOGIN_H

+ 6 - 2
src/tool/server_fwupdate/fwadminproc.cpp

@@ -32,9 +32,13 @@ iv::queryrpcReply fwadminproc::ProcLogin(const iv::queryrpcReq *req)
     iv::fw::loginreply xloginreply;
     if(xloginreq.ParseFromArray(req->data().data(),req->ndatasize()))
     {
-        int nauth = mpfwdb->CheckAuth(xloginreq.username(),xloginreq.password());
+        std::string strerrorcode = "username or password error.";
+        int nauth = mpfwdb->CheckAuth(xloginreq.username(),xloginreq.password(),strerrorcode);
         xloginreply.set_nauth(nauth);
-        xloginreply.set_errstr("username or password error.");
+        if(nauth == 0)
+        {
+            xloginreply.set_errstr(strerrorcode);
+        }
     }
     else
     {

+ 106 - 8
src/tool/server_fwupdate/fwdb.cpp

@@ -74,7 +74,7 @@ void fwdb::OpenDataBase()
     {
         if(!sql_query.exec("create table accountdata(id INTEGER primary key AUTOINCREMENT,username TEXT, password TEXT,"
                            "phone TEXT, passneedchange BOOL, passerrorcount INTEGER,"
-                           "uservalidtime DATETIME, usercreatetime DATETIME)"))
+                           "uservalidtime DATETIME, usercreatetime DATETIME,lastloginerrortime DATETIME)"))
         {
             qDebug() << "Error: Fail to create table."<< sql_query.lastError();
             return;
@@ -98,9 +98,9 @@ void fwdb::OpenDataBase()
         QDateTime now = QDateTime::currentDateTime();
 
         QString strnow = now.toString("yyyy-MM-dd hh:mm:ss");
-        snprintf(strsen,1000,"INSERT INTO accountdata(username,password,passerrorcount,uservalidtime,usercreatetime)"
-                 " VALUES(\"%s\",\"%s\",%d,\"%s\",\"%s\")",
-                                     "admin","adc",0,"2099-12-31 23:59:59",strnow.toLatin1().data());
+        snprintf(strsen,1000,"INSERT INTO accountdata(username,password,passerrorcount,uservalidtime,usercreatetime,lastloginerrortime)"
+                 " VALUES(\"%s\",\"%s\",%d,\"%s\",\"%s\",\"%s\")",
+                                     "admin","adc",0,"2099-12-31 23:59:59",strnow.toLatin1().data(),"1970-1-1 1:00:00");
         sql_query.exec(strsen);
         qDebug("Create Table groupdata successfully.");
     }
@@ -111,31 +111,129 @@ void fwdb::threaddb()
     OpenDataBase();
 }
 
-int fwdb::CheckAuth(std::string strusername, std::string strpassword)
+int fwdb::CheckAuth(std::string strusername, std::string strpassword ,std::string & strerrorcode)
 {
     int nrtn = 0;
     mmutexdb.lock();
     QSqlQuery query(mdatabase);
     char strsen[1000];
+
+
+    snprintf(strsen,1000,"select * from accountdata where((accountdata.username = \"%s\"))",
+             strusername.data(),strpassword.data());
+    query.exec(strsen);
+    if(!query.exec(strsen))
+    {
+        std::cout<<query.lastError().text().toLatin1().data()<<std::endl;
+        strerrorcode = "SQL Error.";
+        mmutexdb.unlock();
+        return 0;
+    }
+    else
+    {
+        if(query.next())
+        {
+            int nerrorcount = query.value("passerrorcount").toInt();
+            QString  strerrortime = query.value("lastloginerrortime").toString();
+
+            QDateTime dtlasterr =  QDateTime::fromString(strerrortime,"yyyy-MM-dd hh:mm:ss");
+
+            int64_t nseclasterr = dtlasterr.toSecsSinceEpoch();
+            int64_t nsecnow = QDateTime::currentSecsSinceEpoch();
+
+            int64_t ndiff = nsecnow - nseclasterr ;
+
+
+            if((nerrorcount >= 5) &&(ndiff < 1800))
+            {
+                strerrorcode = "PassWord Error 5 times, Please Wait 30 minutes.";
+                mmutexdb.unlock();
+                return 0;
+            }
+
+        }
+    }
+
     snprintf(strsen,1000,"select * from accountdata where((accountdata.username = \"%s\") &(accountdata.password = \"%s\"))",
              strusername.data(),strpassword.data());
     query.exec(strsen);
     if(!query.exec(strsen))
     {
-        std::cout<<query.lastError().text().data()<<std::endl;
+        std::cout<<query.lastError().text().toLatin1().data()<<std::endl;
+        strerrorcode = "SQL Error.";
+        mmutexdb.unlock();
+        return 0;
     }
     else
     {
-        while(query.next())
+        if(query.next())
         {
             nrtn = 1;
+            std::cout<<" auth ok. "<<std::endl;
+
+            QString strvalidtime = query.value("uservalidtime").toString();
+            QDateTime dtvalid = QDateTime::fromString(strvalidtime,"yyyy-MM-dd hh:mm:ss");
+
+            int64_t nsecvalid = dtvalid.toSecsSinceEpoch();
+            int64_t nsecnow = QDateTime::currentSecsSinceEpoch();
+
+            int nerrorcount = query.value("passerrorcount").toInt();
+
+            int64_t ndiff = nsecvalid - nsecnow;
+
+            if(ndiff <0)
+            {
+                strerrorcode = "Account Expire.";
+                mmutexdb.unlock();
+                return 0;
+
+            }
+
+
+            if(nerrorcount != 0)
+            {
+                snprintf(strsen,1000,"update accountdata set passerrorcount = %d  where((accountdata.username = \"%s\") )",
+                         0, strusername.data());
+                query.exec(strsen);
+                if(!query.exec(strsen))
+                {
+                    std::cout<<query.lastError().text().toLatin1().data()<<std::endl;
+                }
+            }
+
 //            int id = query.value(0).toInt();
 //            qint64 recordid = query.value(1).toLongLong();
 //            mql_xvectorrecordtime.push_back(recordid);
 //            std::string strvehid = query.value(3).toString().toStdString();
 
         }
+        else
+        {
+            snprintf(strsen,1000,"select * from accountdata where((accountdata.username = \"%s\") )",
+                     strusername.data());
+            query.exec(strsen);
+            if(!query.exec(strsen))
+            {
+                std::cout<<query.lastError().text().data()<<std::endl;
+            }
+            else
+            {
+                if(query.next())
+                {
+                    int nerrorcount = 0;
+                    nerrorcount = query.value(5).toInt();
+                    snprintf(strsen,1000,"update accountdata set lastloginerrortime = \"%s\",passerrorcount = %d  where(accountdata.username = \"%s\" )",
+                             QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss").toLatin1().data(),
+                             nerrorcount+1, strusername.data());
+                    query.exec(strsen);
+                    if(!query.exec(strsen))
+                    {
+                        std::cout<<query.lastError().text().toLatin1().data()<<std::endl;
+                    }
+                }
+            }
+        }
     }
     mmutexdb.unlock();
-    return 1;
+    return nrtn;
 }

+ 1 - 1
src/tool/server_fwupdate/fwdb.h

@@ -21,7 +21,7 @@ public:
     ~fwdb();
 
 public:
-    int CheckAuth(std::string strusername,std::string strpassword);
+    int CheckAuth(std::string strusername,std::string strpassword,std::string & strerrorcode);
 
 private:
     std::thread * mpthread;

+ 5 - 1
src/tool/server_fwupdate/main.cpp

@@ -35,9 +35,13 @@ class CommonRPCServiceImpl final : public iv::CommonRPC::Service{
   Status rpcquery(ServerContext* context, const iv::queryrpcReq * request,
                   iv::queryrpcReply * reply) override {
 
+      std::cout<<" request size: "<<request->ByteSize()<<std::endl;
+  //    reply = new iv::queryrpcReply;
     iv::queryrpcReply xreply = gpfwadminproc->ProcReq(request);
-    reply = new iv::queryrpcReply;
+
+
     reply->CopyFrom(xreply);
+    std::cout<<std::chrono::system_clock::now().time_since_epoch().count()/1000<<" reply size: "<<reply->ByteSize()<<std::endl;
     return Status::OK;
   }