def get_value_from_sample(key, sample_path=None): """Get suggestion from sample config.""" fallback_path = None fallback_defaults = None if sample_path is None: container = _helper.sanitize_path( os.path.dirname(os.path.realpath(__file__))) sample_path = container + '/sovpn.json' fallback_path = sample_path override = container + 'local/' if not os.path.isdir(override): override = None if override and os.path.isfile(override + '/sovpn.json'): sample_path = override + '/sovpn.json' sample = _helper.read_file_as_value(sample_path) defaults = json.loads(sample) if fallback_path: fallback = _helper.read_file_as_value(fallback_path) fallback_defaults = json.loads(fallback) if key in defaults['server']: return defaults['server'][key] if fallback_defaults: if key in fallback_defaults['server']: return fallback_defaults['server'][key] return None
def client_page(share_hash): """Display all flavours of client's config files to user.""" slug = DB.find_client_slug_by_share_hash(share_hash) if slug is None: abort(404) if ALLOWED_SLUGS is not None: if slug not in ALLOWED_SLUGS: abort(403) data = dict() data['css'] = SHARE.css data['slug'] = slug data['client_name'] = slug data['list_items'] = '' files = os.listdir(PATH + slug) for config_file in files: if config_file == 'pretty-name.txt': data['client_name'] = _helper.read_file_as_value(PATH + slug + '/' + config_file) continue anchor = '<a href="' + share_hash + '/' + config_file + '">' + config_file + '</a>' data['list_items'] += '<li>' + anchor + '</li>' renderer = pystache.Renderer() return renderer.render_path(SHARE.template_path, data)
def generate_config_files(self, verbose=True): """Generates different flavours of config files.""" ca_path = self._config.client_dir + 'ca.crt' cert_path = self._config.client_dir + self._config.slug + '.crt' key_path = self._config.client_dir + self._config.slug + '.key' ta_path = self._config.client_dir + 'ta.key' options = self.create_config() # Plain Windows flavour. self.write_config(options) # Plain Debian flavour. options['deb'] = True self.write_config(options, 'deb') options['deb'] = False # Plain RedHat flavour. options['rhel'] = True self.write_config(options, 'rhel') options['rhel'] = False # Inline Windows flavour. options['inline'] = True options['ca'] = _helper.read_file_as_value(ca_path) options['cert'] = _helper.read_file_as_value(cert_path) options['key'] = _helper.read_file_as_value(key_path) options['ta'] = _helper.read_file_as_value(ta_path) self.write_config(options, 'inline') # Inline Debian flavour. options['deb'] = True self.write_config(options, 'inline-deb') options['deb'] = False # Inline RedHat flavour. options['rhel'] = True self.write_config(options, 'inline-rhel') options['rhel'] = False # Clean up. self.cleanup_client_certificates() if verbose: print('> Client "' + self._config.slug + '" was successfully created.')
def get_value_from_sample(key, sample_path=None): """Get suggestion from sample config.""" if sample_path is None: sample_path = os.path.dirname( os.path.realpath(__file__)) + '/sovpn.json' sample = _helper.read_file_as_value(sample_path) defaults = json.loads(sample) if key in defaults['server']: return defaults['server'][key] return None
def needs_setup(): """Check if the script needs to run initial setup.""" container = _helper.sanitize_path( os.path.dirname(os.path.realpath(__file__))) sovpn_config_pointer = container + 'sovpn_config_pointer.txt' if not os.path.isfile(sovpn_config_pointer): return True sovpn_config_file = _helper.read_file_as_value(sovpn_config_pointer) if os.path.isfile(sovpn_config_file): return False return True
def load(self): """Populate properties with values if config file exists.""" if self.sovpn_config_file is None: self.sovpn_config_file = _helper.read_file_as_value( self.sovpn_config_pointer) if os.path.isfile(self.sovpn_config_file): with open(self.sovpn_config_file) as config_file: data = json.load(config_file) for pool in data: for key, value in data[pool].items(): if key in dir(self): setattr(self, key, value) self.loaded = True
def read_sql_file(self, sql_file): """Reads sql from file and returns it.""" sql = _helper.read_file_as_value(self._config.container + 'sql/' + sql_file) return sql
def css(self): """Method that return CSS content for sharing page.""" if self.css_path: return _helper.read_file_as_value(self.css_path) return None