njh / delphyne

Mythic Beasts DNS API client implemented as a bash script
MIT License
3 stars 0 forks source link

Changes to work with IPv6 #1

Open DougieLawson opened 4 years ago

DougieLawson commented 4 years ago

I've just switched my domain to Mythic Beasts. So I've hacked your script to run with the IPv4 or IPv6 API rather than the beta endpoint. I've set a default of IPv4.

--- delphyne   2020-06-30 20:24:07.571446216 +0000
+++ delphyne    2020-07-01 11:19:52.507084782 +0000
@@ -11,8 +11,9 @@
 set -u
 set -o pipefail

-VERSION="1.0.0"
-API_BASE_PATH="https://api.mythic-beasts.com/beta/dns"
+VERSION="1.0.1"
+API_IPv4_PATH="https://ipv4.api.mythic-beasts.com/dns/v2"
+API_IPv6_PATH="https://ipv6.api.mythic-beasts.com/dns/v2"

 # Load configuration from .env file, if it exists
 if [ -f .env ]; then
@@ -145,6 +146,12 @@
 # Send http(s) request with specified method
 http_request() {
   local method="${1}"
+  if [ "$ip_version" == "4" ]; then
+    API_BASE_PATH=$API_IPv4_PATH
+  else
+    API_BASE_PATH=$API_IPv6_PATH
+  fi
+
   local url="${API_BASE_PATH}${2}"
   local body="${3:-}"

@@ -382,7 +389,7 @@
   MYTHIC_BEASTS_SECRET="${MYTHIC_BEASTS_SECRET:-}"

   debug=
-  ip_version=
+  ip_version=4
   format=rfc1035
   request_format=
   response_format=
njh commented 4 years ago

Hi Dougie,

Great to hear that someone else is making use of my script. I didn't know that the DNS v2 API was out of beta, that is good. I have just released a new 1.0.1 version that uses the new v2 endpoint.

I am not quite clear why this change needed though? https://api.mythic-beasts.com/dns/v2 should work for either IPv4 or IPv6 and the -4 and -6 switches should already allow you to force a particular version - mainly for use with the dynamic command.

nick.

DougieLawson commented 4 years ago

Hi Nick,

That's why I did it as an issue. My problem is that my IPv6 isn't permanent, it's done with a 6to4 tunnel by my TP-Link router. So I wanted to be certain that I was hitting an IPv6 address when I'm doing IPv6 stuff on their API (as that gives a nudge that I need to hit my router with a bigger stick when it doesn't work).

One day, PlusNet will do a proper IPv6 service (like Sky) and it'll all become moot.

I spent some more time hacking your script today as it didn't like my psuedo RFC1035 zone file.

--- delphyne~~  2020-07-02 00:05:52.498924094 +0000
+++ delphyne    2020-07-02 00:06:56.478475137 +0000
@@ -265,7 +265,7 @@

   echo "Getting zone ${zone}" >&2
   response_format="$(mime_type_for_format)"
-  http_request get "/zones/${zone}/records"
+  http_request get "/zones/${zone}/records?exclude-generated&exclude-template"
 }

 # Usage: delphyne dynamic <hostname>
@@ -309,7 +309,7 @@
   fi

   response_format='application/json'
-  result="$(http_request put "/zones/${zone}/records?exclude-generated" "@${filename}")"
+  result="$(http_request put "/zones/${zone}/records?exclude-generated&exclude-template" "@${filename}")"
   display_message "${result}"
   echo >&2
 }

Thanks for building this script (it's Google's #5 hit for "Mythic Beasts DNS V2 script"), it's saved me a bunch of pain and I've learned some more shell script. In the process I've also found jg which is a neat JSON parsing library. My domain is now fully transferred to Mythic Beasts from Ionos. I've now got a complete script to update stuff when PlusNet renew my IP address (which is getting a full scale test as we're dropping the power on Monday while my EV charger gets installed).

Rgds, Dougie

njh commented 4 years ago

I think I need to add an option for this, as it could be useful to fetch generated and template records:

http_request get "/zones/${zone}/records?exclude-generated&exclude-template"

I should definitely merge this in; I don't use templates but I think putting a zone will be broken without this option:

result="$(http_request put "/zones/${zone}/records?exclude-generated&exclude-template" "@${filename}")"

How exciting to be getting an EV/EV charger!

DougieLawson commented 4 years ago

Thanks Nick, let me know when you've coded it - so I can beta test it.

The reason I want fetch without templates/generated is so I can pull it down, edit it (with a sed script generated by a python program (yes, I am crazy)) to update my IPv4 & IPv6 addresses then publish it.

More exciting than the charger was getting the Kia e-Nero two weeks ago, it's a brilliant car with 400Km WLTP range (although the lead time for delivery was excessive).