-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscummyLogs.py
More file actions
186 lines (150 loc) · 5.93 KB
/
scummyLogs.py
File metadata and controls
186 lines (150 loc) · 5.93 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
import asyncio
import io
import json
import mysql.connector
import re
import requests
import time
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime
from ftplib import FTP
# regex patterns
loginPattern = re.compile(r"(?P<timestamp>\d{4}.\d{2}.\d\d-\d\d.\d\d.\d\d): '(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}) (?P<steamid>\d*):(?P<playername>.*)\((?P<playerid>\d+)\)' (?P<status>logged in)")
logoutPattern = re.compile(r"(?P<timestamp>\d{4}.\d{2}.\d\d-\d\d.\d\d.\d\d): '(?P<playerid>\d+)' (?P<status>logging out)")
chatPattern = re.compile(r"(?P<timestamp>\d{4}.\d{2}.\d\d-\d\d.\d\d.\d\d): '(?P<steamid>\d*):(?P<playername>.*)\((?P<playerid>\d+)\)' '(?P<room>\w+): (?P<message>.*)'")
deathPattern = re.compile(r"(?P<timestamp>\d{4}.\d{2}.\d\d-\d\d.\d\d.\d\d): Died: (?P<victim>.+) \((?P<victimid>\d+)\), Killer: (?P<killer>.+) \((?P<killerid>\d+)\) Weapon: (?P<weapon>.+) \[\w+\]")
killObjectPattern = re.compile(r"{.+[:,].+}|\[.+[,:].+\]")
# start buffers
adminLog = io.BytesIO()
chatLog = io.BytesIO()
deathLog = io.BytesIO()
loginLog = io.BytesIO()
minesLog = io.BytesIO()
violationsLog = io.BytesIO()
# update dynamic dns
requests.get('https://bloodshotstudios.com/cpanelwebcall/hmpmfjzbdxuaaofaneoeetpumofjnqkp')
#connect to db
mydb = mysql.connector.connect(
host="",
user="",
password="",
database=""
)
mycursor = mydb.cursor()
async def getLogs():
while True:
# FTP credentials
host = "107.155.124.138"
port = 8821
username = ""
password = ""
# FTP connection
ftp = FTP()
ftp.connect(host, port)
ftp.login(username, password)
ftp.cwd("//")
# loop over files in directory
for file in ftp.nlst():
if re.match("admin", file):
ftp.retrbinary('RETR ' + file, adminLog.write)
elif re.match("chat", file):
ftp.retrbinary('RETR ' + file, chatLog.write)
elif re.match("kill", file):
ftp.retrbinary('RETR ' + file, deathLog.write)
elif re.match("login", file):
ftp.retrbinary('RETR ' + file, loginLog.write)
elif re.match("mines", file):
ftp.retrbinary('RETR ' + file, minesLog.write)
elif re.match("violations", file):
ftp.retrbinary('RETR ' + file, violationsLog.write)
await asyncio.sleep(1)
print("'getLogs' Task Executed")
async def parseLogs():
while True:
# source time of last run top avoid duplicates
if "lastRun" not in locals():
mycursor.execute(f"""SELECT EventDate FROM RunLog WHERE ServiceType = 'LOGS' ORDER BY EventDate DESC LIMIT 1""")
lastRun = mycursor.fetchall()[0][0]
# print buffers
for match in re.finditer(loginPattern, loginLog.getvalue().decode('utf-16-le')):
if datetime.strptime(match.group('timestamp'), '%Y.%m.%d-%H.%M.%S') < lastRun:
continue
print(match.groupdict())
mycursor.execute(
f"""INSERT INTO Login (
EventDate,
IP,
SteamID,
PlayerName,
PlayerID
) VALUES (
"{match.group('timestamp')}",
"{match.group('ip')}",
{match.group('steamid')},
"{match.group('playername')}",
{match.group('playerid')}
)"""
)
for match in re.finditer(logoutPattern, loginLog.getvalue().decode('utf-16')):
if datetime.strptime(match.group('timestamp'), '%Y.%m.%d-%H.%M.%S') < lastRun:
continue
print(match.groupdict())
mycursor.execute(
f"""INSERT INTO Logout (
EventDate,
PlayerID
) VALUES (
"{match.group('timestamp')}",
{match.group('playerid')}
)"""
)
for match in re.finditer(chatPattern, chatLog.getvalue().decode('utf-16-le')):
if datetime.strptime(match.group('timestamp'), '%Y.%m.%d-%H.%M.%S') < lastRun:
continue
print(match.groupdict())
for match in re.finditer(deathPattern, deathLog.getvalue().decode('utf-16-le')):
if datetime.strptime(match.group('timestamp'), '%Y.%m.%d-%H.%M.%S') < lastRun:
continue
print(match.groupdict())
mycursor.execute(
f"""INSERT INTO Death (
EventDate,
KillerID,
Killer,
VictimID,
Victim,
Weapon
) VALUES (
"{match.group('timestamp')}",
{match.group('killerid')},
"{match.group('killer')}",
{match.group('victimid')},
"{match.group('victim')}",
"{match.group('weapon')}"
)"""
)
# post update cleanup
lastRun = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
mycursor.execute(f"""UPDATE RunLog SET EventDate = '{lastRun}' WHERE ServiceType = 'LOGS'""")
mydb.commit()
# flush buffers
adminLog.flush()
chatLog.flush()
deathLog.flush()
loginLog.flush()
minesLog.flush()
violationsLog.flush()
# close db connection
mydb.close()
await asyncio.sleep(1)
print("'parseLogs' Task Executed")
loop = asyncio.get_event_loop()
try:
asyncio.ensure_future(getLogs())
asyncio.ensure_future(parseLogs())
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
print("Closing Loop")
loop.close()