1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

Merge pull request #75 from x-liao/master

Fix: lastip changes when update request fails
This commit is contained in:
Timothy 2020-07-30 17:54:01 +08:00 committed by GitHub
commit 3eaed255d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -96,8 +96,6 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
if currentIP == lastIP { if currentIP == lastIP {
log.Printf("IP is the same as cached one. Skip update.\n") log.Printf("IP is the same as cached one. Skip update.\n")
} else { } else {
lastIP = currentIP
log.Println("Checking IP for domain", domain.DomainName) log.Println("Checking IP for domain", domain.DomainName)
zoneID := handler.getZone(domain.DomainName) zoneID := handler.getZone(domain.DomainName)
if zoneID != "" { if zoneID != "" {
@ -111,7 +109,7 @@ func (handler *Handler) DomainLoop(domain *godns.Domain, panicChan chan<- godns.
} }
if rec.IP != currentIP { if rec.IP != currentIP {
log.Printf("IP mismatch: Current(%+v) vs Cloudflare(%+v)\r\n", currentIP, rec.IP) log.Printf("IP mismatch: Current(%+v) vs Cloudflare(%+v)\r\n", currentIP, rec.IP)
handler.updateRecord(rec, currentIP) lastIP = handler.updateRecord(rec, currentIP)
// Send notification // Send notification
if err := godns.SendNotify(handler.Configuration, rec.Name, currentIP); err != nil { if err := godns.SendNotify(handler.Configuration, rec.Name, currentIP); err != nil {
@ -206,7 +204,7 @@ func (handler *Handler) getDNSRecords(zoneID string) []DNSRecord {
} }
log.Println("Querying records with type:", recordType) log.Println("Querying records with type:", recordType)
req, client := handler.newRequest("GET", fmt.Sprintf("/zones/"+zoneID+"/dns_records?type=%s", recordType), nil) req, client := handler.newRequest("GET", fmt.Sprintf("/zones/"+zoneID+"/dns_records?type=%s&page=1&per_page=500", recordType), nil)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Println("Request error:", err.Error()) log.Println("Request error:", err.Error())
@ -230,10 +228,11 @@ func (handler *Handler) getDNSRecords(zoneID string) []DNSRecord {
} }
// Update DNS A Record with new IP // Update DNS A Record with new IP
func (handler *Handler) updateRecord(record DNSRecord, newIP string) { func (handler *Handler) updateRecord(record DNSRecord, newIP string) string {
var r DNSRecordUpdateResponse var r DNSRecordUpdateResponse
record.SetIP(newIP) record.SetIP(newIP)
var lastIP string
j, _ := json.Marshal(record) j, _ := json.Marshal(record)
req, client := handler.newRequest("PUT", req, client := handler.newRequest("PUT",
@ -243,7 +242,7 @@ func (handler *Handler) updateRecord(record DNSRecord, newIP string) {
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Println("Request error:", err.Error()) log.Println("Request error:", err.Error())
return return ""
} }
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
@ -251,12 +250,14 @@ func (handler *Handler) updateRecord(record DNSRecord, newIP string) {
if err != nil { if err != nil {
log.Printf("Decoder error: %+v\n", err) log.Printf("Decoder error: %+v\n", err)
log.Printf("Response body: %+v\n", string(body)) log.Printf("Response body: %+v\n", string(body))
return return ""
} }
if r.Success != true { if r.Success != true {
body, _ := ioutil.ReadAll(resp.Body) body, _ := ioutil.ReadAll(resp.Body)
log.Printf("Response failed: %+v\n", string(body)) log.Printf("Response failed: %+v\n", string(body))
} else { } else {
log.Printf("Record updated: %+v - %+v", record.Name, record.IP) log.Printf("Record updated: %+v - %+v", record.Name, record.IP)
lastIP = record.IP
} }
return lastIP
} }