diff --git a/conf/conf.go b/conf/conf.go index ff1706a..f576691 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -8,6 +8,7 @@ import ( // DNSRecord 用于构造 DNS 记录的 JSON 结构体 type DNSRecord struct { + ZoneID string `json:"ZoneID"` Type string `json:"type"` Name string `json:"name"` Content string `json:"content"` @@ -29,17 +30,18 @@ type OneRes struct { } type Mata struct { - Main []DNSRecord - Then []DNSRecord + Target string + Main DNSRecord + Then DNSRecord } type cf struct { - Target string ApiKey string ZoneID string BotToken string ChatID string - Mata Mata + Corn int64 + Mata []Mata } var Config cf @@ -49,7 +51,7 @@ func init() { file, err := os.Open("mata.json") if err != nil { fmt.Println("mata.json配置文件不存在") - return + os.Exit(0) } defer file.Close() @@ -58,6 +60,6 @@ func init() { err = decoder.Decode(&Config) if err != nil { fmt.Println("mata.json配置文件错误") - return + os.Exit(0) } } diff --git a/main.go b/main.go index 1e6950d..db1b18d 100644 --- a/main.go +++ b/main.go @@ -12,43 +12,40 @@ import ( var Once bool func main() { - if conf.Config.Target == "" { - return - } for { - online := utils.Check(conf.Config.Target, 5*time.Second) - if online { - message := "服务器在线" - log.Println(message) - updateDNSRecords(conf.Config.Mata.Main, message) - } else { - message := "服务器离线" - log.Println(message) - updateDNSRecords(conf.Config.Mata.Then, message) + for _, mata := range conf.Config.Mata { + log.Println("开始检测" + mata.Target) + send := false + msg := "服务器在线" + online := utils.Check(mata.Target, 5*time.Second) + if online { + log.Println(msg) + ok, dns := utils.GetDnsRecoid(mata.Main.Name, mata.Main.ZoneID) + if ok && dns.Content != mata.Main.Content { + send = true + log.Printf("修改解析【%s】\n", mata.Main.Name) + utils.Dns(mata.Main, dns.ID, mata.Main.ZoneID) + } + } else { + msg = "服务器离线" + log.Println(msg) + ok, dns := utils.GetDnsRecoid(mata.Then.Name, mata.Then.ZoneID) + if ok && dns.Content != mata.Then.Content { + send = true + log.Printf("修改解析【%s】\n", mata.Then.Name) + utils.Dns(mata.Then, dns.ID, mata.Then.ZoneID) + } + } + if send && conf.Config.BotToken != "" && conf.Config.ChatID != "" { + utils.SendMessage(msg) + } } if Once { return } - time.Sleep(300 * time.Second) + time.Sleep(time.Duration(conf.Config.Corn) * time.Second) } } - -func updateDNSRecords(records []conf.DNSRecord, message string) { - send := false - for _, record := range records { - ok, dns := utils.GetDnsRecoid(record.Name) - if ok && dns.Content != record.Content { - send = true - log.Printf("修改解析【%s】\n", record.Name) - utils.Dns(record, dns.ID) - } - time.Sleep(5 * time.Second) - } - if send && conf.Config.BotToken != "" && conf.Config.ChatID != "" { - utils.SendMessage(message) - } -} - func init() { once := flag.Bool("once", false, "Run once") flag.Parse() diff --git a/mata.sample.json b/mata.sample.json index 71e812c..bfd5698 100644 --- a/mata.sample.json +++ b/mata.sample.json @@ -1,38 +1,43 @@ { "ApiKey": "", - "ZoneID": "", "BotToken": "", "ChatID": "", - "Target":"1.1.1.1:80", - "Mata": { - "Main": [ - { - "type": "A", - "name": "csz.net", - "content": "1.1.1.1", - "proxied": false + "Corn":500, + "Mata": [ + { + "Target": "1.1.1.1:80", + "Main": { + "ZoneID": "", + "Type": "A", + "Name": "csz.net", + "Content": "1.1.1.1", + "Proxied": false }, - { - "type": "A", - "name": "www.csz.net", - "content": "1.1.1.1", - "proxied": false + "Then": { + "ZoneID": "", + "Type": "A", + "Name": "csz..net", + "Content": "2.2.2.2", + "Proxied": true } - ], - "Then": [ - { - "type": "CNAME", - "name": "csz.net", - "content": "2.2.2.2", - "proxied": true + }, + { + "Target": "1.1.1.1:80", + "Main": { + "ZoneID": "", + "Type": "A", + "Name": "www.csz.net", + "Content": "1.1.1.1", + "Proxied": false }, - { - "type": "CNAME", - "name": "www.csz.net", - "content": "2.2.2.2", - "proxied": true + "Then": { + "ZoneID": "", + "Type": "A", + "Name": "www.csz.net", + "Content": "2.2.2.2", + "Proxied": true } - ] - } + } + ] } \ No newline at end of file diff --git a/readme.md b/readme.md index d0d613a..08dde92 100644 --- a/readme.md +++ b/readme.md @@ -49,9 +49,9 @@ Telegram ChatID 当服务器状态改变时发送通知,不启用保持为空即可 -**Target** +**Corn** -需要监控的服务器,采用TCP监控,需带上端口号 +监控间隔,单位 秒 **Mata** @@ -59,6 +59,10 @@ Main为当服务器正常时的解析,Then为当服务器离线时的解析 `proxied`为是否启用CloudFlare CDN +**Target** + +需要监控的服务器,采用TCP监控,需带上端口号 + ### 服务器运行 下载系统对应的编译包,将`mata.sample.json`改名为`mata.json`并运行 \ No newline at end of file diff --git a/utils/utils.go b/utils/utils.go index 5bcca39..5cebab2 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -21,9 +21,9 @@ func Check(address string, timeout time.Duration) bool { return true // 连接成功 } -func Dns(record conf.DNSRecord, recordID string) bool { +func Dns(record conf.DNSRecord, recordID string, ZoneID string) bool { - url := "https://api.cloudflare.com/client/v4/zones/" + conf.Config.ZoneID + "/dns_records/" + recordID + url := "https://api.cloudflare.com/client/v4/zones/" + ZoneID + "/dns_records/" + recordID recordBytes, err := json.Marshal(record) if err != nil { @@ -45,9 +45,9 @@ func Dns(record conf.DNSRecord, recordID string) bool { return true } -func GetDnsRecoid(recoid string) (bool, conf.OneRes) { +func GetDnsRecoid(recoid string, ZoneID string) (bool, conf.OneRes) { var no conf.OneRes - url := "https://api.cloudflare.com/client/v4/zones/" + conf.Config.ZoneID + "/dns_records" + url := "https://api.cloudflare.com/client/v4/zones/" + ZoneID + "/dns_records" req, err := http.NewRequest("GET", url, nil) if err != nil {