blib/lib/Net/DNS/Resolver/DoH.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 120 CC 10 SCAR 28.1
CC
10
Cov
0%
nameservers
42.0
_send_doh
30.0
send
2.0
CRAP 16.6
1 package Net::DNS::Resolver::DoH;
2 use strict;
3 use warnings;
4 our $VERSION = '1.20200220'; # VERSION
5 ## ABSTRACT: Experimental DNS over HTTPS for Net::DNS::Resolver
6  
7 use base 'Net::DNS::Resolver';
8  
9 use Net::DNS::Packet;
10 use Net::DNS::Question;
11 use Net::DNS::ZoneFile;
12 use HTTP::Tiny;
13 use MIME::Base64 qw{ encode_base64url };
14  
15 sub send {
16 my $self = shift;
17 my $packet = $self->_make_query_packet(@_);
18 my $packet_data = $packet->data;
19 $self->_reset_errorstring;
20 return $self->_send_doh( $packet, $packet_data );
21 }
22  
23 sub _defaultdohservers {
24 return (
25 'https://dns.google/dns-query{dns}',
26 'https://cloudflare-dns.com/dns-query/{dns}',
27 );
28 }
29  
30 sub nameservers {
31 my $self = shift;
32 $self = $self->_defaultdohservers unless ref($self); ######
33 if ( @_ ) {
34 my @url;
35 foreach my $ns ( grep defined, @_ ) {
36 next if ! $ns =~ /https:\/\//;
37 next if ! $ns =~ /\{dns\}/;
38 push @url, $ns;
39 }
40 $self->{dohnameservers} = \@url;
41 }
42 return @{$self->{dohnameservers}} if defined $self->{dohnameservers};
43 return @{$self->_defaultdohservers};
44 }
45  
46 sub _send_doh {
47 my ( $self, $query, $query_data ) = @_;
48 my @ns = $self->nameservers();
49 my $fallback;
50 my $timeout = $self->{tcp_timeout}; ## TODO
51 foreach my $url (@ns) {
52 my $this_url = $url;
53 my $data = encode_base64url( $query_data );
54 $this_url =~ s/{dns}/?dns=$data/;
55 $self->_diag( 'doh send', "[$url]" );
56 my $http = HTTP::Tiny->new(
57 'keep_alive' => 0,
58 );
59 my $response = $http->get( $this_url );
60 if ( ! $response->{'success'} ) {
61 # return; #### TODO
62 }
63 $self->_diag( 'reply from', "[$url]", $response->{status}, length($response->{content}), 'bytes' );
64  
65 my $reply = Net::DNS::Packet->decode( \$response->{content}, $self->{debug} );
66 $self->errorstring($@);
67  
68 #$reply->from($ip);
69  
70 if ( $self->{tsig_rr} && !$reply->verify($query) ) {
71 $self->errorstring( $reply->verifyerr );
72 next;
73 }
74  
75 my $rcode = $reply->header->rcode;
76 return $reply if $rcode eq 'NOERROR';
77 return $reply if $rcode eq 'NXDOMAIN';
78  
79 #$fallback = $reply; ## TODO
80  
81 return $reply;
82  
83 }
84  
85 #$self->{errorstring} = $fallback->header->rcode if $fallback; TODO
86 #$self->errorstring('query timed out') unless $self->{errorstring};
87 #return $fallback;
88 }
89  
90 #sub srcaddr { ## TODO
91  
92 sub bgsend { die 'not implemented' }
93 sub bgbusy { die 'not implemented' }
94 sub bgread { die 'not implemented' }
95 sub bgisready { die 'not implemented' }
96 sub axfr { die 'not implemented' }
97  
98 ## TODO override methods not available via DoH
99  
100 1;