From 97858eb8f7e78b004287a2fd82bd84f4739affae Mon Sep 17 00:00:00 2001 From: zhangjiaping Date: Tue, 26 Jan 2021 10:39:01 +0800 Subject: [PATCH] fix(wifi): Wifi can not be connected when psk-flag is 1. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 修复wifi密码策略为当前用户存储时连接失败的bug Log: 修复wifi密码策略为当前用户存储时连接失败的bug Bug: http://172.17.66.192/biz/bug-view-31154.html --- src/backthread.cpp | 6 ++++ src/backthread.h | 1 + src/mainwindow.h | 4 ++- src/oneconnform.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++ src/oneconnform.h | 2 ++ 5 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/backthread.cpp b/src/backthread.cpp index 6ca44541..4505d6d8 100644 --- a/src/backthread.cpp +++ b/src/backthread.cpp @@ -329,6 +329,12 @@ void BackThread::execConnWifiPWD(QString connName, QString password, QString con emit btFinish(); } +void BackThread::execConnWifiPsk(QString cmd) +{ + int res = Utils::m_system(cmd.toUtf8().data()); + emit connDone(res); +} + //to connected wireless network driectly do not need a password void BackThread::execConnWifi(QString connName) { diff --git a/src/backthread.h b/src/backthread.h index 595f25f9..a0b1bbe7 100644 --- a/src/backthread.h +++ b/src/backthread.h @@ -61,6 +61,7 @@ public slots: void execConnLan(QString connName, QString ifname, QString connectType); void execConnWifi(QString connName); void execConnWifiPWD(QString connName, QString password, QString connType); + void execConnWifiPsk(QString cmd); void disConnSparedNetSlot(QString type); void disConnLanOrWifi(QString type); diff --git a/src/mainwindow.h b/src/mainwindow.h index e0686eac..8572dc20 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -190,6 +190,8 @@ public slots: void on_btnHotspotState(); void on_btnWifiList_clicked(); + void connWifiDone(int connFlag); + //flag =0或1为普通点击、2为收到打开信息、3为收到关闭信息、4为无线网卡插入、5为无线网卡拔出 void onBtnWifiClicked(int flag = 0); @@ -328,7 +330,7 @@ private slots: void disWifiStateKeep(); void disWifiDoneChangeUI(); void connLanDone(int connFlag); - void connWifiDone(int connFlag); +// void connWifiDone(int connFlag); void iconStep(); void on_btnFlyMode_clicked(); diff --git a/src/oneconnform.cpp b/src/oneconnform.cpp index 6a76da89..76aa1e1a 100644 --- a/src/oneconnform.cpp +++ b/src/oneconnform.cpp @@ -553,11 +553,61 @@ void OneConnForm::toConnectWirelessNetwork() if (ui->lbConned->text() == "--" || ui->lbConned->text() == " ") { if (!isWifiConfExist(lbNameText->text())) { //没有配置文件,使用有密码的wifi连接 + psk_flag = 0; on_btnConnPWD_clicked(); return; } } + if (isWifiConfExist(lbNameText->text())) { + //有配置文件,获取密码存储策略 + QProcess * process = new QProcess(this); + QString ssid = lbNameText->text(); + if (ssid.contains(" ")) { + ssid.replace(QRegExp("[\\s]"), "\\\ "); //防止名字包含空格导致指令识别错误,需要转义 + } + process->start(QString("nmcli -f 802-11-wireless-security.psk-flags connection show %1").arg(lbNameText->text())); + connect(process, static_cast(&QProcess::finished), this, [ = ]() { + process->deleteLater(); + }); + connect(process, &QProcess::readyReadStandardOutput, this, [ = ]() { + QString str = process->readAllStandardOutput(); + psk_flag = str.mid(str.lastIndexOf(" ") - 1, 1).toInt(); + }); + process->waitForFinished(); + } + if (psk_flag != 0) { //未为所有用户存储密码 + QString homePath = getenv("HOME"); + if (QFile(QString("%1/.config/%2.psk").arg(homePath).arg(lbNameText->text())).exists()) { //已为该用户存储密码 + mw->is_stop_check_net_state = 1; + QThread *t = new QThread(); + BackThread *bt = new BackThread(); + bt->moveToThread(t); + connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); + connect(t, &QThread::started, this, [ = ]() { + this->startWaiting(true); + QString cmdStr = "nmcli connection up " + lbNameText->text() + " passwd-file " + homePath +"/.config/" + lbNameText->text() + ".psk"; + emit this->sigConnWifiPsk(cmdStr); + }); + connect(this, SIGNAL(sigConnWifiPsk(QString)), bt, SLOT(execConnWifiPsk(QString))); + connect(bt, &BackThread::connDone, this, [ = ](int res) { + this->stopWaiting(); + mw->is_stop_check_net_state = 0; + if (res) { + QFile::remove(QString("%1/.config/%2.psk").arg(homePath).arg(lbNameText->text()).toUtf8()); + } + mw->connWifiDone(res); + }); + connect(bt, SIGNAL(btFinish()), t, SLOT(quit())); + t->start(); + } else { //没有为该用户存储密码 + slotConnWifiResult(2); + } + return; + } else { //为所有用户存储密码 + QFile::remove(QString("%1/.config/%2.psk").arg(homePath).arg(lbNameText->text()).toUtf8()); //删除密码文件 + } + mw->is_stop_check_net_state = 1; QThread *t = new QThread(); BackThread *bt = new BackThread(); @@ -577,6 +627,38 @@ void OneConnForm::on_btnConnPWD_clicked() syslog(LOG_DEBUG, "A button named btnConnPWD about wifi net is clicked."); qDebug()<<"A button named btnConnPWD about wifi net is clicked."; + if (psk_flag != 0) { +// QString cmdStr = 0; + QString homePath = getenv("HOME"); + QFile *passwdFile = new QFile(QString("%1/.config/%2.psk").arg(homePath).arg(lbNameText->text())); + if (passwdFile->open(QIODevice::ReadWrite)) { + passwdFile->write(QString("802-11-wireless-security.psk:%1").arg(ui->lePassword->text()).toUtf8()); + passwdFile->close(); +// cmdStr = "nmcli connection up " + lbNameText->text() + " passwd-file " + homePath +"/.config/" + lbNameText->text() + ".psk"; + } + mw->is_stop_check_net_state = 1; + QThread *t = new QThread(); + BackThread *bt = new BackThread(); + bt->moveToThread(t); + connect(t, SIGNAL(finished()), t, SLOT(deleteLater())); + connect(t, &QThread::started, this, [ = ]() { + this->startWaiting(true); + QString cmdStr = "nmcli connection up " + lbNameText->text() + " passwd-file " + homePath +"/.config/" + lbNameText->text() + ".psk"; + emit this->sigConnWifiPsk(cmdStr); + }); + connect(this, SIGNAL(sigConnWifiPsk(QString)), bt, SLOT(execConnWifiPsk(QString))); + connect(bt, &BackThread::connDone, this, [ = ](int res) { + this->stopWaiting(); + mw->is_stop_check_net_state = 0; + if (res) { + QFile::remove(QString("%1/.config/%2.psk").arg(homePath).arg(lbNameText->text()).toUtf8()); + } + mw->connWifiDone(res); + }); + connect(bt, SIGNAL(btFinish()), t, SLOT(quit())); + t->start(); + } + mw->is_stop_check_net_state = 1; QThread *t = new QThread(); BackThread *bt = new BackThread(); diff --git a/src/oneconnform.h b/src/oneconnform.h index 53cc81ba..9b5a83f3 100644 --- a/src/oneconnform.h +++ b/src/oneconnform.h @@ -128,6 +128,7 @@ private: int waitPage; int countCurrentTime; bool isWaiting = false; + int psk_flag = 0; //密码存储策略 Ui::OneConnForm *ui = nullptr; MainWindow *mw = nullptr; @@ -147,6 +148,7 @@ signals: void sigConnWifi(QString); void sigConnWifiPWD(QString, QString, QString); + void sigConnWifiPsk(QString); }; #endif // ONECONNFORM_H