-
Notifications
You must be signed in to change notification settings - Fork 0
/
logout_users.py
86 lines (71 loc) · 2.91 KB
/
logout_users.py
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
from time import time
from math import floor
import json
import requests
# Global config
# location of log files
LOG_DIR = "/home/ec2-user/MinecraftServer/SpigotMC/logs"
# LOG_DIR = "."
LOG_PATH = f"{LOG_DIR}/latest.log" # Spigot log file
PYTHON_LOG_PATH = f"{LOG_DIR}/log_handling.log" # log file for this script
API_VERSION = "v1" # current version of API (sort key for main table)
# base url for API
API_ENDPOINT = f"<API url>/{API_VERSION}"
# headers to use for all request, mainly needs api key
HEADERS = {"x-api-key": "<api key>"}
def print_log(msg):
"""Print to console but also append to log file
"""
print(msg)
with open(PYTHON_LOG_PATH, 'a+') as s:
s.write(msg + "\n")
s.close()
def get_last_login(username):
"""Returns dynamodb item for most recent login as python dict
"""
print_log(f"Getting last login session for {username}...")
endpoint = f"{API_ENDPOINT}/getLogins"
data = json.dumps({'Usernames': [username]})
response = requests.post(endpoint, headers=HEADERS, data=data)
if str(response.content.decode("UTF-8")) == 'null' or str(response.status_code)[0] != '2':
return {} # if this is a first time login, return nothing
return json.loads(response.content.decode("UTF-8"))[0]
def get_users():
"""Returns dynamodb item for most recent login as python dict
"""
print_log(f"Getting last login sessions for users...")
endpoint = f"{API_ENDPOINT}/getLogins"
response = requests.post(endpoint, headers=HEADERS)
if str(response.content.decode("UTF-8")) == 'null' or str(response.status_code)[0] != '2':
return [] # if this is a first time login, return empty list
return json.loads(response.content.decode("UTF-8"))
def logout_user(username, login_time, version=API_VERSION):
"""Sets logout time for open login session
"""
if version == API_VERSION:
print_log(f"Marking user {username} as logged out...")
endpoint = f"{API_ENDPOINT}/upsertLogin"
data = json.dumps({'Username': username,
'Version': version,
'LoginTime': login_time,
'LogoutTime': floor(time())})
response = requests.post(endpoint, headers=HEADERS, data=data)
if str(response.status_code)[0] == '2':
if version == API_VERSION:
return logout_user(username, login_time, str(floor(time())))
return get_last_login(username)
return json.loads(response.content.decode("UTF-8").replace('null', 'None'))
def main():
print_log("Server stopping... marking any logged in users as logged out")
users = get_users()
count = 0
for user in users:
if not user.get('LogoutTime'):
count += 1
logout_user(user.get('Username'), user.get('LoginTime'))
if count == 0:
print_log("All users already logged out. No action taken.")
def logout_users():
main()
if __name__ == "__main__":
main()