-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
205 lines (194 loc) · 6.12 KB
/
main.go
File metadata and controls
205 lines (194 loc) · 6.12 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/ynot01/go-a2s"
)
const MASTER_URL = "https://content.aneurismiv.com/masterlist"
// udpClients["0.0.0.0:7777"] = *a2s.Client
var udpClients map[string]*a2s.Client = make(map[string]*a2s.Client)
// udpClients["0.0.0.0:7777"] = "54/60"
var playerCounts map[string]string = make(map[string]string)
// udpClients["0.0.0.0:7777"] = "SIGNAL 13 1 2025 CLUSTER"
var registeredServers map[string]string = make(map[string]string)
// https://discord.com/api/webhooks/<webhook id>/<webhook token>
var myWebhookURL string
// Loops through all official servers and queries them one by one
// If any change in name, consider them rotted and post to Discord
func main() {
fmt.Printf("[%v] Started rot-tracker-webhook.\n", time.Now().Format(time.RFC850))
content, fileErr := os.ReadFile("webhook.txt")
if fileErr != nil {
fmt.Printf("[%v] fileErr: %v\n", time.Now().Format(time.RFC850), fileErr)
return
}
myWebhookURL = strings.TrimSpace(string(content))
if myWebhookURL == "Replace this text with your Discord webhook URL" || myWebhookURL == "" {
fmt.Printf("[%v] Webhook not set! Please set webhook.txt\n", time.Now().Format(time.RFC850))
return
}
for range time.Tick(time.Second * 10) { // Wait a healthy 10 seconds
official_servers := get_masterlist()
for official := range official_servers {
ipAddr := official_servers[official]
if strings.TrimSpace(ipAddr) == "" {
continue
}
findComment := strings.Index(ipAddr, "//") // Strip comments from masterlist
if findComment != -1 {
ipAddr = strings.TrimSpace(ipAddr[:findComment])
}
dictKey := ipAddr
findPort := strings.Index(ipAddr, ":") // Isolate :port (and ignore entries without a port)
var ipPort string
if findPort == -1 {
ipPort = "7777"
} else {
ipPort = strings.TrimSpace(ipAddr[findPort:])[1:]
}
portInt, atoiErr := strconv.Atoi(ipPort)
if atoiErr != nil {
fmt.Printf("[%v] atoiErr: %v\n", time.Now().Format(time.RFC850), atoiErr)
continue
}
if findPort == -1 {
ipAddr = fmt.Sprintf("%v:%v", ipAddr, portInt+1)
} else {
ipAddr = fmt.Sprintf("%v:%v", ipAddr[:findPort], portInt+1) // Add 1 to server port to get the a2s query port
}
client, weHaveClient := udpClients[dictKey]
if !weHaveClient {
newClient, newClientErr := a2s.NewClient(
ipAddr,
a2s.SetAppID(2773280),
)
if newClientErr != nil {
fmt.Printf("[%v] newClientErr: %v\n", time.Now().Format(time.RFC850), newClientErr)
continue
}
client = newClient
udpClients[dictKey] = newClient
}
info, infoErr := client.QueryInfo()
oldServerName, serverIsRegistered := registeredServers[dictKey]
if infoErr != nil {
// Don't print on server connection errors- a few of them are down a lot
// fmt.Printf("%v \"fail\"\n", ipAddr)
if serverIsRegistered {
fmt.Printf("[%v] Fail on existing %v - %v: %v\n", time.Now().Format(time.RFC850), oldServerName, ipAddr, infoErr)
}
continue
} else {
// fmt.Printf("%v \"success\"\n", ipAddr)
}
// Servers are stored by server port, not query port!
if serverIsRegistered && oldServerName != info.Name { // If the name changed, report it to Discord
region := strings.ToUpper(get_region_from_keywords(info.ExtendedServerInfo.Keywords))
plyCount, havesPlyCnt := playerCounts[dictKey]
if !havesPlyCnt {
plyCount = "Unknown"
}
send_message_to_discord(dictKey, region, oldServerName, info.Name, plyCount)
}
if info.Players > 0 {
playerCounts[dictKey] = fmt.Sprintf("%v/%v", info.Players, info.MaxPlayers)
}
if !serverIsRegistered {
fmt.Printf("[%v] New server added to register: %v - %v\n", time.Now().Format(time.RFC850), dictKey, info.Name)
}
registeredServers[dictKey] = info.Name
}
}
}
// Download updated official server list and split by newline
func get_masterlist() []string {
resp, masterErr := http.Get(MASTER_URL)
if masterErr != nil {
fmt.Printf("[%v] masterErr: %v\n", time.Now().Format(time.RFC850), masterErr)
return []string{}
}
resBody, ioErr := io.ReadAll(resp.Body)
if ioErr != nil {
fmt.Printf("[%v] ioErr: %v\n", time.Now().Format(time.RFC850), ioErr)
return []string{}
}
return strings.Split(string(resBody), "\n")
}
// Http POST request to Discord webhook
func send_message_to_discord(ipAddr string, region string, oldServerName string, newServerName string, playerCount string) {
jsonBody := fmt.Appendf(nil, `{
"embeds": [
{
"title": "An official server has been consumed by the rot!",
"color": 16749144,
"fields": [
{
"name": "Region",
"value": "%v"
},
{
"name": "Old name",
"value": "%v"
},
{
"name": "New name",
"value": "%v"
},
{
"name": "Last recorded playercount",
"value": "%v"
}
],
"footer": {
"text": "IP Address: %v"
}
}
],
"components": []
}`, region, oldServerName, newServerName, playerCount, ipAddr)
bodyReader := bytes.NewReader(jsonBody)
resp, err := http.Post(myWebhookURL, "application/json", bodyReader)
if err != nil {
fmt.Printf("[%v] err: %v\n", time.Now().Format(time.RFC850), err)
return
}
if resp.StatusCode > 299 {
fmt.Printf("[%v] resp.StatusCode: %v\n", time.Now().Format(time.RFC850), resp.StatusCode)
return
}
}
// String operations to isolate region
// region:us,uptime:0,protected:1,rot:0.06 = us
// region:au,uptime:30,protected:1,rot:0.05 = au
// region:cn,uptime:30,protected:1,rot:0.06 = cn
func get_region_from_keywords(keywords string) string {
stringRegion := "Unknown Region"
keywordStrings := strings.Split(strings.TrimSpace(keywords), ",")
for n := range keywordStrings {
keyAndValue := strings.Split(keywordStrings[n], ":")
key := keyAndValue[0]
value := keyAndValue[1]
if key == "region" {
stringRegion = value
}
}
switch strings.ToUpper(stringRegion) {
case "US":
return "🇺🇸"
case "AU":
return "🇦🇺"
case "CN":
return "🇨🇳"
case "RU":
return "🇷🇺"
case "EUROPEANUNION":
return "🇪🇺"
}
return stringRegion
}