mirror of
https://gitlab.com/pnx-tools/dns-updater.git
synced 2026-06-16 05:54:56 +02:00
45 lines
937 B
Go
45 lines
937 B
Go
package app
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
|
|
"dnsupdater/dns"
|
|
"dnsupdater/dns/service"
|
|
)
|
|
|
|
type Updater struct {
|
|
service service.Service
|
|
// cache map[string][]dns.Record
|
|
cache dns.DomainRecordCache
|
|
}
|
|
|
|
func NewUpdater(service service.Service) *Updater {
|
|
return &Updater{
|
|
service: service,
|
|
cache: *dns.NewDomainRecordCache(service.List),
|
|
}
|
|
}
|
|
|
|
func (u Updater) find(domain string, record_name string) (*dns.Record, error) {
|
|
records, err := u.cache.Get(domain)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rec, found := records.FindByName(record_name); found {
|
|
return &rec, nil
|
|
}
|
|
return nil, fmt.Errorf("could not find record %s", record_name)
|
|
}
|
|
|
|
func (u Updater) Update(domain, record string, ip net.IP) error {
|
|
var err error
|
|
var r *dns.Record
|
|
if r, err = u.find(domain, record); err == nil {
|
|
// Update if needed.
|
|
if r.Ip == nil || !r.Ip.Equal(ip) {
|
|
return u.service.Update(domain, r.Id, ip.String())
|
|
}
|
|
}
|
|
return err
|
|
}
|