Пример #1
0
class VpnController(object):
    """ Contains methods to check the vpn status, start and stop the vpn. """

    vpn_service = System.get_vpn_service()
    start_cmd = "systemctl start " + vpn_service + " > /dev/null"
    stop_cmd = "systemctl stop " + vpn_service + " > /dev/null"
    check_cmd = "systemctl is-active " + vpn_service + " > /dev/null"

    def __init__(self):
        self.vpn_connected = False
        t_vpn_connected = Thread(target=self._vpn_connected)
        t_vpn_connected.daemon = True
        t_vpn_connected.start()

    @staticmethod
    def start_vpn():
        """ Start openvpn """
        logger.info('Starting VPN')
        return subprocess.call(VpnController.start_cmd, shell=True) == 0

    @staticmethod
    def stop_vpn():
        """ Stop openvpn """
        logger.info('Stopping VPN')
        return subprocess.call(VpnController.stop_cmd, shell=True) == 0

    @staticmethod
    def check_vpn():
        """ Check if openvpn is running """
        return subprocess.call(VpnController.check_cmd, shell=True) == 0

    def _vpn_connected(self):
        """ Checks if the VPN tunnel is connected """
        while True:
            try:
                routes = subprocess.check_output(
                    'ip r | grep tun | grep via || true', shell=True).strip()
                # example output:
                # 10.0.0.0/24 via 10.37.0.5 dev tun0\n
                # 10.37.0.1 via 10.37.0.5 dev tun0
                result = False
                if routes:
                    vpn_servers = [
                        route.split(' ')[0] for route in routes.split('\n')
                        if '/' not in route
                    ]
                    for vpn_server in vpn_servers:
                        if VPNService.ping(vpn_server, verbose=False):
                            result = True
                            break
                self.vpn_connected = result
            except Exception as ex:
                logger.info(
                    'Exception occured during vpn connectivity test: {0}'.
                    format(ex))
                self.vpn_connected = False
            time.sleep(5)
Пример #2
0
class VpnController(object):
    """ Contains methods to check the vpn status, start and stop the vpn. """

    vpn_service = System.get_vpn_service()
    start_cmd = "systemctl start " + vpn_service + " > /dev/null"
    stop_cmd = "systemctl stop " + vpn_service + " > /dev/null"
    check_cmd = "systemctl is-active " + vpn_service + " > /dev/null"

    def __init__(self):
        pass

    @staticmethod
    def start_vpn():
        """ Start openvpn """
        return subprocess.call(VpnController.start_cmd, shell=True) == 0

    @staticmethod
    def stop_vpn():
        """ Stop openvpn """
        return subprocess.call(VpnController.stop_cmd, shell=True) == 0

    @staticmethod
    def check_vpn():
        """ Check if openvpn is running """
        return subprocess.call(VpnController.check_cmd, shell=True) == 0

    @staticmethod
    def vpn_connected():
        """ Checks if the VPN tunnel is connected """
        try:
            route = subprocess.check_output('ip r | grep tun | grep via || true', shell=True).strip()
            if not route:
                return False
            vpn_server = route.split(' ')[0]
            return ping(vpn_server)
        except Exception as ex:
            LOGGER.log('Exception occured during vpn connectivity test: {0}'.format(ex))
            return False
Пример #3
0
class VpnController(object):
    """ Contains methods to check the vpn status, start and stop the vpn. """
    if System.get_operating_system().get('ID') == System.OS.BUILDROOT:
        vpn_binary = 'openvpn'
        config_location = '/etc/openvpn/client/'
        start_cmd = 'cd {} ; {} --suppress-timestamps --nobind --config vpn.conf > /dev/null'.format(
            config_location, vpn_binary)
        stop_cmd = 'killall {} > /dev/null'.format(vpn_binary)
        check_cmd = 'ps -a | grep {} | grep -v "grep" > /dev/null'.format(
            vpn_binary)
    else:
        vpn_service = System.get_vpn_service()
        start_cmd = 'systemctl start {0} > /dev/null'.format(vpn_service)
        stop_cmd = 'systemctl stop {0} > /dev/null'.format(vpn_service)
        check_cmd = 'systemctl is-active {0} > /dev/null'.format(vpn_service)

    def __init__(self):
        self.vpn_connected = False
        self._vpn_tester = DaemonThread(name='vpnctl',
                                        target=self._vpn_connected,
                                        interval=5)

    def start(self):
        self._vpn_tester.start()

    @staticmethod
    def start_vpn():
        """ Start openvpn """
        logger.info('Starting VPN')
        return subprocess.call(VpnController.start_cmd, shell=True) == 0

    @staticmethod
    def stop_vpn():
        """ Stop openvpn """
        logger.info('Stopping VPN')
        return subprocess.call(VpnController.stop_cmd, shell=True) == 0

    @staticmethod
    def check_vpn():
        """ Check if openvpn is running """
        return subprocess.call(VpnController.check_cmd, shell=True) == 0

    def _vpn_connected(self):
        """ Checks if the VPN tunnel is connected """
        try:
            routes = subprocess.check_output(
                'ip r | grep tun | grep via || true', shell=True).strip()
            # example output:
            # 10.0.0.0/24 via 10.37.0.5 dev tun0\n
            # 10.37.0.1 via 10.37.0.5 dev tun0
            result = False
            if routes:
                if not isinstance(
                        routes, str):  # to ensure python 2 and 3 compatibility
                    routes = routes.decode()

                vpn_servers = [
                    route.split(' ')[0] for route in routes.split('\n')
                    if '/' not in route
                ]
                for vpn_server in vpn_servers:
                    if TaskExecutor._ping(vpn_server, verbose=False):
                        result = True
                        break
            self.vpn_connected = result
        except Exception as ex:
            logger.info(
                'Exception occured during vpn connectivity test: {0}'.format(
                    ex))
            self.vpn_connected = False