line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
12
|
|
|
12
|
|
74005
|
use strict; |
|
12
|
|
|
|
|
29
|
|
|
12
|
|
|
|
|
284
|
|
2
|
12
|
|
|
12
|
|
47
|
use warnings; |
|
12
|
|
|
|
|
20
|
|
|
12
|
|
|
|
|
452
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
package Footprintless::Localhost; |
5
|
|
|
|
|
|
|
$Footprintless::Localhost::VERSION = '1.28'; |
6
|
|
|
|
|
|
|
# ABSTRACT: A localhost alias resolver |
7
|
|
|
|
|
|
|
# PODNAME: Footprintless::Localhost |
8
|
|
|
|
|
|
|
|
9
|
12
|
|
|
12
|
|
414
|
use Log::Any; |
|
12
|
|
|
|
|
8534
|
|
|
12
|
|
|
|
|
66
|
|
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
|
12851
|
return bless( {}, shift )->_init(@_); |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub _add_alias { |
19
|
171
|
|
|
171
|
|
57677
|
my ( $self, $alias ) = @_; |
20
|
|
|
|
|
|
|
|
21
|
171
|
50
|
|
|
|
349
|
if ($alias) { |
22
|
171
|
|
|
|
|
530
|
$logger->debugf( 'adding alias [%s]', $alias ); |
23
|
171
|
|
|
|
|
1808
|
$self->{aliases}{ lc($alias) } = 1; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
171
|
|
|
|
|
386
|
return $self; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _init { |
30
|
36
|
|
|
36
|
|
82
|
my ( $self, %options ) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# what gets returned if is_alias( ); |
33
|
36
|
50
|
|
|
|
165
|
$self->{empty} = exists( $options{empty} ) ? $options{empty} : 1; |
34
|
36
|
|
100
|
|
|
199
|
$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
|
|
|
|
174
|
unless ( $options{none} ); |
38
|
|
|
|
|
|
|
|
39
|
36
|
100
|
|
|
|
110
|
if ( $options{aliases} ) { |
40
|
1
|
|
|
|
|
2
|
$self->_add_alias($_) foreach @{ $options{aliases} }; |
|
1
|
|
|
|
|
2
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
36
|
|
|
|
|
142
|
return $self; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub is_alias { |
47
|
48
|
|
|
48
|
1
|
132
|
my ( $self, $hostname ) = @_; |
48
|
|
|
|
|
|
|
|
49
|
48
|
50
|
|
|
|
120
|
if ($hostname) { |
50
|
|
|
|
|
|
|
return ( $self->{loaded}{'127 subnet'} && $hostname =~ /^\s*$loopback_ipv4_regex\s*$/ ) |
51
|
48
|
|
100
|
|
|
977
|
|| $self->{aliases}{ lc($hostname) }; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
0
|
|
|
|
|
0
|
return $self->{empty}; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub is_loaded { |
59
|
10
|
|
|
10
|
1
|
25
|
my ( $self, $extra ) = @_; |
60
|
10
|
|
33
|
|
|
50
|
return $self->{loaded} && $self->{loaded}{$extra}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub load_127_subnet { |
64
|
32
|
|
|
32
|
1
|
61
|
my ($self) = @_; |
65
|
32
|
|
|
|
|
79
|
$self->{loaded}{'127 subnet'} = 1; |
66
|
32
|
|
|
|
|
127
|
return $self; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub load_all { |
70
|
32
|
|
|
32
|
1
|
101
|
return shift->load_etc_hosts()->load_hostfqdn()->load_hostname()->load_127_subnet(); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub load_etc_hosts { |
74
|
33
|
|
|
33
|
1
|
98
|
my ( $self, $etc_hosts_file ) = @_; |
75
|
33
|
50
|
|
|
|
98
|
$etc_hosts_file = $self->{etc_hosts_file} unless ($etc_hosts_file); |
76
|
|
|
|
|
|
|
|
77
|
33
|
50
|
|
|
|
106
|
if ( !$self->{loaded}{'/etc/hosts'} ) { |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# attempt to load aliases from hosts file |
80
|
33
|
|
|
|
|
117
|
$logger->debug( 'loading /etc/hosts from: %s', $etc_hosts_file ); |
81
|
33
|
|
|
|
|
329
|
eval { |
82
|
33
|
|
|
|
|
1107
|
open( my $handle, '<', $etc_hosts_file ); |
83
|
33
|
|
|
|
|
632
|
while ( my $line = <$handle> ) { |
84
|
228
|
100
|
|
|
|
1775
|
if ( $line =~ /^$loopback_ipv4_regex\s+(\S.*?)\s*$/ ) { |
85
|
34
|
|
|
|
|
171
|
$self->_add_alias($_) foreach map { lc($_) } split( /\s+/, $1 ); |
|
36
|
|
|
|
|
189
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
33
|
|
|
|
|
352
|
close($handle); |
89
|
|
|
|
|
|
|
}; |
90
|
33
|
|
|
|
|
133
|
$self->{loaded}{'/etc/hosts'} = 1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
33
|
|
|
|
|
103
|
return $self; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub load_hostfqdn { |
97
|
33
|
|
|
33
|
1
|
89
|
my ($self) = @_; |
98
|
|
|
|
|
|
|
|
99
|
33
|
50
|
|
|
|
96
|
if ( !$self->{loaded}{hostfqdn} ) { |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# if fqdn is present, add it too... |
102
|
33
|
|
|
|
|
119
|
$logger->debug('loading hostfqdn'); |
103
|
33
|
|
|
|
|
4811
|
require Net::Domain; |
104
|
33
|
|
|
|
|
82586
|
$self->_add_alias( Net::Domain::hostfqdn() ); |
105
|
33
|
|
|
|
|
93
|
$self->{loaded}{hostfqdn} = 1; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
33
|
|
|
|
|
119
|
return $self; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub load_hostname { |
112
|
33
|
|
|
33
|
1
|
74
|
my ($self) = @_; |
113
|
|
|
|
|
|
|
|
114
|
33
|
50
|
|
|
|
131
|
if ( !$self->{loaded}{hostname} ) { |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# load alias from hostname |
117
|
33
|
|
|
|
|
142
|
$logger->debug('loading hostname'); |
118
|
33
|
|
|
|
|
294
|
eval { |
119
|
|
|
|
|
|
|
# hostname() croaks on fail, so just ignore |
120
|
33
|
|
|
|
|
4150
|
require Sys::Hostname; |
121
|
33
|
|
|
|
|
9833
|
$self->_add_alias( Sys::Hostname::hostname() ); |
122
|
|
|
|
|
|
|
}; |
123
|
33
|
|
|
|
|
69
|
$self->{loaded}{hostname} = 1; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
33
|
|
|
|
|
104
|
return $self; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
__END__ |