From 9a11f355ea9a3621d9924aa1e75878105dd11a89 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Mon, 2 Mar 2015 21:22:56 -0800 Subject: [PATCH] relay: expire any rendezvous channel after one hour --- src/wormhole/servers/relay.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/wormhole/servers/relay.py b/src/wormhole/servers/relay.py index 5952bab..8357a1d 100644 --- a/src/wormhole/servers/relay.py +++ b/src/wormhole/servers/relay.py @@ -1,14 +1,18 @@ from __future__ import print_function -import re, json +import re, json, time from collections import defaultdict from twisted.python import log from twisted.internet import protocol -from twisted.application import strports, service +from twisted.application import strports, service, internet from twisted.web import server, static, resource, http SECONDS = 1.0 +MINUTE = 60*SECONDS +HOUR = 60*MINUTE MB = 1000*1000 +CHANNEL_EXPIRATION_TIME = 1*HOUR + class Channel(resource.Resource): isLeaf = True @@ -22,6 +26,7 @@ class Channel(resource.Resource): resource.Resource.__init__(self) self.channel_id = channel_id self.relay = relay + self.expire_at = time.time() + CHANNEL_EXPIRATION_TIME self.sides = set() self.messages = {"pake": defaultdict(list), # side -> [strings] "data": defaultdict(list), # side -> [strings] @@ -83,6 +88,14 @@ class Relay(resource.Resource): self.channels = {} self.next_channel = 1 + def prune_old_channels(self): + now = time.time() + for channel_id in list(self.channels): + c = self.channels[channel_id] + if c.expire_at < now: + log.msg("expiring %d" % channel_id) + self.free_child(channel_id) + def getChild(self, path, request): if path == "allocate": # be more clever later. Rotate through 1-99 unless they're all @@ -243,8 +256,10 @@ class RelayServer(service.MultiService): site = server.Site(self.root) self.relayport_service = strports.service(relayport, site) self.relayport_service.setServiceParent(self) - self.relay = Relay() # for tests + self.relay = Relay() # accessible from tests self.root.putChild("relay", self.relay) + t = internet.TimerService(5*MINUTE, self.relay.prune_old_channels) + t.setServiceParent(self) self.transit = Transit() self.transit.setServiceParent(self) # for the timer self.transport_service = strports.service(transitport, self.transit)