| 1 | package Net::DNS::CloudFlare::DDNS; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Object Orientated Dynamic DNS Interface to CloudFlare DNS | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Modern::Perl '2012'; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use autodie ':all'; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | no indirect 'fatal'; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Moose; use MooseX::StrictConstructor; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Types::Standard qw( Bool Str); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Net::DNS::CloudFlare::DDNS::Types qw( CloudFlareClient LWPUserAgent); | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use TryCatch; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use List::Util 'shuffle'; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | # first dotted decimal version and args as hash interface | ||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use CloudFlare::Client 'v0.55.1'; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 2 | our $VERSION = 'v0.63.1'; # VERSION | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 2 | has 'verbose' => ( is => 'rw', isa => Bool); | |||||||||||||||||||||||||||||||
| 23 | # CF credentials | ||||||||||||||||||||||||||||||||
| 24 | 2 | has '_user' => ( is => 'ro', isa => Str, required => 1, init_arg => 'user'); | |||||||||||||||||||||||||||||||
| 25 | 2 | has '_key' => ( is => 'ro', isa => Str, required => 1, init_arg => 'apikey'); | |||||||||||||||||||||||||||||||
| 26 | # Configuration of zones, and their domains, to update | ||||||||||||||||||||||||||||||||
| 27 | 2 | has '_config' => ( is => 'ro', required => 1, init_arg => 'zones'); | |||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # Provides CF access | ||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub _buildApi { CloudFlare::Client::->new( user => $_[0]->_user, | ||||||||||||||||||||||||||||||
|
Subroutine
_buildApi: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | apikey => $_[0]->_key)} | ||||||||||||||||||||||||||||||||
| 32 | 2 | has '_api' => ( is => 'ro', isa => CloudFlareClient, builder => '_buildApi', | |||||||||||||||||||||||||||||||
| 33 | lazy => 1, init_arg => undef); | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | # Fetch zone IDs for a single zone | ||||||||||||||||||||||||||||||||
| 36 | # The api call can fail and this will die | ||||||||||||||||||||||||||||||||
| 37 | # Returns a map of domain => id | ||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | sub _getDomainIds { | ||||||||||||||||||||||||||||||
|
Subroutine
_getDomainIds: called CC=6 SCAR=31.5
| |||||||||||||||||||||||||||||||||
| 39 | 2 | Readonly my $self => shift; Readonly my $zone => shift; | |||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | # Query CloudFlare | ||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | say "Trying domain IDs lookup for $zone" if $self->verbose; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 2 | Readonly my $info => $self->_api->rec_load_all( z => $zone); | |||||||||||||||||||||||||||||||
| 44 | # Filter to just A records and get a list of [domain => id] | ||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my @pairs = map { $_->{type} eq 'A' ? [ $_->{name} => $_->{rec_id} ] | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 0 | : () } @{ $info->{recs}{objs}}; | |||||||||||||||||||||||||||||||
| 47 | # Localise hostnames to within zone, set zone itself to undef | ||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | map { $_->[0] eq $zone ? $_->[0] = undef : $_->[0] =~ s/\.$zone$//} | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | @pairs; | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | # Build into a hash of domain => id | ||||||||||||||||||||||||||||||||
| 52 | 0 | my $map; foreach (@pairs) { | |||||||||||||||||||||||||||||||
| 53 | 0 | my ($domain, $id) = @$_; | |||||||||||||||||||||||||||||||
| 54 | carp "Duplicate domain $domain found in $zone - ", | ||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | 'this is probably a mistake' if exists $map->{$domain}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | 0 | $map->{$domain} = $id} | |||||||||||||||||||||||||||||||
| 57 | 0 | return $map | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | # Build a mapping of zones to domain ID mappings from CF | ||||||||||||||||||||||||||||||||
| 60 | # Zones will be missing if their fetch fails but the show must go on | ||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sub _buildDomIds { | ||||||||||||||||||||||||||||||
|
Subroutine
_buildDomIds: called CC=6 SCAR=18.5
| |||||||||||||||||||||||||||||||||
| 62 | 1 | Readonly my $self => shift; | |||||||||||||||||||||||||||||||
| 63 | # $zone is a hash of config info | ||||||||||||||||||||||||||||||||
| 64 | 1 | my $map; for my $zone (@{ $self->_config }) { | |||||||||||||||||||||||||||||||
| 65 | 2 | Readonly my $name => $zone->{zone}; | |||||||||||||||||||||||||||||||
| 66 | 2 | Readonly my $domains => $zone->{domains}; | |||||||||||||||||||||||||||||||
| 67 | # Try to fetch domain ids for this zone | ||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | my $zoneMap; try { $zoneMap = $self->_getDomainIds($name)} | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | catch (CloudFlare::Client::Exception::Upstream $e) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.5
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 70 | 0 | carp "Fetching zone IDs for $name failed because the " , | |||||||||||||||||||||||||||||||
| 71 | 'CloudFlare API threw an error: ', $e->errorCode, ' ', | ||||||||||||||||||||||||||||||||
| 72 | $e->message} | ||||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | catch (CloudFlare::Client::Exception::Connection $e) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 74 | 2 | carp "Fetching zone IDs for $name failed because the " , | |||||||||||||||||||||||||||||||
| 75 | 'connection to the CloudFlare API failed: ', $e->status, ' ', | ||||||||||||||||||||||||||||||||
| 76 | $e->message} | ||||||||||||||||||||||||||||||||
| 77 | 0 | # Install ids into map under | |||||||||||||||||||||||||||||||
| 78 | 1 | $map->{\$_} = $zoneMap->{$_} foreach @$domains} | |||||||||||||||||||||||||||||||
| 79 | return $map | ||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | # For storing domain IDs | ||||||||||||||||||||||||||||||||
| 82 | 2 | # A map of domain ref => IP | |||||||||||||||||||||||||||||||
| 83 | has '_domIds' => ( | ||||||||||||||||||||||||||||||||
| 84 | is => 'ro', init_arg => undef, | ||||||||||||||||||||||||||||||||
| 85 | # Clear this and use lazy rebuilding to update IDs each run | ||||||||||||||||||||||||||||||||
| 86 | clearer => '_clearDomIds', builder => '_buildDomIds', lazy => 1); | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | # For keeping track of what we last set the IPs to | ||||||||||||||||||||||||||||||||
| 89 | 2 | ▶ | # A hash of domain ref -> IP | ||||||||||||||||||||||||||||||
|
Subroutine
_buildLastIps: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 90 | 2 | sub _buildLastIps { {} } | |||||||||||||||||||||||||||||||
| 91 | has _lastIps => ( is => 'rw', init_arg => undef, builder => '_buildLastIps'); | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 2 | # Used for fetching the IP | |||||||||||||||||||||||||||||||
| 94 | 2 | ▶ | Readonly my $UA_STRING => __PACKAGE__ . "/$VERSION"; | ||||||||||||||||||||||||||||||
|
Subroutine
_buildUa: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 95 | 2 | sub _buildUa { Readonly my $ua => LWP::UserAgent::->new; | |||||||||||||||||||||||||||||||
| 96 | 2 | $ua->agent($UA_STRING); | |||||||||||||||||||||||||||||||
| 97 | 2 | return $ua} | |||||||||||||||||||||||||||||||
| 98 | has _ua => ( is => 'ro', isa => LWPUserAgent, builder => '_buildUa', | ||||||||||||||||||||||||||||||||
| 99 | init_arg => undef); | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | # Get an IP from any of a number of web services | ||||||||||||||||||||||||||||||||
| 102 | 2 | # each of which return just an IP | |||||||||||||||||||||||||||||||
| 103 | Readonly my @IP_URLS => map { "http://$_" } ( | ||||||||||||||||||||||||||||||||
| 104 | 'icanhazip.com', | ||||||||||||||||||||||||||||||||
| 105 | 'ifconfig.me/ip', | ||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | 'curlmyip.com'); | ||||||||||||||||||||||||||||||
|
Subroutine
_getIp: called CC=6 SCAR=20.2
| |||||||||||||||||||||||||||||||||
| 107 | 1 | sub _getIp { | |||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | Readonly my $self => shift; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | say 'Trying to get current IP' if $self->verbose; | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | # Try each service till we get an IP | ||||||||||||||||||||||||||||||||
| 112 | 1 | # Randomised order for balancing | |||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | for my $serviceUrl (shuffle @IP_URLS) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | say "Trying IP lookup at $serviceUrl" if $self->verbose; | ||||||||||||||||||||||||||||||||
| 115 | 1 | # Get and return IP | |||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | Readonly my $res => $self->_ua->get($serviceUrl); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | if($res->is_success) { | ||||||||||||||||||||||||||||||||
| 118 | 1 | # Chop off the newline | |||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | chomp(my $ip = $res->decoded_content); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 1 | say "IP lookup at $serviceUrl returned $ip" if $self->verbose; | |||||||||||||||||||||||||||||||
| 121 | return $ip | ||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | 0 | # Else log this lookup as failing and try another service | |||||||||||||||||||||||||||||||
| 124 | carp "IP lookup at $serviceUrl failed: ", $res->status_line; | ||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | 0 | # All lookups have failed | |||||||||||||||||||||||||||||||
| 127 | 0 | carp 'Could not lookup IP'; return | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | 2 | ||||||||||||||||||||||||||||||||
| 130 | 2 | Readonly my $REC_TYPE => 'A'; | |||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | Readonly my $TTL => '1'; | ||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=14 SCAR=41.2
Pod
documented
| |||||||||||||||||||||||||||||||||
| 132 | 1 | sub update { | |||||||||||||||||||||||||||||||
| 133 | Readonly my $self => shift; | ||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | # Try to get the current IP address | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition20%1/5
&Readonly(\my $ip, $self->_getIp) || carp('Cannot update records without an IP') && (return)
carp("Cannot update records without an IP") and return
MC/DC0%0/2
&Readonly(\my $ip, $self->_getIp) || carp('Cannot update records without an IP') && (return)
&Readonly(\my $ip, $self->_getIp)
carp("Cannot update records without an IP")
Truth table33%1/3
A || B
A= &Readonly(\my $ip, $self->_getIp)B= carp("Cannot update records without an IP")
| |||||||||||||||||||||||||||||||||
| 135 | carp "Cannot update records without an IP" and return unless | ||||||||||||||||||||||||||||||||
| 136 | Readonly my $ip => $self->_getIp; | ||||||||||||||||||||||||||||||||
| 137 | 1 | # Try to update each zone | |||||||||||||||||||||||||||||||
| 138 | for my $zone (@{ $self->_config }) { | ||||||||||||||||||||||||||||||||
| 139 | 2 | ▶ | # Try to update each domain | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 140 | 2 | say "Trying to update records for $zone->{zone}" if $self->verbose; | |||||||||||||||||||||||||||||||
| 141 | for my $dom (@{ $zone->{domains} }) { | ||||||||||||||||||||||||||||||||
| 142 | 2 | ▶ | # Skip update unless there is a change in IP | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 143 | 4 | ▶ | do { no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if ($self->_lastIps->{\$dom} eq $ip) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 145 | say "IP not changed for $dom, skipping update" if | ||||||||||||||||||||||||||||||||
| 146 | 0 | $self->verbose; | |||||||||||||||||||||||||||||||
| 147 | next}}; | ||||||||||||||||||||||||||||||||
| 148 | # Cannot update if domain ID couldn't be found | ||||||||||||||||||||||||||||||||
| 149 | # At this point new domain IDs will be pulled in from CF | ||||||||||||||||||||||||||||||||
| 150 | 4 | ▶ | warn "Domain ID not found for $dom, cannot update" and next | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
defined $self->_domIds->{\$dom} || warn("Domain ID not found for $dom, cannot update") && (next)
warn "Domain ID not found for $dom, cannot update" and next
MC/DC0%0/2
defined $self->_domIds->{\$dom} || warn("Domain ID not found for $dom, cannot update") && (next)
defined $self->_domIds->{\$dom}
warn "Domain ID not found for $dom, cannot update"
Truth table33%1/3
A || B
A= defined $self->_domIds->{\$dom}B= warn "Domain ID not found for $dom, cannot update"
| |||||||||||||||||||||||||||||||||
| 151 | unless defined $self->_domIds->{\$dom}; | ||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | # Update IP | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | say "Trying to update IP for $dom" if $self->verbose; | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=4.1
| |||||||||||||||||||||||||||||||||
| 154 | try { $self->_api->rec_edit( | ||||||||||||||||||||||||||||||||
| 155 | z => $zone->{zone}, | ||||||||||||||||||||||||||||||||
| 156 | 0 | type => $REC_TYPE, | |||||||||||||||||||||||||||||||
| 157 | id => $self->_domIds->{\$dom}, | ||||||||||||||||||||||||||||||||
| 158 | name => $dom, | ||||||||||||||||||||||||||||||||
| 159 | content => $ip, | ||||||||||||||||||||||||||||||||
| 160 | ttl => $TTL,); | ||||||||||||||||||||||||||||||||
| 161 | 0 | # Record the new IP - won't happen if we fail above | |||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | $self->_lastIps->{\$dom} = $ip} | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=5.6
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 163 | 0 | catch (CloudFlare::Client::Exception::Upstream $e) { | |||||||||||||||||||||||||||||||
| 164 | carp "Updating IP for $dom in $zone->{zone} failed ", | ||||||||||||||||||||||||||||||||
| 165 | 'because the CloudFlare API threw an error: ', | ||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | $e->errorCode, ' ', $e->message} | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||
| 167 | 0 | catch (CloudFlare::Client::Exception::Connection $e) { | |||||||||||||||||||||||||||||||
| 168 | carp "Updating IP for $dom in $zone->{zone} failed ", | ||||||||||||||||||||||||||||||||
| 169 | 0 | 'because the connection to the CloudFlare API failed: ', | |||||||||||||||||||||||||||||||
| 170 | $e->status, ' ', $e->message}}} | ||||||||||||||||||||||||||||||||
| 171 | # Flush domain IDs so they're updated next run | ||||||||||||||||||||||||||||||||
| 172 | 1 | $self->_clearDomIds | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | 2 | ||||||||||||||||||||||||||||||||
| 175 | 1; # End of Net::DNS::CloudFlare::DDNS | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||||||