From 7c07f13117c25a2155d915e473e259eebaaa8fc9 Mon Sep 17 00:00:00 2001 From: Curt Hash Date: Thu, 8 Oct 2009 11:11:41 -0600 Subject: [PATCH] config and points submission stuff added to polster --- ctf/config.py | 4 +++ pollster/pollster.py | 80 ++++++++++++++++++++++++++++++++------------ 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/ctf/config.py b/ctf/config.py index 56ab872..65f9ae2 100755 --- a/ctf/config.py +++ b/ctf/config.py @@ -37,6 +37,10 @@ else: 'house_team': 'dirtbags', 'passwd': '/var/lib/ctf/passwd', 'team_colors': team_colors, + 'poll_interval': 60, + 'poll_timeout': 0.5, + 'heartbeat_dir': '/var/lib/pollster', + 'poll_dir': '/var/lib/www', }, 'puzzler': {'dir': '/usr/lib/www/puzzler', diff --git a/pollster/pollster.py b/pollster/pollster.py index e0bb62b..bde9fea 100755 --- a/pollster/pollster.py +++ b/pollster/pollster.py @@ -6,16 +6,37 @@ import sys import time import socket import traceback +import threading +import queue -# TODO: -# scoring interface -# config interface +from ctf import config +from ctf import pointscli DEBUG = False -POLL_INTERVAL = 60 -IP_DIR = 'iptest/' -REPORT_PATH = 'iptest/pollster.html' -SOCK_TIMEOUT = 0.5 +POLL_INTERVAL = config.get('poll_interval') +IP_DIR = config.get('heartbeat_dir') +REPORT_PATH = config.get('poll_dir') +SOCK_TIMEOUT = config.get('poll_timeout') + +class PointSubmitter(threading.Thread): + ''' Pulls point allocations from the queue and submits them. ''' + def __init__(self, point_queue): + threading.Thread.__init__(self) + self.point_queue = point_queue + self.sock = pointscli.makesock('localhost') + + def run(self): + # loop forever + while(True): + cat, team, score = self.point_queue.get() + if None in [cat, team, score]: + continue + + try: + pointscli.submit(cat, team, score, sock=self.sock) + except ValueError: + print('pollster: error submitting score (%s, %s, %d)' % (cat, team, score)) + traceback.print_exc() def socket_poll(ip, port, msg, prot, max_recv=1): ''' Connect via socket to the specified : using the @@ -137,6 +158,11 @@ POLLS = { ip_re = re.compile('(\d{1,3}\.){3}\d{1,3}') +# start point submitter thread +point_queue = queue.Queue() +t = PointSubmitter(point_queue) +t.start() + # loop forever while True: @@ -154,11 +180,17 @@ while True: except Exception as e: pass - out = open(REPORT_PATH, 'w') - out.write('\n\n') - out.write('Pollster Results\n') - out.write('\n') - out.write('\n

Polling Results

\n') + try: + out = open(REPORT_PATH, 'w') + except Exception as e: + out = None + pass + + if out is not None: + out.write('\n\n') + out.write('Pollster Results\n') + out.write('\n') + out.write('\n

Polling Results

\n') for ip in ips: @@ -178,9 +210,10 @@ while True: if DEBUG is True: print('ip: %s' % ip) - out.write('

%s

\n' % ip) - out.write('\n') - out.write('\n') + if out is not None: + out.write('

%s

\n' % ip) + out.write('
Service NameFlag Holder
\n') + out.write('\n') # perform polls for service,func in POLLS.items(): @@ -191,9 +224,13 @@ while True: if DEBUG is True: print('\t%s - %s' % (service, team)) - out.write('\n' % (service, team)) - - out.write('
Service NameFlag Holder
%s%s
\n') + if out is not None: + out.write('%s%s\n' % (service, team)) + + point_queue.put((service, team, 1)) + + if out is not None: + out.write('\n') if DEBUG is True: print('+-----------------------------------------+') @@ -202,9 +239,10 @@ while True: exec_time = int(t_end - t_start) sleep_time = POLL_INTERVAL - exec_time - out.write('

Next poll in: %ds

\n' % sleep_time) - out.write('\n\n') - out.close() + if out is not None: + out.write('

Next poll in: %ds

\n' % sleep_time) + out.write('\n\n') + out.close() # sleep until its time to poll again time.sleep(sleep_time)