Changeset View
Changeset View
Standalone View
Standalone View
vpn/wireguard/wireguardwidget.cpp
Show All 21 Lines | |||||
22 | #include "wireguardwidget.h" | 22 | #include "wireguardwidget.h" | ||
23 | #include "wireguardadvancedwidget.h" | 23 | #include "wireguardadvancedwidget.h" | ||
24 | #include "simpleipv4addressvalidator.h" | 24 | #include "simpleipv4addressvalidator.h" | ||
25 | #include "simpleiplistvalidator.h" | 25 | #include "simpleiplistvalidator.h" | ||
26 | #include "wireguardkeyvalidator.h" | 26 | #include "wireguardkeyvalidator.h" | ||
27 | 27 | | |||
28 | #include <QDBusMetaType> | 28 | #include <QDBusMetaType> | ||
29 | #include <QPointer> | 29 | #include <QPointer> | ||
30 | #include <QUrl> | ||||
30 | #include <KColorScheme> | 31 | #include <KColorScheme> | ||
31 | 32 | | |||
32 | #include "nm-wireguard-service.h" | 33 | #include "nm-wireguard-service.h" | ||
33 | 34 | | |||
34 | class WireGuardSettingWidget::Private | 35 | class WireGuardSettingWidget::Private | ||
35 | { | 36 | { | ||
36 | public: | 37 | public: | ||
37 | Private(); | 38 | Private(); | ||
Show All 39 Lines | 77 | KColorScheme::adjustBackground(d->normalPalette, KColorScheme::NormalBackground, QPalette::Base, | |||
77 | KColorScheme::ColorSet::View, d->config); | 78 | KColorScheme::ColorSet::View, d->config); | ||
78 | 79 | | |||
79 | 80 | | |||
80 | connect(d->ui.addressIPv4LineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAddressValid); | 81 | connect(d->ui.addressIPv4LineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAddressValid); | ||
81 | connect(d->ui.addressIPv6LineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAddressValid); | 82 | connect(d->ui.addressIPv6LineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAddressValid); | ||
82 | connect(d->ui.privateKeyLineEdit, &PasswordField::textChanged, this, &WireGuardSettingWidget::checkPrivateKeyValid); | 83 | connect(d->ui.privateKeyLineEdit, &PasswordField::textChanged, this, &WireGuardSettingWidget::checkPrivateKeyValid); | ||
83 | connect(d->ui.publicKeyLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkPublicKeyValid); | 84 | connect(d->ui.publicKeyLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkPublicKeyValid); | ||
84 | connect(d->ui.allowedIPsLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAllowedIpsValid); | 85 | connect(d->ui.allowedIPsLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAllowedIpsValid); | ||
85 | connect(d->ui.endpointLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkEndpointValid); | 86 | connect(d->ui.endpointAddressLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkEndpointValid); | ||
87 | connect(d->ui.endpointPortLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkEndpointValid); | ||||
86 | 88 | | |||
87 | d->ui.privateKeyLineEdit->setPasswordModeEnabled(true); | 89 | d->ui.privateKeyLineEdit->setPasswordModeEnabled(true); | ||
88 | 90 | | |||
89 | connect(d->ui.btnAdvanced, &QPushButton::clicked, this, &WireGuardSettingWidget::showAdvanced); | 91 | connect(d->ui.btnAdvanced, &QPushButton::clicked, this, &WireGuardSettingWidget::showAdvanced); | ||
90 | 92 | | |||
91 | SimpleIpV4AddressValidator *ip4WithCidrValidator = | 93 | SimpleIpV4AddressValidator *ip4WithCidrValidator = | ||
92 | new SimpleIpV4AddressValidator(this, SimpleIpV4AddressValidator::AddressStyle::WithCidr); | 94 | new SimpleIpV4AddressValidator(this, SimpleIpV4AddressValidator::AddressStyle::WithCidr); | ||
93 | d->ui.addressIPv4LineEdit->setValidator(ip4WithCidrValidator); | 95 | d->ui.addressIPv4LineEdit->setValidator(ip4WithCidrValidator); | ||
94 | 96 | | |||
95 | // Create a validator for the IPv6 address line edit | 97 | // Create a validator for the IPv6 address line edit | ||
96 | // Address must be a valid IP address with a CIDR suffix | 98 | // Address must be a valid IP address with a CIDR suffix | ||
97 | SimpleIpV6AddressValidator *ip6WithCidrValidator = | 99 | SimpleIpV6AddressValidator *ip6WithCidrValidator = | ||
98 | new SimpleIpV6AddressValidator(this, SimpleIpV6AddressValidator::AddressStyle::WithCidr); | 100 | new SimpleIpV6AddressValidator(this, SimpleIpV6AddressValidator::AddressStyle::WithCidr); | ||
99 | 101 | | |||
100 | d->ui.addressIPv6LineEdit->setValidator(ip6WithCidrValidator); | 102 | d->ui.addressIPv6LineEdit->setValidator(ip6WithCidrValidator); | ||
101 | 103 | | |||
102 | // This is done as a private variable rather than a local variable so it can be | 104 | // This is done as a private variable rather than a local variable so it can be | ||
103 | // used both here and to validate the private key later | 105 | // used both here and to validate the private key later | ||
104 | d->keyValidator = new WireGuardKeyValidator(this); | 106 | d->keyValidator = new WireGuardKeyValidator(this); | ||
105 | d->ui.publicKeyLineEdit->setValidator(d->keyValidator); | 107 | d->ui.publicKeyLineEdit->setValidator(d->keyValidator); | ||
106 | 108 | | |||
107 | // Create validator for Endpoint | | |||
108 | SimpleIpV4AddressValidator *endpointValidator = | | |||
109 | new SimpleIpV4AddressValidator(this, SimpleIpV4AddressValidator::AddressStyle::WithPort); | | |||
110 | d->ui.endpointLineEdit->setValidator(endpointValidator); | | |||
111 | | ||||
112 | // Create validator for AllowedIPs | 109 | // Create validator for AllowedIPs | ||
113 | SimpleIpListValidator *allowedIPsValidator = new SimpleIpListValidator(this, SimpleIpListValidator::WithCidr, | 110 | SimpleIpListValidator *allowedIPsValidator = new SimpleIpListValidator(this, SimpleIpListValidator::WithCidr, | ||
114 | SimpleIpListValidator::Both); | 111 | SimpleIpListValidator::Both); | ||
115 | d->ui.allowedIPsLineEdit->setValidator(allowedIPsValidator); | 112 | d->ui.allowedIPsLineEdit->setValidator(allowedIPsValidator); | ||
116 | 113 | | |||
114 | // Create validator for endpoint port | ||||
115 | QIntValidator *portValidator = new QIntValidator(this); | ||||
116 | portValidator->setBottom(0); | ||||
117 | portValidator->setTop(65535); | ||||
118 | d->ui.endpointPortLineEdit->setValidator(portValidator); | ||||
119 | | ||||
117 | // Connect for setting check | 120 | // Connect for setting check | ||
118 | watchChangedSetting(); | 121 | watchChangedSetting(); | ||
119 | 122 | | |||
120 | KAcceleratorManager::manage(this); | 123 | KAcceleratorManager::manage(this); | ||
121 | 124 | | |||
122 | if (setting && !setting->isNull()) { | 125 | if (setting && !setting->isNull()) { | ||
123 | loadConfig(d->setting); | 126 | loadConfig(d->setting); | ||
124 | } | 127 | } | ||
Show All 19 Lines | 144 | { | |||
144 | // General settings | 147 | // General settings | ||
145 | const NMStringMap dataMap = d->setting->data(); | 148 | const NMStringMap dataMap = d->setting->data(); | ||
146 | 149 | | |||
147 | d->ui.addressIPv4LineEdit->setText(dataMap[NM_WG_KEY_ADDR_IP4]); | 150 | d->ui.addressIPv4LineEdit->setText(dataMap[NM_WG_KEY_ADDR_IP4]); | ||
148 | d->ui.addressIPv6LineEdit->setText(dataMap[NM_WG_KEY_ADDR_IP6]); | 151 | d->ui.addressIPv6LineEdit->setText(dataMap[NM_WG_KEY_ADDR_IP6]); | ||
149 | d->ui.privateKeyLineEdit->setText(dataMap[NM_WG_KEY_PRIVATE_KEY]); | 152 | d->ui.privateKeyLineEdit->setText(dataMap[NM_WG_KEY_PRIVATE_KEY]); | ||
150 | d->ui.publicKeyLineEdit->setText(dataMap[NM_WG_KEY_PUBLIC_KEY]); | 153 | d->ui.publicKeyLineEdit->setText(dataMap[NM_WG_KEY_PUBLIC_KEY]); | ||
151 | d->ui.allowedIPsLineEdit->setText(dataMap[NM_WG_KEY_ALLOWED_IPS]); | 154 | d->ui.allowedIPsLineEdit->setText(dataMap[NM_WG_KEY_ALLOWED_IPS]); | ||
152 | d->ui.endpointLineEdit->setText(dataMap[NM_WG_KEY_ENDPOINT]); | 155 | | ||
156 | // An endpoint is stored as <ipv4 | [ipv6] | fqdn>:<port> | ||||
157 | QString storedEndpoint = dataMap[NM_WG_KEY_ENDPOINT]; | ||||
158 | QStringList endpointList = storedEndpoint.contains("]:") ? | ||||
159 | dataMap[NM_WG_KEY_ENDPOINT].split("]:") : | ||||
160 | dataMap[NM_WG_KEY_ENDPOINT].split(":"); | ||||
161 | | ||||
162 | d->ui.endpointAddressLineEdit->setText(endpointList[0].remove("[")); | ||||
163 | d->ui.endpointPortLineEdit->setText(endpointList[1]); | ||||
153 | } | 164 | } | ||
154 | 165 | | |||
155 | void WireGuardSettingWidget::loadSecrets(const NetworkManager::Setting::Ptr &setting) | 166 | void WireGuardSettingWidget::loadSecrets(const NetworkManager::Setting::Ptr &setting) | ||
156 | { | 167 | { | ||
157 | // Currently WireGuard does not have any secrets | 168 | // Currently WireGuard does not have any secrets | ||
158 | Q_UNUSED(setting) | 169 | Q_UNUSED(setting) | ||
159 | } | 170 | } | ||
160 | 171 | | |||
161 | QVariantMap WireGuardSettingWidget::setting() const | 172 | QVariantMap WireGuardSettingWidget::setting() const | ||
162 | { | 173 | { | ||
163 | NMStringMap data = d->setting->data(); | 174 | NMStringMap data = d->setting->data(); | ||
164 | NetworkManager::VpnSetting setting; | 175 | NetworkManager::VpnSetting setting; | ||
165 | setting.setServiceType(QLatin1String(NM_DBUS_SERVICE_WIREGUARD)); | 176 | setting.setServiceType(QLatin1String(NM_DBUS_SERVICE_WIREGUARD)); | ||
166 | 177 | | |||
167 | // required settings | 178 | // required settings | ||
168 | setProperty(data, QLatin1String(NM_WG_KEY_ADDR_IP4), d->ui.addressIPv4LineEdit->displayText()); | 179 | setProperty(data, QLatin1String(NM_WG_KEY_ADDR_IP4), d->ui.addressIPv4LineEdit->displayText()); | ||
169 | setProperty(data, QLatin1String(NM_WG_KEY_ADDR_IP6), d->ui.addressIPv6LineEdit->displayText()); | 180 | setProperty(data, QLatin1String(NM_WG_KEY_ADDR_IP6), d->ui.addressIPv6LineEdit->displayText()); | ||
170 | setProperty(data, QLatin1String(NM_WG_KEY_PRIVATE_KEY), d->ui.privateKeyLineEdit->text()); | 181 | setProperty(data, QLatin1String(NM_WG_KEY_PRIVATE_KEY), d->ui.privateKeyLineEdit->text()); | ||
171 | setProperty(data, QLatin1String(NM_WG_KEY_PUBLIC_KEY), d->ui.publicKeyLineEdit->displayText()); | 182 | setProperty(data, QLatin1String(NM_WG_KEY_PUBLIC_KEY), d->ui.publicKeyLineEdit->displayText()); | ||
172 | setProperty(data, QLatin1String(NM_WG_KEY_ALLOWED_IPS), d->ui.allowedIPsLineEdit->displayText()); | 183 | setProperty(data, QLatin1String(NM_WG_KEY_ALLOWED_IPS), d->ui.allowedIPsLineEdit->displayText()); | ||
173 | setProperty(data, QLatin1String(NM_WG_KEY_ENDPOINT), d->ui.endpointLineEdit->displayText()); | 184 | | ||
185 | // Endpoint isn't required and is created from <address>:<port> | ||||
186 | QString addressString = d->ui.endpointAddressLineEdit->displayText(); | ||||
187 | if (!addressString.isEmpty()) { | ||||
188 | // If there is a ':' in the address string then it is an IPv6 address and | ||||
189 | // the output needs to be formatted as '[1:2:3:4:5:6:7:8]:123' otherwhise | ||||
190 | // it is formatted as '1.2.3.4:123' or 'ab.com:123' | ||||
191 | if (addressString.contains(":")) | ||||
192 | setProperty(data, QLatin1String(NM_WG_KEY_ENDPOINT), | ||||
193 | "[" + | ||||
194 | d->ui.endpointAddressLineEdit->displayText().trimmed() + | ||||
195 | "]:" + | ||||
196 | d->ui.endpointPortLineEdit->displayText().trimmed()); | ||||
197 | else | ||||
198 | setProperty(data, QLatin1String(NM_WG_KEY_ENDPOINT), | ||||
199 | d->ui.endpointAddressLineEdit->displayText().trimmed() + | ||||
200 | ":" + | ||||
201 | d->ui.endpointPortLineEdit->displayText().trimmed()); | ||||
202 | } | ||||
174 | 203 | | |||
175 | setting.setData(data); | 204 | setting.setData(data); | ||
176 | 205 | | |||
177 | return setting.toMap(); | 206 | return setting.toMap(); | ||
178 | } | 207 | } | ||
179 | 208 | | |||
180 | void WireGuardSettingWidget::setProperty(NMStringMap &data, const QLatin1String &key, const QString &value) const | 209 | void WireGuardSettingWidget::setProperty(NMStringMap &data, const QLatin1String &key, const QString &value) const | ||
181 | { | 210 | { | ||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Line(s) | 289 | { | |||
264 | d->allowedIpsValid = QValidator::Acceptable == widget->validator()->validate(value, pos); | 293 | d->allowedIpsValid = QValidator::Acceptable == widget->validator()->validate(value, pos); | ||
265 | setBackground(widget, d->allowedIpsValid); | 294 | setBackground(widget, d->allowedIpsValid); | ||
266 | slotWidgetChanged(); | 295 | slotWidgetChanged(); | ||
267 | } | 296 | } | ||
268 | 297 | | |||
269 | void WireGuardSettingWidget::checkEndpointValid() | 298 | void WireGuardSettingWidget::checkEndpointValid() | ||
270 | { | 299 | { | ||
271 | int pos = 0; | 300 | int pos = 0; | ||
272 | QLineEdit *widget = d->ui.endpointLineEdit; | 301 | QLineEdit *addressWidget = d->ui.endpointAddressLineEdit; | ||
273 | QString value = widget->displayText(); | 302 | QLineEdit *portWidget = d->ui.endpointPortLineEdit; | ||
274 | d->endpointValid = QValidator::Acceptable == widget->validator()->validate(value, pos) | 303 | QString addressValue = addressWidget->displayText(); | ||
275 | || value.isEmpty(); | 304 | QString portString = portWidget->displayText(); | ||
276 | setBackground(widget, d->endpointValid); | 305 | | ||
306 | QUrl temp; | ||||
307 | static QRegExpValidator fqdnValidator(QRegExp(QLatin1String("(?=.{5,254}$)([a-zA-Z0-9][a-zA-Z0-9-]{1,62}\\.){1,63}[a-zA-Z]{2,63}")), 0); | ||||
308 | static SimpleIpV4AddressValidator ipv4Validator(0); | ||||
309 | static SimpleIpV6AddressValidator ipv6Validator(0); | ||||
310 | | ||||
311 | bool addressValid = QValidator::Acceptable == fqdnValidator.validate(addressValue, pos) | ||||
312 | || QValidator::Acceptable == ipv4Validator.validate(addressValue, pos) | ||||
313 | || QValidator::Acceptable == ipv6Validator.validate(addressValue, pos); | ||||
314 | bool bothEmpty = addressValue.isEmpty() && portString.isEmpty(); | ||||
315 | // Because of the validator, if the port is non-empty, it is valid | ||||
316 | bool portValid = !portString.isEmpty(); | ||||
317 | d->endpointValid = bothEmpty || (addressValid && portValid); | ||||
318 | setBackground(addressWidget, bothEmpty || addressValid); | ||||
319 | setBackground(portWidget, bothEmpty || portValid); | ||||
320 | | ||||
277 | slotWidgetChanged(); | 321 | slotWidgetChanged(); | ||
278 | } | 322 | } | ||
279 | 323 | | |||
280 | void WireGuardSettingWidget::setBackground(QWidget *w, bool result) const | 324 | void WireGuardSettingWidget::setBackground(QWidget *w, bool result) const | ||
281 | { | 325 | { | ||
282 | if (result) | 326 | if (result) | ||
283 | w->setPalette(d->normalPalette); | 327 | w->setPalette(d->normalPalette); | ||
284 | else | 328 | else | ||
285 | w->setPalette(d->warningPalette); | 329 | w->setPalette(d->warningPalette); | ||
286 | } | 330 | } |