line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
12
|
|
|
12
|
|
67595
|
use strict; |
|
12
|
|
|
|
|
31
|
|
|
12
|
|
|
|
|
289
|
|
2
|
12
|
|
|
12
|
|
55
|
use warnings; |
|
12
|
|
|
|
|
14
|
|
|
12
|
|
|
|
|
451
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Footprintless::Localhost; |
5
|
|
|
|
|
|
|
$Footprintless::Localhost::VERSION = '1.26'; |
6
|
|
|
|
|
|
|
# ABSTRACT: A localhost alias resolver |
7
|
|
|
|
|
|
|
# PODNAME: Footprintless::Localhost |
8
|
|
|
|
|
|
|
|
9
|
12
|
|
|
12
|
|
341
|
use Log::Any; |
|
12
|
|
|
|
|
9296
|
|
|
12
|
|
|
|
|
59
|
|
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
|
12290
|
return bless( {}, shift )->_init(@_); |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub _add_alias { |
19
|
171
|
|
|
171
|
|
44664
|
my ( $self, $alias ) = @_; |
20
|
|
|
|
|
|
|
|
21
|
171
|
50
|
|
|
|
332
|
if ($alias) { |
22
|
171
|
|
|
|
|
496
|
$logger->debugf( 'adding alias [%s]', $alias ); |
23
|
171
|
|
|
|
|
1731
|
$self->{aliases}{ lc($alias) } = 1; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
171
|
|
|
|
|
413
|
return $self; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _init { |
30
|
36
|
|
|
36
|
|
90
|
my ( $self, %options ) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# what gets returned if is_alias( ); |
33
|
36
|
50
|
|
|
|
173
|
$self->{empty} = exists( $options{empty} ) ? $options{empty} : 1; |
34
|
36
|
|
100
|
|
|
205
|
$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
|
|
|
|
156
|
unless ( $options{none} ); |
38
|
|
|
|
|
|
|
|
39
|
36
|
100
|
|
|
|
101
|
if ( $options{aliases} ) { |
40
|
1
|
|
|
|
|
1
|
$self->_add_alias($_) foreach @{ $options{aliases} }; |
|
1
|
|
|
|
|
4
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
36
|
|
|
|
|
151
|
return $self; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub is_alias { |
47
|
48
|
|
|
48
|
1
|
158
|
my ( $self, $hostname ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
48
|
50
|
|
|
|
132
|
if ($hostname) { |
50
|
|
|
|
|
|
|
return ( $self->{loaded}{'127 subnet'} && $hostname =~ /^\s*$loopback_ipv4_regex\s*$/ ) |
51
|
48
|
|
100
|
|
|
1126
|
|| $self->{aliases}{ lc($hostname) }; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
0
|
|
|
|
|
0
|
return $self->{empty}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub is_loaded { |
59
|
10
|
|
|
10
|
1
|
26
|
my ( $self, $extra ) = @_; |
60
|
10
|
|
33
|
|
|
54
|
return $self->{loaded} && $self->{loaded}{$extra}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub load_127_subnet { |
64
|
32
|
|
|
32
|
1
|
73
|
my ($self) = @_; |
65
|
32
|
|
|
|
|
86
|
$self->{loaded}{'127 subnet'} = 1; |
66
|
32
|
|
|
|
|
132
|
return $self; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub load_all { |
70
|
32
|
|
|
32
|
1
|
94
|
return shift->load_etc_hosts()->load_hostfqdn()->load_hostname()->load_127_subnet(); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub load_etc_hosts { |
74
|
33
|
|
|
33
|
1
|
79
|
my ( $self, $etc_hosts_file ) = @_; |
75
|
33
|
50
|
|
|
|
95
|
$etc_hosts_file = $self->{etc_hosts_file} unless ($etc_hosts_file); |
76
|
|
|
|
|
|
|
|
77
|
33
|
50
|
|
|
|
126
|
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
|
|
|
|
|
1259
|
eval { |
82
|
33
|
|
|
|
|
872
|
open( my $handle, '<', $etc_hosts_file ); |
83
|
33
|
|
|
|
|
483
|
while ( my $line = <$handle> ) { |
84
|
228
|
100
|
|
|
|
1635
|
if ( $line =~ /^$loopback_ipv4_regex\s+(\S.*?)\s*$/ ) { |
85
|
34
|
|
|
|
|
182
|
$self->_add_alias($_) foreach map { lc($_) } split( /\s+/, $1 ); |
|
36
|
|
|
|
|
191
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
33
|
|
|
|
|
248
|
close($handle); |
89
|
|
|
|
|
|
|
}; |
90
|
33
|
|
|
|
|
121
|
$self->{loaded}{'/etc/hosts'} = 1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
33
|
|
|
|
|
117
|
return $self; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub load_hostfqdn { |
97
|
33
|
|
|
33
|
1
|
79
|
my ($self) = @_; |
98
|
|
|
|
|
|
|
|
99
|
33
|
50
|
|
|
|
101
|
if ( !$self->{loaded}{hostfqdn} ) { |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# if fqdn is present, add it too... |
102
|
33
|
|
|
|
|
109
|
$logger->debug('loading hostfqdn'); |
103
|
33
|
|
|
|
|
3937
|
require Net::Domain; |
104
|
33
|
|
|
|
|
68670
|
$self->_add_alias( Net::Domain::hostfqdn() ); |
105
|
33
|
|
|
|
|
100
|
$self->{loaded}{hostfqdn} = 1; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
33
|
|
|
|
|
130
|
return $self; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub load_hostname { |
112
|
33
|
|
|
33
|
1
|
89
|
my ($self) = @_; |
113
|
|
|
|
|
|
|
|
114
|
33
|
50
|
|
|
|
118
|
if ( !$self->{loaded}{hostname} ) { |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# load alias from hostname |
117
|
33
|
|
|
|
|
126
|
$logger->debug('loading hostname'); |
118
|
33
|
|
|
|
|
976
|
eval { |
119
|
|
|
|
|
|
|
# hostname() croaks on fail, so just ignore |
120
|
33
|
|
|
|
|
2579
|
require Sys::Hostname; |
121
|
33
|
|
|
|
|
8284
|
$self->_add_alias( Sys::Hostname::hostname() ); |
122
|
|
|
|
|
|
|
}; |
123
|
33
|
|
|
|
|
85
|
$self->{loaded}{hostname} = 1; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
33
|
|
|
|
|
105
|
return $self; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |