Skip to content

Commit

Permalink
Add option to disable IPv4. (#4)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
kithack and Hentra authored Jan 10, 2021
1 parent 7d853e0 commit a24ab4e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
14 changes: 14 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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
}
4 changes: 4 additions & 0 deletions example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
- '@'
Expand All @@ -26,6 +29,7 @@ DOMAINS:

- NAME: 'example.com'
IPV6: false
IPV4: true
TTL: 350
HOSTS:
- '@'
Expand Down
38 changes: 27 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const (
func main() {
login()

loadIPv4()
if iPv4Enabled() {
loadIPv4()
}

if iPv6Enabled() {
loadIPv6()
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a24ab4e

Please sign in to comment.