Ejemplo n.º 1
0
 def create_config(self, vpn_acc: str, replacements: dict):
     config_file = self._to_config_file(vpn_acc)
     logger.log(self.log_lvl,
                f'Create DHCP client VPN config[{config_file}]...')
     FileHelper.copy(self.resource_dir.joinpath(self.DHCLIENT_CONFIG_TMPL),
                     config_file,
                     force=True)
     FileHelper.replace_in_file(config_file, replacements, backup='')
     FileHelper.chmod(config_file, mode=0o0644)
Ejemplo n.º 2
0
 def add_hook(self, service_name: str, replacements: dict):
     exit_hook_file = self._to_hook_file(service_name)
     logger.log(self.log_lvl,
                f'Create DHCP client VPN hook[{exit_hook_file}]...')
     FileHelper.copy(self.resource_dir.joinpath(
         self.DHCLIENT_EXIT_HOOK_TMPL),
                     exit_hook_file,
                     force=True)
     FileHelper.replace_in_file(exit_hook_file, replacements, backup='')
     FileHelper.chmod(exit_hook_file, mode=0o0744)
Ejemplo n.º 3
0
 def _common_adapt_dnsmasq(self, vpn_service: str):
     identity = self.config.identity
     logger.debug(
         f'Adapt [{identity}] DNS resolver service to compatible with [dnsmasq] and [{vpn_service}]...'
     )
     FileHelper.mkdirs(self.config.config_dir)
     FileHelper.copy(self.resource_dir.joinpath(f'dnsmasq-{identity}.conf'),
                     self.config.to_fqn_cfg(self.DNSMASQ_TUNED_CFG), True)
     FileHelper.chmod(self.config.to_fqn_cfg(self.DNSMASQ_TUNED_CFG),
                      mode=0o0644)
     return self.config.runtime_resolv
Ejemplo n.º 4
0
 def restore_config(self, backup_dir: Path, keep_backup: bool):
     logger.info(
         f'Restore VPN configuration [{backup_dir}] to [{self.opts.vpn_dir}]...'
     )
     FileHelper.copy(backup_dir.joinpath(self.opts.VPN_CONFIG_FILE),
                     self.opts.config_file,
                     force=True)
     FileHelper.copy(backup_dir.joinpath(self.opts.RUNTIME_FOLDER),
                     self.opts.runtime_dir,
                     force=True)
     FileHelper.rm(backup_dir, force=not keep_backup)
Ejemplo n.º 5
0
 def backup_config(self):
     backup_dir = self.opts.backup_dir()
     logger.info(
         f'Backup VPN configuration [{self.opts.vpn_dir}] to [{backup_dir}] ...'
     )
     FileHelper.mkdirs(backup_dir)
     FileHelper.copy(self.opts.config_file, backup_dir, force=True)
     FileHelper.copy(self.opts.runtime_dir,
                     backup_dir.joinpath(self.opts.RUNTIME_FOLDER),
                     force=True)
     default_acc = self.storage.get_default()
     current_acc = self.storage.get_current()
     svc_opt = self._standard_service_opt()
     return default_acc, current_acc, svc_opt, backup_dir
Ejemplo n.º 6
0
 def create(self,
            svc_opts: UnixServiceOpts,
            replacements: dict,
            auto_startup: bool = False):
     service_fqn = self.to_service_fqn(svc_opts.service_dir,
                                       svc_opts.service_name)
     logger.info(
         f'Add new service [{svc_opts.service_name}] in [{service_fqn}]...')
     FileHelper.copy(self.resource_dir.joinpath(Systemd.SERVICE_FILE_TMPL),
                     service_fqn,
                     force=True)
     FileHelper.replace_in_file(service_fqn, replacements, backup='')
     FileHelper.chmod(service_fqn, mode=0o0644)
     SystemHelper.exec_command("systemctl daemon-reload",
                               silent=True,
                               log_lvl=logger.INFO)
     if auto_startup:
         self.enable(svc_opts.service_name)
Ejemplo n.º 7
0
 def setup(self, vpn_service: str, origin_resolv_conf: Path,
           vpn_resolv_conf: Path, vpn_nameserver_hook_conf: Path):
     if not self._available:
         logger.error('[dnsmasq] is not yet installed or is corrupted')
         sys.exit(ErrorCode.MISSING_REQUIREMENT)
     logger.info('Setup DNS resolver[dnsmasq]...')
     dnsmasq_vpn_cfg = self._dnsmasq_vpn_cfg(vpn_service)
     runtime_resolv_cfg = self.adapt_dnsmasq(origin_resolv_conf,
                                             vpn_service)
     dnsmasq_opts = {
         '{{DNS_RESOLVED_FILE}}':
         self.__build_dnsmasq_conf('resolv-file', runtime_resolv_cfg),
         '{{PORT}}':
         self.__build_dnsmasq_conf('port',
                                   self.dnsmasq_options().get('port',
                                                              None)),
         '{{CACHE_SIZE}}':
         self.__build_dnsmasq_conf(
             'cache-size',
             self.dnsmasq_options().get('cache_size', None))
     }
     logger.debug(
         f'Add [dnsmasq] config for {vpn_service}[{dnsmasq_vpn_cfg}]...')
     FileHelper.copy(self.resource_dir.joinpath(self.DNSMASQ_CONFIG_TMPL),
                     dnsmasq_vpn_cfg,
                     force=True)
     FileHelper.replace_in_file(dnsmasq_vpn_cfg, dnsmasq_opts, backup='')
     FileHelper.chmod(dnsmasq_vpn_cfg, mode=0o0644)
     logger.debug(
         f'Symlink [dnsmasq] VPN nameserver runtime configuration [{vpn_nameserver_hook_conf}]...'
     )
     FileHelper.create_symlink(vpn_nameserver_hook_conf,
                               self._dnsmasq_vpn_hook_cfg,
                               force=True)
     logger.info(f'Generate System DNS config file from VPN service...')
     FileHelper.write_file(vpn_resolv_conf,
                           self.__dnsmasq_resolv(vpn_service),
                           mode=0o0644)
     FileHelper.create_symlink(vpn_resolv_conf,
                               DNSResolver.DNS_SYSTEM_FILE,
                               force=True)
     self.service.enable(self.config.identity)