def list_configurations(): """Prints a list of installed OpenVPN configurations.""" lis = properties.get_default_hosts_list(names_only=True) configs = dict() # Checks if configuration is installed for a given config_id for c in sorted(lis): config = {'host': re.sub('_', ' ', c), 'openvpn': '', 'apps': ''} for app_name in appstrategy.get_supported_apps(): app = appstrategy.get_app(app_name) if app.configure and app.find_config(c): logger.debug('Configuring %s for %s' % (c, app.strategy)) if app_name == 'openvpn': config['openvpn'] = '*' else: config['apps'] += '[' + app_name + ']' configs[c] = config print("List of OpenVPN configurations") for c in configs: if configs[c]['openvpn']: print(' {openvpn} {host} {apps}'.format(**configs[c])) else: print(' {host} {apps}'.format(**configs[c])) sys.exit()
def remove_configs(self): """Removes all configurations for a strategy Raises: OSError: Throws if config cannot be removed FileNotFoundError: Thrown if the configuration directory doesn't exists """ hosts = [re.sub(' ', '_', h) for h in properties.get_default_hosts_list(names_only=True)] conf_dir = self.app.conf_dir cdir = [] try: cdir = os.listdir(conf_dir) except FileNotFoundError: pass if hosts: regex = re.compile("(%s)" % "|".join(map(escape, hosts))) cdir = [d for d in cdir if regex.match(d)] try: for f in cdir: path = os.path.join(conf_dir, f) os.remove(path) except OSError: pass
def remove_configs(self): """Removes all configurations for a strategy Raises: OSError: Throws if config cannot be removed FileNotFoundError: Thrown if the configuration directory doesn't exists """ hosts = [ re.sub(' ', '_', h) for h in properties.get_default_hosts_list(names_only=True) ] conf_dir = self.app.conf_dir cdir = [] try: cdir = os.listdir(conf_dir) except FileNotFoundError: pass if hosts: regex = re.compile("(%s)" % "|".join(map(escape, hosts))) cdir = [d for d in cdir if regex.match(d)] try: for f in cdir: path = os.path.join(conf_dir, f) os.remove(path) except OSError: pass
def get_remote_address(config_id): """Finds the remote server host/ip address """ return [ h.fqdn for h in properties.get_default_hosts_list() if h.name == re.sub('_', ' ', config_id) ][0]
def set_hosts(): """Creates custom hosts list The host list is built from either commandline (by listing them after all other options) or in the config file in '/etc/private-internet-access-vpn' and combines the lists together. This then replaces the openvpn config list on which hosts to modify. """ configs = list() # Gets list of Hosts input from commandline if props.commandline.auto_configure: configs.extend(props.commandline.hosts) if configs: # Removes any duplicate names openvpn.configs = list(set([re.sub(' ', '_', h.strip()) for h in configs])) else: openvpn.configs = [re.sub(' ', '_', h) for h in properties.get_default_hosts_list(names_only=True)]