perf: 改用dbus提权检测IP冲突

This commit is contained in:
zhangyuanyuan1 2023-08-02 11:48:05 +08:00
parent 4cf54f01df
commit 71bed0b9e0
16 changed files with 270 additions and 53 deletions

View File

@ -1,14 +0,0 @@
#!/bin/sh
set -e
PROGRAM=$(dpkg-divert --truename /usr/bin/kylin-nm)
if setcap cap_net_raw+ep $PROGRAM; then
chmod u-s $PROGRAM
fi
echo "kylin nm set cap success"
exit 0

View File

@ -8,6 +8,7 @@ CONFIG += \
SUBDIRS = \
plugins/plugin.pro \
src-vpn/src-vpn.pro \
src
src \
sys-dbus-register \
QT += widgets

View File

@ -4,17 +4,12 @@ include(dbus-interface/dbus-interface.pri)
HEADERS += \
$$PWD/dbusadaptor.h \
$$PWD/kylinarping.h \
$$PWD/kylinipv4arping.h \
$$PWD/kylinipv6arping.h \
$$PWD/sysdbusregister.h \
$$PWD/utils.h \
$$PWD/wifi-auth-thread.h
SOURCES += \
$$PWD/dbusadaptor.cpp \
$$PWD/kylinipv4arping.cpp \
$$PWD/kylinipv6arping.cpp \
$$PWD/sysdbusregister.cpp \
$$PWD/utils.cpp \
$$PWD/wifi-auth-thread.cpp

View File

@ -18,8 +18,6 @@
*
*/
#include "netdetail.h"
#include "backend/kylinipv4arping.h"
#include "backend/kylinipv6arping.h"
//#include "xatom/xatom-helper.h"
#define THEME_SCHAME "org.ukui.style"
@ -57,6 +55,9 @@
#define PEAP_SCRO_HEIGHT 300
#define TLS_SCRO_HEIGHT 480
#define MAX_TAB_TEXT_LENGTH 44
#define SYSTEM_DBUS_SERVICE "com.kylin.network.qt.systemdbus"
#define SYSTEM_DBUS_PATH "/"
#define SYSTEM_DBUS_INTERFACE "com.kylin.network.interface"
//extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed);
@ -1268,33 +1269,37 @@ void ThreadObject::checkIpv4ConflictThread(const QString &ipv4Address)
return;
}
bool isConflict = false;
KyIpv4Arping* ipv4Arping = new KyIpv4Arping(m_devName, ipv4Address);
if (ipv4Arping->ipv4ConflictCheck() >= 0) {
isConflict = ipv4Arping->ipv4IsConflict();
if (isConflict) {
QString mac = ipv4Arping->getMacAddress();
qDebug() << "conflict mac" << mac;
QDBusInterface dbusInterface(SYSTEM_DBUS_SERVICE,
SYSTEM_DBUS_PATH,
SYSTEM_DBUS_INTERFACE,
QDBusConnection::systemBus());
if(!dbusInterface.isValid()) {
qWarning ()<< "check IPv4 conflict failed, init kylin.network.qt.systemdbus error";
Q_EMIT ipv4IsConflict(isConflict);
return;
}
KyNetworkDeviceResourse resource;
QStringList devList,devList1,devList2;
QStringList devList, devList1, devList2, macList;
resource.getNetworkDeviceList(NetworkManager::Device::Type::Ethernet, devList1);
resource.getNetworkDeviceList(NetworkManager::Device::Type::Wifi, devList2);
devList << devList1 << devList2;
for(int i = 0; i < devList.size(); ++i){
for (int i = 0; i < devList.size(); ++i) {
QString hardAddress;
int band;
resource.getHardwareInfo(devList.at(i), hardAddress, band);
if (hardAddress == mac) {
qDebug() << "conflict local card" << devList.at(i);
isConflict = false;
}
}
}
} else {
qWarning() << "checkIpv4Conflict internal error";
macList << hardAddress;
}
QDBusReply <bool> reply = dbusInterface.call("checkIpv4IsConflict", m_devName, ipv4Address, macList);
if (reply.isValid()) {
isConflict = reply.value();
} else {
qWarning () << "check IPv4 conflict failed, dbus reply invalid";
}
delete ipv4Arping;
ipv4Arping = nullptr;
Q_EMIT ipv4IsConflict(isConflict);
}
@ -1304,14 +1309,21 @@ void ThreadObject::checkIpv6ConflictThread(const QString &ipv6Address)
return;
}
bool isConflict = false;
KyIpv6Arping* ipv6rping = new KyIpv6Arping(m_devName, ipv6Address);
if (ipv6rping->ipv6ConflictCheck() >= 0) {
isConflict = ipv6rping->ipv6IsConflict();
QDBusInterface dbusInterface(SYSTEM_DBUS_SERVICE,
SYSTEM_DBUS_PATH,
SYSTEM_DBUS_INTERFACE,
QDBusConnection::systemBus());
if(!dbusInterface.isValid()) {
qWarning () << "check IPv6 conflict failed, init kylin.network.qt.systemdbus error";
} else {
qWarning() << "checkIpv6Conflict internal error";
QDBusReply <bool> reply = dbusInterface.call("checkIpv6IsConflict", m_devName, ipv6Address);
if (reply.isValid()) {
isConflict = reply.value();
} else {
qWarning () << "check IPv6 conflict failed, dbus reply invalid";
}
}
delete ipv6rping;
ipv6rping = nullptr;
Q_EMIT ipv6IsConflict(isConflict);
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <!-- -*- XML -*- -->
<!DOCTYPE busconfig PUBLIC
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<!-- Only root can own the service -->
<policy user="root">
<allow own="com.kylin.network.qt.systemdbus"/>
<allow send_interface="com.kylin.network.interface"/>
</policy>
<!-- Allow anyone to invoke methods on the interfaces -->
<policy context="default">
<allow send_destination="com.kylin.network.qt.systemdbus"
send_interface="com.kylin.network.interface"/>
<allow send_destination="com.kylin.network.qt.systemdbus"
send_interface="org.freedesktop.DBus.Introspectable"/>
<allow send_destination="com.kylin.network.qt.systemdbus"
send_interface="org.freedesktop.DBus.Properties"/>
</policy>
</busconfig>

View File

@ -0,0 +1,4 @@
[D-BUS Service]
Name=com.kylin.network.qt.systemdbus
Exec=/usr/bin/kylin-nm-sysdbus
User=root

View File

@ -0,0 +1,12 @@
INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/kylinarping.h \
$$PWD/kylinipv4arping.h \
$$PWD/kylinipv6arping.h \
SOURCES += \
$$PWD/kylinipv4arping.cpp \
$$PWD/kylinipv6arping.cpp \
DISTFILES +=

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "kynmsystemdbus.h"
#include "kylinipv4arping.h"
#include "kylinipv6arping.h"
#define LOG_FLAG "[KynmSystemDbus]"
KynmSystemDbus::KynmSystemDbus(QObject *parent) : QObject(parent)
{
}
KynmSystemDbus::~KynmSystemDbus()
{
}
bool KynmSystemDbus::checkIpv4IsConflict(const QString devName, const QString ipv4Address, QStringList macList)
{
bool isConflict = false;
KyIpv4Arping* ipv4Arping = new KyIpv4Arping(devName, ipv4Address);
if (ipv4Arping->ipv4ConflictCheck() >= 0) {
isConflict = ipv4Arping->ipv4IsConflict();
if (isConflict && !macList.isEmpty()) {
QString macAddress = ipv4Arping->getMacAddress();
for (const auto mac : macList) {
if (macAddress == mac) {
qDebug() << LOG_FLAG << "IPv4 conflict mac" << mac;
isConflict = false;
break;
}
}
}
} else {
qWarning() << LOG_FLAG << "checkIpv4Conflict internal error";
}
delete ipv4Arping;
ipv4Arping = nullptr;
return isConflict;
}
bool KynmSystemDbus::checkIpv6IsConflict(const QString devName, const QString ipv6Address)
{
bool isConflict = false;
KyIpv6Arping* ipv6rping = new KyIpv6Arping(devName, ipv6Address);
if (ipv6rping->ipv6ConflictCheck() >= 0) {
isConflict = ipv6rping->ipv6IsConflict();
} else {
qWarning() << LOG_FLAG << "checkIpv6Conflict internal error";
}
delete ipv6rping;
ipv6rping = nullptr;
return isConflict;
}

View File

@ -0,0 +1,38 @@
/*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef KYNMSYSTEMDBUS_H
#define KYNMSYSTEMDBUS_H
#include <QObject>
class KynmSystemDbus : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "com.kylin.network.interface")
public:
explicit KynmSystemDbus(QObject *parent = nullptr);
~KynmSystemDbus();
public Q_SLOTS:
Q_SCRIPTABLE bool checkIpv4IsConflict(const QString devName, const QString ipv4Address, QStringList macList);
Q_SCRIPTABLE bool checkIpv6IsConflict(const QString devName, const QString ipv6Address);
};
#endif // KYNMSYSTEMDBUS_H

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2023, KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "kynmsystemdbus.h"
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusError>
#include <QDebug>
int main(int argc, char *argv[]){
QCoreApplication app(argc, argv);
QDBusConnection systemBus = QDBusConnection::systemBus();
if (!systemBus.registerService("com.kylin.network.qt.systemdbus")){
qCritical() << "QDbus register service failed reason:" << systemBus.lastError();
exit(1);
}
if (!systemBus.registerObject("/", new KynmSystemDbus(), QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals)){
qCritical() << "QDbus register object failed reason:" << systemBus.lastError();
exit(2);
}
return app.exec();
}

View File

@ -0,0 +1,31 @@
QT += dbus network
TARGET = kylin-nm-sysdbus
TEMPLATE = app
CONFIG += c++14 qt warn_on link_pkgconfig no_keywords
CONFIG -= app_bundle
TARGET = kylin-nm-sysdbus
TEMPLATE = app
PKGCONFIG +=gio-2.0 glib-2.0 gio-unix-2.0 libcap
inst1.files += conf/com.kylin.network.qt.systemdbus.service
inst1.path = /usr/share/dbus-1/system-services/
inst2.files += conf/com.kylin.network.qt.systemdbus.conf
inst2.path = /etc/dbus-1/system.d/
target.source += $$TARGET
target.path = /usr/bin
INSTALLS += \
target \
inst1 \
inst2 \
include(kyarping/kyarping.pri)
SOURCES += \
kynmsystemdbus.cpp \
main.cpp
HEADERS += \
kynmsystemdbus.h