2018-11-05 12:39:02 +00:00
|
|
|
import time
|
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-11-05 14:12:21 +00:00
|
|
|
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")
|
|
|
|
|
2018-11-05 13:03:45 +00:00
|
|
|
def _set_policy(self, server, port, duration, one_shot):
|
2018-11-05 12:39:02 +00:00
|
|
|
expiration = None
|
2018-11-05 14:12:21 +00:00
|
|
|
self._set_policy(server, {
|
2018-11-05 12:39:02 +00:00
|
|
|
"port": port,
|
2018-11-05 14:12:59 +00:00
|
|
|
"from": time.time(),
|
2018-11-05 14:12:21 +00:00
|
|
|
"duration": duration,
|
2018-11-05 13:03:45 +00:00
|
|
|
"one-shot": one_shot})
|
2018-11-05 12:39:02 +00:00
|
|
|
def _change_duration(self, server, info):
|
2018-11-05 13:26:11 +00:00
|
|
|
duration = int(info["duration"])
|
|
|
|
if duration == 0:
|
2018-11-05 14:12:21 +00:00
|
|
|
self._remove_policy(server)
|
2018-11-05 13:26:11 +00:00
|
|
|
else:
|
|
|
|
port = event["server"].port
|
|
|
|
if "port" in info:
|
|
|
|
port = int(info["port"])
|
|
|
|
self._set_policy(server, port, duration, False)
|
2018-11-05 12:39:02 +00:00
|
|
|
|
|
|
|
@utils.hook("received.cap.ls")
|
|
|
|
def on_cap_ls(self, event):
|
|
|
|
has_sts = "sts" in event["capabilities"]
|
|
|
|
if "sts" in event["capabilities"]:
|
|
|
|
info = utils.parse.keyvalue(event["capabilities"]["sts"],
|
|
|
|
delimiter=",")
|
|
|
|
if not event["server"].tls:
|
|
|
|
self._set_policy(event["server"], int(info["port"]),
|
2018-11-05 13:03:45 +00:00
|
|
|
None, True)
|
2018-11-05 12:39:02 +00:00
|
|
|
event["server"].disconnect()
|
|
|
|
else:
|
|
|
|
self._change_duration(event["server"], info)
|
|
|
|
|
|
|
|
@utils.hook("received.cap.new")
|
|
|
|
def on_cap_new(self, event):
|
|
|
|
if "sts" in event["capabilities"] and event["server"].tls:
|
2018-11-05 13:25:51 +00:00
|
|
|
info = utils.parse.keyvalue(event["capabilities"]["sts"],
|
|
|
|
delimiter=",")
|
2018-11-05 12:39:02 +00:00
|
|
|
if event["server"].tls:
|
|
|
|
self._change_duration(event["server"], info)
|
|
|
|
|
|
|
|
@utils.hook("new.server")
|
|
|
|
def new_server(self, event):
|
2018-11-05 14:12:21 +00:00
|
|
|
sts_policy = self._get_policy(event["server"])
|
2018-11-05 13:14:00 +00:00
|
|
|
if sts_policy:
|
|
|
|
if sts_policy["one-shot"]:
|
2018-11-05 14:12:21 +00:00
|
|
|
self._remove_policy(event["server"])
|
2018-11-05 13:14:00 +00:00
|
|
|
if not event["server"].tls:
|
2018-11-05 14:12:21 +00:00
|
|
|
expiration = sts_policy["from"]+sts_policy
|
|
|
|
if not sts_policy["duration"] or time.time() <= (
|
|
|
|
sts_policy["from"]+sts_policy["duration"]):
|
2018-11-05 14:16:14 +00:00
|
|
|
self.log.trace("Applying STS policy for '%s'",
|
2018-11-05 13:14:00 +00:00
|
|
|
[str(event["server"])])
|
|
|
|
event["server"].tls = True
|
|
|
|
event["server"].port = sts_policy["port"]
|
2018-11-05 14:12:21 +00:00
|
|
|
|
|
|
|
@utils.hook("server.disconnect")
|
|
|
|
def on_disconnect(self, event):
|
|
|
|
sts_policy = self._get_policy(event["server"])
|
|
|
|
if sts_policy:
|
|
|
|
sts_policy["from"] = time.time()
|
2018-11-05 14:13:27 +00:00
|
|
|
self._set_policy(event["server"], sts_policy)
|