From a24ab4e057ac1531d0552289d2e3c2dc1f19801a Mon Sep 17 00:00:00 2001 From: kithack Date: Sun, 10 Jan 2021 12:58:57 +0100 Subject: [PATCH] Add option to disable IPv4. (#4) * Add option to disable IPv4. It is now possible to disable the update of the 'A' record. That might be useful when the ISP only provide IPv6 addresses. When not specified the 'IPv4' option is true. This helps with updating as the option was not present before and would break old configurations. Co-authored-by: Henri Burau --- config.go | 14 ++++++++++++++ example.yml | 4 ++++ main.go | 38 +++++++++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/config.go b/config.go index 3cd6390..6cffb70 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,7 @@ type Config struct { type Domain struct { Name string `yaml:"NAME"` IPv6 bool `yaml:"IPV6"` + IPv4 bool `yaml:"IPV4"` TTL int `yaml:"TTL"` Hosts []string `yaml:"HOSTS"` } @@ -35,3 +36,16 @@ func LoadConfig(filename string) (*Config, error) { return &config, nil } + +func (d *Domain) UnmarshalYAML(unmarshal func(interface{}) error) error { + type rawDomain Domain + raw := rawDomain{ + IPv4: true, + } + if err := unmarshal(&raw); err != nil { + return err + } + + *d = Domain(raw) + return nil +} diff --git a/example.yml b/example.yml index 06542c1..9bbc9a4 100644 --- a/example.yml +++ b/example.yml @@ -18,6 +18,9 @@ DOMAINS: - NAME: 'example.de' # Your domain name without any subdomains. IPV6: true # Whether the 'AAAA' entries of this host should be # updated with the IPv6 address or not. + IPV4: true # Whether the 'A' entries of this host should be + # updated with the IPv4 address or not. This option defaults + # to true when not present. TTL: 300 # Time to live for this zone. Around 300 is good for dyndns. HOSTS: # Every host that should get your public ip - '@' @@ -26,6 +29,7 @@ DOMAINS: - NAME: 'example.com' IPV6: false + IPV4: true TTL: 350 HOSTS: - '@' diff --git a/main.go b/main.go index df4d70f..6b12366 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,9 @@ const ( func main() { login() - loadIPv4() + if iPv4Enabled() { + loadIPv4() + } if iPv6Enabled() { loadIPv6() @@ -114,6 +116,16 @@ func iPv6Enabled() bool { return false } +func iPv4Enabled() bool { + for _, domain := range config.Domains { + if domain.IPv4 { + return true + } + } + + return false +} + func configureDomains() { for _, domain := range config.Domains { if needsUpdate(domain) { @@ -132,10 +144,12 @@ func needsUpdate(domain Domain) bool { update := false for _, host := range domain.Hosts { - hostIPv4 := cache.GetIPv4(domain.Name, host) - if hostIPv4 == "" || hostIPv4 != ipv4 { - cache.SetIPv4(domain.Name, host, ipv4) - update = true + if domain.IPv4 { + hostIPv4 := cache.GetIPv4(domain.Name, host) + if hostIPv4 == "" || hostIPv4 != ipv4 { + cache.SetIPv4(domain.Name, host, ipv4) + update = true + } } if domain.IPv6 { @@ -186,12 +200,14 @@ func configureRecords(domain Domain) { var updateRecords []netcup.DNSRecord for _, host := range domain.Hosts { - if records.GetRecordOccurences(host, "A") > 1 { - logInfo("Too many A records for host '%s'. Please specify only Hosts with one corresponding A record", host) - } else { - newRecord, needsUpdate := configureARecord(host, records) - if needsUpdate { - updateRecords = append(updateRecords, *newRecord) + if domain.IPv4 { + if records.GetRecordOccurences(host, "A") > 1 { + logInfo("Too many A records for host '%s'. Please specify only Hosts with one corresponding A record", host) + } else { + newRecord, needsUpdate := configureARecord(host, records) + if needsUpdate { + updateRecords = append(updateRecords, *newRecord) + } } } if domain.IPv6 {