line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
12
|
|
|
12
|
|
85924
|
use strict; |
|
12
|
|
|
|
|
29
|
|
|
12
|
|
|
|
|
6803
|
|
2
|
12
|
|
|
12
|
|
66
|
use warnings; |
|
12
|
|
|
|
|
18
|
|
|
12
|
|
|
|
|
530
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Footprintless::Localhost; |
5
|
|
|
|
|
|
|
$Footprintless::Localhost::VERSION = '1.29'; |
6
|
|
|
|
|
|
|
# ABSTRACT: A localhost alias resolver |
7
|
|
|
|
|
|
|
# PODNAME: Footprintless::Localhost |
8
|
|
|
|
|
|
|
|
9
|
12
|
|
|
12
|
|
477
|
use Log::Any; |
|
12
|
|
|
|
|
8779
|
|
|
12
|
|
|
|
|
73
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $logger = Log::Any->get_logger(); |
12
|
|
|
|
|
|
|
my $loopback_ipv4_regex = qr/127(?:.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
36
|
|
|
36
|
1
|
14875
|
return bless( {}, shift )->_init(@_); |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub _add_alias { |
19
|
171
|
|
|
171
|
|
64350
|
my ( $self, $alias ) = @_; |
20
|
|
|
|
|
|
|
|
21
|
171
|
50
|
|
|
|
341
|
if ($alias) { |
22
|
171
|
|
|
|
|
518
|
$logger->debugf( 'adding alias [%s]', $alias ); |
23
|
171
|
|
|
|
|
2287
|
$self->{aliases}{ lc($alias) } = 1; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
171
|
|
|
|
|
445
|
return $self; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _init { |
30
|
36
|
|
|
36
|
|
113
|
my ( $self, %options ) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# what gets returned if is_alias( ); |
33
|
36
|
50
|
|
|
|
217
|
$self->{empty} = exists( $options{empty} ) ? $options{empty} : 1; |
34
|
36
|
|
100
|
|
|
214
|
$self->{etc_hosts_file} = $options{etc_hosts_file} || '/etc/hosts'; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
$self->_add_alias('localhost')->_add_alias('127.0.0.1') |
37
|
36
|
100
|
|
|
|
184
|
unless ( $options{none} ); |
38
|
|
|
|
|
|
|
|
39
|
36
|
100
|
|
|
|
92
|
if ( $options{aliases} ) { |
40
|
1
|
|
|
|
|
3
|
$self->_add_alias($_) foreach @{ $options{aliases} }; |
|
1
|
|
|
|
|
4
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
36
|
|
|
|
|
139
|
return $self; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub is_alias { |
47
|
48
|
|
|
48
|
1
|
143
|
my ( $self, $hostname ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
48
|
50
|
|
|
|
189
|
if ($hostname) { |
50
|
|
|
|
|
|
|
return ( $self->{loaded}{'127 subnet'} && $hostname =~ /^\s*$loopback_ipv4_regex\s*$/ ) |
51
|
48
|
|
100
|
|
|
1102
|
|| $self->{aliases}{ lc($hostname) }; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
0
|
|
|
|
|
0
|
return $self->{empty}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub is_loaded { |
59
|
10
|
|
|
10
|
1
|
45
|
my ( $self, $extra ) = @_; |
60
|
10
|
|
33
|
|
|
64
|
return $self->{loaded} && $self->{loaded}{$extra}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub load_127_subnet { |
64
|
32
|
|
|
32
|
1
|
85
|
my ($self) = @_; |
65
|
32
|
|
|
|
|
172
|
$self->{loaded}{'127 subnet'} = 1; |
66
|
32
|
|
|
|
|
145
|
return $self; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub load_all { |
70
|
32
|
|
|
32
|
1
|
100
|
return shift->load_etc_hosts()->load_hostfqdn()->load_hostname()->load_127_subnet(); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub load_etc_hosts { |
74
|
33
|
|
|
33
|
1
|
64
|
my ( $self, $etc_hosts_file ) = @_; |
75
|
33
|
50
|
|
|
|
102
|
$etc_hosts_file = $self->{etc_hosts_file} unless ($etc_hosts_file); |
76
|
|
|
|
|
|
|
|
77
|
33
|
50
|
|
|
|
140
|
if ( !$self->{loaded}{'/etc/hosts'} ) { |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# attempt to load aliases from hosts file |
80
|
33
|
|
|
|
|
124
|
$logger->debug( 'loading /etc/hosts from: %s', $etc_hosts_file ); |
81
|
33
|
|
|
|
|
324
|
eval { |
82
|
33
|
|
|
|
|
1231
|
open( my $handle, '<', $etc_hosts_file ); |
83
|
33
|
|
|
|
|
678
|
while ( my $line = <$handle> ) { |
84
|
228
|
100
|
|
|
|
1930
|
if ( $line =~ /^$loopback_ipv4_regex\s+(\S.*?)\s*$/ ) { |
85
|
34
|
|
|
|
|
233
|
$self->_add_alias($_) foreach map { lc($_) } split( /\s+/, $1 ); |
|
36
|
|
|
|
|
219
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
33
|
|
|
|
|
371
|
close($handle); |
89
|
|
|
|
|
|
|
}; |
90
|
33
|
|
|
|
|
149
|
$self->{loaded}{'/etc/hosts'} = 1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
33
|
|
|
|
|
133
|
return $self; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub load_hostfqdn { |
97
|
33
|
|
|
33
|
1
|
79
|
my ($self) = @_; |
98
|
|
|
|
|
|
|
|
99
|
33
|
50
|
|
|
|
108
|
if ( !$self->{loaded}{hostfqdn} ) { |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# if fqdn is present, add it too... |
102
|
33
|
|
|
|
|
117
|
$logger->debug('loading hostfqdn'); |
103
|
33
|
|
|
|
|
4749
|
require Net::Domain; |
104
|
33
|
|
|
|
|
95816
|
$self->_add_alias( Net::Domain::hostfqdn() ); |
105
|
33
|
|
|
|
|
111
|
$self->{loaded}{hostfqdn} = 1; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
33
|
|
|
|
|
149
|
return $self; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub load_hostname { |
112
|
33
|
|
|
33
|
1
|
109
|
my ($self) = @_; |
113
|
|
|
|
|
|
|
|
114
|
33
|
50
|
|
|
|
121
|
if ( !$self->{loaded}{hostname} ) { |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# load alias from hostname |
117
|
33
|
|
|
|
|
135
|
$logger->debug('loading hostname'); |
118
|
33
|
|
|
|
|
289
|
eval { |
119
|
|
|
|
|
|
|
# hostname() croaks on fail, so just ignore |
120
|
33
|
|
|
|
|
4101
|
require Sys::Hostname; |
121
|
33
|
|
|
|
|
9619
|
$self->_add_alias( Sys::Hostname::hostname() ); |
122
|
|
|
|
|
|
|
}; |
123
|
33
|
|
|
|
|
150
|
$self->{loaded}{hostname} = 1; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
33
|
|
|
|
|
138
|
return $self; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |