Skip to content

Commit

Permalink
log
Browse files Browse the repository at this point in the history
  • Loading branch information
huyinghuan committed Mar 29, 2021
1 parent 4db93df commit 038475f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
28 changes: 16 additions & 12 deletions ialidns/domainrecords.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
)

type Domain struct {
DomainName string
RR string
IP string
DomainName string
RR string
IP string
OriginDomain string
}

/**
Expand Down Expand Up @@ -40,7 +41,7 @@ func AddDomainRecord(domain Domain) error {
request.RR = domain.RR
_, err := client.AddDomainRecord(request)
if err == nil {
log.Println("增加解析记录成功:", domain.RR, domain.IP)
log.Println("增加解析记录:", domain.OriginDomain, domain.IP)
}
return err
}
Expand All @@ -54,8 +55,9 @@ func Parse(domain string) (Domain, error) {
domainName := strings.Join(arr[len(arr)-2:], ".")
rr := strings.Join(arr[0:len(arr)-2], ".")
return Domain{
DomainName: domainName,
RR: rr,
DomainName: domainName,
RR: rr,
OriginDomain: domain,
}, nil
}

Expand All @@ -68,24 +70,26 @@ func UpdateDomainRecord(id string, domain Domain) error {
request.RR = domain.RR
_, err := client.UpdateDomainRecord(request)
if err == nil {
log.Println("修改解析记录成功:", domain.RR, domain.IP)
log.Println("修改解析记录成功:", domain.OriginDomain, domain.IP)
}
return err
}

func AddOrUpdateDomain(domain Domain) error {
// AddOrUpdateDomain
// @return isChange error
func AddOrUpdateDomain(domain Domain) (bool, error) {
records, err := GetDomainRecords(domain.DomainName)
if err != nil {
return err
return false, err
}
for _, record := range records {
if record.RR == domain.RR {
// ip 没有变化,不需要重新解析
if record.Value == domain.IP {
return nil
return false, nil
}
return UpdateDomainRecord(record.RecordId, domain)
return true, UpdateDomainRecord(record.RecordId, domain)
}
}
return AddDomainRecord(domain)
return true, AddDomainRecord(domain)
}
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ func setDNS(domains string, ip string) {
log.Printf("域名配置错误: %s \n", item)
continue
}
if err := ialidns.AddOrUpdateDomain(domain); err != nil {
if hasChange, err := ialidns.AddOrUpdateDomain(domain); err != nil {
log.Fatalf("域名更新失败: %s \n", item)
log.Println(err)
continue
} else if hasChange {
log.Printf("域名: %s 更新成功! \n", domain.DomainName)
}
log.Printf("域名: %s 更新成功! \n", domain.DomainName)
}
}

Expand Down Expand Up @@ -70,7 +71,7 @@ func main() {
log.Fatalln("关键参数不能为空: accessId, accessKey, domain")
}

log.Printf("监控ip变动间隔: %ds 目标域名: %s\n", config.Refresh, config.Domain)
log.Printf("监控ip变动间隔: %ds 目标域名:\n -- %s \n", config.Refresh, strings.Join(strings.Split(config.Domain, ","), "\n -- "))

timer := time.NewTicker(time.Duration(fresh) * time.Second)
lastestIp := ""
Expand Down

0 comments on commit 038475f

Please sign in to comment.