-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
138 lines (132 loc) · 3.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"csz.net/mata/conf"
"csz.net/mata/utils"
)
var Once bool
var web bool
func main() {
for {
for _, mata := range conf.Config.Mata {
if mata.PS == "" {
mata.PS = mata.Target
}
log.Println("开始检测" + mata.PS)
send := false
msg := "服务器在线"
onlineCount := 0
online := false
// 判断是否为主动检测
if !strings.ContainsAny(mata.Target, ".") {
if !web {
go utils.Web()
web = true
}
// 被动检测
if !strings.ContainsAny(mata.Target, ".") {
// 判断是否存在map中
if value, exists := conf.Array[mata.Target]; !exists {
// 初始化时多添加一个周期
conf.Array[mata.Target] = int(time.Now().Unix()) + int(conf.Config.Corn)
online = true
log.Printf("被动检测, %s\n", mata.Target)
} else {
if value >= int(time.Now().Unix()) {
online = true
}
}
}
} else {
// 主动检测
for i := 0; i < 3; i++ {
check, status := utils.Check(mata.Target, 5*time.Second)
if !check {
log.Println("检测失败")
continue
}
if status {
onlineCount++
}
time.Sleep(10 * time.Second)
}
if onlineCount >= 2 {
online = true
}
}
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 {
log.Printf("无需修改解析【%s】\n", mata.Main.Name)
}
} else {
msg = "服务器离线"
log.Println(msg)
if mata.Then.ZoneID == "" {
// 不填写then的zoneid则默认为main的zoneid
mata.Then.ZoneID = mata.Main.ZoneID
}
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 {
// 发送到TG
if conf.Config.BotToken != "" && conf.Config.ChatID != "" {
msg = "【" + mata.PS + "】" + msg
go utils.SendTG("#MATA " + msg)
}
// 发送到Server酱
if conf.Config.ServerJiang != "" {
go utils.SendSJ(msg)
}
}
}
if Once {
return
}
time.Sleep(time.Duration(conf.Config.Corn) * time.Second)
}
}
func init() {
once := flag.Bool("once", false, "Run once")
port := flag.String("port", "8080", "Web port")
conf.WebPort = *port
configPath := flag.String("config", "mata.json", "Config file path")
flag.Parse()
if *once {
Once = true
}
configInit(*configPath)
}
func configInit(path string) {
// 读取配置文件
file, err := os.Open(path)
if err != nil {
fmt.Println(path + "配置文件不存在")
os.Exit(0)
}
defer file.Close()
// 解码 JSON
decoder := json.NewDecoder(file)
err = decoder.Decode(&conf.Config)
if err != nil {
fmt.Println(path + "配置文件错误")
os.Exit(0)
}
conf.Config.TgApiUrl = strings.TrimRight(conf.Config.TgApiUrl, "/")
}