updated ban command

This commit is contained in:
Ayush Saini 2022-12-04 14:36:54 +05:30
parent 7f11eca20b
commit 79aaf7a48b
5 changed files with 80 additions and 9 deletions

View file

@ -219,7 +219,7 @@ def ban(arguments):
ac_id="" ac_id=""
for ros in ba.internal.get_game_roster(): for ros in ba.internal.get_game_roster():
if ros["client_id"]==cl_id: if ros["client_id"]==cl_id:
_thread.start_new_thread(pdata.ban_player,(ros['account_id'],)) pdata.ban_player(ros['account_id'])
ac_id=ros['account_id'] ac_id=ros['account_id']
if ac_id in serverdata.clients: if ac_id in serverdata.clients:

View file

@ -0,0 +1,11 @@
{
"ban": {
"ids": [
],
"ips": [
],
"deviceids": [
]
},
"muted-ids": []
}

View file

@ -16,7 +16,7 @@ from tools.file_handle import OpenJson
import _ba import _ba
import ba.internal import ba.internal
import json import json
import datetime
if TYPE_CHECKING: if TYPE_CHECKING:
pass pass
@ -34,6 +34,7 @@ class CacheData: # pylint: disable=too-few-public-methods
custom: dict = {} custom: dict = {}
profiles: dict = {} profiles: dict = {}
whitelist: list[str] = [] whitelist: list[str] = []
blacklist: dict = {}
def get_info(account_id: str) -> dict | None: def get_info(account_id: str) -> dict | None:
@ -65,8 +66,12 @@ def get_profiles() -> dict:
""" """
if CacheData.profiles=={}: if CacheData.profiles=={}:
try: try:
f=open(PLAYERS_DATA_PATH + "profiles.json","r") if os.stat(PLAYERS_DATA_PATH+"profiles.json").st_size > 1000:
profiles = json.load(f) shutil.copyfile(PLAYERS_DATA_PATH + "profiles.json",PLAYERS_DATA_PATH + "profiles.json"+str(datetime.datetime.now()))
profiles = {"pb-sdf":{}}
else:
f=open(PLAYERS_DATA_PATH + "profiles.json","r")
profiles = json.load(f)
CacheData.profiles=profiles CacheData.profiles=profiles
f.close() f.close()
except: except:
@ -78,6 +83,27 @@ def get_profiles() -> dict:
else: else:
return CacheData.profiles return CacheData.profiles
def get_blacklist() -> dict:
if CacheData.blacklist == {}:
try:
f = open(PLAYERS_DATA_PATH + "blacklist.json","r")
CacheData.blacklist = json.load(f)
except:
print('error opening blacklist json')
return {
"ban":{
"ids":[],
"ips":[],
"deviceids":[]
},
"muted-ids":[]
}
return CacheData.blacklist
def update_blacklist():
with open(PLAYERS_DATA_PATH + "blacklist.json","w") as f:
json.dump(CacheData.blacklist,f,indent=4)
def commit_profiles(data={}) -> None: def commit_profiles(data={}) -> None:
"""Commits the given profiles in the database. """Commits the given profiles in the database.
@ -141,6 +167,9 @@ def add_profile(
device_id = _ba.get_client_public_device_uuid(cid) device_id = _ba.get_client_public_device_uuid(cid)
if(device_id==None): if(device_id==None):
device_id = _ba.get_client_device_uuid(cid) device_id = _ba.get_client_device_uuid(cid)
if device_id in get_blacklist()["ban"]["deviceids"]:
serverdata.clients[account_id]["isBan"]=True
ba.internal.disconnect_client(cid)
serverdata.clients[account_id]["deviceUUID"] = device_id serverdata.clients[account_id]["deviceUUID"] = device_id
@ -213,8 +242,21 @@ def ban_player(account_id: str) -> None:
if account_id in profiles: if account_id in profiles:
profiles[account_id]["isBan"] = True profiles[account_id]["isBan"] = True
CacheData.profiles=profiles CacheData.profiles=profiles
_thread.start_new_thread(commit_profiles, (profiles,)) # _thread.start_new_thread(commit_profiles, (profiles,))
cid = -1
for ros in ba.internal.get_game_roster():
if ros['account_id'] == account_id:
cid = ros['client_id']
ip = _ba.get_client_ip(cid)
device_id = _ba.get_client_public_device_uuid(cid)
if(device_id==None):
device_id = _ba.get_client_device_uuid(cid)
CacheData.blacklist["ban"]["ips"].append(ip)
CacheData.blacklist["ban"]["ids"].append(account_id)
CacheData.blacklist["ban"]["deviceids"].append(device_id)
_thread.start_new_thread(update_blacklist,())
def mute(account_id: str) -> None: def mute(account_id: str) -> None:
"""Mutes the player. """Mutes the player.
@ -602,5 +644,5 @@ def dump_cache():
custom= copy.deepcopy(CacheData.custom) custom= copy.deepcopy(CacheData.custom)
with open(PLAYERS_DATA_PATH + "custom.json", "w") as f: with open(PLAYERS_DATA_PATH + "custom.json", "w") as f:
json.dump(custom, f, indent=4) json.dump(custom, f, indent=4)
time.sleep(20) time.sleep(60)
dump_cache() dump_cache()

View file

@ -9,7 +9,7 @@ from typing import TYPE_CHECKING
from dataclasses import dataclass, field from dataclasses import dataclass, field
import os import os
import datetime import datetime,shutil
import threading import threading
import setting import setting
import _ba import _ba
@ -92,7 +92,10 @@ class dumplogs(threading.Thread):
log_path = SERVER_DATA_PATH + "cmndusage.log" log_path = SERVER_DATA_PATH + "cmndusage.log"
else: else:
log_path = SERVER_DATA_PATH + "logs.log" log_path = SERVER_DATA_PATH + "logs.log"
if os.path.exists(log_path):
if os.stat(log_path).st_size > 1000:
shutil.copy(log_path, log_path+str(datetime.datetime.now()))
os.remove(log_path)
with open(log_path, mode="a+", encoding="utf-8") as file: with open(log_path, mode="a+", encoding="utf-8") as file:
for msg in self.msg: for msg in self.msg:
file.write(msg) file.write(msg)

View file

@ -18,8 +18,9 @@ import setting
import _thread import _thread
from tools import logger from tools import logger
from features import profanity from features import profanity
from playersData import pdata
blacklist = pdata.get_blacklist()
settings = setting.get_settings_data() settings = setting.get_settings_data()
@ -88,6 +89,7 @@ def on_player_join_server(pbid, player_data):
for ros in ba.internal.get_game_roster(): for ros in ba.internal.get_game_roster():
if ros["account_id"] == pbid: if ros["account_id"] == pbid:
clid = ros["client_id"] clid = ros["client_id"]
if pbid in serverdata.clients: if pbid in serverdata.clients:
rejoinCount = serverdata.clients[pbid]["rejoincount"] rejoinCount = serverdata.clients[pbid]["rejoincount"]
spamCount = serverdata.clients[pbid]["spamCount"] spamCount = serverdata.clients[pbid]["spamCount"]
@ -126,6 +128,8 @@ def on_player_join_server(pbid, player_data):
return return
else: else:
if pbid not in serverdata.clients: if pbid not in serverdata.clients:
if check_ban(clid,pbid):
return
serverdata.clients[pbid] = player_data serverdata.clients[pbid] = player_data
serverdata.clients[pbid]["warnCount"] = 0 serverdata.clients[pbid]["warnCount"] = 0
serverdata.clients[pbid]["lastWarned"] = time.time() serverdata.clients[pbid]["lastWarned"] = time.time()
@ -175,6 +179,17 @@ def on_player_join_server(pbid, player_data):
# pdata.add_profile(pbid,d_string,d_string) # pdata.add_profile(pbid,d_string,d_string)
def check_ban(clid,pbid):
ip = _ba.get_client_ip(clid)
device_id = _ba.get_client_public_device_uuid(clid)
if(device_id==None):
device_id = _ba.get_client_device_uuid(clid)
if (ip in blacklist["ban"]['ips'] or device_id in blacklist['ban']['deviceids'] or pbid in blacklist["ban"]["ids"]):
_ba.chatmessage('sad ,your account is flagged contact server owner for unban',clients=[clid])
ba.internal.disconnect_client(clid)
return True
return False
def verify_account(pb_id, p_data): def verify_account(pb_id, p_data):
d_string = "" d_string = ""