实现防火墙网络模式设置

This commit is contained in:
zhangyuanyuan1 2023-03-06 10:48:08 +08:00
parent fba0df917c
commit 4c8b7a37c3
5 changed files with 556 additions and 0 deletions

View File

@ -0,0 +1,165 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2022 Tianjin KYLIN Information Technology 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "firewalldialog.h"
#include <QApplication>
#define THEME_SCHAME "org.ukui.style"
#define COLOR_THEME "styleName"
#define ICON_SIZE 16,16
FixLabel::FixLabel(QWidget *parent) :
QLabel(parent)
{
const QByteArray id("org.ukui.style");
QGSettings * fontSetting = new QGSettings(id, QByteArray(), this);
connect(fontSetting, &QGSettings::changed,[=](QString key) {
if ("systemFont" == key || "systemFontSize" ==key) {
changedLabelSlot();
}
});
}
void FixLabel::setLabelText(QString text) {
mStr = text;
changedLabelSlot();
}
QString FixLabel::getText(){
return mStr;
}
void FixLabel::changedLabelSlot() {
QFontMetrics fontMetrics(this->font());
int fontSize = fontMetrics.width(mStr);
if (fontSize > this->width()) {
setText(fontMetrics.elidedText(mStr, Qt::ElideRight, this->width()));
setToolTip(mStr);
} else {
setText(mStr);
setToolTip("");
}
}
FirewallDialog::FirewallDialog(QWidget *parent): KDialog(parent)
{
initUI();
this->setWindowIcon(QIcon::fromTheme("kylin-network"));
this->setFixedSize(480, 204);
setAttribute(Qt::WA_DeleteOnClose);
// centerToScreen();
connect(qApp, &QApplication::paletteChanged, this, &FirewallDialog::onPaletteChanged);
}
FirewallDialog::~FirewallDialog()
{
}
void FirewallDialog::initUI()
{
m_iconLabel = new QLabel(this);
m_contentLabel = new FixLabel(this);
m_suggestLabel = new FixLabel(this);
m_PublicBtn = new QPushButton(this);
m_PrivateBtn = new QPushButton(this);
m_dialogLayout = new QVBoxLayout(this);
m_contentLabel->setFixedWidth(405);
m_suggestLabel->setFixedWidth(405);
QWidget *contentWidget = new QWidget(this);
QGridLayout *contentLayout = new QGridLayout(contentWidget);
contentLayout->setHorizontalSpacing(0);
contentLayout->setContentsMargins(0, 0, 0, 0);
contentLayout->addWidget(m_iconLabel, 0, 0);
contentLayout->addWidget(m_contentLabel, 0, 1);
contentLayout->addWidget(m_suggestLabel, 1, 1);
m_iconLabel->setFixedWidth(24);
QWidget *btnWidget = new QWidget(this);
QHBoxLayout *btnLayout = new QHBoxLayout(btnWidget);
btnLayout->setContentsMargins(0, 0, 0, 0);
btnLayout->setSpacing(16);
btnLayout->addStretch();
btnLayout->addWidget(m_PublicBtn);
btnLayout->addWidget(m_PrivateBtn);
m_dialogLayout->setContentsMargins(22, 16, 22, 24);
m_dialogLayout->setSpacing(0);
m_dialogLayout->addWidget(contentWidget);
m_dialogLayout->addStretch();
m_dialogLayout->addWidget(btnWidget);
QIcon icon = QIcon::fromTheme("dialog-info");
m_iconLabel->setPixmap(icon.pixmap(ICON_SIZE));
QFont font = m_contentLabel->font();
font.setWeight(57);
m_contentLabel->setFont(font);
//是否允许此网络上的其他设备发现这台电脑?
m_contentLabel->setLabelText(tr("Allow other devices on this network to discover this computer?"));
//不建议在公共网络上开启此功能
m_suggestLabel->setLabelText(tr("It is not recommended to enable this feature on public networks"));
m_PublicBtn->setText(tr("Not allowed (recommended)"));
m_PrivateBtn->setText(tr("Allowed"));
this->closeButton();
this->mainWidget()->setLayout(m_dialogLayout);
onPaletteChanged();
connect(m_PublicBtn, &QPushButton::clicked, this, &FirewallDialog::setPublicNetMode);
connect(m_PrivateBtn, &QPushButton::clicked, this, &FirewallDialog::setPrivateNetMode);
}
void FirewallDialog::centerToScreen()
{
QDesktopWidget* m = QApplication::desktop();
QRect desk_rect = m->screenGeometry(m->screenNumber(QCursor::pos()));
int desk_x = desk_rect.width();
int desk_y = desk_rect.height();
int x = this->width();
int y = this->height();
this->move(desk_x / 2 - x / 2 + desk_rect.left(), desk_y / 2 - y / 2 + desk_rect.top());
}
void FirewallDialog::onPaletteChanged()
{
QPalette pal = qApp->palette();
// QGSettings * styleGsettings = nullptr;
// const QByteArray style_id(THEME_SCHAME);
// if (QGSettings::isSchemaInstalled(style_id)) {
// styleGsettings = new QGSettings(style_id);
// QString currentTheme = styleGsettings->get(COLOR_THEME).toString();
// if(currentTheme == "ukui-default"){
// pal = lightPalette(this);
// }
// }
this->setPalette(pal);
// if (styleGsettings != nullptr) {
// delete styleGsettings;
// styleGsettings = nullptr;
// }
}

View File

@ -0,0 +1,92 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2022 Tianjin KYLIN Information Technology 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef FIREWALLDIALOG_H
#define FIREWALLDIALOG_H
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QLabel>
#include <QDesktopWidget>
#include <QApplication>
#include "kwidget.h"
#include "kdialog.h"
using namespace kdk;
#include <QLabel>
class FixLabel : public QLabel
{
Q_OBJECT
public:
explicit FixLabel(QWidget *parent = 0);
public:
void setLabelText(QString text);
QString getText();
private Q_SLOTS:
void changedLabelSlot();
private:
QString mStr;
};
class FirewallDialog : public KDialog
{
Q_OBJECT
public:
FirewallDialog(QWidget *parent = nullptr);
~FirewallDialog();
void setUuid(QString uuid) {
m_uuid = uuid;
}
void centerToScreen();
private:
void initUI();
QString m_uuid;
QLabel * m_iconLabel = nullptr;
FixLabel * m_contentLabel = nullptr;
FixLabel * m_suggestLabel = nullptr;
QVBoxLayout *m_dialogLayout = nullptr;
QPushButton *m_PublicBtn = nullptr;
QPushButton *m_PrivateBtn = nullptr;
Q_SIGNALS:
void setPublicNetMode();
void setPrivateNetMode();
private Q_SLOTS:
void onPaletteChanged();
public Q_SLOTS:
void closeMyself(QString uuid, int status) {
if (uuid == m_uuid && status == 4) {
this->close();
}
}
};
#endif // FIREWALLDIALOG_H

View File

@ -0,0 +1,10 @@
INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/firewalldialog.h \
$$PWD/networkmodeconfig.h
SOURCES += \
$$PWD/firewalldialog.cpp \
$$PWD/networkmodeconfig.cpp

View File

@ -0,0 +1,226 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2022 Tianjin KYLIN Information Technology 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "networkmodeconfig.h"
#include "firewalldialog.h"
#include <QDebug>
#define LOG_FLAG "[NetworkModeConfig]"
NetworkModeConfig::NetworkModeConfig(QObject *parent)
:QObject(parent)
{
qRegisterMetaType<KyConnectState>("KyConnectState");
m_thread = new QThread;
m_manager = new KyNetworkManager();
m_manager->moveToThread(m_thread);
connect(m_thread, &QThread::started, m_manager, &KyNetworkManager::kylinNetworkManagerInit);
connect(m_thread, &QThread::finished, m_manager, &KyNetworkManager::deleteLater);
m_thread->start();
while (!m_manager->isInitFinished()) {
::usleep(1000);
}
//init dbus
m_firewallDbusIface = new QDBusInterface("com.ksc.defender",
"/firewall",
"com.ksc.defender.firewall",
QDBusConnection::systemBus());
if(!m_firewallDbusIface->isValid()) {
qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message());
}
//connect
connect(m_manager, &KyNetworkManager::wiredStateChange, this, [=](QString deviceName, QString uuid, KyConnectState status) {
KyConnectSetting connectSetting;
m_manager->getConnectIpInfo(uuid, connectSetting);
onActiveConnectionChanged(deviceName, connectSetting.m_connectName, uuid, status);
});
connect(m_manager, &KyNetworkManager::wirelessStateChange, this, &NetworkModeConfig::onActiveConnectionChanged);
}
void NetworkModeConfig::initWiredNetworkMode()
{
qDebug()<< LOG_FLAG << "initWiredNetworkMode";
QStringList wiredDevList;
m_manager->getNetworkDeviceList(DEVICE_TYPE_ETHERNET, wiredDevList);
if (wiredDevList.isEmpty()) {
return;
}
for (auto devName : wiredDevList) {
QList<KyActivateItem> activedList;
m_manager->getActiveConnectionList(devName, CONNECT_TYPE_WIRED, activedList);
if (activedList.isEmpty()) {
continue;
}
int configType = getNetworkModeConfig(activedList.at(0).m_uuid);
if (configType == DBUS_INVAILD) {
return;
} else if (configType == NO_CONFIG) {
//已连接网络无配置 默认公有配置
setNetworkModeConfig(activedList.at(0).m_uuid, devName,
activedList.at(0).m_connName, KSC_FIREWALL_PUBLIC);
} else {
setNetworkModeConfig(activedList.at(0).m_uuid, devName,
activedList.at(0).m_connName, configType);
}
}
}
void NetworkModeConfig::initWirelessNetworkMode()
{
qDebug()<< LOG_FLAG << "initWirelessNetworkMode";
QStringList wirelessDevList;
m_manager->getNetworkDeviceList(DEVICE_TYPE_WIFI, wirelessDevList);
if (wirelessDevList.isEmpty()) {
return;
}
for (auto devName : wirelessDevList) {
QList<KyActivateItem> activedList;
m_manager->getActiveConnectionList(devName, CONNECT_TYPE_WIRELESS, activedList);
if (activedList.isEmpty()) {
return;
}
int configType = getNetworkModeConfig(activedList.at(0).m_uuid);
if (configType == DBUS_INVAILD) {
return;
} else if (configType == NO_CONFIG) {
//已连接网络无配置 默认公有配置
setNetworkModeConfig(activedList.at(0).m_uuid, devName,
activedList.at(0).m_ssid, KSC_FIREWALL_PUBLIC);
} else {
setNetworkModeConfig(activedList.at(0).m_uuid, devName,
activedList.at(0).m_ssid, configType);
}
}
}
int NetworkModeConfig::getNetworkModeConfig(QString uuid)
{
if (uuid.isEmpty()) {
qWarning()<< /*LOG_FLAG <<*/ "uuid is empty, so can not get network mode config";
return NO_CONFIG;
}
if(m_firewallDbusIface == nullptr || !m_firewallDbusIface->isValid()) {
qWarning () << "com.ksc.defender dbus is invalid";
return DBUS_INVAILD;
}
QDBusReply<int> reply = m_firewallDbusIface->call("get_networkModeConfig", uuid);
if (reply.isValid()) {
return reply.value();
} else {
qWarning() << "call get_networkModeConfig failed" << reply.error().message();
}
return NO_CONFIG;
}
void NetworkModeConfig::setNetworkModeConfig(QString uuid, QString cardName, QString ssid, int mode)
{
if(m_firewallDbusIface == nullptr || !m_firewallDbusIface->isValid()) {
qWarning () << "com.ksc.defender dbus is invalid";
return;
}
QDBusReply<int> reply = m_firewallDbusIface->call("set_networkModeConfig", uuid, cardName, ssid, mode);
if (reply.isValid()) {
qDebug() << "set_networkModeConfig" << ssid << uuid << cardName << mode << ",result" << reply.value();
} else {
qWarning() << "call set_networkModeConfig" << reply.error().message();
}
}
int NetworkModeConfig::breakNetworkConnect(QString uuid, QString cardName, QString ssid)
{
if(m_firewallDbusIface == nullptr || !m_firewallDbusIface->isValid()) {
qWarning () << "com.ksc.defender dbus is invalid";
return -1;
}
QDBusReply<int> reply = m_firewallDbusIface->call("break_networkConnect", uuid, cardName, ssid);
if (reply.isValid()) {
qDebug() << "break_networkConnect" << ssid << uuid << cardName << ",result" << reply.value();
return reply.value();
} else {
qWarning() << "call break_networkConnect failed" << reply.error().message();
return -1;
}
}
void NetworkModeConfig::setFirstConnectNetworkMode(QString uuid, QString deviceName, QString ssid)
{
setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PUBLIC); //默认公有配置
FirewallDialog *fireWallDialog = new FirewallDialog();
fireWallDialog->setUuid(uuid);
fireWallDialog->setWindowTitle(ssid);
connect(fireWallDialog, &FirewallDialog::setPrivateNetMode, this, [=](){
fireWallDialog->hide();
setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PRIVATE);
});
connect(fireWallDialog, &FirewallDialog::setPublicNetMode, this, [=](){
fireWallDialog->hide();
setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PUBLIC);
});
connect(m_manager, &KyNetworkManager::wiredStateChange, this, [=](QString devName, QString connUuid, KyConnectState state) {
Q_UNUSED(devName)
fireWallDialog->closeMyself(connUuid, state);
});
connect(m_manager, &KyNetworkManager::wirelessStateChange, this, [=](QString devName,
QString connsSsid, QString connUuid, KyConnectState state) {
Q_UNUSED(devName)
Q_UNUSED(connsSsid)
fireWallDialog->closeMyself(connUuid, state);
});
fireWallDialog->show();
fireWallDialog->centerToScreen();
}
void NetworkModeConfig::onActiveConnectionChanged(QString deviceName, QString ssid, QString uuid, KyConnectState status)
{
qDebug()<< LOG_FLAG << "onActiveConnectionChanged" << deviceName << ssid << uuid << status;
if (ssid.isEmpty()) {
breakNetworkConnect(uuid, deviceName, ssid);
return;
}
if (status == CONNECT_STATE_ACTIVATED) {
int configType = getNetworkModeConfig(uuid);
if (configType == NO_CONFIG) {
setFirstConnectNetworkMode(uuid, deviceName, ssid);
} else if (configType == KSC_FIREWALL_PUBLIC) {
setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PUBLIC);
} else if (configType == KSC_FIREWALL_PRIVATE) {
setNetworkModeConfig(uuid, deviceName, ssid, KSC_FIREWALL_PRIVATE);
}
} else if (status == CONNECT_STATE_DEACTIVATED) {
breakNetworkConnect(uuid, deviceName, ssid);
}
}

View File

@ -0,0 +1,63 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* Copyright (C) 2022 Tianjin KYLIN Information Technology 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef NETWORKMODECONFIG_H
#define NETWORKMODECONFIG_H
#include <QObject>
#include <QThread>
#include <QDBusInterface>
#include <QDBusReply>
#include <kylin-nm/kylinnetworkmanager.h>
enum NetworkModeType {
DBUS_INVAILD = -2,
NO_CONFIG = -1,
KSC_FIREWALL_PUBLIC = 0,
KSC_FIREWALL_PRIVATE
};
class NetworkModeConfig : public QObject
{
Q_OBJECT
public:
NetworkModeConfig(QObject *parent = 0);
~NetworkModeConfig() = default;
void initWiredNetworkMode();
void initWirelessNetworkMode();
private:
KyNetworkManager* m_manager;
QThread* m_thread;
QDBusInterface *m_firewallDbusIface = nullptr;
//安全中心-获取网络模式配置
int getNetworkModeConfig(QString uuid);
//安全中心-设置网络模式配置
void setNetworkModeConfig(QString uuid, QString cardName, QString ssid, int mode);
//安全中心-解除连接(用于防火墙处从正在使用的网络中删除)
int breakNetworkConnect(QString uuid, QString cardName, QString ssid);
void setFirstConnectNetworkMode(QString uuid, QString deviceName, QString ssid);
private Q_SLOTS:
void onActiveConnectionChanged(QString deviceName, QString ssid, QString uuid, KyConnectState status);
};
#endif // NETWORKMODECONFIG_H