Skip to content

Commit

Permalink
now save the hostname if we got it (#535)
Browse files Browse the repository at this point in the history
  • Loading branch information
VVelox authored Jul 10, 2024
1 parent e0a263b commit eafd341
Showing 1 changed file with 29 additions and 23 deletions.
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

0 comments on commit eafd341

Please sign in to comment.