From bc3e46e11f41a8cc08ad51b127a7cecc629f5530 Mon Sep 17 00:00:00 2001 From: Mahesh Asolkar Date: Wed, 17 Feb 2016 22:46:22 -0800 Subject: [PATCH] Introducing Google Domains Dynamic DNS Updater --- README.md | 66 ++++++++ googledomains_dyndns_update.rb | 285 +++++++++++++++++++++++++++++++++ 2 files changed, 351 insertions(+) create mode 100644 README.md create mode 100755 googledomains_dyndns_update.rb diff --git a/README.md b/README.md new file mode 100644 index 0000000..6248433 --- /dev/null +++ b/README.md @@ -0,0 +1,66 @@ +Google Domains Dynamic DNS Updater +== + +`googledomains_dyndns_update` is a simple tool written in Ruby to update +Dynamic DNS entries on Google Domains. More information on Dynamic DNS +pertaining to Google Domains can be found in the following support answer: + + > https://support.google.com/domains/answer/6147083 + +Installation +-- + +The tool can be run by executing `googledomains_dyndns_update.rb` script from a +clone of this repository. + +Configuration +-- + +This tool uses a configuration file, located by default in `~/.gddyndns.yaml`, +to get information on hosts to update. An alternate configuration file can be +specified with the `-f|--config_file` option. Configuration is specified in +YAML format with a template as follows: + +```YAML +hosts: +- host: hostone.com + username: + password: +- host: hosttwo.com + username: + password: +- ... +``` + +Username and password for this file can be obtained from the Google Domains +dashboard as mentioned in the URL above. + +Caching +-- + +To avoid frequent queries to Google Domains, store the status of latest update +to a cache file, located by default in `~/.gddyndns.cache`. Alternate location +may be provided with `-c|--cache_file` option. + +Google Domains is updated only if public IP of the host has changed. An update +can, however, be forced with the `-u|--force_update` option. + +Usage +-- + +Following usage help is available with the `--help` option: + +``` +$ googledomains_dyndns_update.rb --help +Usage: googledomains_dyndns_update.rb [options] + -d, --debug Enable debug messages + -u, --force_update Force DNS update + -f, --config_file FILE Location of configuration file + -c, --cache_file FILE Location of cache file + -h, --help Display this help +``` + +License +-- + +This tool is licensed under the MIT License. diff --git a/googledomains_dyndns_update.rb b/googledomains_dyndns_update.rb new file mode 100755 index 0000000..f0117f3 --- /dev/null +++ b/googledomains_dyndns_update.rb @@ -0,0 +1,285 @@ +#!/usr/bin/env ruby + +# ----- +# +# Copyright (c) 2016 Mahesh Asolkar +# +# Permission is hereby granted, free of charge, to any person obtaining a copy of +# this software and associated documentation files (the "Software"), to deal in +# the Software without restriction, including without limitation the rights to +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +# of the Software, and to permit persons to whom the Software is furnished to do +# so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +# ----- +require 'pp' +require 'optparse' +require 'open-uri' +require 'yaml' + +# ------------------ +# Helper Classes +# ------------------ +class GoogleDomainsDynDNSUpdater + attr_accessor :options, :myip, :config, :cache, :responses + + def initialize(options) + @options = options + @config = read_config_file + @responses = learn_responses + end + + def run() + # + # Load the cache so we know hosts and their IPs were updated + # in the past + # + @cache = read_cache_file + + # + # Get the Public IP (WAN). We will compare this with the IP + # in the cache to know if the IP has changed and needs to be + # updated in Google Domains' DNS + # + @myip = get_public_ip + info_put "Public IP: #{@myip}" + + # + # Update IP of known hosts + # + update_dns + end + + def report() + end + + private + + # + # Read configuration file, which contains the list of hosts and + # their credentials. Configuration file uses the YAML format + # and has the following template: + # + # ----------- + # hosts: + # - host: hostone.com + # username: + # password: + # - host: hosttwo.com + # username: + # password: + # - ... + # ----------- + # + # Default location of the configuration file is: + # + # ~/.gddyndns.yaml + # + # Alternate location may be provided with [-f|--config_file] option + # + def read_config_file() + if File.exists?(options[:config_file]) + return YAML.load_file(options[:config_file]) + else + error_put "Config file [#{options[:config_file]}] missing!" + end + end + + # + # To avoid frequent queries to Google Domains, store the status of + # latest update to a cache file. Google Domains is updated only if + # our public IP has changed. + # + # Default location of cache file is: + # + # ~/.gddyndns.cache + # + # Alternate location may be provided with [-c|--cache_file] option + # + def read_cache_file() + # + # Update to Google Domains may be forced by forgetting cached + # values. This is controlled by the [-u|--force_update] option + # + if options[:force_update] + if File.exists?(options[:cache_file]) + File.delete(options[:cache_file]) + end + end + + # + # Load the cache file + # + return File.exists?(options[:cache_file]) ? YAML.load_file(options[:cache_file]) : {} + end + + # + # Store IPs from current update into cache + # + def write_cache_file() + debug_put @cache.inspect + File.open(options[:cache_file], "w") {|f| f.write(@cache.to_yaml)} + end + + # + # Acquire the public IP from an external service + # + def get_public_ip() + return open('http://ipinfo.io/ip').read.chomp + end + + # + # Update Google Domains if IP has changed + # + def update_dns() + # + # Handle each host in the config file at a time + # + @config['hosts'].each {|h| + # + # Skip update if current public IP matches the IP for the host in the cache file + # + if @cache[h['host']] && @myip.eql?(@cache[h['host']]['ip']) + info_put "Skipping #{h['host']} - Already pointing to #{@myip}" + else + url = "https://domains.google.com/nic/update?hostname=#{h['host']}&myip=#{@myip}" + info_put "Updating host [#{h['host']}] - #{url}" + + # + # Access Google Domains API to update IP + # + open(url, + :http_basic_authentication => [h['username'],h['password']], + "User-Agent" => "#{@options[:user_agent]}") {|r| + if r.status[0] == "200" + r.each_line {|line| + if (/(?(good|nochg))\s+(?(\d+\.\d+\.\d+\.\d+)?)/ =~ line) + # + # Cache if API call was successful + # + @cache[h['host']] = {'ip' => ip} + debug_put "[#{@responses[sts][0]}][#{sts}] : [#{@responses[sts][1]}]" + else + warn_put "[#{@responses[line][0]}][#{line}] : [#{@responses[line][1]}]" + end + } + else + error_put "Error status returned #{r.status.inspect}" + end + } + write_cache_file + end + } + end + + # + # Learn message text for different Google Domains status codes + # + def learn_responses() + return {'good' => ['Success', 'The update was successful. Followed by a space and the updated IP address. You should not attempt another update until your IP address changes.'], + 'nochg' => ['Success', 'The supplied IP address is already set for this host. You should not attempt another update until your IP address changes.'], + 'nohost' => ['Error', 'The hostname does not exist, or does not have Dynamic DNS enabled.'], + 'badauth' => ['Error', 'The username / password combination is not valid for the specified host.'], + 'notfqdn' => ['Error', 'The supplied hostname is not a valid fully-qualified domain name.'], + 'badagent' => ['Error', 'Your Dynamic DNS client is making bad requests. Ensure the user agent is set in the request, and that you’re only attempting to set an IPv4 address. IPv6 is not supported.'], + 'abuse' => ['Error', 'Dynamic DNS access for the hostname has been blocked due to failure to interpret previous responses correctly.'], + '911' => ['Error', 'An error happened on our end. Wait 5 minutes and retry.']} + end + + # + # Debug message + # + def debug_put(str) + if @options[:debug] + puts "[DEBUG] #{str}" + end + end + + # + # Informational message + # + def info_put(str) + puts "[INFO] #{str}" + end + + # + # Warning message + # + def warn_put(str) + puts "[WARNING] #{str}" + end + + # + # Error message + # + def error_put(str) + puts "[ERROR] #{str}" + exit 1 + end +end + +# ------------------- +# Options parsing +# ------------------- +# +# Default values of command line options +# +options = { + :config_file => "#{Dir.home}/.gddyndns.yaml", + :cache_file => "#{Dir.home}/.gddyndns.cache", + :user_agent => 'MMA GoogleDomains DynDNS Updater Under Dev - please do not block!', + :force_update => false, + :debug => false +} + +# +# Get command line options +# +op = OptionParser.new do |opts| + opts.banner = "Usage: googledomains_dyndns_update.rb [options]" + + opts.on("-d", "--debug", "Enable debug messages") do |debug| + options[:debug] = debug + end + opts.on("-u", "--force_update", "Force DNS update") do |force_update| + options[:force_update] = force_update + end + opts.on("-f", "--config_file FILE", "Location of configuration file") do |file| + options[:config_file] = file + end + opts.on("-c", "--cache_file FILE", "Location of cache file") do |file| + options[:cache_file] = file + end + opts.on("-h", "--help", "Display this help") do |help| + options[:help] = help + end +end + +op.parse! + +if options[:help] + puts op.help + exit +end + +# ------------------ +# Execution part +# ------------------ +updater = GoogleDomainsDynDNSUpdater.new(options) +updater.run +updater.report + +# +# Mahesh Asolkar, 2016 +# vim: ai ts=2 sts=2 et sw=2 filetype=ruby