def do_install(self, service_opts: UnixServiceOpts, auto_startup: bool = False, auto_connman_dhcp: bool = False): FileHelper.mkdirs(self.opts.vpn_dir.parent) FileHelper.unpack_archive(ClientOpts.get_resource(ClientOpts.VPN_ZIP), self.opts.vpn_dir) FileHelper.mkdirs([self.opts.vpn_dir, self.opts.runtime_dir]) FileHelper.chmod(self.opts.runtime_dir, mode=0o0755) FileHelper.chmod([ os.path.join(self.opts.vpn_dir, p) for p in ('vpnclient', 'vpncmd') ], mode=0o0755) _, cmd = EnvHelper.build_executable_command() svc_opts = self._standard_service_opt(service_opts) self.device.unix_service.create( svc_opts, { '{{WORKING_DIR}}': f'{self.opts.vpn_dir}', '{{VPN_DESC}}': svc_opts.service_name, '{{START_CMD}}': f'{cmd} start --vpn-dir {self.opts.vpn_dir}', '{{STOP_CMD}}': f'{cmd} stop --vpn-dir {self.opts.vpn_dir}' }, auto_startup) self._dump_cache_service(svc_opts) self.device.ip_resolver.add_hook(svc_opts.service_name, { '{{WORKING_DIR}}': f'{self.opts.vpn_dir}', '{{VPN_CLIENT_CLI}}': cmd }) self.device.dns_resolver.create_config(svc_opts.service_name, auto_connman_dhcp) self.storage.empty() self.opts.export_env()
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)
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)
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
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)
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)