Reschedule STS expiration on disconnect

This commit is contained in:
jesopo 2018-11-05 14:12:21 +00:00
parent 3fa16fe678
commit aaf0c8cf2a
2 changed files with 24 additions and 9 deletions

View file

@ -2,18 +2,24 @@ import time
from src import ModuleManager, utils from src import ModuleManager, utils
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _get_policy(self, server):
return server.get_setting("sts-policy", None)
def _set_policy(self, server, policy):
server.set_setting("sts-policy", policy)
def _remove_policy(self, server):
server.del_setting("sts-policy")
def _set_policy(self, server, port, duration, one_shot): def _set_policy(self, server, port, duration, one_shot):
expiration = None expiration = None
if duration: self._set_policy(server, {
expiration = time.time()+duration
server.set_setting("sts-policy", {
"port": port, "port": port,
"expiration": expiration, "from": time.time()
"duration": duration,
"one-shot": one_shot}) "one-shot": one_shot})
def _change_duration(self, server, info): def _change_duration(self, server, info):
duration = int(info["duration"]) duration = int(info["duration"])
if duration == 0: if duration == 0:
server.del_setting("sts-policy") self._remove_policy(server)
else: else:
port = event["server"].port port = event["server"].port
if "port" in info: if "port" in info:
@ -43,14 +49,22 @@ class Module(ModuleManager.BaseModule):
@utils.hook("new.server") @utils.hook("new.server")
def new_server(self, event): def new_server(self, event):
sts_policy = event["server"].get_setting("sts-policy") sts_policy = self._get_policy(event["server"])
if sts_policy: if sts_policy:
if sts_policy["one-shot"]: if sts_policy["one-shot"]:
event["server"].del_setting("sts-policy") self._remove_policy(event["server"])
if not event["server"].tls: if not event["server"].tls:
expiration = sts_policy["expiration"] expiration = sts_policy["from"]+sts_policy
if not expiration or time.time() <= expiration: if not sts_policy["duration"] or time.time() <= (
sts_policy["from"]+sts_policy["duration"]):
self.log.debug("Applying STS policy for '%s'", self.log.debug("Applying STS policy for '%s'",
[str(event["server"])]) [str(event["server"])])
event["server"].tls = True event["server"].tls = True
event["server"].port = sts_policy["port"] event["server"].port = sts_policy["port"]
@utils.hook("server.disconnect")
def on_disconnect(self, event):
sts_policy = self._get_policy(event["server"])
if sts_policy:
sts_policy["from"] = time.time()
self._set_policy(event["server"], sts_policy

View file

@ -199,6 +199,7 @@ class Bot(object):
server.send_ping() server.send_ping()
server.ping_sent = True server.ping_sent = True
if not server.connected: if not server.connected:
self._events.on("server.disconnect").call(server=server)
self.disconnect(server) self.disconnect(server)
reconnect_delay = self.config.get("reconnect-delay", 10) reconnect_delay = self.config.get("reconnect-delay", 10)