From 3712526ff0e4a715ea9548f331edfc43d1502eb9 Mon Sep 17 00:00:00 2001 From: Cerapter Date: Mon, 13 Aug 2018 14:39:09 +0200 Subject: [PATCH] Added a HDID-based banning system. --- server/aoprotocol.py | 5 ++++- server/ban_manager.py | 31 +++++++++++++++++++++++++++++++ server/commands.py | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/server/aoprotocol.py b/server/aoprotocol.py index 9bddb0a..75ee824 100644 --- a/server/aoprotocol.py +++ b/server/aoprotocol.py @@ -65,7 +65,7 @@ class AOProtocol(asyncio.Protocol): buf = data - if not self.client.is_checked and self.server.ban_manager.is_banned(self.client.ipid): + if not self.client.is_checked and (self.server.ban_manager.is_banned(self.client.ipid) or self.server.ban_manager.is_hdid_banned(self.client.hdid)): self.client.transport.close() else: self.client.is_checked = True @@ -165,6 +165,9 @@ class AOProtocol(asyncio.Protocol): if not self.validate_net_cmd(args, self.ArgType.STR, needs_auth=False): return self.client.hdid = args[0] + if self.server.ban_manager.is_hdid_banned(self.client.hdid): + self.client.disconnect() + return if self.client.hdid not in self.client.server.hdid_list: self.client.server.hdid_list[self.client.hdid] = [] if self.client.ipid not in self.client.server.hdid_list[self.client.hdid]: diff --git a/server/ban_manager.py b/server/ban_manager.py index 24518b2..b4f97b7 100644 --- a/server/ban_manager.py +++ b/server/ban_manager.py @@ -25,6 +25,9 @@ class BanManager: self.bans = [] self.load_banlist() + self.hdid_bans = [] + self.load_hdid_banlist() + def load_banlist(self): try: with open('storage/banlist.json', 'r') as banlist_file: @@ -52,3 +55,31 @@ class BanManager: def is_banned(self, ipid): return (ipid in self.bans) + + def load_hdid_banlist(self): + try: + with open('storage/banlist_hdid.json', 'r') as banlist_file: + self.hdid_bans = json.load(banlist_file) + except FileNotFoundError: + return + + def write_hdid_banlist(self): + with open('storage/banlist_hdid.json', 'w') as banlist_file: + json.dump(self.hdid_bans, banlist_file) + + def add_hdid_ban(self, hdid): + if hdid not in self.hdid_bans: + self.hdid_bans.append(hdid) + else: + raise ServerError('This HDID is already banned.') + self.write_hdid_banlist() + + def remove_hdid_ban(self, hdid): + if hdid in self.hdid_bans: + self.hdid_bans.remove(hdid) + else: + raise ServerError('This HDID is not banned.') + self.write_hdid_banlist() + + def is_hdid_banned(self, hdid): + return (hdid in self.hdid_bans) \ No newline at end of file diff --git a/server/commands.py b/server/commands.py index eb42470..c1143d2 100644 --- a/server/commands.py +++ b/server/commands.py @@ -275,10 +275,39 @@ def ooc_cmd_unban(client, arg): try: client.server.ban_manager.remove_ban(int(arg.strip())) except: - raise ClientError('You must specify \'hdid\'') + raise ClientError('You must specify ipid') + logger.log_server('Unbanned {}.'.format(arg), client) + client.send_host_message('Unbanned {}'.format(arg)) + +def ooc_cmd_ban_hdid(client, arg): + if not client.is_mod: + raise ClientError('You must be authorized to do that.') + try: + hdid = int(arg.strip()) + except: + raise ClientError('You must specify hdid') + try: + client.server.ban_manager.add_hdid_ban(hdid) + except ServerError: + raise + if hdid != None: + targets = client.server.client_manager.get_targets(client, TargetType.HDID, hdid, False) + if targets: + for c in targets: + c.disconnect() + client.send_host_message('{} clients was kicked.'.format(len(targets))) + client.send_host_message('{} was banned.'.format(hdid)) + logger.log_server('Banned {}.'.format(hdid), client) + +def ooc_cmd_unban_hdid(client, arg): + if not client.is_mod: + raise ClientError('You must be authorized to do that.') + try: + client.server.ban_manager.remove_hdid_ban(int(arg.strip())) + except: + raise ClientError('You must specify hdid') logger.log_server('Unbanned {}.'.format(arg), client) client.send_host_message('Unbanned {}'.format(arg)) - def ooc_cmd_play(client, arg): if not client.is_mod: