-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1484 lines (1264 loc) · 53.9 KB
/
server.py
File metadata and controls
1484 lines (1264 loc) · 53.9 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import anyio
import asyncio
import json
import logging
import socket
import sqlite3
import time
from dataclasses import dataclass
from datetime import datetime, timezone
from email.utils import formatdate
from enum import Enum
from pathlib import Path
from typing import Dict, List, Set, Any, Optional, Union
import httpx
import uvicorn
import re
from fastapi import FastAPI
from fastapi.responses import JSONResponse
REFRESH_INTERVAL = 15 # seconds
STATS_INTERVAL = 300 # seconds
API_TIMEOUT = 5.0 # seconds
DB_PATH = "database/database.sqlite"
# --- ElDewrito configuration ---
ELDEWRITO_MASTER_LIST = "dewrito.json"
LEGACY_ELDEWRITO_STATS_URL = "https://eldewrito.pauwlo.com/api/stats"
# --- Cartographer configuration ---
CARTOGRAPHER_BASE = "https://cartographer.online"
CARTOGRAPHER_LIST_URL = f"{CARTOGRAPHER_BASE}/live/server_list.php"
CARTOGRAPHER_SERVER_URL = f"{CARTOGRAPHER_BASE}/live/servers"
CARTOGRAPHER_WORKERS = 16
# --- Cartographer Property Maps ---
PROPERTY_MAP = {
1073775152: "server_name",
536904239: "xuid",
536904219: "unknown_int64_1",
1073775141: "server_desc",
268468743: "map_id",
1073775142: "map_name",
1073775143: "map_hash_1",
1073775144: "gametype_name",
268468744: "unknown_int32_1",
268468745: "gametype_id",
268468746: "map_id_2",
1073775145: "map_name_2",
1073775146: "map_hash_2",
1073775147: "gametype_name_2",
268468747: "unknown_int32_2",
268468748: "unknown_int32_3",
268468749: "unknown_int32_4",
268468750: "version_1",
268468751: "version_2",
268468752: "party_privacy",
268468753: "game_status",
268468754: "unknown_int32_6",
268468755: "unknown_int32_7",
}
# --- Cartographer Map Info Table ---
MAP_ID_TO_INFO = {
1: ("00a_introduction", "The Heretic"),
101: ("01a_tutorial", "Armory"),
105: ("01b_spacestation", "Cairo Station"),
301: ("03a_oldmombasa", "Outskirts"),
305: ("03b_newmombasa", "Metropolis"),
401: ("04a_gasgiant", "The Arbiter"),
405: ("04b_floodlab", "Oracle"),
501: ("05a_deltaapproach", "Delta Halo"),
505: ("05b_deltatowers", "Regret"),
601: ("06a_sentinelwalls", "Sacred Icon"),
605: ("06b_floodzone", "Quarantine Zone"),
701: ("07a_highcharity", "Gravemind"),
801: ("07b_forerunnership", "High Charity"),
705: ("08a_deltacliffs", "Uprising"),
805: ("08b_deltacontrol", "The Great Journey"),
80: ("ascension", "Ascension"),
1201: ("backwash", "Backwash"),
100: ("beavercreek", "Beaver Creek"),
60: ("burial_mounds", "Burial Mounds"),
110: ("coagulation", "Coagulation"),
70: ("colossus", "Colossus"),
1300: ("containment", "Containment"),
10: ("cyclotron", "Ivory Tower"),
1302: ("deltatap", "Sanctuary"),
1400: ("derelict", "Desolation"),
3001: ("derelict", "Desolation"),
1200: ("dune", "Relic"),
1001: ("elongation", "Elongation"),
120: ("foundation", "Foundation"),
1002: ("gemini", "Gemini"),
800: ("headlong", "Headlong"),
1402: ("highplains", "Tombstone"),
3000: ("highplains", "Tombstone"),
50: ("lockout", "Lockout"),
20: ("midship", "Midship"),
444678: ("needle", "Uplift"),
91101: ("street_sweeper", "District"),
1101: ("triplicate", "Terminal"),
1000: ("turf", "Turf"),
1109: ("warlock", "Warlock"),
40: ("waterworks", "Waterworks"),
30: ("zanzibar", "Zanzibar"),
}
# --- Cartographer Gametype Table ---
GAMETYPE_ID_TO_NAME = {
0: "None",
1: "CTF",
2: "Slayer",
3: "Oddball",
4: "KOTH",
5: "Race",
6: "Headhunter",
7: "Juggernaut",
8: "Territories",
9: "Assault",
10: "Stub",
}
# --- GameSpy Protocol Implementation ---
class GameKeys(Enum):
HALO = "QW88cv"
HALOD = "yG3d9w"
HALOMACD = "e4Rd9J"
HALOMAC = "e4Rd9J"
HALOM = "e4Rd9J"
HALOR = "e4Rd9J"
GAMESPY_IP_PORT_LENGTH = 6
class GameSpyFlags:
A, B, C, D = 0x02, 0x08, 0x10, 0x20
@dataclass
class GameSpyServerAddress:
address: str
port: int
@dataclass
class GameSpyServer:
address: str
port: int
game: Optional[str] = None
@dataclass
class GameSpyServerResponse:
address: str
port: int
game: Optional[str]
data: Union[str, Dict[str, Any]]
# GameSpy Decryption Algorithm
def _enctypex_func5(encxkey: bytearray, cnt: int, id_bytes: bytes, idlen: int, n1: int, n2: int) -> tuple:
if cnt == 0: return 0, n1, n2
mask = 1
if cnt > 1:
while mask < cnt: mask = (mask << 1) + 1
i = 0
while True:
n1 = encxkey[n1 & 0xff] + id_bytes[n2]
n2 += 1
if n2 >= idlen:
n2 = 0
n1 += idlen
tmp = n1 & mask
i += 1
if i > 11: tmp %= cnt
if tmp <= cnt: break
return tmp, n1, n2
def _enctypex_func4(encxkey: bytearray, id_bytes: bytes, idlen: int):
if idlen < 1: return
for i in range(256): encxkey[i] = i
n1 = n2 = 0
for i in range(255, -1, -1):
t1, n1, n2 = _enctypex_func5(encxkey, i, id_bytes, idlen, n1, n2)
t2 = encxkey[i]
encxkey[i] = encxkey[t1]
encxkey[t1] = t2
encxkey[256] = encxkey[1]
encxkey[257] = encxkey[3]
encxkey[258] = encxkey[5]
encxkey[259] = encxkey[7]
encxkey[260] = encxkey[n1 & 0xff]
def _enctypex_func7(encxkey: bytearray, d: int) -> int:
a = encxkey[256]
b = encxkey[257]
c = encxkey[a]
encxkey[256] = (a + 1) & 0xff
encxkey[257] = (b + c) & 0xff
a = encxkey[260]
b = encxkey[257]
b = encxkey[b]
c = encxkey[a]
encxkey[a] = b
a = encxkey[259]
b = encxkey[257]
a = encxkey[a]
encxkey[b] = a
a = encxkey[256]
b = encxkey[259]
a = encxkey[a]
encxkey[b] = a
a = encxkey[256]
encxkey[a] = c
b = encxkey[258]
a = encxkey[c]
c = encxkey[259]
b = (b + a) & 0xff
encxkey[258] = b
a = b
c = encxkey[c]
b = encxkey[257]
b = encxkey[b]
a = encxkey[a]
c = (c + b) & 0xff
b = encxkey[260]
b = encxkey[b]
c = (c + b) & 0xff
b = encxkey[c]
c = encxkey[256]
c = encxkey[c]
a = (a + c) & 0xff
c = encxkey[b]
b = encxkey[a]
encxkey[260] = d & 0xff
c = c ^ b ^ (d & 0xff)
encxkey[259] = c & 0xff
return c & 0xff
def _enctypex_func6(encxkey: bytearray, data: bytearray, length: int) -> int:
for i in range(length): data[i] = _enctypex_func7(encxkey, data[i])
return length
def _enctypex_funcx(encxkey: bytearray, key: bytes, encxvalidate: bytearray, data: bytes, datalen: int):
keylen = len(key)
for i in range(datalen):
idx1 = (key[i % keylen] * i) & 7
idx2 = i & 7
encxvalidate[idx1] = (encxvalidate[idx1] ^ encxvalidate[idx2] ^ data[i]) & 0xff
_enctypex_func4(encxkey, bytes(encxvalidate), 8)
def _enctypex_init(encxkey: bytearray, key: bytes, validate: bytes, data: bytearray) -> tuple:
datalen = len(data)
if datalen < 1: return None, 0
a = (data[0] ^ 0xec) + 2
if datalen < a: return None, 0
b = data[a - 1] ^ 0xea
if datalen < (a + b): return None, 0
encxvalidate = bytearray(validate[:8])
_enctypex_funcx(encxkey, key, encxvalidate, bytes(data[a:a+b]), b)
a += b
return data[a:], a
def _enctypex_decoder(key: bytes, validate: bytes, data: bytes) -> bytes:
encxkey = bytearray(261)
data_array = bytearray(data)
result, offset = _enctypex_init(encxkey, key, validate, data_array)
if result is None: return None
result_array = bytearray(result)
_enctypex_func6(encxkey, result_array, len(result_array))
return bytes(result_array)
def _gamespy_decryptx(key: str, validate: str, data: bytes) -> bytes:
try:
return _enctypex_decoder(key.encode('ascii'), validate.encode('ascii'), data)
except: return None
def _make_validation_key() -> str:
return format(int(time.time() * 1000), 'x')[-8:].zfill(8)
class GameSpyUDPClient:
def __init__(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.settimeout(0.1)
def close(self):
try: self.socket.close()
except: pass
def send_query(self, address: str, port: int, data: str):
try:
self.socket.sendto(data.encode('utf-8'), (address, port))
return True
except: return False
async def write(self, address: str, port: int, data: str):
return await asyncio.get_event_loop().run_in_executor(None, self.send_query, address, port, data)
def receive_responses(self, end_delay: float = 0.5) -> Optional[List[Dict]]:
responses = []
last_receive_time = time.time()
while True:
try:
data, addr = self.socket.recvfrom(4096)
responses.append({'address': addr[0], 'port': addr[1], 'data': data})
last_receive_time = time.time()
except socket.timeout:
if time.time() - last_receive_time > end_delay: break
except: break
return responses if responses else None
async def read_all(self, end_delay: float = 0.5) -> Optional[List[Dict]]:
return await asyncio.get_event_loop().run_in_executor(None, self.receive_responses, end_delay)
class GameSpyTCPClient:
def __init__(self, host: str, port: int, timeout: float = 5.0):
self.host, self.port, self.timeout = host, port, timeout
self.socket = None
async def connect(self):
def _conn():
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(self.timeout)
self.socket.connect((self.host, self.port))
await asyncio.get_event_loop().run_in_executor(None, _conn)
async def request(self, data: bytes) -> bytes:
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, self.socket.sendall, data)
response = b''
while True:
try:
chunk = await asyncio.wait_for(loop.run_in_executor(None, self.socket.recv, 4096), timeout=1.0)
if not chunk: break
response += chunk
except asyncio.TimeoutError: break
return response
def close(self):
if self.socket: self.socket.close()
def _encode_master_server_request(game: str, validation_key: str) -> bytes:
def cstring(s: str) -> List[int]: return [ord(c) for c in s] + [0]
data = [0, 0, 0] + [1, 3, 0, 0, 0, 0] + cstring(game) + cstring(game) + cstring(validation_key) + [0]*5
data[1] = len(data)
return bytes(data)
def _decode_master_server_response(data: bytes) -> Optional[Dict]:
if len(data) < GAMESPY_IP_PORT_LENGTH: return None
scanner = 0
request_ip = '.'.join(str(b) for b in data[scanner:scanner+4])
common_port = (data[scanner+4] << 8) | data[scanner+5]
scanner += GAMESPY_IP_PORT_LENGTH
if common_port == 0xFFFF: return None
def extract_pascal_string(offset: int):
if offset >= len(data): return "", offset
size = data[offset]
return data[offset+1:offset+1+size].decode('ascii', errors='ignore'), offset + size + 1
_, scanner = extract_pascal_string(scanner)
_, scanner = extract_pascal_string(scanner)
servers = []
while scanner < len(data):
flag = data[scanner]
scanner += 1
extra_len = sum([3 if flag & GameSpyFlags.A else 0, 4 if flag & GameSpyFlags.B else 0, 2 if flag & GameSpyFlags.C else 0, 2 if flag & GameSpyFlags.D else 0])
if scanner + GAMESPY_IP_PORT_LENGTH > len(data): break
ip = '.'.join(str(b) for b in data[scanner:scanner+4])
port = (data[scanner+4] << 8) | data[scanner+5]
scanner += GAMESPY_IP_PORT_LENGTH + extra_len - 1
if flag == 0 and ip == "255.255.255.255": break
servers.append(GameSpyServerAddress(address=ip, port=port))
return {'request_ip': request_ip, 'servers': servers}
async def _get_gamespy_master_server_list(game, host="hosthpc.com", port=28910, timeout=5.0):
game_map = {'halom':'HALOM','halor':'HALOR','halod':'HALOD','halomac':'HALOMAC','halomacd':'HALOMACD','halo':'HALO'}
game_enum = game_map.get(game.lower())
if not game_enum: return []
client = GameSpyTCPClient(host, port, timeout)
try:
await client.connect()
vkey = _make_validation_key()
encrypted = await client.request(_encode_master_server_request(game, vkey))
decrypted = _gamespy_decryptx(GameKeys[game_enum].value, vkey, encrypted)
decoded = _decode_master_server_response(decrypted) if decrypted else None
return decoded['servers'] if decoded else []
except: return []
finally: client.close()
async def _resolve_gamespy_servers(args, master_host="hosthpc.com", master_port=28910, timeout=5.0):
servers = []
game_map = {'ce':'halom','pc':'halor','trial':'halod','mac':'halomac','macdemo':'halomacd','beta':'halo'}
for arg in args:
if isinstance(arg, str):
game_key = game_map.get(arg.lower(), arg)
s_list = await _get_gamespy_master_server_list(game_key, master_host, master_port, timeout)
servers.extend([GameSpyServer(s.address, s.port, arg) for s in s_list])
else: servers.append(GameSpyServer(arg.address, arg.port))
return servers
async def _query_gamespy_server_info(servers, timeout=2.0):
if not servers: return None
address_game_map = {f"{s.address}:{s.port}": s.game for s in servers}
client = GameSpyUDPClient()
try:
for s in servers: await client.write(s.address, s.port, '\\')
await asyncio.sleep(0.2)
responses = await client.read_all(end_delay=timeout)
if not responses: return None
return [GameSpyServerResponse(r['address'], r['port'], address_game_map.get(f"{r['address']}:{r['port']}"), r['data'].decode('utf-8', errors='ignore')) for r in responses]
finally: client.close()
def _clean_gamespy_string(s: str) -> str:
result, i = [], 0
while i < len(s):
c = s[i]
if ord(c) < 32 and c not in '\t\n\r': i += 1; continue
if c == '^' and i + 1 < len(s) and (s[i+1].isdigit() or s[i+1].islower()): i += 2; continue
if c == '\\': result.append('/'); i += 1; continue
if 32 <= ord(c) < 127 or ord(c) >= 128: result.append(c)
i += 1
return ''.join(result)
def _parse_gamespy_server_info(response_str: str) -> Dict[str, Any]:
parts = response_str.split('\\')
if parts and parts[0] == '': parts = parts[1:]
info = {}
for i in range(0, len(parts) - 1, 2):
key, value = parts[i], _clean_gamespy_string(parts[i+1])
if value.isdigit(): value = int(value)
elif re.match(r'^\d+\.\d+$', value): value = float(value)
info[key] = value
return info
# --- Logging Setup ---
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# --- Global State (Cache) ---
eldewrito_cache: Dict[str, Any] = {}
cartographer_cache: Dict[str, Any] = {}
cartographer_summarized_cache: Dict[str, Any] = {}
haloce_cache: Dict[str, Any] = {}
halopc_cache: Dict[str, Any] = {}
app = FastAPI()
# --- Cartographer Helper Functions ---
def clean_string_field(s: Any) -> Any:
"""Clean and normalize string fields from Cartographer API."""
if not isinstance(s, str):
return s
s = s.strip()
# Remove wrapping quotes
if s.startswith('"'):
s = s[1:]
if s.endswith('"'):
s = s[:-1]
# Strip control chars but keep all other unicode
s = ''.join(c for c in s if ord(c) >= 0x20 and ord(c) != 0x7f)
return s.strip()
def decode_properties(pp: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Decode properties array into named fields."""
out = {}
raw_props = []
pm = PROPERTY_MAP
for prop in pp:
pid = prop.get('dwPropertyId')
ptype = prop.get('type')
val = prop.get('value')
if isinstance(val, str):
val = clean_string_field(val)
name = pm.get(pid)
out_key = name if name else f"prop_{hex(pid) if isinstance(pid,int) else pid}"
out[out_key] = { 'value': val, 'type': ptype }
raw_props.append({ 'dwPropertyId': pid, 'type': ptype, 'value': val })
out['_raw'] = raw_props
return out
def summarize_server(data: Dict[str, Any]) -> Dict[str, Any]:
"""Summarize Cartographer server data into a clean format."""
if data is None:
return {}
summary = {}
summary['xuid'] = data.get('xuid') or data.get('XUID') or None
summary['players'] = { 'filled': data.get('dwFilledPublicSlots'), 'max': data.get('dwMaxPublicSlots') }
pp = data.get('pProperties') or []
decoded = decode_properties(pp)
server_name = decoded.get('server_name', {}).get('value') or decoded.get('prop_0x40008230', {}).get('value') or ''
server_name = server_name.strip()
map_name = decoded.get('map_name', {}).get('value') or decoded.get('map_name_2', {}).get('value') or ''
map_id = decoded.get('map_id', {}).get('value') or decoded.get('map_id_2', {}).get('value')
gt1 = decoded.get('gametype_name', {}).get('value') or ''
gt2 = decoded.get('gametype_name_2', {}).get('value') or ''
gtid_raw = decoded.get('gametype_id', {}).get('value')
gametype_display = ''
if gtid_raw is not None:
try:
gtid_int = int(gtid_raw)
gametype_display = GAMETYPE_ID_TO_NAME.get(gtid_int, str(gtid_int))
except Exception:
gametype_display = str(gtid_raw)
variant_display = gt1 or gt2 or ''
if not map_name and map_id is not None:
try:
mid = int(map_id)
info = MAP_ID_TO_INFO.get(mid)
if info and len(info) > 1:
map_name = info[1]
else:
map_name = f"<map id {mid}>"
except Exception:
map_name = f"<map id {map_id}>"
description = (decoded.get('server_desc', {}).get('value') or '').strip()
summary['server_name'] = server_name
summary['map_name'] = map_name
summary['map_id'] = map_id
summary['gametype'] = gametype_display
summary['variant'] = variant_display
summary['description'] = description
summary['decoded_properties'] = decoded
# Preserve some raw top-level Cartographer fields that are not part
# of the property list but are present in the upstream JSON.
# These are useful for debugging/network info (saddr, abenet, abonline, xnaddr).
try:
if 'saddr' in data:
summary['saddr'] = data.get('saddr')
if 'abenet' in data:
summary['abenet'] = data.get('abenet')
if 'abonline' in data:
summary['abonline'] = data.get('abonline')
if 'xnaddr' in data:
summary['xnaddr'] = data.get('xnaddr')
except Exception:
# don't fail summarization if unexpected types are present
pass
return summary
async def fetch_cartographer_server_details(client: httpx.AsyncClient, server_id: Any) -> Optional[Dict[str, Any]]:
"""Fetch details for a single Cartographer server."""
url = f"{CARTOGRAPHER_SERVER_URL}/{server_id}"
try:
r = await client.get(url, timeout=15.0)
r.raise_for_status()
json_data = r.json()
summary = summarize_server(json_data)
# Preserve the original upstream payload for debugging and inspection
try:
summary['_raw_cartographer'] = json_data
except Exception:
pass
if summary:
return summary
# Remote returned an empty/null payload — return a consistent failure object
logger.warning(f"Cartographer server {server_id} returned empty data")
return {'xuid': server_id, 'server_name': '', 'map_name': '', 'gametype': '', 'variant': '', 'description': '<failed>'}
except Exception as e:
logger.warning(f"Failed to fetch Cartographer server {server_id}: {e}")
return {'xuid': server_id, 'server_name': '', 'map_name': '', 'gametype': '', 'variant': '', 'description': '<failed>'}
# --- Database Functions ---
async def fetch_legacy_eldewrito_stats() -> Optional[Dict[str, List[List[int]]]]:
"""Fetch historical ElDewrito stats from legacy API."""
try:
async with httpx.AsyncClient() as client:
logger.info(f"Fetching legacy ElDewrito stats from {LEGACY_ELDEWRITO_STATS_URL}...")
response = await client.get(LEGACY_ELDEWRITO_STATS_URL, timeout=30.0)
response.raise_for_status()
data = response.json()
if "players" in data and "servers" in data:
if isinstance(data["players"], list) and isinstance(data["servers"], list):
logger.info(f"Successfully fetched {len(data['players'])} historical ElDewrito data points")
return data
logger.warning("Legacy stats API returned unexpected format")
return None
except Exception as e:
logger.warning(f"Failed to fetch legacy ElDewrito stats: {e}")
return None
def populate_from_legacy_eldewrito_stats(legacy_data: Dict[str, List[List[int]]]):
"""Populate ElDewrito database with legacy stats data."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
players_dict = {entry[0]: entry[1] for entry in legacy_data.get("players", [])}
servers_dict = {entry[0]: entry[1] for entry in legacy_data.get("servers", [])}
all_timestamps = set(players_dict.keys()) | set(servers_dict.keys())
records_added = 0
for timestamp_ms in sorted(all_timestamps):
timestamp_dt = datetime.fromtimestamp(timestamp_ms / 1000, tz=timezone.utc)
player_count = players_dict.get(timestamp_ms, 0)
server_count = servers_dict.get(timestamp_ms, 0)
cursor.execute("""
INSERT INTO server_stats (player_count, server_count, recorded_at)
VALUES (?, ?, ?)
""", (player_count, server_count, timestamp_dt))
records_added += 1
conn.commit()
conn.close()
logger.info(f"Successfully populated ElDewrito database with {records_added} historical records")
except Exception as e:
logger.error(f"Failed to populate ElDewrito stats from legacy data: {e}")
async def init_db():
"""Initialize the stats tables and populate ElDewrito stats with legacy data if empty."""
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
# ElDewrito stats table
cursor.execute("""
CREATE TABLE IF NOT EXISTS server_stats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
player_count INTEGER NOT NULL DEFAULT 0,
server_count INTEGER NOT NULL DEFAULT 0,
recorded_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_recorded_at
ON server_stats(recorded_at)
""")
# Cartographer stats table
cursor.execute("""
CREATE TABLE IF NOT EXISTS cartographer_stats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
player_count INTEGER NOT NULL DEFAULT 0,
server_count INTEGER NOT NULL DEFAULT 0,
recorded_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_cartographer_recorded_at
ON cartographer_stats(recorded_at)
""")
# Halo CE stats table
cursor.execute("""
CREATE TABLE IF NOT EXISTS haloce_stats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
player_count INTEGER NOT NULL DEFAULT 0,
server_count INTEGER NOT NULL DEFAULT 0,
recorded_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_halo_ce_recorded_at
ON haloce_stats(recorded_at)
""")
# Halo PC stats table
cursor.execute("""
CREATE TABLE IF NOT EXISTS halopc_stats (
id INTEGER PRIMARY KEY AUTOINCREMENT,
player_count INTEGER NOT NULL DEFAULT 0,
server_count INTEGER NOT NULL DEFAULT 0,
recorded_at TIMESTAMP NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_halo_pc_recorded_at
ON halopc_stats(recorded_at)
""")
# Only check ElDewrito stats for legacy data population
cursor.execute("SELECT COUNT(*) FROM server_stats")
eldewrito_count = cursor.fetchone()[0]
conn.commit()
conn.close()
logger.info("Database initialized")
# Only populate legacy data for ElDewrito stats table
if eldewrito_count == 0:
logger.info("ElDewrito stats table is empty, attempting to fetch legacy data...")
legacy_data = await fetch_legacy_eldewrito_stats()
if legacy_data and (legacy_data.get("players") or legacy_data.get("servers")):
populate_from_legacy_eldewrito_stats(legacy_data)
else:
logger.info("No legacy data available, starting with empty ElDewrito stats database")
def save_eldewrito_stats(player_count: int, server_count: int):
"""Save current ElDewrito stats to database."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
now = datetime.now(timezone.utc)
cursor.execute("""
INSERT INTO server_stats (player_count, server_count, recorded_at)
VALUES (?, ?, ?)
""", (player_count, server_count, now))
conn.commit()
conn.close()
logger.info(f"Saved ElDewrito stats: {server_count} servers, {player_count} players")
except Exception as e:
logger.error(f"Failed to save ElDewrito stats: {e}")
def save_cartographer_stats(player_count: int, server_count: int):
"""Save current Cartographer stats to database."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
now = datetime.now(timezone.utc)
cursor.execute("""
INSERT INTO cartographer_stats (player_count, server_count, recorded_at)
VALUES (?, ?, ?)
""", (player_count, server_count, now))
conn.commit()
conn.close()
logger.info(f"Saved Cartographer stats: {server_count} servers, {player_count} players")
except Exception as e:
logger.error(f"Failed to save Cartographer stats: {e}")
def save_haloce_stats(player_count: int, server_count: int):
"""Save current Halo CE stats to database."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
now = datetime.now(timezone.utc)
cursor.execute("""
INSERT INTO haloce_stats (player_count, server_count, recorded_at)
VALUES (?, ?, ?)
""", (player_count, server_count, now))
conn.commit()
conn.close()
logger.info(f"Saved Halo CE stats: {server_count} servers, {player_count} players")
except Exception as e:
logger.error(f"Failed to save Halo CE stats: {e}")
def save_halopc_stats(player_count: int, server_count: int):
"""Save current Halo PC stats to database."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
now = datetime.now(timezone.utc)
cursor.execute("""
INSERT INTO halopc_stats (player_count, server_count, recorded_at)
VALUES (?, ?, ?)
""", (player_count, server_count, now))
conn.commit()
conn.close()
logger.info(f"Saved Halo PC stats: {server_count} servers, {player_count} players")
except Exception as e:
logger.error(f"Failed to save Halo PC stats: {e}")
def get_eldewrito_stats_history() -> Dict[str, List[List[int]]]:
"""Retrieve historical ElDewrito stats from database."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
SELECT
CAST(strftime('%s', recorded_at) AS INTEGER) * 1000 as timestamp,
player_count,
server_count
FROM server_stats
ORDER BY recorded_at ASC
""")
rows = cursor.fetchall()
conn.close()
players = [[row[0], row[1]] for row in rows]
servers = [[row[0], row[2]] for row in rows]
return {
"players": players,
"servers": servers
}
except Exception as e:
logger.error(f"Failed to retrieve ElDewrito stats: {e}")
return {
"players": [],
"servers": []
}
def get_cartographer_stats_history() -> Dict[str, List[List[int]]]:
"""Retrieve historical Cartographer stats from database."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
SELECT
CAST(strftime('%s', recorded_at) AS INTEGER) * 1000 as timestamp,
player_count,
server_count
FROM cartographer_stats
ORDER BY recorded_at ASC
""")
rows = cursor.fetchall()
conn.close()
players = [[row[0], row[1]] for row in rows]
servers = [[row[0], row[2]] for row in rows]
return {
"players": players,
"servers": servers
}
except Exception as e:
logger.error(f"Failed to retrieve Cartographer stats: {e}")
return {
"players": [],
"servers": []
}
def get_haloce_stats_history() -> Dict[str, List[List[int]]]:
"""Retrieve historical Halo CE stats from database."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
SELECT
CAST(strftime('%s', recorded_at) AS INTEGER) * 1000 as timestamp,
player_count,
server_count
FROM haloce_stats
ORDER BY recorded_at ASC
""")
rows = cursor.fetchall()
conn.close()
players = [[row[0], row[1]] for row in rows]
servers = [[row[0], row[2]] for row in rows]
return {
"players": players,
"servers": servers
}
except Exception as e:
logger.error(f"Failed to retrieve Halo CE stats: {e}")
return {
"players": [],
"servers": []
}
def get_halopc_stats_history() -> Dict[str, List[List[int]]]:
"""Retrieve historical Halo PC stats from database."""
try:
conn = sqlite3.connect(DB_PATH)
cursor = conn.cursor()
cursor.execute("""
SELECT
CAST(strftime('%s', recorded_at) AS INTEGER) * 1000 as timestamp,
player_count,
server_count
FROM halopc_stats
ORDER BY recorded_at ASC
""")
rows = cursor.fetchall()
conn.close()
players = [[row[0], row[1]] for row in rows]
servers = [[row[0], row[2]] for row in rows]
return {
"players": players,
"servers": servers
}
except Exception as e:
logger.error(f"Failed to retrieve Halo PC stats: {e}")
return {
"players": [],
"servers": []
}
async def update_eldewrito_cache():
"""Main logic: Pulls master lists, dedupes, queries servers, updates cache."""
global eldewrito_cache
# 1. Load Master Server URLs from local JSON
try:
async with await anyio.open_file(ELDEWRITO_MASTER_LIST, 'r') as f:
data = await f.read()
config = json.loads(data)
master_entries = config.get("masterServers", [])
# Extract only the 'list' attribute
master_urls = [m['list'] for m in master_entries if 'list' in m]
except Exception as e:
logger.error(f"Error reading {ELDEWRITO_MASTER_LIST}: {e}")
return
async with httpx.AsyncClient() as client:
# 2. Query all master servers concurrently
logger.info(f"Querying {len(master_urls)} master servers...")
master_tasks = [fetch_master_list(client, url) for url in master_urls]
results = await asyncio.gather(*master_tasks)
# 3. Deduplicate IP:Port combos
unique_servers: Set[str] = set()
for server_list in results:
for ip_port in server_list:
unique_servers.add(ip_port)
logger.info(f"Found {len(unique_servers)} unique game servers. Querying details...")
# 4. Query all game servers concurrently
# We limit concurrency slightly to avoid file descriptor limits if the list is huge,
# but for <100 servers, full concurrency is fine.
game_tasks = [fetch_game_server_info(client, srv) for srv in unique_servers]
game_results = await asyncio.gather(*game_tasks)
# 5. Build the final data structure
successful_servers = {}
total_players = 0
for res in game_results:
if res:
ip_port, data = res
successful_servers[ip_port] = data
# Safely add player count
if "numPlayers" in data:
try:
total_players += int(data["numPlayers"])
except ValueError:
pass
# 6. Format Final JSON
new_cache = {
"count": {
"players": total_players,
"servers": len(successful_servers)
},
"updatedAt": get_current_http_date(),
"servers": successful_servers
}
# Atomically update global cache
eldewrito_cache = new_cache
logger.info(f"ElDewrito Cache updated. Servers: {len(successful_servers)}, Players: {total_players}")
async def update_cartographer_cache():
"""Fetch Cartographer server list and update cache with summarized data."""
global cartographer_cache, cartographer_summarized_cache
try:
async with httpx.AsyncClient(verify=False) as client:
logger.info("Fetching Cartographer server list...")
response = await client.get(CARTOGRAPHER_LIST_URL, timeout=15.0)
response.raise_for_status()
data = response.json()
if isinstance(data, list):
raw_list = data
elif isinstance(data, dict):
raw_list = data.get('servers', data.get('list', data.get('data', [])))
else:
raw_list = []
# Try to map servers directly first
mapped = []
ids = []
for item in raw_list:
if isinstance(item, dict) and (item.get('pProperties') or item.get('server_desc') or item.get('name')):