|
10 | 10 | #include <QFile> |
11 | 11 | #include <QProcess> |
12 | 12 | #include <QCryptographicHash> |
| 13 | +#include <QRegularExpression> |
13 | 14 | #include <QDebug> |
14 | 15 |
|
15 | 16 | #include <net/if.h> |
@@ -140,17 +141,53 @@ void EnableUtils::disableInDevice() |
140 | 141 | bool EnableUtils::ioctlOperateNetworkLogicalName(const QString &logicalName, bool enable) |
141 | 142 | { |
142 | 143 | if (logicalName.startsWith("wlan") || logicalName.startsWith("wlp")) { // Wireless LAN |
143 | | - QString cmd = QString("rfkill %1 $(rfkill list | grep -A 2 \"phy$(iw dev %2 info 2>/dev/null | awk '/wiphy/{print $2}')\" | awk 'NR==1{print $1}' | tr -d ':')") |
144 | | - .arg(enable ? "unblock" : "block") |
145 | | - .arg(logicalName); |
146 | | - int ret = system(cmd.toStdString().c_str()); |
147 | | - if (ret != 0) { |
148 | | - qCritical() << "Failed to block/unblock wifi: " << " error code: " << ret ; |
| 144 | + // 第一步:获取 wiphy 编号 |
| 145 | + QProcess iwProcess; |
| 146 | + iwProcess.start("iw", QStringList() << "dev" << logicalName << "info"); |
| 147 | + iwProcess.waitForFinished(); |
| 148 | + QString iwOutput = QString::fromUtf8(iwProcess.readAllStandardOutput()); |
| 149 | + |
| 150 | + // 解析 wiphy 编号 |
| 151 | + QRegularExpression wiphyRe("wiphy\\s+(\\d+)"); |
| 152 | + QRegularExpressionMatch wiphyMatch = wiphyRe.match(iwOutput); |
| 153 | + if (!wiphyMatch.hasMatch()) { |
| 154 | + qCritical() << "Failed to get wiphy number for interface: " << logicalName; |
| 155 | + return false; |
149 | 156 | } |
150 | | - cmd = QString("/sbin/ifconfig %1 %2").arg(logicalName).arg(enable ? "up" : "down"); |
151 | | - ret = system(cmd.toStdString().c_str()); |
| 157 | + QString phyNum = wiphyMatch.captured(1); |
| 158 | + |
| 159 | + // 第二步:获取 rfkill 设备编号 |
| 160 | + QProcess rfkillListProcess; |
| 161 | + rfkillListProcess.start("rfkill", QStringList() << "list"); |
| 162 | + rfkillListProcess.waitForFinished(); |
| 163 | + QString rfkillOutput = QString::fromUtf8(rfkillListProcess.readAllStandardOutput()); |
| 164 | + |
| 165 | + // 查找对应的 rfkill 编号 |
| 166 | + QRegularExpression rfkillRe("^(\\d+):.*\\n.*\\n.*phy" + phyNum); |
| 167 | + QRegularExpressionMatch rfkillMatch = rfkillRe.match(rfkillOutput); |
| 168 | + QString rfkillId; |
| 169 | + if (rfkillMatch.hasMatch()) { |
| 170 | + rfkillId = rfkillMatch.captured(1); |
| 171 | + } |
| 172 | + |
| 173 | + // 第三步:执行 rfkill block/unblock |
| 174 | + if (!rfkillId.isEmpty()) { |
| 175 | + QProcess rfkillBlockProcess; |
| 176 | + rfkillBlockProcess.start("rfkill", QStringList() << (enable ? "unblock" : "block") << rfkillId); |
| 177 | + rfkillBlockProcess.waitForFinished(); |
| 178 | + int ret = rfkillBlockProcess.exitCode(); |
| 179 | + if (ret != 0) { |
| 180 | + qCritical() << "Failed to block/unblock wifi: error code: " << ret; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + // 第四步:执行 ifconfig up/down |
| 185 | + QProcess ifconfigProcess; |
| 186 | + ifconfigProcess.start("/sbin/ifconfig", QStringList() << logicalName << (enable ? "up" : "down")); |
| 187 | + ifconfigProcess.waitForFinished(); |
| 188 | + int ret = ifconfigProcess.exitCode(); |
152 | 189 | if (ret != 0) { |
153 | | - qCritical() << "Failed to up/down network: " << logicalName << enable << " error code: " << ret ; |
| 190 | + qCritical() << "Failed to up/down network: " << logicalName << enable << " error code: " << ret; |
154 | 191 | return false; |
155 | 192 | } |
156 | 193 | } else { |
|
0 commit comments