minor reconstruction of codes about hidden wifi

This commit is contained in:
crrs666 2021-03-25 21:17:57 +08:00
parent 051a165ddc
commit ee0e99c043
9 changed files with 161 additions and 104 deletions

View File

@ -329,6 +329,74 @@ void BackThread::execConnWifiPWD(QString connName, QString password, QString con
emit btFinish();
}
void BackThread::execConnHiddenWifiWPA(QString wifiName, QString wifiPassword)
{
int x(1), n(0);
do {
n += 1;
if (n >= 3) {
syslog(LOG_ERR, "connection attempt of hidden wifi %s failed for 3 times, no more attempt", wifiName);
x = 0;
emit connDone(1);
}
QString tmpPath = "/tmp/kylin-nm-btoutput-" + QDir::home().dirName();
QString cmd = "nmcli device wifi connect '" + wifiName + "' password '" + wifiPassword + "' hidden yes > " + tmpPath + " 2>&1";
// qDebug() << Q_FUNC_INFO << cmd << tmpPath;
int status = Utils::m_system(cmd.toUtf8().data());
if (status != 0) {
syslog(LOG_ERR, "execute 'nmcli device wifi connect' in function 'on_btnConnect_clicked' failed");
}
QFile file(tmpPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug()<<"Can't open the file!"<<endl;
}
QString text = file.readAll();
file.close();
if(text.indexOf("Scanning not allowed") != -1
|| text.isEmpty()
|| text.indexOf("No network with SSID") != -1){
x = 1;
sleep(15);//nm扫描冷却为10s
} else {
emit connDone(0);
x = 0;
}
// qDebug() << Q_FUNC_INFO << x << text;
} while (x == 1);
emit btFinish();
}
void BackThread::execConnRememberedHiddenWifi(QString wifiName)
{
QProcess shellProcess;
shellProcess.start("nmcli -f ssid device wifi");
shellProcess.waitForFinished(3000); // 等待最多3s
if (shellProcess.exitCode() == 0)
{
QString shellOutput = shellProcess.readAllStandardOutput();
QStringList wlist = shellOutput.split("\n");
bool is_hidden = true;
foreach (QString wifi, wlist) {
if (wifi.trimmed() == wifiName) {
is_hidden = false;
}
}
if (! is_hidden) {
QString cmd = "nmcli connection up '" + wifiName + "'";
int res = Utils::m_system(cmd.toUtf8().data());
emit connDone(res);
} else {
//已保存的wifi没有在wifi列表找到隐藏wifi保存后也会出现在wifi列表则当前区域无法连接此wifi
syslog(LOG_DEBUG, "Choosen wifi can not be sacnned in finishedProcess() in dlghidewifiwpa.cpp 377.");
emit connDone(5);
}
}
emit btFinish();
}
void BackThread::execConnWifiPsk(QString cmd)
{
int res = Utils::m_system(cmd.toUtf8().data());

View File

@ -63,6 +63,8 @@ public slots:
void execReconnWIfi(QString uuid);
void execConnWifiPWD(QString connName, QString password, QString connType);
void execConnWifiPsk(QString cmd);
void execConnHiddenWifiWPA(QString connName, QString password);
void execConnRememberedHiddenWifi(QString connName);
void disConnSparedNetSlot(QString type);
void disConnLanOrWifi(QString type);

View File

@ -3310,7 +3310,7 @@ void MainWindow::connLanDone(int connFlag)
void MainWindow::connWifiDone(int connFlag)
{
// Wifi连接结果0点击连接成功 1失败 2没有配置文件 3开机启动网络工具时已经连接
// Wifi连接结果0点击连接成功 1失败 2没有配置文件 3开机启动网络工具时已经连接 4密码错误 5找不到已保存的网络
if (connFlag == 0) {
WifiAuthThread *wifi_auth_thread=new WifiAuthThread();
wifi_auth_thread->start();
@ -3331,7 +3331,12 @@ void MainWindow::connWifiDone(int connFlag)
is_connect_net_failed = 1;
QString txt(tr("Confirm your Wi-Fi password"));
objKyDBus->showDesktopNotify(txt);
} else if (connFlag == 5) {
is_connect_net_failed = 1;
QString txt(tr("Selected Wifi has not been scanned."));
objKyDBus->showDesktopNotify(txt);
}
this->stopLoading();
is_stop_check_net_state = 0;
}

Binary file not shown.

View File

@ -1257,11 +1257,6 @@
<source>Confirm your Wi-Fi password or usable of wireless card</source>
<translation type="unfinished">Wi-Fi密码或无线设备</translation>
</message>
<message>
<location filename="../wireless-security/dlghidewifiwpa.cpp" line="379"/>
<source>Selected Wifi has not been scanned.</source>
<translation type="unfinished">wifi</translation>
</message>
<message>
<source>WEP 40/128-bit Key (Hex or ASCII)</source>
<translation type="obsolete">WEP 40/128 (ASCII)</translation>
@ -1521,6 +1516,11 @@
<source>Confirm your Wi-Fi password</source>
<translation type="unfinished">Wi-Fi密码</translation>
</message>
<message>
<location filename="../src/mainwindow.cpp" line="3331"/>
<source>Selected Wifi has not been scanned.</source>
<translation type="unfinished">wifi</translation>
</message>
<message>
<location filename="../src/mainwindow.cpp" line="216"/>
<source>Ethernet Networks</source>

View File

@ -276,45 +276,31 @@ void DlgHideWifi::on_btnCancel_clicked()
void DlgHideWifi::on_btnConnect_clicked()
{
mw->is_stop_check_net_state = 1;
mw->is_connect_hide_wifi = 1;
QThread *t = new QThread();
connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, SIGNAL(started()), this, SLOT(slotStartLoading()));
connect(this, SIGNAL(stopSignal()), t, SLOT(quit()));
t->start();
QString wifiName = ui->leNetName->text();
BackThread *bt = new BackThread();
strWifiname = wifiName;
//点击连接按钮以连接隐藏WiFi
BackThread *bt = new BackThread();
if (isUsed == 0) {
int x = 0;
do {
sleep(1);
QString tmpPath = "/tmp/kylin-nm-btoutput-" + QDir::home().dirName();
QString cmd = "nmcli device wifi connect '" + wifiName + "' password '' hidden yes > " + tmpPath;
int status = system(cmd.toUtf8().data());
if (status != 0)
syslog(LOG_ERR, "execute 'nmcli device wifi connect' in function 'on_btnConnect_clicked' failed");
QFile file(tmpPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug()<<"Can't open the file!"<<endl;
}
QString text = file.readAll();
file.close();
if (text.indexOf("Scanning not allowed") != -1) {
x = 1;
bt->moveToThread(t);
connect(t, SIGNAL(started()), this, SLOT(slotStartConnectHiddenOpenWifi()));
connect(this, SIGNAL(sigConnHiddenWifi(QString, QString)), bt, SLOT(execConnHiddenWifiWPA(QString,QString)));
connect(bt, SIGNAL(connDone(int)), mw, SLOT(connWifiDone(int)));
connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
} else {
x = 0;
}
} while(x);
QTimer::singleShot(5*1000, this, SLOT(on_execSecConn() ));
} else {
bt->execConnWifi(wifiName);
QTimer::singleShot(6*1000, this, SLOT(emitSignal() ));
bt->moveToThread(t);
connect(t, SIGNAL(started()), this, SLOT(slotStartConnectRememberedHiddenWifi()));
connect(this, SIGNAL(sigConnRememberedHiddenWifi(QString)), bt, SLOT(execConnRememberedHiddenWifi(QString)));
connect(bt, SIGNAL(connDone(int)), mw, SLOT(connWifiDone(int)));
connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
}
t->start();
//this->close();
this->hide();
}
@ -338,6 +324,18 @@ void DlgHideWifi::slotStartLoading()
mw->startLoading();
}
void DlgHideWifi::slotStartConnectHiddenOpenWifi()
{
mw->startLoading();
emit sigConnHiddenWifi(ui->leNetName->text(), QString(""));
}
void DlgHideWifi::slotStartConnectRememberedHiddenWifi()
{
mw->startLoading();
emit sigConnRememberedHiddenWifi(ui->leNetName->text());
}
void DlgHideWifi::on_execSecConn()
{
QString str = "nmcli device wifi connect '" + strWifiname + "' password ''";

View File

@ -48,6 +48,8 @@ public slots:
void emitSignal();
void on_execSecConn();
void slotStartLoading();
void slotStartConnectHiddenOpenWifi();
void slotStartConnectRememberedHiddenWifi();
private slots:
void on_btnCancel_clicked();
@ -59,6 +61,8 @@ private slots:
signals:
void reSetWifiList();
void stopSignal();
void sigConnHiddenWifi(QString wifiName, QString wifiPasswd);
void sigConnRememberedHiddenWifi(QString wifiName);
private:
Ui::DlgHideWifi *ui;

View File

@ -292,84 +292,54 @@ void DlgHideWifiWpa::on_btnConnect_clicked()
QThread *t = new QThread();
connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
connect(t, SIGNAL(started()), this, SLOT(slotStartLoading()));
connect(this, SIGNAL(stopSignal()), t, SLOT(quit()));
t->start();
QString wifiName = ui->leNetName->text();
QString wifiPassword = ui->lePassword->text();
strWifiname = wifiName;
strWifiPassword = wifiPassword;
BackThread *bt = new BackThread();
if (isUsed == 0) {
QFuture < void > future1 = QtConcurrent::run([=](){
int x(1), n(0);
do {
sleep(2);
n += 1;
if (n >= 20) {
x = 0;
mw->is_connect_hide_wifi = 0;
}
QString tmpPath = "/tmp/kylin-nm-btoutput-" + QDir::home().dirName();
QString cmd = "nmcli device wifi connect '" + wifiName + "' password '" + wifiPassword + "' hidden yes > " + tmpPath + " 2>&1";
// qDebug() << Q_FUNC_INFO << cmd << tmpPath;
int status = system(cmd.toUtf8().data());
if (status != 0) {
syslog(LOG_ERR, "execute 'nmcli device wifi connect' in function 'on_btnConnect_clicked' failed");
}
QFile file(tmpPath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug()<<"Can't open the file!"<<endl;
}
QString text = file.readAll();
file.close();
if(text.indexOf("Scanning not allowed") != -1 || text.isEmpty()){x = 1;} else { x = 0;}
// qDebug() << Q_FUNC_INFO << x << text;
} while (x == 1);
//屏蔽下面两行可以大大减小崩溃的概率
// emit reSetWifiList();
// emit stopSignal();
});
bt->moveToThread(t);
connect(t, SIGNAL(started()), this, SLOT(slotStartConnectHiddenWifi()));
connect(this, SIGNAL(sigConnHiddenWifi(QString, QString)), bt, SLOT(execConnHiddenWifiWPA(QString,QString)));
connect(bt, SIGNAL(connDone(int)), mw, SLOT(connWifiDone(int)));
connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
} else {
shellProcess = new QProcess(this);
shellProcess->start("nmcli -f ssid device wifi");
connect(shellProcess, &QProcess::readyRead, [ = ]() {
QString output = shellProcess->readAll();
shellOutput += output;
});
connect(shellProcess, SIGNAL(finished(int)), this, SLOT(finishedProcess(int)));
bt->moveToThread(t);
connect(t, SIGNAL(started()), this, SLOT(slotStartConnectRememberedHiddenWifi()));
connect(this, SIGNAL(sigConnRememberedHiddenWifi(QString)), bt, SLOT(execConnRememberedHiddenWifi(QString)));
connect(bt, SIGNAL(connDone(int)), mw, SLOT(connWifiDone(int)));
connect(bt, SIGNAL(btFinish()), t, SLOT(quit()));
}
t->start();
this->close();
}
void DlgHideWifiWpa::finishedProcess(int state) {
if (! state) syslog(LOG_ERR, "Scan wifi list failed in functin on_btnConnect_clicked() in dlghidewifiwpa.cpp 359.");
wlist = shellOutput.split("\n");
bool is_hidden = true;
foreach (QString wifi, wlist) {
if (wifi.trimmed() == ui->leNetName->text()) {
is_hidden = false;
}
}
if (! is_hidden) {
BackThread *bt = new BackThread();
bt->execConnWifi(ui->leNetName->text());
QTimer::singleShot(4*1000, this, SLOT(emitSignal() ));
} else {
//已保存的wifi没有在wifi列表找到隐藏wifi保存后也会出现在wifi列表则当前区域无法连接此wifi
syslog(LOG_DEBUG, "Choosen wifi can not be sacnned in finishedProcess() in dlghidewifiwpa.cpp 377.");
QString txt(tr("Selected Wifi has not been scanned."));
mw->objKyDBus->showDesktopNotify(txt);
emit this->stopSignal();
emit reSetWifiList();
}
shellProcess->deleteLater();
}
//void DlgHideWifiWpa::finishedProcess(int state) {
// if (! state) syslog(LOG_ERR, "Scan wifi list failed in functin on_btnConnect_clicked() in dlghidewifiwpa.cpp 359.");
// wlist = shellOutput.split("\n");
// bool is_hidden = true;
// foreach (QString wifi, wlist) {
// if (wifi.trimmed() == ui->leNetName->text()) {
// is_hidden = false;
// }
// }
// if (! is_hidden) {
// BackThread *bt = new BackThread();
// bt->execConnWifi(ui->leNetName->text());
// QTimer::singleShot(4*1000, this, SLOT(emitSignal() ));
// } else {
// //已保存的wifi没有在wifi列表找到隐藏wifi保存后也会出现在wifi列表则当前区域无法连接此wifi
// syslog(LOG_DEBUG, "Choosen wifi can not be sacnned in finishedProcess() in dlghidewifiwpa.cpp 377.");
// QString txt(tr("Selected Wifi has not been scanned."));
// mw->objKyDBus->showDesktopNotify(txt);
// emit this->stopSignal();
// emit reSetWifiList();
// }
// shellProcess->deleteLater();
//}
void DlgHideWifiWpa::on_checkBoxPwd_stateChanged(int arg1)
{
@ -403,9 +373,16 @@ void DlgHideWifiWpa::on_lePassword_textEdited(const QString &arg1)
}
}
void DlgHideWifiWpa::slotStartLoading()
void DlgHideWifiWpa::slotStartConnectHiddenWifi()
{
mw->startLoading();
emit sigConnHiddenWifi(ui->leNetName->text(), ui->lePassword->text());
}
void DlgHideWifiWpa::slotStartConnectRememberedHiddenWifi()
{
mw->startLoading();
emit sigConnRememberedHiddenWifi(ui->leNetName->text());
}
void DlgHideWifiWpa::on_execSecConn()

View File

@ -46,7 +46,8 @@ public slots:
void changeWindow();
void emitSignal();
void on_execSecConn();
void slotStartLoading();
void slotStartConnectHiddenWifi();
void slotStartConnectRememberedHiddenWifi();
private slots:
void on_btnCancel_clicked();
@ -59,12 +60,14 @@ private slots:
void on_lePassword_textEdited(const QString &arg1);
void finishedProcess(int res);
// void finishedProcess(int res);
signals:
void reSetWifiList();
void stopSignal();
void stopSignalAgain();
void sigConnHiddenWifi(QString wifiName, QString wifiPasswd);
void sigConnRememberedHiddenWifi(QString wifiName);
private:
Ui::DlgHideWifiWpa *ui;