From 71bed0b9e03c70886f6d5fe5a3f2b90a7153f83d Mon Sep 17 00:00:00 2001 From: zhangyuanyuan1 Date: Wed, 2 Aug 2023 11:48:05 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=94=B9=E7=94=A8dbus=E6=8F=90?= =?UTF-8?q?=E6=9D=83=E6=A3=80=E6=B5=8BIP=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debian/kylin-nm.postinst | 14 ---- kylin-nm.pro | 3 +- src/backend/backend.pri | 5 -- src/frontend/netdetails/netdetail.cpp | 78 +++++++++++-------- .../conf/com.kylin.network.qt.systemdbus.conf | 22 ++++++ .../com.kylin.network.qt.systemdbus.service | 4 + sys-dbus-register/kyarping/kyarping.pri | 12 +++ .../kyarping}/kylinarping.h | 0 .../kyarping}/kylinipv4arping.cpp | 0 .../kyarping}/kylinipv4arping.h | 0 .../kyarping}/kylinipv6arping.cpp | 0 .../kyarping}/kylinipv6arping.h | 0 sys-dbus-register/kynmsystemdbus.cpp | 75 ++++++++++++++++++ sys-dbus-register/kynmsystemdbus.h | 38 +++++++++ sys-dbus-register/main.cpp | 41 ++++++++++ sys-dbus-register/sys-dbus-register.pro | 31 ++++++++ 16 files changed, 270 insertions(+), 53 deletions(-) delete mode 100755 debian/kylin-nm.postinst create mode 100644 sys-dbus-register/conf/com.kylin.network.qt.systemdbus.conf create mode 100644 sys-dbus-register/conf/com.kylin.network.qt.systemdbus.service create mode 100644 sys-dbus-register/kyarping/kyarping.pri rename {src/backend => sys-dbus-register/kyarping}/kylinarping.h (100%) rename {src/backend => sys-dbus-register/kyarping}/kylinipv4arping.cpp (100%) rename {src/backend => sys-dbus-register/kyarping}/kylinipv4arping.h (100%) rename {src/backend => sys-dbus-register/kyarping}/kylinipv6arping.cpp (100%) rename {src/backend => sys-dbus-register/kyarping}/kylinipv6arping.h (100%) create mode 100644 sys-dbus-register/kynmsystemdbus.cpp create mode 100644 sys-dbus-register/kynmsystemdbus.h create mode 100644 sys-dbus-register/main.cpp create mode 100644 sys-dbus-register/sys-dbus-register.pro diff --git a/debian/kylin-nm.postinst b/debian/kylin-nm.postinst deleted file mode 100755 index e99563ba..00000000 --- a/debian/kylin-nm.postinst +++ /dev/null @@ -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 - diff --git a/kylin-nm.pro b/kylin-nm.pro index d51ab8c6..21f0d085 100644 --- a/kylin-nm.pro +++ b/kylin-nm.pro @@ -8,6 +8,7 @@ CONFIG += \ SUBDIRS = \ plugins/plugin.pro \ src-vpn/src-vpn.pro \ - src + src \ + sys-dbus-register \ QT += widgets diff --git a/src/backend/backend.pri b/src/backend/backend.pri index feb4853f..0d383624 100644 --- a/src/backend/backend.pri +++ b/src/backend/backend.pri @@ -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 diff --git a/src/frontend/netdetails/netdetail.cpp b/src/frontend/netdetails/netdetail.cpp index 8db86748..3898bf52 100644 --- a/src/frontend/netdetails/netdetail.cpp +++ b/src/frontend/netdetails/netdetail.cpp @@ -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; - KyNetworkDeviceResourse resource; - QStringList devList,devList1,devList2; - 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){ - 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"; + + 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, 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) { + QString hardAddress; + int band; + resource.getHardwareInfo(devList.at(i), hardAddress, band); + macList << hardAddress; + } + + QDBusReply 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 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); } diff --git a/sys-dbus-register/conf/com.kylin.network.qt.systemdbus.conf b/sys-dbus-register/conf/com.kylin.network.qt.systemdbus.conf new file mode 100644 index 00000000..4a3e4887 --- /dev/null +++ b/sys-dbus-register/conf/com.kylin.network.qt.systemdbus.conf @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + diff --git a/sys-dbus-register/conf/com.kylin.network.qt.systemdbus.service b/sys-dbus-register/conf/com.kylin.network.qt.systemdbus.service new file mode 100644 index 00000000..297fe77c --- /dev/null +++ b/sys-dbus-register/conf/com.kylin.network.qt.systemdbus.service @@ -0,0 +1,4 @@ +[D-BUS Service] +Name=com.kylin.network.qt.systemdbus +Exec=/usr/bin/kylin-nm-sysdbus +User=root diff --git a/sys-dbus-register/kyarping/kyarping.pri b/sys-dbus-register/kyarping/kyarping.pri new file mode 100644 index 00000000..7d4372ca --- /dev/null +++ b/sys-dbus-register/kyarping/kyarping.pri @@ -0,0 +1,12 @@ +INCLUDEPATH += $$PWD + +HEADERS += \ + $$PWD/kylinarping.h \ + $$PWD/kylinipv4arping.h \ + $$PWD/kylinipv6arping.h \ + +SOURCES += \ + $$PWD/kylinipv4arping.cpp \ + $$PWD/kylinipv6arping.cpp \ + +DISTFILES += diff --git a/src/backend/kylinarping.h b/sys-dbus-register/kyarping/kylinarping.h similarity index 100% rename from src/backend/kylinarping.h rename to sys-dbus-register/kyarping/kylinarping.h diff --git a/src/backend/kylinipv4arping.cpp b/sys-dbus-register/kyarping/kylinipv4arping.cpp similarity index 100% rename from src/backend/kylinipv4arping.cpp rename to sys-dbus-register/kyarping/kylinipv4arping.cpp diff --git a/src/backend/kylinipv4arping.h b/sys-dbus-register/kyarping/kylinipv4arping.h similarity index 100% rename from src/backend/kylinipv4arping.h rename to sys-dbus-register/kyarping/kylinipv4arping.h diff --git a/src/backend/kylinipv6arping.cpp b/sys-dbus-register/kyarping/kylinipv6arping.cpp similarity index 100% rename from src/backend/kylinipv6arping.cpp rename to sys-dbus-register/kyarping/kylinipv6arping.cpp diff --git a/src/backend/kylinipv6arping.h b/sys-dbus-register/kyarping/kylinipv6arping.h similarity index 100% rename from src/backend/kylinipv6arping.h rename to sys-dbus-register/kyarping/kylinipv6arping.h diff --git a/sys-dbus-register/kynmsystemdbus.cpp b/sys-dbus-register/kynmsystemdbus.cpp new file mode 100644 index 00000000..1057f1e5 --- /dev/null +++ b/sys-dbus-register/kynmsystemdbus.cpp @@ -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 . + * + */ + +#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; +} diff --git a/sys-dbus-register/kynmsystemdbus.h b/sys-dbus-register/kynmsystemdbus.h new file mode 100644 index 00000000..d786bdde --- /dev/null +++ b/sys-dbus-register/kynmsystemdbus.h @@ -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 . + * + */ + +#ifndef KYNMSYSTEMDBUS_H +#define KYNMSYSTEMDBUS_H + +#include + +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 diff --git a/sys-dbus-register/main.cpp b/sys-dbus-register/main.cpp new file mode 100644 index 00000000..a725bee3 --- /dev/null +++ b/sys-dbus-register/main.cpp @@ -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 . + * + */ + +#include "kynmsystemdbus.h" +#include +#include +#include +#include + +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(); +} diff --git a/sys-dbus-register/sys-dbus-register.pro b/sys-dbus-register/sys-dbus-register.pro new file mode 100644 index 00000000..40d22c73 --- /dev/null +++ b/sys-dbus-register/sys-dbus-register.pro @@ -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