Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wireguard extend now returns the hostname if one is found and does not include the pointless name key #535

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions snmp/wireguard.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ =head1 NAME

=head1 VERSION

0.0.2
0.0.3

=cut

our $VERSION = '0.0.2';
our $VERSION = '0.0.3';

=head1 SYNOPSIS

Expand Down Expand Up @@ -395,14 +395,12 @@ sub getent_hosts {
my @line_split = split( /[\t\ ]+/, $command_split[0] );
if ( defined( $line_split[1] ) ) {
$line_split[1] =~ s/^\.//;
if ( $config->{use_short_hostname} ) {
$line_split[1] =~ s/\..*$//;
}
return $line_split[1];
}
} ## end if ( defined( $command_split[0] ) )
}
return undef;
} ## end sub getent_hosts

our $hosts_read = 0;
our $hosts = {};

Expand All @@ -420,20 +418,17 @@ sub hosts {
my @line_split = split( /[\t\ ]+/, $line );
if ( defined( $line_split[0] ) && defined( $line_split[1] ) ) {
$line_split[1] =~ s/^\.//;
if ( $config->{use_short_hostname} ) {
$line_split[1] =~ s/\..*$//;
}

$hosts->{ $line_split[0] } = $line_split[1];
}
} ## end foreach my $line (@hosts_split)
}
};
} ## end if ( !$hosts_read )
if ( defined( $hosts->{$ip} ) ) {
return $hosts->{$ip};
}
return undef;
} ## end sub hosts

my @interfaces = keys(%wg_info);
my @resolvers = split( /\,+/, $config->{pubkey_resolvers} );
foreach my $interface (@interfaces) {
Expand All @@ -445,8 +440,9 @@ sub hosts {
my $resolver = $resolvers[$resolvers_int];
if ( !$matched && $resolver eq 'config' ) {
if ( defined( $config->{public_key_to_arbitrary_name}{$pubkey} ) ) {
$wg_info{$interface}{$pubkey}{name} = $config->{public_key_to_arbitrary_name}{$pubkey};
$matched = 1;
$wg_info{$interface}{$pubkey}{name} = $config->{public_key_to_arbitrary_name}{$pubkey};
$wg_info{$interface}{$pubkey}{hostname} = undef;
$matched = 1;
}
} elsif ( !$matched && $resolver eq 'endpoint_if_first_allowed_is_subnet_use_getent' ) {
if ( defined( $wg_info{$interface}{$pubkey}{allowed_ips}[0] )
Expand All @@ -455,7 +451,7 @@ sub hosts {
{
my $name = getent_hosts( $wg_info{$interface}{$pubkey}{endpoint_host} );
if ( defined($name) ) {
$wg_info{$interface}{$pubkey}{name} = $name;
$wg_info{$interface}{$pubkey}{hostname} = $name;
$matched = 1;
}
} ## end if ( defined( $wg_info{$interface}{$pubkey...}))
Expand All @@ -466,7 +462,7 @@ sub hosts {
{
my $name = hosts( $wg_info{$interface}{$pubkey}{endpoint_host} );
if ( defined($name) ) {
$wg_info{$interface}{$pubkey}{name} = $name;
$wg_info{$interface}{$pubkey}{hostname} = $name;
$matched = 1;
}
} ## end if ( defined( $wg_info{$interface}{$pubkey...}))
Expand All @@ -475,15 +471,15 @@ sub hosts {
&& $wg_info{$interface}{$pubkey}{allowed_ips}[0] =~ /\//
&& defined( $wg_info{$interface}{$pubkey}{endpoint_host} ) )
{
$wg_info{$interface}{$pubkey}{name} = $wg_info{$interface}{$pubkey}{endpoint_host};
$wg_info{$interface}{$pubkey}{hostname} = $wg_info{$interface}{$pubkey}{endpoint_host};
$matched = 1;
}
} elsif ( !$matched && $resolver eq 'first_allowed_use_getent' ) {
if ( defined( $wg_info{$interface}{$pubkey}{allowed_ips}[0] ) ) {
my $host = $wg_info{$interface}{$pubkey}{allowed_ips}[0];
my $name = getent_hosts($host);
if ( defined($name) ) {
$wg_info{$interface}{$pubkey}{name} = $name;
$wg_info{$interface}{$pubkey}{hostname} = $name;
$matched = 1;
}
}
Expand All @@ -492,12 +488,12 @@ sub hosts {
my $host = $wg_info{$interface}{$pubkey}{allowed_ips}[0];
my $name = hosts($host);
if ( defined($name) ) {
$wg_info{$interface}{$pubkey}{name} = $name;
$wg_info{$interface}{$pubkey}{hostname} = $name;
$matched = 1;
}
}
} elsif ( !$matched && $resolver eq 'first_allowed_use_ip' ) {
$wg_info{$interface}{$pubkey}{name} = $wg_info{$interface}{$pubkey}{allowed_ips}[0];
$wg_info{$interface}{$pubkey}{hostname} = $wg_info{$interface}{$pubkey}{allowed_ips}[0];
$matched = 1;
}
$resolvers_int++;
Expand All @@ -514,15 +510,25 @@ sub hosts {
foreach my $interface (@interfaces) {
my @pubkeys = keys( %{ $wg_info{$interface} } );
foreach my $pubkey (@pubkeys) {
if ( defined( $wg_info{$interface}{$pubkey}{name} ) ) {
if ( defined( $wg_info{$interface}{$pubkey}{name} ) || $wg_info{$interface}{$pubkey}{hostname} ) {
if ( !defined( $return_json->{data}{$interface} ) ) {
$return_json->{data}{$interface} = {};
}
$return_json->{data}{$interface}{ $wg_info{$interface}{$pubkey}{name} } = $wg_info{$interface}{$pubkey};
my $name;
if ( defined( $wg_info{$interface}{$pubkey}{name} ) ) {
$name = $wg_info{$interface}{$pubkey}{name};
delete( $wg_info{$interface}{$pubkey}{name} );
} else {
$name = $wg_info{$interface}{$pubkey}{hostname};
if ( $config->{use_short_hostname} ) {
$name =~ s/\..*$//;
}
}
$return_json->{data}{$interface}{$name} = $wg_info{$interface}{$pubkey};
if ( $config->{include_pubkey} ) {
$return_json->{data}{$interface}{ $wg_info{$interface}{$pubkey}{name} }{pubkey} = $pubkey;
$return_json->{data}{$interface}{$name}{pubkey} = $pubkey;
} else {
$return_json->{data}{$interface}{ $wg_info{$interface}{$pubkey}{name} }{pubkey} = undef;
$return_json->{data}{$interface}{$name}{pubkey} = undef;
}
} ## end if ( defined( $wg_info{$interface}{$pubkey...}))
} ## end foreach my $pubkey (@pubkeys)
Expand Down
Loading