Skip to content

Commit

Permalink
fixing control scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ShyftXero committed Dec 26, 2023
1 parent 9b598dc commit 68f6c86
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.10.13
3.11.6
19 changes: 13 additions & 6 deletions ctrl_ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
79 changes: 42 additions & 37 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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():
...
Expand Down Expand Up @@ -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}")

Expand Down
1 change: 0 additions & 1 deletion requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 68f6c86

Please sign in to comment.