From 593481a6b3b613dcabff936f2c9fedb87f778900 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 14 Aug 2024 18:14:26 +0200 Subject: [PATCH] wireguard.cgi: Don't show an empty table if there are no peers Signed-off-by: Michael Tremer --- html/cgi-bin/wireguard.cgi | 219 +++++++++++++++++++------------------ 1 file changed, 111 insertions(+), 108 deletions(-) diff --git a/html/cgi-bin/wireguard.cgi b/html/cgi-bin/wireguard.cgi index 22ee8fc36..8349fd622 100644 --- a/html/cgi-bin/wireguard.cgi +++ b/html/cgi-bin/wireguard.cgi @@ -515,142 +515,144 @@ END # Show a list with all peers &Header::opensection(); - # Fetch the dump - my %dump = &dump($INTF); + if (%peers) { + # Fetch the dump + my %dump = &dump($INTF); - print < - - - $Lang::tr{'name'} - - - - $Lang::tr{'status'} - - - - $Lang::tr{'remark'} - - - - $Lang::tr{'action'} - - + print < + + + $Lang::tr{'name'} + + + + $Lang::tr{'status'} + + + + $Lang::tr{'remark'} + + + + $Lang::tr{'action'} + + END - # Iterate through all peers... - foreach my $key (sort { $peers{$a}[2] cmp $peers{$b}[2] } keys %peers) { - my $enabled = $peers{$key}[0]; - my $type = $peers{$key}[1]; - my $name = $peers{$key}[2]; - my $pubkey = $peers{$key}[3]; - my $endpoint = $peers{$key}[4]; - my $port = $peers{$key}[5]; - my $routes = $peers{$key}[6]; - my $remarks = &decode_remarks($peers{$key}[7]); + # Iterate through all peers... + foreach my $key (sort { $peers{$a}[2] cmp $peers{$b}[2] } keys %peers) { + my $enabled = $peers{$key}[0]; + my $type = $peers{$key}[1]; + my $name = $peers{$key}[2]; + my $pubkey = $peers{$key}[3]; + my $endpoint = $peers{$key}[4]; + my $port = $peers{$key}[5]; + my $routes = $peers{$key}[6]; + my $remarks = &decode_remarks($peers{$key}[7]); - my $connected = $Lang::tr{'capsclosed'}; - my $country = "ZZ"; - my $location = ""; + my $connected = $Lang::tr{'capsclosed'}; + my $country = "ZZ"; + my $location = ""; - my $gif = ($enabled eq "on") ? "on.gif" : "off.gif"; - my @status = ("status"); + my $gif = ($enabled eq "on") ? "on.gif" : "off.gif"; + my @status = ("status"); - # Fetch the status of the peer (if possible) - my $status = $dump{$pubkey} || (); + # Fetch the status of the peer (if possible) + my $status = $dump{$pubkey} || (); - # Fetch the actual endpoint - my ($actual_endpoint, $actual_port) = split(/:/, $status->{"endpoint"}, 2); + # Fetch the actual endpoint + my ($actual_endpoint, $actual_port) = split(/:/, $status->{"endpoint"}, 2); - # WireGuard performs a handshake very two minutes, so we should be considered online then - my $is_connected = (time - $status->{"latest-handshake"}) <= 120; + # WireGuard performs a handshake very two minutes, so we should be considered online then + my $is_connected = (time - $status->{"latest-handshake"}) <= 120; - # We are connected! - if ($is_connected) { - push(@status, "is-connected"); + # We are connected! + if ($is_connected) { + push(@status, "is-connected"); - $connected = $Lang::tr{'capsopen'}; + $connected = $Lang::tr{'capsopen'}; - # If we have an endpoint lets lookup the country - if ($actual_endpoint) { - $country = &Location::Functions::lookup_country_code($actual_endpoint); + # If we have an endpoint lets lookup the country + if ($actual_endpoint) { + $country = &Location::Functions::lookup_country_code($actual_endpoint); - # If we found a country, let's show it - if ($country) { - my $icon = &Location::Functions::get_flag_icon($country); + # If we found a country, let's show it + if ($country) { + my $icon = &Location::Functions::get_flag_icon($country); - $location = < - $country - + $location = < + $country + EOF + } } - } - # We are not connected... - } else { - push(@status, "is-disconnected"); - } + # We are not connected... + } else { + push(@status, "is-disconnected"); + } - # Escape remarks - if ($remarks) { - $remarks = &Header::escape($remarks); - } + # Escape remarks + if ($remarks) { + $remarks = &Header::escape($remarks); + } - print < - - $name - + print < + + $name + - - $connected - + + $connected + - - $location - + + $location + - - $remarks - + + $remarks + - -
- - - -
- + +
+ + + +
+ - -
- - - -
- + +
+ + + +
+ - -
- - - -
- - + +
+ + + +
+ + END - } + } - print""; + print""; + } # Show controls - print < @@ -661,6 +663,7 @@ END END + &Header::closesection(); &Header::closepage(); -- 2.39.5