From 689f08f330a50ab81cb3029cd7165aac26717aed Mon Sep 17 00:00:00 2001 From: Tobias Bossert Date: Fri, 24 Mar 2023 15:01:36 +0100 Subject: [PATCH 1/2] correctly sort numbers, send only on success, include email template in pkg, version++ Signed-off-by: Tobias Bossert --- CHANGES | 9 +++ VERSION | 2 +- etc/Makefile.am | 2 +- etc/wgwrangler.dist.yaml | 4 +- .../GuiPlugin/WireguardAddPeerForm.pm | 29 +++++---- lib/WGwrangler/GuiPlugin/WireguardShow.pm | 60 ++++++++++--------- lib/WGwrangler/GuiPlugin/WireguardVersions.pm | 2 +- lib/WGwrangler/Model/WireguardDataAdapter.pm | 16 ++++- t/dummy_home/show_dummy | 6 +- 9 files changed, 82 insertions(+), 48 deletions(-) diff --git a/CHANGES b/CHANGES index f226b128..4dac073a 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,12 @@ +wg-wrangler (0.3.1) UNRELEASED; urgency=medium + + * Sort numeric columns numerically + * Send emails only if everything before successes + * 'Send by Email' is now the default option + * Also show 'Last Handshake' column + + -- Tobias Bossert Fri, 24 Mar 2023 14:57:27 +0100 + wg-wrangler (0.3.0) unstable; urgency=medium * Fixed incorrect handling of #+comment tags diff --git a/VERSION b/VERSION index 0d91a54c..9e11b32f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.0 +0.3.1 diff --git a/etc/Makefile.am b/etc/Makefile.am index b7acd7a6..07c9c086 100644 --- a/etc/Makefile.am +++ b/etc/Makefile.am @@ -1,5 +1,5 @@ -ETC = wgwrangler.dist.yaml env.dist.conf +ETC = wgwrangler.dist.yaml env.dist.conf send_config_by_email.dist.ep EXTRA_DIST = $(ETC) sysconf_DATA = $(ETC) diff --git a/etc/wgwrangler.dist.yaml b/etc/wgwrangler.dist.yaml index 6b8aabd7..34d37e3a 100644 --- a/etc/wgwrangler.dist.yaml +++ b/etc/wgwrangler.dist.yaml @@ -7,8 +7,8 @@ BACKEND: vpn_name: 'Awesome Wireguard VPN' # Enable versioning for wireguard configurations enable_git: false - # Instead of applying the configuration automatically after every edit, show a button to apply the configuration manually - no_apply: false + # Instead of applying the configuration automatically after every edit (true) show a button to manually apply the configuration + no_apply: true not_applied_suffix: .not_applied # Where does your wireguard configuration reside? (trailing slash expected) wireguard_home: /etc/wireguard/ diff --git a/lib/WGwrangler/GuiPlugin/WireguardAddPeerForm.pm b/lib/WGwrangler/GuiPlugin/WireguardAddPeerForm.pm index 0b8d78af..fbaf61fc 100644 --- a/lib/WGwrangler/GuiPlugin/WireguardAddPeerForm.pm +++ b/lib/WGwrangler/GuiPlugin/WireguardAddPeerForm.pm @@ -284,8 +284,15 @@ has formCfg => sub ($self) { { key => 'send_by_email', label => => trm('Send by Email'), - widget => 'checkBox' + widget => 'checkBox', + set => { + value => true + } }, + { + widget => 'header', + label => trm('Note: The configuration rendered in the preview is also completely valid'), + } ]; }; @@ -316,6 +323,17 @@ has actionCfg => sub ($self) { $self->app->wireguardModel->update_peer_data($interface, $peer_pub_key, 'device', $device); $self->app->wireguardModel->update_peer_data($interface, $peer_pub_key, 'created', $created); + # Commit changes + $self->app->wireguardModel->commit_changes({}); + + # Check into VCS if enabled + if ($self->config->{'no_apply'} && $self->config->{'enable_git'}) { + my $commit_message = "Created peer for `$name` on interface `$interface`"; + my $user_string = $self->user->{userInfo}{cbuser_login}; + $self->app->versionManager->checkin_new_version($commit_message, $user_string, 'dummy@example.com'); + } + + # And send mail if no error occurred so far if (defined $send_by_email && $send_by_email == 1) { my $email_cfg = { 'name' => $name, @@ -335,15 +353,6 @@ has actionCfg => sub ($self) { }; $self->mailHandler->prepare_and_send($email_cfg); } - # Commit changes - $self->app->wireguardModel->commit_changes({}); - - # Check into VCS if enabled - if ($self->config->{'no_apply'} && $self->config->{'enable_git'}) { - my $commit_message = "Created peer for `$name` on interface `$interface`"; - my $user_string = $self->user->{userInfo}{cbuser_login}; - $self->app->versionManager->checkin_new_version($commit_message, $user_string, 'dummy@example.com'); - } }; if ($@) { my $error_id = int(rand(100000)); diff --git a/lib/WGwrangler/GuiPlugin/WireguardShow.pm b/lib/WGwrangler/GuiPlugin/WireguardShow.pm index 93981a98..0583bd89 100644 --- a/lib/WGwrangler/GuiPlugin/WireguardShow.pm +++ b/lib/WGwrangler/GuiPlugin/WireguardShow.pm @@ -35,6 +35,12 @@ has formCfg => sub($self) { ] }; +has tableCfgHash => sub($self) { + # Simply convert the tableCfg to a hash where the 'key'-field becomes the key + my %hash = map {$_->{'key'} => $_} @{$self->tableCfg()}; + return \%hash; +}; + has tableCfg => sub($self) { return [ { @@ -93,12 +99,12 @@ has tableCfg => sub($self) { width => '2*', key => 'transfer-rx', sortable => true, - # format => { - # unitPrefix => 'metric', - # maximumFractionDigits => 2, - # postfix => 'Byte', - # locale => 'en' - # }, + format => { + unitPrefix => 'metric', + maximumFractionDigits => 2, + postfix => 'Byte', + locale => 'en' + }, }, { label => trm('Transfer-TX'), @@ -106,33 +112,31 @@ has tableCfg => sub($self) { width => '1*', key => 'transfer-tx', sortable => true, - # format => { - # unitPrefix => 'metric', - # maximumFractionDigits => 2, - # postfix => 'Byte', - # locale => 'en' - # }, + format => { + unitPrefix => 'metric', + maximumFractionDigits => 2, + postfix => 'Byte', + locale => 'en' + }, }, - # { - # label => trm('Size'), - # type => 'number', - # format => { - # unitPrefix => 'metric', - # maximumFractionDigits => 2, - # postfix => 'Byte', - # locale => 'en' - # }, - # width => '1*', - # key => 'song_size', - # sortable => true, - # }, { label => trm('Endpoint'), type => 'string', - width => '3*', + width => '2*', key => 'endpoint', sortable => true, }, + { + label => trm('Latest Handshake'), + type => 'number', + width => '1*', + key => 'latest-handshake', + sortable => true, + format => { + postfix => 's', + maximumFractionDigits => 2, + }, + }, ] }; @@ -317,7 +321,9 @@ sub getTableData ($self, $args, $qx_locale) { my $filter = $args->{formData}{wg_interface}; my $data = $self->app->wireguardModel->get_peer_table_data($args->{firstRow}, $args->{lastRow}, $filter); if ($args->{sortColumn}) { - $data = $self->app->wireguardModel->sort_table_data($data, $args->{sortColumn}, $args->{sortDesc}); + my $table_cfg = $self->tableCfgHash(); + my $column_type = $table_cfg->{$args->{sortColumn}}{'type'} // 'string'; + $data = $self->app->wireguardModel->sort_table_data($data, $args->{sortColumn}, $args->{sortDesc}, $column_type); } # add action set to each row for my $row (@{$data}) { diff --git a/lib/WGwrangler/GuiPlugin/WireguardVersions.pm b/lib/WGwrangler/GuiPlugin/WireguardVersions.pm index 1aec226a..a0ae3007 100644 --- a/lib/WGwrangler/GuiPlugin/WireguardVersions.pm +++ b/lib/WGwrangler/GuiPlugin/WireguardVersions.pm @@ -113,7 +113,7 @@ has actionCfg => sub ($self) { }, actionHandler => sub ($self, $args) { my $hash = $args->{selection}{'hash'}; - die mkerror(4992, "You have to select a peer first") if not $hash; + die mkerror(4992, "You have to select an entry first") if not $hash; $self->app->versionManager->go_back_to_revision($hash); diff --git a/lib/WGwrangler/Model/WireguardDataAdapter.pm b/lib/WGwrangler/Model/WireguardDataAdapter.pm index 6304cc38..0cfe3cf4 100644 --- a/lib/WGwrangler/Model/WireguardDataAdapter.pm +++ b/lib/WGwrangler/Model/WireguardDataAdapter.pm @@ -52,7 +52,7 @@ has 'wg_not_installed' => sub { return defined $ENV{'WGwrangler_NO_WG'} }; -has 'wg_meta' => sub ($self) { +has 'wg_meta' => sub($self) { my $custom_attr_config = { 'email' => { 'validator' => sub($value) {return $value =~ /^\S+@\S+\.\S+$/;} @@ -254,10 +254,20 @@ sub sort_table_data ($self, $data, $key, $order) { my @keys_to_sort = map {$_->{$key}} @{$data}; my @sorted_indexes; if (defined $order) { - @sorted_indexes = sort {($keys_to_sort[$b] // '') cmp ($keys_to_sort[$a] // '')} 0 .. $#keys_to_sort; + if ($data_type eq 'number') { + @sorted_indexes = sort {($keys_to_sort[$b] // 0) <=> ($keys_to_sort[$a] // 0)} 0 .. $#keys_to_sort; + } + else { + @sorted_indexes = sort {($keys_to_sort[$b] // '') cmp ($keys_to_sort[$a] // '')} 0 .. $#keys_to_sort; + } } else { - @sorted_indexes = sort {($keys_to_sort[$a] // '') cmp ($keys_to_sort[$b] // '')} 0 .. $#keys_to_sort; + if ($data_type eq 'number') { + @sorted_indexes = sort {($keys_to_sort[$b] // 0) <=> ($keys_to_sort[$a] // 0)} 0 .. $#keys_to_sort; + } + else { + @sorted_indexes = sort {($keys_to_sort[$a] // '') cmp ($keys_to_sort[$b] // '')} 0 .. $#keys_to_sort; + } } return [ @{$data}[ @sorted_indexes ] ]; } diff --git a/t/dummy_home/show_dummy b/t/dummy_home/show_dummy index cefd9d4f..8e8f0b68 100644 --- a/t/dummy_home/show_dummy +++ b/t/dummy_home/show_dummy @@ -1,4 +1,4 @@ wg0 eOSNqjwOrtIQMOaVxOXXdGjvgQ6gIzS09ltKJMjAPl8= MN6ipoevVEFdsQ+vciTdsgeD30e9w0qT2jPZOFotmHQ= 51861 off -wg0 2mO59ijYCvfJUi6pnNO0H697xiZHTzs4mLA5da5m13w= (none) 89.333.25.1:5589 10.0.10.50/32 0 1234 5985 off -wg0 KpXASgjXPh5xtIxFclQUZzq4PuqbDfHT1CaU8shTsVE= (none) 8.8.8.8:42 10.0.10.51/32 0 4242 4242 off -wg0 Mu46ORALOMuD/T/uyuBiJNecOps4q80TB6BH6F2W9SA= (none) 45.68.254.22:6633 10.0.10.52/32 0 5 10 off +wg0 2mO59ijYCvfJUi6pnNO0H697xiZHTzs4mLA5da5m13w= (none) 89.333.25.1:5589 10.0.10.50/32 52 1234 5985 off +wg0 KpXASgjXPh5xtIxFclQUZzq4PuqbDfHT1CaU8shTsVE= (none) 8.8.8.8:42 10.0.10.51/32 4589 4528289367867 4242 off +wg0 Mu46ORALOMuD/T/uyuBiJNecOps4q80TB6BH6F2W9SA= (none) 45.68.254.22:6633 10.0.10.52/32 4567898 5 10 off From 342375fe72cbbb98ea139abf75aa7b2879c3a796 Mon Sep 17 00:00:00 2001 From: Tobias Bossert Date: Fri, 24 Mar 2023 15:08:09 +0100 Subject: [PATCH 2/2] fixed function signature of sort_table_data Signed-off-by: Tobias Bossert --- lib/WGwrangler/Model/WireguardDataAdapter.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/WGwrangler/Model/WireguardDataAdapter.pm b/lib/WGwrangler/Model/WireguardDataAdapter.pm index 0cfe3cf4..8257c09b 100644 --- a/lib/WGwrangler/Model/WireguardDataAdapter.pm +++ b/lib/WGwrangler/Model/WireguardDataAdapter.pm @@ -250,7 +250,7 @@ sub commit_changes ($self, $ref_integrity_hashes) { $self->apply_config() if $self->is_hot_config; } -sub sort_table_data ($self, $data, $key, $order) { +sub sort_table_data ($self, $data, $key, $order, $data_type) { my @keys_to_sort = map {$_->{$key}} @{$data}; my @sorted_indexes; if (defined $order) {