Skip to content

Commit

Permalink
[memos] add CHECK to checker
Browse files Browse the repository at this point in the history
  • Loading branch information
keltecc committed Nov 4, 2023
1 parent 2df87f7 commit c604d68
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 15 deletions.
25 changes: 15 additions & 10 deletions checkers/memos/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ class Label:
size: int
text: str

def serialize(self) -> str:
return '|'.join(
def serialize(self) -> bytes:
return b'|'.join(
[
self.font,
self.color,
str(self.position[0]),
str(self.position[1]),
str(self.size),
self.text,
self.font.encode(),
self.color.encode(),
str(self.position[0]).encode(),
str(self.position[1]).encode(),
str(self.size).encode(),
self.text.encode('utf-8'),
],
)

Expand All @@ -62,6 +62,11 @@ def __init__(self, hostname: str, port: int) -> None:
self.session = requests.session()

return

def close(self) -> None:
self.session.close()

return

def index(self) -> None:
response = self.session.get(f'{self.url}/')
Expand Down Expand Up @@ -154,7 +159,7 @@ def change_background(self, draft_uuid: str, image_uuid: str) -> bool | None:
def draw_text(self, draft_uuid: str, labels: List[Label]) -> bool | None:
response = self.session.post(
f'{self.url}/drafts/text/{draft_uuid}',
data = '||'.join(label.serialize() for label in labels),
data = b'||'.join(label.serialize() for label in labels),
)

if response.status_code != 200:
Expand All @@ -165,7 +170,7 @@ def draw_text(self, draft_uuid: str, labels: List[Label]) -> bool | None:

return True

def draft_release(self, draft_uuid: str, password: str = None) -> str | None:
def release_draft(self, draft_uuid: str, password: str = None) -> str | None:
response = self.session.post(
f'{self.url}/drafts/release/{draft_uuid}',
data = password,
Expand Down
Binary file added checkers/memos/images/00.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkers/memos/images/01.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkers/memos/images/02.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkers/memos/images/03.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkers/memos/images/04.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkers/memos/images/05.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkers/memos/images/06.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkers/memos/images/07.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkers/memos/images/08.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added checkers/memos/images/09.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
170 changes: 165 additions & 5 deletions checkers/memos/memos.checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
checker = gornilo.NewChecker()


def hash_image(content: bytes) -> str:
return hashlib.sha256(content).hexdigest()


def generate_password() -> str:
return secrets.token_hex(8)

Expand Down Expand Up @@ -69,11 +73,82 @@ def generate_flag_labels(flag: str) -> api.Label:


def generate_meme_background() -> bytes:
return generate_flag_background()
backgrounds = [
'images/00.jpg',
'images/01.jpg',
'images/02.jpg',
'images/03.jpg',
'images/04.jpg',
'images/05.jpg',
'images/06.jpg',
'images/07.jpg',
'images/08.jpg',
'images/09.jpg',
]

background = random.choice(backgrounds)

img = Image.open(background)
width, height = img.size

coeff = 0.2 * random.random() - 0.1

width = width + int(width * coeff)
height = height + int(height * coeff)

buffer = io.BytesIO()

img = img.resize((width, height))
img.save(buffer, 'jpeg')

buffer.seek(0)

return buffer.read()


def generate_meme_labels() -> List[api.Label]:
return generate_flag_labels('kekosik')
labels_count = random.randint(1, 4)
labels = []

for _ in range(labels_count):
font = random.choice(api.FONTS)

if random.choice([True, False]):
x = random.randrange(0, 500)
y = random.randrange(0, 500)
else:
x = random.randrange(500, 1000)
y = random.randrange(500, 1000)

size = random.randrange(16, 256)

r = random.randrange(0, 256)
g = random.randrange(0, 100)
b = random.randrange(0, 100)

choice = random.randint(0, 1)

if choice == 0:
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !,.'
else:
alphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ !,.'

length = random.randrange(8, 32)
text = ''.join(
random.choice(alphabet) for _ in range(length)
)

label = api.Label(
font = font,
color = '#%02x%02x%02x' % (r, g, b),
position = (x, y),
size = size,
text = text,
)

labels.append(label)

return labels


def recognize_text(image: bytes) -> str:
Expand Down Expand Up @@ -132,7 +207,7 @@ def do_put(request: gornilo.PutRequest) -> gornilo.Verdict:
return gornilo.Verdict.MUMBLE('failed to draw text')

password = generate_password()
image_uuid = client.draft_release(draft_uuid, password)
image_uuid = client.release_draft(draft_uuid, password)

if image_uuid is None:
return gornilo.Verdict.MUMBLE('failed to release draft')
Expand All @@ -142,7 +217,7 @@ def do_put(request: gornilo.PutRequest) -> gornilo.Verdict:
if image is None:
return gornilo.Verdict.MUMBLE('failed to get image')

image_hash = hashlib.sha256(image).hexdigest()
image_hash = hash_image(image)

public_flag_id = image_uuid
private_flag_id = password + '|' + image_hash
Expand All @@ -165,7 +240,7 @@ def do_get(request: gornilo.GetRequest) -> gornilo.Verdict:
if image is None:
return gornilo.Verdict.CORRUPT('failed to get flag')

actual_image_hash = hashlib.sha256(image).hexdigest()
actual_image_hash = hash_image(image)

if expected_image_hash != actual_image_hash:
return gornilo.Verdict.MUMBLE('image was changed')
Expand All @@ -188,6 +263,91 @@ def do_check(request: gornilo.CheckRequest) -> gornilo.Verdict:
client = api.Api(request.hostname, port)
client.index()

background = generate_meme_background()
labels = generate_meme_labels()

image_uuid = client.upload_image(background)

if image_uuid is None:
return gornilo.Verdict.MUMBLE('failed to upload image')

draft_uuid = client.create_draft(image_uuid)

if draft_uuid is None:
return gornilo.Verdict.MUMBLE('failed to create draft')

result = client.draw_text(draft_uuid, labels)

if result is None:
return gornilo.Verdict.MUMBLE('failed to draw text')

draft_content = client.view_draft(draft_uuid)

if draft_content is None:
return gornilo.Verdict.MUMBLE('failed to view draft')

draft_content_hash = hash_image(draft_content)

client.close()

client = api.Api(request.hostname, port)
client.index()

regenerate = random.choice([True, False, False, False])

if regenerate:
background = generate_meme_background()
labels = generate_meme_labels()

image_uuid = client.upload_image(background)

if image_uuid is None:
return gornilo.Verdict.MUMBLE('failed to upload image')

result = client.change_background(draft_uuid, image_uuid)

if result is None:
return gornilo.Verdict.MUMBLE('failed to change background')

result = client.draw_text(draft_uuid, labels)

if result is None:
return gornilo.Verdict.MUMBLE('failed to draw text')

draft_content = client.view_draft(draft_uuid)

if draft_content is None:
return gornilo.Verdict.MUMBLE('failed to view draft')

draft_content_hash = hash_image(draft_content)

client.close()

client = api.Api(request.hostname, port)
client.index()

use_password = random.choice([True, False])

if use_password:
password = generate_password()
else:
password = None

image_uuid = client.release_draft(draft_uuid, password)

if image_uuid is None:
return gornilo.Verdict.MUMBLE('failed to release draft')

image_content = client.get_image(image_uuid, password)

if image_content is None:
return gornilo.Verdict.MUMBLE('failed to get image')

image_content_hash = hash_image(image_content)

if image_content_hash != draft_content_hash:
return gornilo.Verdict.MUMBLE('invalid image release')

return gornilo.Verdict.OK()


Expand Down

0 comments on commit c604d68

Please sign in to comment.