Skip to content

Commit

Permalink
Better faust & volga protocols, logs
Browse files Browse the repository at this point in the history
  • Loading branch information
pomo-mondreganto committed Sep 23, 2023
1 parent 09a21d3 commit f340ca2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
42 changes: 27 additions & 15 deletions server/app/protocols/faust.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import socket
import logging
import time

from models import FlagStatus, SubmitResult

Expand Down Expand Up @@ -52,23 +53,34 @@ def submit_flags(flags, config):
raise Exception('Checksystem does not greet us: {}'.format(greeting))

unknown_responses = set()
for item in flags:
sock.sendall(item.flag.encode() + b'\n')
sock.sendall(b'\n'.join(item.flag.encode() for item in flags) + b'\n')

while len(flags) > 0:
response = recvall(sock).decode().strip()
if response:
response = response.splitlines()[0]
response = response.replace('{} '.format(item.flag), '')
if not response:
break

for status, substrings in RESPONSES.items():
if any(s in response for s in substrings):
found_status = status
break
else:
found_status = FlagStatus.QUEUED
if response not in unknown_responses:
unknown_responses.add(response)
logger.warning('Unknown checksystem response (flag will be resent): %s', response)
response = response.splitlines()
for line in response:
flag = flags[0]
line = line.replace(f'{flag.flag} ', '')

for status, substrings in RESPONSES.items():
if any(s in line for s in substrings):
found_status = status
break
else:
found_status = FlagStatus.QUEUED
if line not in unknown_responses:
unknown_responses.add(line)
logger.warning('Unknown checksystem response (flag will be resent): %s', line)

if found_status == FlagStatus.QUEUED and time.time() - flag.time > 10:
found_status = FlagStatus.REJECTED
line = f'was response {line}, but inv flag too old'

yield SubmitResult(flag.flag, found_status, line)

yield SubmitResult(item.flag, found_status, response)
flags = flags[1:]

sock.close()
8 changes: 3 additions & 5 deletions server/app/protocols/volgactf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from itertools import chain

import dateutil.parser
import grequests
import pytz
import requests
import logging

from models import FlagStatus, SubmitResult

Expand Down Expand Up @@ -88,8 +88,7 @@ def parse_flag_info_response(self, flag: str, response: requests.Response):
return False, SubmitResult(flag, FlagStatus.QUEUED, f'error response from flag getinfo: {respcode}')

def info_flags(self, *flags: str):
pending = (grequests.get(f'{self.api_base}/info/{flag}') for flag in flags)
responses = grequests.map(pending)
responses = list(map(lambda flag: requests.get(f'{self.api_base}/info/{flag}'), flags))
return dict(zip(flags, map(self.parse_flag_info_response, flags, responses)))

@staticmethod
Expand All @@ -107,8 +106,7 @@ def parse_flag_submit_response(flag: str, response: requests.Response):

def submit_flags(self, *flags: str):
h = {'Content-Type': 'text/plain'}
pending = (grequests.post(f'{self.api_base}/submit', data=flag, headers=h) for flag in flags)
responses = grequests.map(pending)
responses = [requests.post(f'{self.api_base}/submit', data=flag, headers=h) for flag in flags]
return map(self.parse_flag_submit_response, flags, responses)


Expand Down
3 changes: 2 additions & 1 deletion server/app/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import importlib
import logging
import random
import traceback
from typing import List, TypeVar

from models import Flag, FlagStatus, SubmitResult
Expand Down Expand Up @@ -51,5 +52,5 @@ def submit_flags(flags: List[Flag], config) -> List[SubmitResult]:
return list(module.submit_flags(flags, config))
except Exception as e:
message = '{}: {}'.format(type(e).__name__, str(e))
logger.error('Exception in submit protocol: %s', message)
logger.error('Exception in submit protocol: %s\n%s', message, traceback.format_exc())
return [SubmitResult(item.flag, FlagStatus.QUEUED, message) for item in flags]

0 comments on commit f340ca2

Please sign in to comment.