Advertisement
Guest User

Untitled

a guest
Jun 17th, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 2.67 KB | None | 0 0
  1. #!/usr/bin/env perl6
  2.  
  3. use LibCurl::HTTP;
  4. use JSON::Fast;
  5.  
  6. sub MAIN ( Bool :d(:$debug) ) {
  7.   my $ZONE_NAME = '[HOST]';
  8.   my $ZONE_ID = '[HASH]';
  9.   my $LOGIN_EMAIL = '[EMAIL]';
  10.   my $API_KEY = '[KEY]';
  11.  
  12.   my $start_time = time;
  13.  
  14.   my $browser = LibCurl::HTTP.new;
  15.   $browser.useragent("perl6 LibCurl");
  16.   $browser.set-header(
  17.     X-Auth-Email => "$LOGIN_EMAIL",
  18.     X-Auth-Key => "$API_KEY"
  19.   );
  20.  
  21.   my %current_ips;
  22.   %current_ips<4> = $browser.GET(
  23.     "http://ipv4.icanhazip.com"
  24.   ).perform.content;
  25.   debug($browser, "get ipv4", $debug);
  26.   %current_ips<6> = $browser.GET(
  27.     "http://ipv6.icanhazip.com"
  28.   ).perform.content;
  29.   debug($browser, "get ipv6", $debug);
  30.  
  31.   my $records_json = $browser.GET(
  32.     "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records"
  33.   ).perform.content;
  34.   debug($browser, "get records", $debug);
  35.  
  36.   my %records_hash := from-json $records_json;
  37.   my @records := %records_hash<result>;
  38.   my Int $updated = 0;
  39.   for @records -> %record {
  40.     my Str $current_ip;
  41.     if ("%record<type>" eq 'A') {
  42.       # IPV4
  43.       next if (%record<content> ne %current_ips<4>);
  44.       $current_ip = %current_ips<4>;
  45.     } elsif (%record<type> eq 'AAAA') {
  46.       # IPV6
  47.       next if (%record<content> ne %current_ips<6>);
  48.       $current_ip = %current_ips<6>;
  49.     }
  50.     say "Need to update record %record<name>, has content: %record<content>, should be $current_ip";
  51.     my Str $record_id = %record<id>;
  52.     my %data = (
  53.       "type" => "%record<type>",
  54.       "name" => "%record<name>",
  55.       "content" => "$current_ip"
  56.     );
  57.     my Str $json_data = to-json %data;
  58.     try {
  59.       CATCH {
  60.         say "FATAL ERROR";
  61.         debug($browser, "perform update of $record_id from %record<content> to $current_ip", $debug);
  62.       }
  63.       $browser.setopt(
  64.         URL => "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$record_id",
  65.         send => "$json_data"
  66.       ).perform;
  67.     }
  68.     debug($browser, "perform update of $record_id from %record<content> to $current_ip", $debug);
  69.     say "Done";
  70.     $updated++;
  71.   }
  72.   my Int $seconds = time - $start_time;
  73.   print "All done";
  74.   if ($updated > 0) {
  75.     print ". Updated $updated records in $seconds seconds.\n";
  76.   } else {
  77.     print " in $seconds seconds. No updates performed.\n";
  78.   }
  79. }
  80.  
  81. sub debug(
  82.   LibCurl::HTTP $browser,
  83.   Str $label,
  84.   Bool $debug
  85.  ) {
  86.   if ! $browser.success {
  87.     fail "HTTP error performing $label.";
  88.   };
  89.   if $debug {
  90.     say "\n--- Debug output for $label ---";
  91.     say "Status: ", $browser.statusline;
  92.     say "Headers: ", $browser.receiveheaders.perl;
  93.     say "Content: ", $browser.content;
  94.     say "--- END DEBUG ---";
  95.   }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement