From: Michael Tremer Date: Tue, 22 Apr 2025 17:47:23 +0000 (+0200) Subject: wireguard-functions.pl: Return a hash reference instead of a hash when loading a... X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3f538a827627ae70fc2c892abab6e8d73f73aaad;p=ipfire-2.x.git wireguard-functions.pl: Return a hash reference instead of a hash when loading a peer Perl is so absolutely fucking broken and dealing with hashes is such a massive pain in the rear. I don't want to see this any more. Signed-off-by: Michael Tremer --- diff --git a/config/cfgroot/wireguard-functions.pl b/config/cfgroot/wireguard-functions.pl index d38a46918..ab1e4a49b 100644 --- a/config/cfgroot/wireguard-functions.pl +++ b/config/cfgroot/wireguard-functions.pl @@ -186,18 +186,18 @@ sub load_peer($) { "KEEPALIVE" => $peers{$key}[12], ); - return %peer; + return \%peer; } sub get_peer_by_name($) { my $name = shift; foreach my $key (keys %peers) { - my %peer = &load_peer($key); + my $peer = &load_peer($key); # Return the peer if the name matches - if ($peer{"NAME"} eq $name) { - return %peer; + if ($peer->{"NAME"} eq $name) { + return $peer; } } @@ -373,15 +373,15 @@ sub generate_peer_configuration($$) { my @conf = (); # Load the peer - my %peer = &load_peer($key); + my $peer = &load_peer($key); # Return if we could not find the peer - return undef unless (%peer); + return undef unless ($peer); my @allowed_ips = (); # Convert all subnets into CIDR notation - foreach my $subnet ($peer{'LOCAL_SUBNETS'}) { + foreach my $subnet ($peer->{'LOCAL_SUBNETS'}) { my $netaddress = &Network::get_netaddress($subnet); my $prefix = &Network::get_prefix($subnet); @@ -395,32 +395,32 @@ sub generate_peer_configuration($$) { my $endpoint = &get_endpoint(); # Net-2-Net - if ($peer{'TYPE'} eq "net") { + if ($peer->{'TYPE'} eq "net") { # Derive our own public key - my $public_key = &derive_public_key($peer{'PRIVATE_KEY'}); + my $public_key = &derive_public_key($peer->{'PRIVATE_KEY'}); push(@conf, "[Interface]", "PrivateKey = $private_key", - "Port = $peer{'ENDPOINT_PORT'}", + "Port = $peer->{'ENDPOINT_PORT'}", "", "[Peer]", - "Endpoint = ${endpoint}:$peer{'PORT'}", + "Endpoint = ${endpoint}:$peer->{'PORT'}", "PublicKey = $public_key", - "PresharedKey = $peer{'PSK'}", + "PresharedKey = $peer->{'PSK'}", "AllowedIPs = " . join(", ", @allowed_ips), - "PersistentKeepalive = $peer{'KEEPALIVE'}", + "PersistentKeepalive = $peer->{'KEEPALIVE'}", ); # Host-2-Net - } elsif ($peer{'TYPE'} eq "host") { + } elsif ($peer->{'TYPE'} eq "host") { # Fetch any DNS servers for hosts my @dns = split(/\|/, $settings{'CLIENT_DNS'}); push(@conf, "[Interface]", "PrivateKey = $private_key", - "Address = $peer{'CLIENT_ADDRESS'}", + "Address = $peer->{'CLIENT_ADDRESS'}", ); # Optionally add DNS servers @@ -436,7 +436,7 @@ sub generate_peer_configuration($$) { "[Peer]", "Endpoint = ${endpoint}:$settings{'PORT'}", "PublicKey = $settings{'PUBLIC_KEY'}", - "PresharedKey = $peer{'PSK'}", + "PresharedKey = $peer->{'PSK'}", "AllowedIPs = " . join(", ", @allowed_ips), "PersistentKeepalive = $DEFAULT_KEEPALIVE", )); diff --git a/html/cgi-bin/firewall.cgi b/html/cgi-bin/firewall.cgi index 3c02a9a92..855be095d 100644 --- a/html/cgi-bin/firewall.cgi +++ b/html/cgi-bin/firewall.cgi @@ -1202,13 +1202,13 @@ EOF # Sort peers by name foreach my $key (sort { $Wireguard::peers{$a}[2] cmp $Wireguard::peers{$b}[2] } keys %Wireguard::peers) { # Load the peer - my %peer = &Wireguard::load_peer($key); + my $peer = &Wireguard::load_peer($key); # Is this peer selected? - my $selected = ($fwdfwsettings{$fwdfwsettings{$grp}} eq $peer{'NAME'}) ? "selected" : ""; + my $selected = ($fwdfwsettings{$fwdfwsettings{$grp}} eq $peer->{'NAME'}) ? "selected" : ""; print <$peer{'NAME'} + EOF } diff --git a/html/cgi-bin/wireguard.cgi b/html/cgi-bin/wireguard.cgi index 8e763540a..8bfb5df95 100644 --- a/html/cgi-bin/wireguard.cgi +++ b/html/cgi-bin/wireguard.cgi @@ -332,7 +332,7 @@ END my $key = $cgiparams{'KEY'}; # Load the existing peer - my %peer = &Wireguard::load_peer($key); + my $peer = &Wireguard::load_peer($key); # Check if the name is valid unless (&Wireguard::name_is_valid($cgiparams{"NAME"})) { @@ -432,7 +432,7 @@ END # 3 = Public Key $cgiparams{"PUBLIC_KEY"}, # 4 = Private Key - $peer{"PRIVATE_KEY"}, + $peer->{"PRIVATE_KEY"}, # 5 = Port $cgiparams{"PORT"}, # 6 = Endpoint Address @@ -581,7 +581,7 @@ END &Header::openpage($Lang::tr{'wireguard'}, 1, ''); # Load the peer - my %peer = &Wireguard::load_peer($key); + my $peer = &Wireguard::load_peer($key); # Generate the client configuration my $config = &Wireguard::generate_peer_configuration($key, $private_key); @@ -614,10 +614,10 @@ END $config = &MIME::Base64::encode_base64($config); # Open a new box - &Header::openbox('100%', '', "$Lang::tr{'wg peer configuration'}: $peer{'NAME'}"); + &Header::openbox('100%', '', "$Lang::tr{'wg peer configuration'}: $peer->{'NAME'}"); # Make the filename for files - my $filename = &Header::normalize($peer{'NAME'}) . ".conf"; + my $filename = &Header::normalize($peer->{'NAME'}) . ".conf"; print <