| 1 | # PODNAME: Domain::Details | ||
| 2 | # ABSTRACT: Domain class with DNS/SSL/WHOIS fields | ||
| 3 | |||
| 4 | use v5.36; | ||
| 5 | use autouse 'Carp' => qw( carp croak ); | ||
| 6 | use autouse 'Data::Printer' => qw( p ); | ||
| 7 | use Object::Pad 0.78 ':experimental(init_expr)'; | ||
| 8 | |||
| 9 | package Domain::Details; | ||
| 10 | |||
| 11 | # @formatter:off | ||
| 12 | class Domain::Details :strict( params ) :does(Domain::Details::SSL) { | ||
| 13 | # @formatter:on | ||
| 14 | |||
| 15 | use experimental qw( try ); | ||
| 16 | use Syntax::Keyword::Match; | ||
| 17 | use Net::Domain::ExpireDate; # Function: expire_date | ||
| 18 | use Domain::PublicSuffix; # Method: get_root_domain | ||
| 19 | use POSIX; # Functions: setlocale, LC_ALL | ||
| 20 | use Net::DNS; | ||
| 21 | use Geo::IP; | ||
| 22 | use Term::ANSIColor qw( colorstrip ); | ||
| 23 | use Clipboard; # Class method: copy_to_all_selections | ||
| 24 | |||
| 25 | # @formatter:off | ||
| 26 | |||
| 27 | field $domain :param :reader; | ||
| 28 | |||
| 29 | |||
| 30 | field $description :param :accessor = undef; | ||
| 31 | |||
| 32 | |||
| 33 | method whois_expiration ( $format //= '%B %d, %Y' ) { | ||
| 34 | my $publicsuffix = Domain::PublicSuffix -> new; | ||
| 35 | setlocale( LC_ALL , 'en_US.UTF-8' ); | ||
| 36 | return expire_date( $publicsuffix -> get_root_domain( $domain ) , $format ); # domain without the www. prefix | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | method dns ( ) { | ||
| 41 | # @formatter:on | ||
| 42 | my $resolver = Net::DNS::Resolver -> new; | ||
| 43 | my $geo = Geo::IP -> open( '/usr/share/GeoIP/GeoIP.dat' ); | ||
| 44 | # Set path explicitly because Perlbrew 5.36 on Debian fails to open for searching in /usr/local when Geo::IP is being installed with "cpan" | ||
| 45 | # Installing the Debian package libgeo-ip-perl probably fixes that | ||
| 46 | |||
| 47 | my ( $cname , $ptr ); | ||
| 48 | |||
| 49 | my $a = $resolver -> query( $domain , 'A' ); | ||
| 50 | my @a = $a -> answer if defined $a; | ||
| 51 | |||
| 52 | my $mx = $resolver -> query( $domain , 'MX' ); | ||
| 53 | my @mx = $mx -> answer if defined $mx; | ||
| 54 | |||
| 55 | my $ns = $resolver -> query( $domain , 'NS' ); | ||
| 56 | my @ns = $ns -> answer if defined $ns; | ||
| 57 | |||
| 58 | my $txt = $resolver -> query( $domain , 'TXT' ); | ||
| 59 | my @txt = $txt -> answer if defined $txt; | ||
| 60 | |||
| 61 | my $soa = $resolver -> query( $domain , 'SOA' ); | ||
| 62 | my @soa = $soa -> answer if defined $soa; | ||
| 63 | |||
| 64 | my $answer; | ||
| 65 | |||
| 66 | my %dns = ( | ||
| 67 | a => [] , | ||
| 68 | cname => '' , | ||
| 69 | ptr => [] , | ||
| 70 | mx => [] , | ||
| 71 | ns => [] , | ||
| 72 | txt => [] , | ||
| 73 | ); # to push individual record values here | ||
| 74 | |||
| 75 | for my $record ( @a , @mx , @ns , @txt , @soa ) { | ||
| 76 | match( $record -> type : eq ) | ||
| 77 | { | ||
| 78 | case( 'A' ) | ||
| 79 | { | ||
| 80 | $answer .= sprintf( "A:\t%s (%s)\n" , $record -> address , $geo -> country_code_by_addr( $record -> address )); | ||
| 81 | push( $dns{a} -> @* , $record -> address ); | ||
| 82 | $ptr = $resolver -> query( $record -> address , 'PTR' ); # Net::DNS::Packet | ||
| 83 | if ( $ptr ) { | ||
| 84 | my @ptr = $ptr -> answer; # [ Net::DNS:RR, ... ] | ||
| 85 | $answer .= Term::ANSIColor::colored( [ 'bright_cyan' ] , sprintf( "P:\t%s (%s)\n" , $ptr[0] -> ptrdname , $geo -> country_code_by_name( $ptr[0] -> ptrdname ))); | ||
| 86 | # eq. rdatastr (undocumented) | ||
| 87 | push( $dns{ptr} -> @* , $ptr[0] -> ptrdname ); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | case( 'CNAME' ) | ||
| 91 | { | ||
| 92 | # fetched by A, for instance | ||
| 93 | $cname = Term::ANSIColor::colored( [ 'bright_magenta' ] , sprintf( "C:\t%s (%s)\n" , $record -> cname , $geo -> country_code_by_name( $record -> cname ))); | ||
| 94 | # 4 - 5 times (seemingly by A) | ||
| 95 | $dns{cname} = $record -> cname; | ||
| 96 | } | ||
| 97 | case( 'MX' ) | ||
| 98 | { | ||
| 99 | $answer .= Term::ANSIColor::colored( [ 'bright_yellow' ] , sprintf( "M:\t%s (%s)\n" , $record -> exchange , $geo -> country_code_by_name( $record -> exchange ))); | ||
| 100 | push( $dns{mx} -> @* , $record -> exchange ); | ||
| 101 | } | ||
| 102 | case( 'NS' ) | ||
| 103 | { | ||
| 104 | $answer .= Term::ANSIColor::colored( [ 'bright_green' ] , sprintf( "N:\t%s (%s)\n" , $record -> nsdname , $geo -> country_code_by_name( $record -> nsdname ))); | ||
| 105 | push $dns{ns} -> @* , $record -> nsdname; | ||
| 106 | } | ||
| 107 | case( 'TXT' ) | ||
| 108 | { | ||
| 109 | $answer .= Term::ANSIColor::colored( [ 'bright_white' ] , sprintf( "T:\t%s\n" , $record -> txtdata )); | ||
| 110 | push $dns{txt} -> @* , $record -> txtdata; | ||
| 111 | } | ||
| 112 | case( 'SOA' ) | ||
| 113 | { # fetched by A, for instance | ||
| 114 | $answer .= Term::ANSIColor::colored( | ||
| 115 | [ 'bright_red' ] , | ||
| 116 | sprintf( "S:\t%s %s\n" , | ||
| 117 | $record -> mname , | ||
| 118 | $record -> rname | ||
| 119 | ) | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | $answer = $answer . $cname if defined $cname; | ||
| 127 | return $answer; | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | # @formatter:off | ||
| 132 | method summary () { | ||
| 133 | # @formatter:on | ||
| 134 | |||
| 135 | my $summary = <<~ "SUMMARY"; | ||
| 136 | SSL Expiration : @{[$self -> ssl_expiration]} @{[$self -> ssl_expires_soon ? '(Expires Soon)' : '' ]} | ||
| 137 | SSL Issue : @{[$self -> ssl_issue]} | ||
| 138 | WHOIS Expiration : @{[$self -> whois_expiration]} | ||
| 139 | |||
| 140 | DNS : | ||
| 141 | |||
| 142 | @{[$self->dns]} | ||
| 143 | SUMMARY | ||
| 144 | say $summary; | ||
| 145 | Clipboard -> copy_to_all_selections( colorstrip $summary ); | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | __END__ |