diff --git a/.python-version b/.python-version index 9919bf8..375f5ca 100644 --- a/.python-version +++ b/.python-version @@ -1 +1 @@ -3.10.13 +3.11.6 diff --git a/ctrl_ctf.py b/ctrl_ctf.py index afc29fb..c1a0aa2 100755 --- a/ctrl_ctf.py +++ b/ctrl_ctf.py @@ -7,9 +7,13 @@ from settings import * import toml + if is_initialized() == False: init_config() +from loguru import logger + +logger.add(SETTINGS.get("_logfile")) from terminaltables import AsciiTable as mdTable @@ -302,7 +306,10 @@ def INIT(self): cmd = """kill -9 `ps -ef |grep byoctf_discord.py |grep -v grep | awk {'print $2'}`""" print(f"killing bot via {cmd}") + import database as db + with db_session: + # teams; These passwords are sha256 of teamname. # botteam = db.Team(name='botteam', password='c588d8717b7c6a898889864d588dbe73b123e751814e8fb7e02ca9a08727fd2f') bestteam = db.Team( @@ -356,9 +363,9 @@ def INIT(self): users = [shyft, fie, r3d, malloc, aykay, jsm, moonkaptain, fractumseraph] for u in users: db.rotate_player_keys(u) - db.commit() + db.db.commit() shyft.api_key = '644fccfc-2c12-4fa1-8e05-2aa40c4ef756' # to make testing and development easier. - db.commit() + db.db.commit() os.system(cmd) # print('Deleting logs') @@ -390,17 +397,16 @@ def DEV_RESET(self): print(f"killing bot via {cmd}") os.system(cmd) - - print("Deleting logs") os.remove(SETTINGS["_logfile"]) print("Deleting and recreating database") if SETTINGS["_db_type"] == "sqlite": os.remove(SETTINGS["_db_database"]) - from database import db + self.reinit_config() + # print("Populating test data") # os.system("python populateTestData.py") @@ -672,13 +678,14 @@ def bulk_add_chall(self, start_path:str='.'): chall_files = self._find_chall_files(start_path) for chall in chall_files: self.add_chall(chall, byoc=False, bypass_cost=True) + print('-'*30) def add_chall(self, toml_file, byoc=False, bypass_cost=True): """load a challenge via the BYOC mechanism. If byoc is True, it will be marked as a byoc challenge and points will be awarded to the author of the challenge and the solver. Add a challenge on behalf of a user.""" - print(f'trying {toml_file}') + logger.debug(f'trying {toml_file}') try: raw = open(toml_file).read() chall_obj = toml.loads(raw) diff --git a/database.py b/database.py index dad4dc3..6803b55 100644 --- a/database.py +++ b/database.py @@ -21,6 +21,7 @@ from datetime import datetime from pony.orm import * +from pony.orm.core import BindingError # https://editor.ponyorm.com/user/shyft/byoctf/designer # this is probably a bit of an overcomplicated db architecture. @@ -153,42 +154,46 @@ class Rating(db.Entity): def generateMapping(): # https://docs.ponyorm.org/database.html - if SETTINGS["_db_type"] == "sqlite": - db.bind(provider="sqlite", filename=SETTINGS["_db_database"], create_db=True) - - elif SETTINGS["_db_type"] == "postgres": - print("postgres is tested less than sqlite... good luck...") - db.bind( - provider="postgres", - user=SETTINGS["_db_user"], - password=SETTINGS["_db_pass"], - host=SETTINGS["_db_host"], - database=SETTINGS["_db_database"], - port=SETTINGS["_db_port"] - ) - - elif SETTINGS["_db_type"] == "mysql": - print("mysql is untested... good luck...") - db.bind( - provider="mysql", - user=SETTINGS["_db_user"], - password=SETTINGS["_db_pass"], - host=SETTINGS["_db_host"], - database=SETTINGS["_db_database"], - ) - # elif SETTINGS['_db_type'] == 'cockroach': - # print('using cockroachdb') - # db.bind( - # provider='cockroach', - # user=SETTINGS["_db_user"], - # password=SETTINGS["_db_pass"], - # host=SETTINGS["_db_host"], - # database=SETTINGS["_db_database"], - # sslmode='require' - # ) - - # db.create_tables() - db.generate_mapping(create_tables=True) + try: + if SETTINGS["_db_type"] == "sqlite": + db.bind(provider="sqlite", filename=SETTINGS["_db_database"], create_db=True) + + elif SETTINGS["_db_type"] == "postgres": + print("postgres is tested less than sqlite... good luck...") + db.bind( + provider="postgres", + user=SETTINGS["_db_user"], + password=SETTINGS["_db_pass"], + host=SETTINGS["_db_host"], + database=SETTINGS["_db_database"], + port=SETTINGS["_db_port"] + ) + + elif SETTINGS["_db_type"] == "mysql": + print("mysql is untested... good luck...") + db.bind( + provider="mysql", + user=SETTINGS["_db_user"], + password=SETTINGS["_db_pass"], + host=SETTINGS["_db_host"], + database=SETTINGS["_db_database"], + ) + # elif SETTINGS['_db_type'] == 'cockroach': + # print('using cockroachdb') + # db.bind( + # provider='cockroach', + # user=SETTINGS["_db_user"], + # password=SETTINGS["_db_pass"], + # host=SETTINGS["_db_host"], + # database=SETTINGS["_db_database"], + # sslmode='require' + # ) + + # db.create_tables() + + db.generate_mapping(create_tables=True) + except BindingError as e: + print(e) def set_custom_methods(): ... @@ -1185,7 +1190,7 @@ def challengeComplete(chall: Challenge, user: User): @db_session() def validateChallenge(challenge_object, bypass_length=False, bypass_cost=False): if SETTINGS["_debug"]: - logger.debug(f"validating the challenge from {challenge_object.get('author')}") + logger.debug(f"validating the challenge '{challenge_object.get('challenge_title','')}' from {challenge_object.get('author')}") if SETTINGS["_debug_level"] >= 2: logger.debug(f"Got challenge object: { challenge_object}") diff --git a/requirements-dev.lock b/requirements-dev.lock index 6278eb4..ce6fa62 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -22,7 +22,6 @@ deprecated==1.2.14 discord==2.3.2 discord-py==2.3.2 diskcache==5.6.3 -exceptiongroup==1.1.3 executing==2.0.1 faker==20.0.3 fire==0.5.0 diff --git a/requirements.lock b/requirements.lock index 6278eb4..ce6fa62 100644 --- a/requirements.lock +++ b/requirements.lock @@ -22,7 +22,6 @@ deprecated==1.2.14 discord==2.3.2 discord-py==2.3.2 diskcache==5.6.3 -exceptiongroup==1.1.3 executing==2.0.1 faker==20.0.3 fire==0.5.0