From 4db93df69b8edb4866297150e8c5e792ff323dc2 Mon Sep 17 00:00:00 2001 From: huyinghuan Date: Mon, 22 Mar 2021 09:27:09 +0800 Subject: [PATCH] support multiple domain --- main.go | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index e39667c..66d6702 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "log" + "strings" "time" "github.com/huyinghuan/ddns/ialidns" @@ -12,12 +13,34 @@ import ( var Version = "v1.0.0" var BuildTime = "Dev" +func setDNS(domains string, ip string) { + list := strings.Split(domains, ",") + for _, item := range list { + item = strings.TrimSpace(item) + if item == "" { + continue + } + domain, err := ialidns.Parse(item) + domain.IP = ip + if err != nil { + log.Printf("域名配置错误: %s \n", item) + continue + } + if err := ialidns.AddOrUpdateDomain(domain); err != nil { + log.Fatalf("域名更新失败: %s \n", item) + log.Println(err) + continue + } + log.Printf("域名: %s 更新成功! \n", domain.DomainName) + } +} + func main() { var accessId, accessKey, domainName string flag.StringVar(&accessId, "accessId", "", "阿里云access id") flag.StringVar(&accessKey, "accessKey", "", "阿里云access key") - flag.StringVar(&domainName, "domain", "", "目标域名") + flag.StringVar(&domainName, "domain", "", "目标域名, 多个域名用逗号隔开") var fresh int flag.IntVar(&fresh, "refresh", 30, "监控ip变动时间间隔【建议30s以上,30s】") @@ -49,11 +72,6 @@ func main() { log.Printf("监控ip变动间隔: %ds 目标域名: %s\n", config.Refresh, config.Domain) - domain, err := ialidns.Parse(config.Domain) - if err != nil { - log.Fatalf("域名配置错误: %s", config.Domain) - } - timer := time.NewTicker(time.Duration(fresh) * time.Second) lastestIp := "" for { @@ -68,12 +86,7 @@ func main() { if ip == "" { log.Println("获取本机ip失败") } else if ip != lastestIp { - domain.IP = ip - if err := ialidns.AddOrUpdateDomain(domain); err != nil { - log.Println(err) - } else { - lastestIp = ip - } + setDNS(config.Domain, ip) } <-timer.C }