Skip to content

Commit

Permalink
support multiple domain
Browse files Browse the repository at this point in the history
  • Loading branch information
huyinghuan committed Mar 29, 2021
1 parent ead0b29 commit 4db93df
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"log"
"strings"
"time"

"github.com/huyinghuan/ddns/ialidns"
Expand All @@ -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】")
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 4db93df

Please sign in to comment.