forked from DiscordGSM/DiscordGSM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservers.py
147 lines (115 loc) · 5.3 KB
/
servers.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
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
import json
import socket
import urllib
import asyncio
from bin import *
# load servers.json -> get all servers type, addr, port
class Servers:
def __init__(self):
self.refresh()
# refresh query server list
def refresh(self):
servers = self.get()
# get country code from ipinfo.io
is_edited = False
for server in servers:
if 'country' not in server:
try:
with urllib.request.urlopen(f'https://ipinfo.io/{socket.gethostbyname(server["addr"])}/country') as response:
country = response.read().decode("utf8")
if '{' not in country: # may response error json
server['country'] = country.rstrip() # rstrip is used because of \n
is_edited = True
except:
pass
if is_edited:
with open('configs/servers.json', 'w', encoding='utf8') as file:
json.dump(servers, file, ensure_ascii=False, indent=4)
self.servers = servers
# get servers data
def get(self):
with open('configs/servers.json', 'r') as file:
data = file.read()
return json.loads(data)
# add a server
def add(self, type, game, addr, port, channel):
data = {}
data['type'], data['game'] = type, game
data['addr'], data['port'] = addr, int(port)
data['channel'] = int(channel)
servers = self.get()
servers.append(data)
with open('configs/servers.json', 'w', encoding='utf8') as file:
json.dump(servers, file, ensure_ascii=False, indent=4)
# delete a server by id
def delete(self, id):
servers = self.get()
if 0 < int(id) <= len(servers):
del servers[int(id) - 1]
with open('configs/servers.json', 'w', encoding='utf8') as file:
json.dump(servers, file, ensure_ascii=False, indent=4)
return True
return False
# save the servers query data to cache/
def query(self):
tasks = [self.query_save_cache(server) for server in self.servers]
asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED, timeout=15)
def query_save_cache(self, server):
if server['type'] == 'SourceQuery':
query = SourceQuery(str(server['addr']), int(server['port']))
result = query.getInfo()
query.disconnect()
server_cache = ServerCache(server['addr'], server['port'])
if result:
server_cache.save_data(server['game'], result['GamePort'], result['Hostname'], result['Map'], result['MaxPlayers'], result['Players'], result['Bots'], result['Password'] == 0x01)
else:
server_cache.set_status('Offline')
elif server['type'] == 'UT3Query':
query = UT3Query(str(server['addr']), int(server['port']))
result = query.getInfo()
query.disconnect()
server_cache = ServerCache(server['addr'], server['port'])
if result:
server_cache.save_data(server['game'], result['hostport'], result['hostname'], result['map'], result['maxplayers'], result['numplayers'], 0, False)
else:
server_cache.set_status('Offline')
elif server['type'] == 'GamedigQuery':
query = GamedigQuery(str(server['game']), str(server['addr']), int(server['port']))
result = query.getInfo()
server_cache = ServerCache(server['addr'], server['port'])
if result:
server_cache.save_data(server['game'], server['port'], result['Hostname'], result['Map'], result['MaxPlayers'], result['Players'], result['Bots'], result['Password'])
else:
server_cache.set_status('Offline')
# Game Server Data
class ServerCache:
def __init__(self, addr, port):
self.addr, self.port = addr, port
self.file_name = addr.replace(':', '.') + '-' + str(port)
self.file_name = "".join(i for i in self.file_name if i not in "\/:*?<>|")
def get_status(self):
try:
with open(f'cache/{self.file_name}.txt', 'r', encoding='utf8') as file:
return file.read()
except:
return False
def set_status(self, status):
with open(f'cache/{self.file_name}.txt', 'w', encoding='utf8') as file:
file.write(str(status))
def get_data(self):
try:
with open(f'cache/{self.file_name}.json', 'r', encoding='utf8') as file:
return json.load(file)
except EnvironmentError:
return False
def save_data(self, game, gameport, name, map, maxplayers, players, bots, password):
data = {}
# save game name, ip address, query port
data['game'], data['addr'], data['port'] = game, self.addr, gameport
# save server name, map name, max players count
data['name'], data['map'], data['maxplayers'] = name, map, maxplayers
# save current players count, bots count
data['players'], data['bots'], data['password'] = players, bots, password
self.set_status('Online')
with open(f'cache/{self.file_name}.json', 'w', encoding='utf8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)