mirror of https://github.com/dirtbags/moth.git
Fix bug with out-of-order UDP replies
This commit is contained in:
parent
f5b6373164
commit
dc7d2174fb
3
flagd.py
3
flagd.py
|
@ -8,6 +8,7 @@ import hmac
|
||||||
import optparse
|
import optparse
|
||||||
import points
|
import points
|
||||||
import pointscli
|
import pointscli
|
||||||
|
import traceback
|
||||||
|
|
||||||
key = b'My First Shared Secret (tm)'
|
key = b'My First Shared Secret (tm)'
|
||||||
def hexdigest(data):
|
def hexdigest(data):
|
||||||
|
@ -39,7 +40,7 @@ class Submitter(threading.Thread):
|
||||||
try:
|
try:
|
||||||
pointscli.submit(self.sock, cat, team, 1)
|
pointscli.submit(self.sock, cat, team, 1)
|
||||||
except:
|
except:
|
||||||
print('Uh oh, exception submitting')
|
traceback.print_exc()
|
||||||
|
|
||||||
|
|
||||||
class CategoryHandler(socketserver.StreamRequestHandler):
|
class CategoryHandler(socketserver.StreamRequestHandler):
|
||||||
|
|
32
points.py
32
points.py
|
@ -84,10 +84,15 @@ def decode_response(b):
|
||||||
##
|
##
|
||||||
## Storage
|
## Storage
|
||||||
##
|
##
|
||||||
|
def incdict(dict, key, amt=1):
|
||||||
|
dict.setdefault(key, 0)
|
||||||
|
dict[key] += amt
|
||||||
|
|
||||||
class Storage:
|
class Storage:
|
||||||
def __init__(self, fn):
|
def __init__(self, fn):
|
||||||
self.points_by_team = {}
|
self.points_by_team = {}
|
||||||
self.points_by_cat = {}
|
self.points_by_cat = {}
|
||||||
|
self.points_by_cat_team = {}
|
||||||
self.log = []
|
self.log = []
|
||||||
self.events = set()
|
self.events = set()
|
||||||
self.f = io.BytesIO()
|
self.f = io.BytesIO()
|
||||||
|
@ -98,7 +103,7 @@ class Storage:
|
||||||
while True:
|
while True:
|
||||||
l = f.read(4)
|
l = f.read(4)
|
||||||
if not l:
|
if not l:
|
||||||
return
|
break
|
||||||
(l,) = struct.unpack('!I', l)
|
(l,) = struct.unpack('!I', l)
|
||||||
b = f.read(l)
|
b = f.read(l)
|
||||||
req = decode_request(b)
|
req = decode_request(b)
|
||||||
|
@ -118,20 +123,16 @@ class Storage:
|
||||||
|
|
||||||
when, cat, team, score = req
|
when, cat, team, score = req
|
||||||
|
|
||||||
if team not in self.points_by_team:
|
incdict(self.points_by_team, team, score)
|
||||||
self.points_by_team[team] = 0
|
incdict(self.points_by_cat, cat, score)
|
||||||
self.points_by_team[team] += score
|
incdict(self.points_by_cat_team, (cat, team), score)
|
||||||
if cat not in self.points_by_cat:
|
|
||||||
self.points_by_cat[cat] = 0
|
|
||||||
self.points_by_cat[cat] += score
|
|
||||||
self.log.append(req)
|
self.log.append(req)
|
||||||
self.events.add(req)
|
self.events.add(req)
|
||||||
|
|
||||||
b = encode_request(*req)
|
b = encode_request(*req)
|
||||||
l = struct.pack('!I', len(b))
|
lb = struct.pack('!I', len(b)) + b
|
||||||
self.f.write(l)
|
self.f.write(lb)
|
||||||
self.f.write(b)
|
self.f.flush()
|
||||||
print('added %d points to [%s] in [%s]' % (score, team, cat))
|
|
||||||
|
|
||||||
def categories(self):
|
def categories(self):
|
||||||
return sorted(self.points_by_cat)
|
return sorted(self.points_by_cat)
|
||||||
|
@ -140,10 +141,13 @@ class Storage:
|
||||||
return sorted(self.points_by_team)
|
return sorted(self.points_by_team)
|
||||||
|
|
||||||
def cat_points(self, cat):
|
def cat_points(self, cat):
|
||||||
return self.points_by_cat[cat]
|
return self.points_by_cat.get(cat, 0)
|
||||||
|
|
||||||
def team_points(self, team):
|
def team_points(self, team):
|
||||||
return self.points_by_team[team]
|
return self.points_by_team.get(team, 0)
|
||||||
|
|
||||||
|
def team_points_in_cat(self, cat, team):
|
||||||
|
return self.points_by_cat_team.get((cat, team), 0)
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -176,6 +180,8 @@ def test():
|
||||||
assert s.categories() == ['cat1', 'merf']
|
assert s.categories() == ['cat1', 'merf']
|
||||||
assert s.team_points('aardvarks') == 60
|
assert s.team_points('aardvarks') == 60
|
||||||
assert s.cat_points('cat1') == 30
|
assert s.cat_points('cat1') == 30
|
||||||
|
assert s.team_points_in_cat('cat1', 'aardvarks') == 10
|
||||||
|
assert s.team_points_in_cat('merf', 'zebras') == 0
|
||||||
|
|
||||||
del s
|
del s
|
||||||
s = Storage('test.dat')
|
s = Storage('test.dat')
|
||||||
|
|
20
pointscli.py
20
pointscli.py
|
@ -7,19 +7,29 @@ import socket
|
||||||
import time
|
import time
|
||||||
|
|
||||||
def submit(sock, cat, team, score):
|
def submit(sock, cat, team, score):
|
||||||
now = int(time.time())
|
begin = time.time()
|
||||||
req = points.encode_request(now, cat, team, score)
|
mark = int(begin)
|
||||||
|
req = points.encode_request(mark, cat, team, score)
|
||||||
while True:
|
while True:
|
||||||
sock.send(req)
|
sock.send(req)
|
||||||
r, w, x = select.select([sock], [], [], 0.2)
|
r, w, x = select.select([sock], [], [], begin + 2 - time.time())
|
||||||
if r:
|
if not r:
|
||||||
|
break
|
||||||
b = sock.recv(500)
|
b = sock.recv(500)
|
||||||
|
try:
|
||||||
when, txt = points.decode_response(b)
|
when, txt = points.decode_response(b)
|
||||||
assert when == now
|
except ValueError:
|
||||||
|
# Ignore invalid packets
|
||||||
|
continue
|
||||||
|
if when != mark:
|
||||||
|
# Ignore wrong timestamp
|
||||||
|
continue
|
||||||
if txt == 'OK':
|
if txt == 'OK':
|
||||||
return
|
return
|
||||||
|
else:
|
||||||
raise ValueError(txt)
|
raise ValueError(txt)
|
||||||
|
|
||||||
|
|
||||||
def makesock(host):
|
def makesock(host):
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
s.connect((host, 6667))
|
s.connect((host, 6667))
|
||||||
|
|
Loading…
Reference in New Issue