7#include "wireguardsetting.h"
8#include "wireguardsetting_p.h"
12#if !NM_CHECK_VERSION(1, 16, 0)
13#define NM_SETTING_WIREGUARD_SETTING_NAME "wireguard"
15#define NM_SETTING_WIREGUARD_FWMARK "fwmark"
16#define NM_SETTING_WIREGUARD_LISTEN_PORT "listen-port"
17#define NM_SETTING_WIREGUARD_PRIVATE_KEY "private-key"
18#define NM_SETTING_WIREGUARD_PRIVATE_KEY_FLAGS "private-key-flags"
19#define NM_SETTING_WIREGUARD_PEERS "peers"
20#define NM_SETTING_WIREGUARD_MTU "mtu"
21#define NM_SETTING_WIREGUARD_PEER_ROUTES "peer-routes"
23#define NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY "preshared-key"
24#define NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY_FLAGS "preshared-key-flags"
25#define NM_WIREGUARD_PEER_ATTR_PUBLIC_KEY "public-key"
28NetworkManager::WireGuardSettingPrivate::WireGuardSettingPrivate()
29 : name(NM_SETTING_WIREGUARD_SETTING_NAME)
38NetworkManager::WireGuardSetting::WireGuardSetting()
39 : Setting(Setting::WireGuard)
40 , d_ptr(new WireGuardSettingPrivate())
44NetworkManager::WireGuardSetting::WireGuardSetting(
const Ptr &other)
46 , d_ptr(new WireGuardSettingPrivate())
48 setFwmark(other->fwmark());
49 setListenPort(other->listenPort());
51 setPeerRoutes(other->peerRoutes());
52 setPeers(other->peers());
53 setPrivateKey(other->privateKey());
54 setPrivateKeyFlags(other->privateKeyFlags());
57NetworkManager::WireGuardSetting::~WireGuardSetting()
69quint32 NetworkManager::WireGuardSetting::fwmark()
const
76void NetworkManager::WireGuardSetting::setFwmark(quint32 fwmark)
78 Q_D(WireGuardSetting);
83quint32 NetworkManager::WireGuardSetting::listenPort()
const
85 Q_D(
const WireGuardSetting);
90void NetworkManager::WireGuardSetting::setListenPort(quint32 port)
92 Q_D(WireGuardSetting);
97quint32 NetworkManager::WireGuardSetting::mtu()
const
99 Q_D(
const WireGuardSetting);
104void NetworkManager::WireGuardSetting::setMtu(quint32 mtu)
106 Q_D(WireGuardSetting);
111bool NetworkManager::WireGuardSetting::peerRoutes()
const
113 Q_D(
const WireGuardSetting);
115 return d->peerRoutes;
118void NetworkManager::WireGuardSetting::setPeerRoutes(
bool peerRoutes)
120 Q_D(WireGuardSetting);
122 d->peerRoutes = peerRoutes;
127 Q_D(
const WireGuardSetting);
132void NetworkManager::WireGuardSetting::setPeers(
const NMVariantMapList &peers)
134 Q_D(WireGuardSetting);
139QString NetworkManager::WireGuardSetting::privateKey()
const
141 Q_D(
const WireGuardSetting);
143 return d->privateKey;
146void NetworkManager::WireGuardSetting::setPrivateKey(
const QString &key)
148 Q_D(WireGuardSetting);
155 Q_D(
const WireGuardSetting);
157 return d->privateKeyFlags;
162 Q_D(WireGuardSetting);
164 d->privateKeyFlags = flags;
167void NetworkManager::WireGuardSetting::secretsFromMap(
const QVariantMap &secrets)
169 if (secrets.contains(
QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY))) {
173 if (secrets.contains(
QLatin1String(NM_SETTING_WIREGUARD_PEERS))) {
177 for (
const QVariantMap &peer : listOfPeers) {
178 if (peer.contains(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY))) {
179 QString presharedKey = peer.value(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY)).toString();
181 for (
int i = 0; i < origPeers.
size(); i++) {
192QVariantMap NetworkManager::WireGuardSetting::secretsToMap()
const
196 if (!privateKey().isEmpty()) {
197 secrets.insert(
QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY), privateKey());
202 for (
const QVariantMap &map : peers()) {
208 peersSecrets << newMap;
221 QVariantMap secretsMap;
224 auto it = map.constBegin();
225 while (it != map.constEnd()) {
226 if (it.key() ==
QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY)) {
227 secretsMap.insert(it.key(), it.value());
230 if (it.key().startsWith(
QLatin1String(NM_SETTING_WIREGUARD_PEERS)) && it.key().endsWith(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY))) {
235 peer.insert(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY), it.value());
246 secretsFromMap(secretsMap);
252 QVariantMap secretsMap = secretsToMap();
254 auto it = secretsMap.constBegin();
255 while (it != secretsMap.constEnd()) {
256 if (it.key() ==
QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY)) {
257 ret.
insert(it.key(), it.value().toString());
263 for (
const QVariantMap &map : listOfPeers) {
264 const QString str = QStringLiteral(
"%1.%2.%3")
266 .
arg(map.value(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PUBLIC_KEY)).toString())
268 ret.
insert(str, map.value(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY)).toString());
277QStringList NetworkManager::WireGuardSetting::needSecrets(
bool requestNew)
const
281 if (!privateKeyFlags().testFlag(Setting::NotRequired)) {
282 if (privateKey().isEmpty() || requestNew) {
287 for (
const QVariantMap &map : peers()) {
289 SecretFlags preSharedKeyFlags = (SecretFlags)
map.value(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY_FLAGS)).toInt();
295 if (preSharedKeyFlags.testFlag(Setting::NotRequired)) {
299 const QString str = QStringLiteral(
"%1.%2.%3")
311 if (setting.contains(
QLatin1String(NM_SETTING_WIREGUARD_FWMARK))) {
312 setFwmark(setting.value(
QLatin1String(NM_SETTING_WIREGUARD_FWMARK)).toInt());
315 if (setting.contains(
QLatin1String(NM_SETTING_WIREGUARD_LISTEN_PORT))) {
316 setListenPort(setting.value(
QLatin1String(NM_SETTING_WIREGUARD_LISTEN_PORT)).toInt());
319 if (setting.contains(
QLatin1String(NM_SETTING_WIREGUARD_MTU))) {
320 setMtu(setting.value(
QLatin1String(NM_SETTING_WIREGUARD_MTU)).toInt());
323 if (setting.contains(
QLatin1String(NM_SETTING_WIREGUARD_PEER_ROUTES))) {
324 setPeerRoutes(setting.value(
QLatin1String(NM_SETTING_WIREGUARD_PEER_ROUTES)).toBool());
327 if (setting.contains(
QLatin1String(NM_SETTING_WIREGUARD_PEERS))) {
328 setPeers(qdbus_cast<NMVariantMapList>(setting.value(
QLatin1String(NM_SETTING_WIREGUARD_PEERS))));
331 if (setting.contains(
QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY))) {
332 setPrivateKey(setting.value(
QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY)).toString());
335 if (setting.contains(
QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY_FLAGS))) {
345 setting.insert(
QLatin1String(NM_SETTING_WIREGUARD_LISTEN_PORT), listenPort());
346 setting.insert(
QLatin1String(NM_SETTING_WIREGUARD_MTU), mtu());
347 setting.insert(
QLatin1String(NM_SETTING_WIREGUARD_PEER_ROUTES), peerRoutes());
349 if (!peers().isEmpty()) {
354 for (QVariantMap &map : fixedPeers) {
355 if (map.contains(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY_FLAGS))) {
356 map.insert(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY_FLAGS),
357 map.value(
QLatin1String(NM_WIREGUARD_PEER_ATTR_PRESHARED_KEY_FLAGS)).toUInt());
364 if (!privateKey().isEmpty()) {
365 setting.insert(
QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY), privateKey());
367 setting.insert(
QLatin1String(NM_SETTING_WIREGUARD_PRIVATE_KEY_FLAGS), (
int)privateKeyFlags());
374 dbg.
nospace() <<
"type: " << setting.typeAsString(setting.type()) <<
'\n';
375 dbg.
nospace() <<
"initialized: " << !setting.isNull() <<
'\n';
377 dbg.
nospace() << NM_SETTING_WIREGUARD_FWMARK <<
": " << setting.fwmark() <<
'\n';
378 dbg.
nospace() << NM_SETTING_WIREGUARD_LISTEN_PORT <<
": " << setting.listenPort() <<
'\n';
379 dbg.
nospace() << NM_SETTING_WIREGUARD_MTU <<
": " << setting.mtu() <<
'\n';
380 dbg.
nospace() << NM_SETTING_WIREGUARD_PEER_ROUTES <<
": " << setting.peerRoutes() <<
'\n';
381 dbg.
nospace() << NM_SETTING_WIREGUARD_PEERS <<
": " << setting.peers() <<
'\n';
382 dbg.
nospace() << NM_SETTING_WIREGUARD_PRIVATE_KEY <<
": " << setting.privateKey() <<
'\n';
383 dbg.
nospace() << NM_SETTING_WIREGUARD_PRIVATE_KEY_FLAGS <<
": " << setting.privateKeyFlags() <<
'\n';
Represents generic setting.
QVariantMap toMap() const override
Must be reimplemented, default implementationd does nothing.
void fromMap(const QVariantMap &setting) override
Must be reimplemented, default implementation does nothing.
NMStringMap secretsToStringMap() const override
secretsToStringMap is a convenience function to get the secrets to map of strings.
QString name() const override
Must be reimplemented, default implementationd does nothing.
void secretsFromStringMap(const NMStringMap &map) override
secretsFromStringMap is a convenience function to set the secrets from a map of strings.
char * toString(const EngineQuery &query)
This class allows querying the underlying system to discover the available network interfaces and rea...
const_reference at(qsizetype i) const const
iterator insert(const_iterator before, parameter_type value)
bool isEmpty() const const
qsizetype size() const const
iterator insert(const Key &key, const T &value)
QString arg(Args &&... args) const const
bool isEmpty() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QVariant fromValue(T &&value)