Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ctf): store solves walkthrough #167

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ [email protected]
OVISBOT_HTB_CREDS_PASS=myhtbpassword
OVISBOT_HTB_TEAM_ID=1000
OVISBOT_CTFTIME_TEAM_ID=999
OVISBOT_ADMIN_ROLE=moderator
OVISBOT_ADMIN_ROLE=moderator
OVISBOT_DB_URL=mongodb://localhost/ovisdb
OVISBOT_THIRD_PARTY_COGS_INSTALL_DIR=~/cogs
OVISBOT_GITHUB_TOKEN=<github_token>
OVISBOT_GITHUB_REPO_API_PATH=<github_solves_repo>
k4rt0fl3r marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ gitpython = "*"
pynacl = "*"
python-dateutil = "*"
click = "*"
discord = "*"
k4rt0fl3r marked this conversation as resolved.
Show resolved Hide resolved

[requires]
python_version = "3.7"
Expand Down
Empty file added general.md
Empty file.
2 changes: 2 additions & 0 deletions ovisbot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ class Config(AbstractConfig):
DB_URL = environ.get("OVISBOT_DB_URL", "mongodb://mongo/ovisdb")
COMMAND_PREFIX = environ.get("OVISBOT_COMMAND_PREFIX", "!")
DISCORD_BOT_TOKEN = environ.get("OVISBOT_DISCORD_TOKEN")
GITHUB_TOKEN = environ.get("OVISBOT_GITHUB_TOKEN")
GITHUB_SOLVES_REPO = environ.get("OVISBOT_GITHUB_REPO_API_PATH")

THIRD_PARTY_COGS_INSTALL_DIR = environ.get(
"OVISBOT_THIRD_PARTY_COGS_INSTALL_DIR", "/usr/local/share/ovisbot/cogs"
Expand Down
57 changes: 55 additions & 2 deletions ovisbot/extensions/ctf/ctf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import base64
import copy
import os
import discord
import datetime
import logging
Expand All @@ -8,6 +10,7 @@
import dateutil.parser
import pytz
import random
import json

import ovisbot.locale as i18n

Expand Down Expand Up @@ -124,13 +127,21 @@ async def archive(self, ctx, ctf_name):
ctf = CTF.objects.get({"name": ctf_name})
category = discord.utils.get(ctx.guild.categories, name=ctf_name)
ctfrole = discord.utils.get(ctx.guild.roles, name="Team-" + ctf_name)

if ctfrole is not None:
await ctfrole.delete()

for c in category.channels:

k4rt0fl3r marked this conversation as resolved.
Show resolved Hide resolved
challenge = next((ch for ch in ctf.challenges if ch.name == c.name), None)
if not not challenge and challenge.solved_at:
k4rt0fl3r marked this conversation as resolved.
Show resolved Hide resolved
await harvestPins(c)
self.pushToGitHub(ctf_name,c.name)

await c.delete()

await category.delete()

ctf.name = "__ARCHIVED__" + ctf.name # bug fix (==)
ctf.save()

Expand Down Expand Up @@ -954,7 +965,49 @@ async def check_reminders(self):
)
except CTF.DoesNotExist:
continue


# TODO Make async somehow to avoid request stall
def pushToGitHub(self,ctf_name,challenge_name):

k4rt0fl3r marked this conversation as resolved.
Show resolved Hide resolved
pinsDir = os.path.abspath(os.getcwd()) + '/pins/'
k4rt0fl3r marked this conversation as resolved.
Show resolved Hide resolved
for filename in os.listdir(pinsDir):
url = self.bot.config_cls.GITHUB_SOLVES_REPO + ctf_name + "/" + challenge_name + "/" + filename
file = base64.b64encode(open(pinsDir + filename, "rb").read())

token = self.bot.config_cls.GITHUB_TOKEN
message = json.dumps({
"message":"store pins of challenge " + challenge_name + " from " + ctf_name + " ctf.",
"branch": "main",
"content": file.decode("utf-8")
})

resp=requests.put(url, data = message, headers = {"Accept": "application/vnd.github.v3+json", "Authorization": "token "+token})
os.remove(pinsDir + filename)

# Gathers all pinned messages and files in the /pins folder
async def harvestPins(channel):
pinsDir = os.path.abspath(os.getcwd()) + '/pins'
completeName = os.path.join(pinsDir, channel.name + ".md")
f = open(completeName,"w")

pinnedMessages = await channel.pins()

# reversed as to write first pinned message, first.
# otherwise, the last pinned message is the first to be written.
for pinned in reversed(pinnedMessages):

k4rt0fl3r marked this conversation as resolved.
Show resolved Hide resolved
f.write(pinned.content + os.linesep)

attachs = pinned.attachments
if(len(attachs) != 0):
for a in attachs:
await a.save(os.path.join(pinsDir, a.filename))
file = "[" + a.filename + "]" + "(./" + a.filename + ")"
f.write(os.linesep + file + os.linesep)

f.write(os.linesep + "---" + os.linesep)

f.close()

def setup(bot):
bot.add_cog(Ctf(bot))
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta:__legacy__"