From 22b62ae5913813425ac3aa38446e6b78e180a631 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Thu, 20 Aug 2009 15:18:15 -0600 Subject: [PATCH] Flag daemon, for categories that have a flag --- flagd.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 flagd.py diff --git a/flagd.py b/flagd.py new file mode 100755 index 0000000..27e198d --- /dev/null +++ b/flagd.py @@ -0,0 +1,92 @@ +#! /usr/bin/env python3 + +import socketserver +import threading +import queue +import time +import hmac +import optparse +import points +import pointscli + +key = b'My First Shared Secret (tm)' +def hexdigest(data): + return hmac.new(key, data).hexdigest() + +house = 'dirtbags' # House team name +flags = {} +toscore = queue.Queue(50) + +class Submitter(threading.Thread): + def run(self): + self.sock = pointscli.makesock('localhost') + while True: + try: + delay = 60 - (time.time() % 60) + cat, team = toscore.get(True, delay) + self.submit(cat, team) + except queue.Empty: + self.once() + + def once(self): + global flags + global toscore + + for cat, team in flags.items(): + self.submit(cat, team) + + def submit(self, cat, team): + try: + pointscli.submit(self.sock, cat, team, 1) + except: + print('Uh oh, exception submitting') + + +class CategoryHandler(socketserver.StreamRequestHandler): + def handle(self): + global flags + + try: + catpass = self.rfile.readline().strip() + cat, passwd = catpass.split(b':::') + passwd = passwd.decode('utf-8') + print(cat, passwd, hexdigest(cat)) + if passwd != hexdigest(cat): + self.wfile.write(b'ERROR :Closing Link: Invalid password\n') + return + cat = cat.decode('utf-8') + except ValueError as foo: + self.wfile.write(b'ERROR :Closing Link: Invalid command\n') + print(foo) + return + + flags[cat] = house + while True: + team = self.rfile.readline().strip().decode('utf-8') + if not team: + break + flags[cat] = team + toscore.put((cat, team)) # score a point immediately + flags[cat] = house + +class MyServer(socketserver.ThreadingTCPServer): + allow_reuse_address = True + + +def main(): + p = optparse.OptionParser() + p.add_option('-p', '--genpass', dest='cat', default=None, + help='Generate a password for the given category') + opts, args = p.parse_args() + if opts.cat: + print('%s:::%s' % (opts.cat, hexdigest(opts.cat.encode('utf-8')))) + return + + submitter = Submitter() + submitter.start() + server = MyServer(('', 6668), CategoryHandler) + server.serve_forever() + + +if __name__ == '__main__': + main()