Commit e6141ecf authored by Jonas Jelten's avatar Jonas Jelten

hardened by accepting only from the newest connection

parent 38863896
......@@ -10,24 +10,57 @@ class UrlReceiver(asyncio.Protocol):
protocol for receiving the media url
"""
MAX_WAIT = 10
LINEBUF_MAX = 4096
def __init__(self, screen):
self.buf = bytearray()
self.screen = screen
self.transport = None
self.peername = None
self.timer = None
def connection_made(self, transport):
self.seq_nr = self.screen.get_play_id()
self.peername = transport.get_extra_info('peername')
self.transport = transport
loop = asyncio.get_event_loop()
self.timer = loop.call_later(self.MAX_WAIT, self.timeout)
def data_received(self, data):
if len(self.buf) + len(data) > self.LINEBUF_MAX:
self.transport.write(b"too much!\n")
self.close()
return
if not self.screen.is_newest_id(self.seq_nr):
self.transport.write(b"too slow!\n")
self.close()
return
self.buf += data
# a url is \n-terminated
npos = self.buf.find(b"\n")
if npos != -1:
data = self.buf[:npos]
self.screen.display(self.peername, data.decode())
self.timer.cancel()
self.transport.write(b"yay!\n")
self.transport.close()
data = self.buf[:npos]
self.screen.display(self.seq_nr, self.peername, data.decode())
def timeout(self):
""" no answer for some time """
self.transport.write(b"too late!\n")
def close(self):
""" cancel the timer and close the connection """
if self.timer:
self.timer.cancel()
self.transport.close()
def connection_lost(self, exc):
pass
......@@ -28,12 +28,30 @@ class VideoScreen:
self.mpd = MPD(args.mpdhost, args.mpdport)
self.mpd_was_playing = False
def display(self, sender, data):
self.connection_count = 0
self.last_played_seq_id = -1
def get_play_id(self):
""" return the connection sequence number """
ret = self.connection_count
self.connection_count += 1
return ret
def is_newest_id(self, seq_nr):
""" return if the given sequence number """
return seq_nr > self.last_played_seq_id
def display(self, seq_nr, sender, data):
"""
show a new video and kill the old one
TODO: player selection e.g. for jpgs
"""
if not self.is_newest_id(seq_nr):
raise Exception("can't play too old id")
self.last_played_seq_id = seq_nr
if self.mpv is not None:
self.mpv.kill()
self.mpv.join()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment