-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
316 lines (252 loc) · 7.08 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"bufio"
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/bwmarrin/discordgo"
_ "github.com/go-sql-driver/mysql"
"github.com/hako/durafmt"
"github.com/tkanos/gonfig"
)
type config struct {
Database string
Token string
Channel string
Server string
Port string
Rcon string
EUServers []string
NAServers []string
AUServers []string
Others []string
}
type player struct {
PlayerInfo struct {
RegUnixTime int64
}
}
var (
filename string = ""
configFile config
)
func main() {
exe, _ := os.Executable()
exePath := filepath.Dir(exe)
filename = exePath + "/config.json"
println(filename)
err := gonfig.GetConf(filename, &configFile)
if err != nil {
panic(err.Error())
}
var wg sync.WaitGroup
// launch a bsion bot for each official kag server
for i := 0; i < len(configFile.EUServers); i++ {
wg.Add(1)
go bsion(&wg, configFile.EUServers[i], configFile.Rcon)
}
for i := 0; i < len(configFile.NAServers); i++ {
wg.Add(1)
go bsion(&wg, configFile.NAServers[i], configFile.Rcon)
}
for i := 0; i < len(configFile.AUServers); i++ {
wg.Add(1)
go bsion(&wg, configFile.AUServers[i], configFile.Rcon)
}
wg.Wait()
}
func bsion(wg *sync.WaitGroup, serverIP string, pw string) {
conn := connectToKag(serverIP, pw)
discord := connectToDiscord()
db := connectToSQL()
if conn != nil && discord != nil && db != nil {
defer conn.Close()
defer discord.Close()
defer db.Close()
defer wg.Done()
listen(conn, discord, db, pw)
}
}
func listen(conn net.Conn, session *discordgo.Session, db *sql.DB, pw string) {
reader := bufio.NewReader(conn)
for {
message, err := reader.ReadString('\n')
if err != nil {
log.Println("error reading message on ip ", conn.RemoteAddr().String(), " ", err)
conn = connectToKag(conn.RemoteAddr().String(), pw)
reader.Reset(reader)
reader = bufio.NewReader(conn)
continue
}
// fmt.Println([]byte(message))
fmt.Println(message)
if isValidTcprMessage(&message, "*REPORT") {
regex := regexp.MustCompile("\\*REPORT \\*PLAYER=\\\"(.*?)\\\" \\*BADDIE=\\\"(.*?)\\\" \\*COUNT=\\\"(\\d*?)\\\" \\*SERVERNAME=\\\"(.*?)\\\" \\*SERVERIP=\\\"(.*?)\\\" \\*REASON=\\\"(.*?)\\\"")
tokens := regex.FindStringSubmatch(message)
if len(tokens) < 6 {
log.Println("incoming report was not valid: ", message)
continue
}
fmt.Println(tokens[1:])
// tokens := strings.Split(message, " ")
player, baddie, reportcount, servername, serverip, reason := tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]
serverlink := "<kag://" + serverip + "/>"
reportcount = strings.TrimSpace(reportcount)
reason = strings.TrimSpace(reason)
reportCountInt, err := strconv.Atoi(strings.TrimSpace(reportcount))
if err != nil {
log.Println("reportcount isn't an int,", err)
break
}
fmt.Println("got message")
regUnixTime := getKagPlayer(baddie).PlayerInfo.RegUnixTime
regTime := time.Unix(regUnixTime, 0)
nowTime := time.Now()
duration := nowTime.Sub(regTime)
age := durafmt.Parse(duration).LimitFirstN(2).String()
if reportCountInt >= 2 {
_, err := session.ChannelMessageSend(configFile.Channel,
"@here "+baddie+" has been reported by "+player+" for a total of "+reportcount+" reports\n"+
"Reason: "+"\""+reason+"\"\n"+
"Server: "+servername+"\n"+
"Address: "+serverlink+"\n"+
"Account Age: "+age)
if err != nil {
log.Println("cant send message,", err)
break
}
} else {
_, err := session.ChannelMessageSend(configFile.Channel,
"@here "+baddie+" has been reported by "+player+" for a total of "+reportcount+" report\n"+
"Reason: "+"\""+reason+"\"\n"+
"Server: "+servername+"\n"+
"Address: "+serverlink+"\n"+
"Account Age: "+age)
if err != nil {
log.Println("cant send message,", err)
break
}
}
dbwrite(db, baddie, reportcount)
} else if isValidTcprMessage(&message, "*LOG") {
regex := regexp.MustCompile("\\*LOG \\*MESSAGE=\\\"(.*?)\\\" \\*SERVERNAME=\\\"(.*?)\\\" \\*SERVERIP=\\\"(.*?)\\\"")
tokens := regex.FindStringSubmatch(message)
if len(tokens) < 3 {
log.Println("incoming log was not valid: ", message)
continue
}
fmt.Println(tokens[1:])
message, servername, serverip := tokens[1], tokens[2], tokens[3]
serverlink := "<kag://" + serverip + "/>"
fmt.Println("got a log message")
_, err := session.ChannelMessageSend(configFile.Channel, message+"\n"+
"Server: "+servername+"\n"+
"Address: "+serverlink)
if err != nil {
log.Println("cant send message,", err)
break
}
}
}
}
// Check's if the incoming message type is made by a user
// Example
// <Vamist> *REPORT... will return false
// *REPORT... will return true
func isValidTcprMessage(message *string, wantedType string) bool {
typeIndex := strings.Index(*message, wantedType)
// Does this message contain our type?
if typeIndex > 0 {
// Let's search for the start of a user message
userIndex := strings.Index(*message, "<")
if userIndex > 0 && userIndex < typeIndex {
log.Println("possible user forged tcpr message?")
return false
}
return true
}
return false
}
func dbwrite(db *sql.DB, playerName, reportcount string) {
_, err := db.Exec("insert into `reports` (`player_name`, `report_count`, `last_date`) values (?, ?, NOW()) on duplicate key update `report_count` = `report_count` + 1, `last_date` = NOW()", playerName, reportcount)
// if there is an error inserting, handle it
if err != nil {
log.Println("cant write message to db,", err)
}
// defer insert.Close()
log.Println("wrote to db")
}
func connectToKag(serverIP string, pw string) net.Conn {
var conn net.Conn
var err error
connectedToKag := false
// start tcp connection to kag server
for connectedToKag != true {
log.Println("connecting to KAG server...")
conn, err = net.Dial("tcp", serverIP)
if err != nil {
log.Println("couldn't connect to kag server. ", err)
} else {
log.Println("connected successfully")
connectedToKag = true
break
}
time.Sleep(30 * 1000 * time.Millisecond)
}
if connectedToKag {
// authenticate to server as rcon
_, err = conn.Write([]byte(pw + "\n"))
if err != nil {
log.Println("couldn't login as rcon, ", err)
}
}
return conn
}
func connectToSQL() *sql.DB {
db, err := sql.Open("mysql", configFile.Database)
if err != nil {
panic(err)
}
return db
}
func connectToDiscord() *discordgo.Session {
discord, err := discordgo.New("Bot " + configFile.Token)
if err != nil {
panic(err)
}
// open a websocket connection to Discord and begin listening.
err = discord.Open()
if err != nil {
panic(err)
return nil
}
return discord
}
func getKagPlayer(username string) *player {
resp, err := http.Get("https://api.kag2d.com/v1/player/" + username)
if err != nil {
panic(err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
p := &player{}
err = json.Unmarshal(body, p)
if err != nil {
panic(err.Error())
}
return p
}