From 701fe5ebc70fb49bb32e81e6d6605f27ad09925b Mon Sep 17 00:00:00 2001 From: "Kali Kaneko (leap communications)" Date: Sat, 4 Feb 2017 17:01:25 +0100 Subject: [feature] parse status - simple status parsing - add separate firewall status - set status for abnormal termination --- src/leap/bitmask/vpn/_status.py | 77 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/leap/bitmask/vpn/_status.py (limited to 'src/leap/bitmask/vpn/_status.py') diff --git a/src/leap/bitmask/vpn/_status.py b/src/leap/bitmask/vpn/_status.py new file mode 100644 index 00000000..11d564fa --- /dev/null +++ b/src/leap/bitmask/vpn/_status.py @@ -0,0 +1,77 @@ +from itertools import chain, repeat +from twisted.logger import Logger + +logger = Logger() + + +# TODO implement a state machine in this class + + +class VPNStatus(object): + """ + A class containing different patterns in the openvpn output that + we can react upon. + """ + + _events = { + 'NETWORK_UNREACHABLE': ( + 'Network is unreachable (code=101)',), + 'PROCESS_RESTART_TLS': ( + "SIGTERM[soft,tls-error]",), + 'PROCESS_RESTART_PING': ( + "SIGTERM[soft,ping-restart]",), + 'INITIALIZATION_COMPLETED': ( + "Initialization Sequence Completed",), + } + + def __init__(self): + self.status = 'OFFLINE' + self._traffic_down = None + self._traffic_up = None + + def watch(self, line): + """ + Inspects line searching for the different patterns. If a match + is found, try to emit the corresponding signal. + + :param line: a line of openvpn output + :type line: str + """ + chained_iter = chain(*[ + zip(repeat(key, len(l)), l) + for key, l in self._events.iteritems()]) + for event, pattern in chained_iter: + if pattern in line: + break + else: + return + + status, errcode = self._status_codes(event) + self.set_status(status, errcode) + + + def set_status(self, status, errcode): + self.status = status + self.errcode = errcode + + def set_traffic_status(self, status): + down, up = status + self._traffic_up = up + self._traffic_down = down + + def get_traffic_status(self): + # XXX return Human readable too + return {'down': self._traffic_down, + 'up': self._traffic_up} + + def _status_codes(self, event): + # TODO check good transitions + # TODO check valid states + + _table = { + "network_unreachable": ('OFFLINE', 'network unreachable'), + "process_restart_tls": ('RESTARTING', 'restart tls'), + "process_restart_ping": ('CONNECTING', None), + "initialization_completed": ('ONLINE', None) + } + return _table.get(event.lower()) -- cgit v1.2.3