Skip to content

Commit

Permalink
1v1
Browse files Browse the repository at this point in the history
  • Loading branch information
csznet committed Mar 31, 2024
1 parent 5f90db8 commit eee81e5
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 70 deletions.
14 changes: 8 additions & 6 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand All @@ -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()

Expand All @@ -58,6 +60,6 @@ func init() {
err = decoder.Decode(&Config)
if err != nil {
fmt.Println("mata.json配置文件错误")
return
os.Exit(0)
}
}
57 changes: 27 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
61 changes: 33 additions & 28 deletions mata.sample.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}
]
}

8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ Telegram ChatID

当服务器状态改变时发送通知,不启用保持为空即可

**Target**
**Corn**

需要监控的服务器,采用TCP监控,需带上端口号
监控间隔,单位 秒

**Mata**

Main为当服务器正常时的解析,Then为当服务器离线时的解析

`proxied`为是否启用CloudFlare CDN

**Target**

需要监控的服务器,采用TCP监控,需带上端口号

### 服务器运行

下载系统对应的编译包,将`mata.sample.json`改名为`mata.json`并运行
8 changes: 4 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit eee81e5

Please sign in to comment.