Changeset View
Changeset View
Standalone View
Standalone View
vpn/wireguard/wireguardwidget.cpp
Show All 39 Lines | 36 | public: | |||
---|---|---|---|---|---|
40 | NetworkManager::VpnSetting::Ptr setting; | 40 | NetworkManager::VpnSetting::Ptr setting; | ||
41 | KSharedConfigPtr config; | 41 | KSharedConfigPtr config; | ||
42 | QPalette warningPalette; | 42 | QPalette warningPalette; | ||
43 | QPalette normalPalette; | 43 | QPalette normalPalette; | ||
44 | WireGuardKeyValidator *keyValidator; | 44 | WireGuardKeyValidator *keyValidator; | ||
45 | bool addressValid; | 45 | bool addressValid; | ||
46 | bool privateKeyValid; | 46 | bool privateKeyValid; | ||
47 | bool publicKeyValid; | 47 | bool publicKeyValid; | ||
48 | bool dnsValid; | | |||
49 | bool allowedIpsValid; | 48 | bool allowedIpsValid; | ||
50 | bool endpointValid; | 49 | bool endpointValid; | ||
51 | }; | 50 | }; | ||
52 | 51 | | |||
53 | WireGuardSettingWidget::Private::Private(void) | 52 | WireGuardSettingWidget::Private::Private(void) | ||
54 | : addressValid(false) | 53 | : addressValid(false) | ||
55 | , privateKeyValid(false) | 54 | , privateKeyValid(false) | ||
56 | , publicKeyValid(false) | 55 | , publicKeyValid(false) | ||
57 | , dnsValid(true) // optional so blank is valid | | |||
58 | , allowedIpsValid(false) | 56 | , allowedIpsValid(false) | ||
59 | , endpointValid(true) // optional so blank is valid | 57 | , endpointValid(true) // optional so blank is valid | ||
60 | { | 58 | { | ||
61 | } | 59 | } | ||
62 | 60 | | |||
63 | WireGuardSettingWidget::WireGuardSettingWidget(const NetworkManager::VpnSetting::Ptr &setting, QWidget *parent) | 61 | WireGuardSettingWidget::WireGuardSettingWidget(const NetworkManager::VpnSetting::Ptr &setting, QWidget *parent) | ||
64 | : SettingWidget(setting, parent) | 62 | : SettingWidget(setting, parent) | ||
65 | , d(new Private) | 63 | , d(new Private) | ||
Show All 14 Lines | 64 | { | |||
80 | 78 | | |||
81 | 79 | | |||
82 | connect(d->ui.addressIPv4LineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAddressValid); | 80 | connect(d->ui.addressIPv4LineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAddressValid); | ||
83 | connect(d->ui.addressIPv6LineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAddressValid); | 81 | connect(d->ui.addressIPv6LineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAddressValid); | ||
84 | connect(d->ui.privateKeyLineEdit, &PasswordField::textChanged, this, &WireGuardSettingWidget::checkPrivateKeyValid); | 82 | connect(d->ui.privateKeyLineEdit, &PasswordField::textChanged, this, &WireGuardSettingWidget::checkPrivateKeyValid); | ||
85 | connect(d->ui.publicKeyLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkPublicKeyValid); | 83 | connect(d->ui.publicKeyLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkPublicKeyValid); | ||
86 | connect(d->ui.allowedIPsLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAllowedIpsValid); | 84 | connect(d->ui.allowedIPsLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkAllowedIpsValid); | ||
87 | connect(d->ui.endpointLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkEndpointValid); | 85 | connect(d->ui.endpointLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkEndpointValid); | ||
88 | connect(d->ui.dNSLineEdit, &QLineEdit::textChanged, this, &WireGuardSettingWidget::checkDnsValid); | | |||
89 | 86 | | |||
90 | d->ui.privateKeyLineEdit->setPasswordModeEnabled(true); | 87 | d->ui.privateKeyLineEdit->setPasswordModeEnabled(true); | ||
91 | 88 | | |||
92 | connect(d->ui.btnAdvanced, &QPushButton::clicked, this, &WireGuardSettingWidget::showAdvanced); | 89 | connect(d->ui.btnAdvanced, &QPushButton::clicked, this, &WireGuardSettingWidget::showAdvanced); | ||
93 | 90 | | |||
94 | SimpleIpV4AddressValidator *ip4WithCidrValidator = | 91 | SimpleIpV4AddressValidator *ip4WithCidrValidator = | ||
95 | new SimpleIpV4AddressValidator(this, SimpleIpV4AddressValidator::AddressStyle::WithCidr); | 92 | new SimpleIpV4AddressValidator(this, SimpleIpV4AddressValidator::AddressStyle::WithCidr); | ||
96 | d->ui.addressIPv4LineEdit->setValidator(ip4WithCidrValidator); | 93 | d->ui.addressIPv4LineEdit->setValidator(ip4WithCidrValidator); | ||
97 | 94 | | |||
98 | // Create a validator for the IPv6 address line edit | 95 | // Create a validator for the IPv6 address line edit | ||
99 | // Address must be a valid IP address with a CIDR suffix | 96 | // Address must be a valid IP address with a CIDR suffix | ||
100 | SimpleIpV6AddressValidator *ip6WithCidrValidator = | 97 | SimpleIpV6AddressValidator *ip6WithCidrValidator = | ||
101 | new SimpleIpV6AddressValidator(this, SimpleIpV6AddressValidator::AddressStyle::WithCidr); | 98 | new SimpleIpV6AddressValidator(this, SimpleIpV6AddressValidator::AddressStyle::WithCidr); | ||
102 | 99 | | |||
103 | d->ui.addressIPv6LineEdit->setValidator(ip6WithCidrValidator); | 100 | d->ui.addressIPv6LineEdit->setValidator(ip6WithCidrValidator); | ||
104 | 101 | | |||
105 | // This is done as a private variable rather than a local variable so it can be | 102 | // This is done as a private variable rather than a local variable so it can be | ||
106 | // used both here and to validate the private key later | 103 | // used both here and to validate the private key later | ||
107 | d->keyValidator = new WireGuardKeyValidator(this); | 104 | d->keyValidator = new WireGuardKeyValidator(this); | ||
108 | d->ui.publicKeyLineEdit->setValidator(d->keyValidator); | 105 | d->ui.publicKeyLineEdit->setValidator(d->keyValidator); | ||
109 | 106 | | |||
110 | // Create validator for DNS | | |||
111 | SimpleIpV4AddressValidator *dnsValidator = new SimpleIpV4AddressValidator(this); | | |||
112 | d->ui.dNSLineEdit->setValidator(dnsValidator); | | |||
113 | | ||||
114 | // Create validator for Endpoint | 107 | // Create validator for Endpoint | ||
115 | SimpleIpV4AddressValidator *endpointValidator = | 108 | SimpleIpV4AddressValidator *endpointValidator = | ||
116 | new SimpleIpV4AddressValidator(this, SimpleIpV4AddressValidator::AddressStyle::WithPort); | 109 | new SimpleIpV4AddressValidator(this, SimpleIpV4AddressValidator::AddressStyle::WithPort); | ||
117 | d->ui.endpointLineEdit->setValidator(endpointValidator); | 110 | d->ui.endpointLineEdit->setValidator(endpointValidator); | ||
118 | 111 | | |||
119 | // Create validator for AllowedIPs | 112 | // Create validator for AllowedIPs | ||
120 | SimpleIpListValidator *allowedIPsValidator = new SimpleIpListValidator(this, SimpleIpListValidator::WithCidr, | 113 | SimpleIpListValidator *allowedIPsValidator = new SimpleIpListValidator(this, SimpleIpListValidator::WithCidr, | ||
121 | SimpleIpListValidator::Both); | 114 | SimpleIpListValidator::Both); | ||
122 | d->ui.allowedIPsLineEdit->setValidator(allowedIPsValidator); | 115 | d->ui.allowedIPsLineEdit->setValidator(allowedIPsValidator); | ||
123 | 116 | | |||
124 | // Connect for setting check | 117 | // Connect for setting check | ||
125 | watchChangedSetting(); | 118 | watchChangedSetting(); | ||
126 | 119 | | |||
127 | KAcceleratorManager::manage(this); | 120 | KAcceleratorManager::manage(this); | ||
128 | 121 | | |||
129 | if (setting && !setting->isNull()) { | 122 | if (setting && !setting->isNull()) { | ||
130 | loadConfig(d->setting); | 123 | loadConfig(d->setting); | ||
131 | } | 124 | } | ||
132 | 125 | | |||
133 | // Set the initial backgrounds on all the widgets | 126 | // Set the initial backgrounds on all the widgets | ||
134 | checkAddressValid(); | 127 | checkAddressValid(); | ||
135 | checkPrivateKeyValid(); | 128 | checkPrivateKeyValid(); | ||
136 | checkPublicKeyValid(); | 129 | checkPublicKeyValid(); | ||
137 | checkDnsValid(); | | |||
138 | checkAllowedIpsValid(); | 130 | checkAllowedIpsValid(); | ||
139 | checkEndpointValid(); | 131 | checkEndpointValid(); | ||
140 | 132 | | |||
141 | } | 133 | } | ||
142 | 134 | | |||
143 | WireGuardSettingWidget::~WireGuardSettingWidget() | 135 | WireGuardSettingWidget::~WireGuardSettingWidget() | ||
144 | { | 136 | { | ||
145 | delete d; | 137 | delete d; | ||
146 | } | 138 | } | ||
147 | 139 | | |||
148 | void WireGuardSettingWidget::loadConfig(const NetworkManager::Setting::Ptr &setting) | 140 | void WireGuardSettingWidget::loadConfig(const NetworkManager::Setting::Ptr &setting) | ||
149 | { | 141 | { | ||
150 | Q_UNUSED(setting) | 142 | Q_UNUSED(setting) | ||
143 | | ||||
151 | // General settings | 144 | // General settings | ||
152 | const NMStringMap dataMap = d->setting->data(); | 145 | const NMStringMap dataMap = d->setting->data(); | ||
153 | 146 | | |||
154 | d->ui.addressIPv4LineEdit->setText(dataMap[NM_WG_KEY_ADDR_IP4]); | 147 | d->ui.addressIPv4LineEdit->setText(dataMap[NM_WG_KEY_ADDR_IP4]); | ||
155 | d->ui.addressIPv6LineEdit->setText(dataMap[NM_WG_KEY_ADDR_IP6]); | 148 | d->ui.addressIPv6LineEdit->setText(dataMap[NM_WG_KEY_ADDR_IP6]); | ||
156 | d->ui.privateKeyLineEdit->setText(dataMap[NM_WG_KEY_PRIVATE_KEY]); | 149 | d->ui.privateKeyLineEdit->setText(dataMap[NM_WG_KEY_PRIVATE_KEY]); | ||
157 | d->ui.dNSLineEdit->setText(dataMap[NM_WG_KEY_DNS]); | | |||
158 | d->ui.publicKeyLineEdit->setText(dataMap[NM_WG_KEY_PUBLIC_KEY]); | 150 | d->ui.publicKeyLineEdit->setText(dataMap[NM_WG_KEY_PUBLIC_KEY]); | ||
159 | d->ui.allowedIPsLineEdit->setText(dataMap[NM_WG_KEY_ALLOWED_IPS]); | 151 | d->ui.allowedIPsLineEdit->setText(dataMap[NM_WG_KEY_ALLOWED_IPS]); | ||
160 | d->ui.endpointLineEdit->setText(dataMap[NM_WG_KEY_ENDPOINT]); | 152 | d->ui.endpointLineEdit->setText(dataMap[NM_WG_KEY_ENDPOINT]); | ||
161 | } | 153 | } | ||
162 | 154 | | |||
163 | void WireGuardSettingWidget::loadSecrets(const NetworkManager::Setting::Ptr &setting) | 155 | void WireGuardSettingWidget::loadSecrets(const NetworkManager::Setting::Ptr &setting) | ||
164 | { | 156 | { | ||
165 | // Currently WireGuard does not have any secrets | 157 | // Currently WireGuard does not have any secrets | ||
166 | Q_UNUSED(setting) | 158 | Q_UNUSED(setting) | ||
167 | } | 159 | } | ||
168 | 160 | | |||
169 | QVariantMap WireGuardSettingWidget::setting() const | 161 | QVariantMap WireGuardSettingWidget::setting() const | ||
170 | { | 162 | { | ||
171 | NMStringMap data = d->setting->data(); | 163 | NMStringMap data = d->setting->data(); | ||
172 | NetworkManager::VpnSetting setting; | 164 | NetworkManager::VpnSetting setting; | ||
173 | setting.setServiceType(QLatin1String(NM_DBUS_SERVICE_WIREGUARD)); | 165 | setting.setServiceType(QLatin1String(NM_DBUS_SERVICE_WIREGUARD)); | ||
174 | 166 | | |||
175 | // required settings | 167 | // required settings | ||
176 | setProperty(data, QLatin1String(NM_WG_KEY_ADDR_IP4), d->ui.addressIPv4LineEdit->displayText()); | 168 | setProperty(data, QLatin1String(NM_WG_KEY_ADDR_IP4), d->ui.addressIPv4LineEdit->displayText()); | ||
177 | setProperty(data, QLatin1String(NM_WG_KEY_ADDR_IP6), d->ui.addressIPv6LineEdit->displayText()); | 169 | setProperty(data, QLatin1String(NM_WG_KEY_ADDR_IP6), d->ui.addressIPv6LineEdit->displayText()); | ||
178 | setProperty(data, QLatin1String(NM_WG_KEY_PRIVATE_KEY), d->ui.privateKeyLineEdit->text()); | 170 | setProperty(data, QLatin1String(NM_WG_KEY_PRIVATE_KEY), d->ui.privateKeyLineEdit->text()); | ||
179 | setProperty(data, QLatin1String(NM_WG_KEY_PUBLIC_KEY), d->ui.publicKeyLineEdit->displayText()); | 171 | setProperty(data, QLatin1String(NM_WG_KEY_PUBLIC_KEY), d->ui.publicKeyLineEdit->displayText()); | ||
180 | setProperty(data, QLatin1String(NM_WG_KEY_ALLOWED_IPS), d->ui.allowedIPsLineEdit->displayText()); | 172 | setProperty(data, QLatin1String(NM_WG_KEY_ALLOWED_IPS), d->ui.allowedIPsLineEdit->displayText()); | ||
181 | setProperty(data, QLatin1String(NM_WG_KEY_DNS), d->ui.dNSLineEdit->displayText()); | | |||
182 | setProperty(data, QLatin1String(NM_WG_KEY_ENDPOINT), d->ui.endpointLineEdit->displayText()); | 173 | setProperty(data, QLatin1String(NM_WG_KEY_ENDPOINT), d->ui.endpointLineEdit->displayText()); | ||
183 | 174 | | |||
184 | setting.setData(data); | 175 | setting.setData(data); | ||
185 | 176 | | |||
186 | return setting.toMap(); | 177 | return setting.toMap(); | ||
187 | } | 178 | } | ||
188 | 179 | | |||
189 | void WireGuardSettingWidget::setProperty(NMStringMap &data, const QLatin1String &key, const QString &value) const | 180 | void WireGuardSettingWidget::setProperty(NMStringMap &data, const QLatin1String &key, const QString &value) const | ||
Show All 25 Lines | 189 | { | |||
215 | adv->show(); | 206 | adv->show(); | ||
216 | } | 207 | } | ||
217 | 208 | | |||
218 | bool WireGuardSettingWidget::isValid() const | 209 | bool WireGuardSettingWidget::isValid() const | ||
219 | { | 210 | { | ||
220 | return d->addressValid | 211 | return d->addressValid | ||
221 | && d->privateKeyValid | 212 | && d->privateKeyValid | ||
222 | && d->publicKeyValid | 213 | && d->publicKeyValid | ||
223 | && d->dnsValid | | |||
224 | && d->allowedIpsValid | 214 | && d->allowedIpsValid | ||
225 | && d->endpointValid; | 215 | && d->endpointValid; | ||
226 | } | 216 | } | ||
227 | 217 | | |||
228 | void WireGuardSettingWidget::checkAddressValid() | 218 | void WireGuardSettingWidget::checkAddressValid() | ||
229 | { | 219 | { | ||
230 | int pos = 0; | 220 | int pos = 0; | ||
231 | QLineEdit *widget = d->ui.addressIPv4LineEdit; | 221 | QLineEdit *widget = d->ui.addressIPv4LineEdit; | ||
Show All 29 Lines | 250 | { | |||
261 | int pos = 0; | 251 | int pos = 0; | ||
262 | QLineEdit *widget = d->ui.publicKeyLineEdit; | 252 | QLineEdit *widget = d->ui.publicKeyLineEdit; | ||
263 | QString value = widget->displayText(); | 253 | QString value = widget->displayText(); | ||
264 | d->publicKeyValid = QValidator::Acceptable == widget->validator()->validate(value, pos); | 254 | d->publicKeyValid = QValidator::Acceptable == widget->validator()->validate(value, pos); | ||
265 | setBackground(widget, d->publicKeyValid); | 255 | setBackground(widget, d->publicKeyValid); | ||
266 | slotWidgetChanged(); | 256 | slotWidgetChanged(); | ||
267 | } | 257 | } | ||
268 | 258 | | |||
269 | void WireGuardSettingWidget::checkDnsValid() | | |||
270 | { | | |||
271 | int pos = 0; | | |||
272 | QLineEdit *widget = d->ui.dNSLineEdit; | | |||
273 | QString value = widget->displayText(); | | |||
274 | d->dnsValid = QValidator::Acceptable == widget->validator()->validate(value, pos) | | |||
275 | || widget->displayText().isEmpty(); | | |||
276 | setBackground(widget, d->dnsValid); | | |||
277 | slotWidgetChanged(); | | |||
278 | } | | |||
279 | | ||||
280 | void WireGuardSettingWidget::checkAllowedIpsValid() | 259 | void WireGuardSettingWidget::checkAllowedIpsValid() | ||
281 | { | 260 | { | ||
282 | int pos = 0; | 261 | int pos = 0; | ||
283 | QLineEdit *widget = d->ui.allowedIPsLineEdit; | 262 | QLineEdit *widget = d->ui.allowedIPsLineEdit; | ||
284 | QString value = widget->displayText(); | 263 | QString value = widget->displayText(); | ||
285 | d->allowedIpsValid = QValidator::Acceptable == widget->validator()->validate(value, pos); | 264 | d->allowedIpsValid = QValidator::Acceptable == widget->validator()->validate(value, pos); | ||
286 | setBackground(widget, d->allowedIpsValid); | 265 | setBackground(widget, d->allowedIpsValid); | ||
287 | slotWidgetChanged(); | 266 | slotWidgetChanged(); | ||
Show All 20 Lines |