def create_config(self, vpn_service: str, auto_connman_dhcp: bool): if self.is_connman(): FileHelper.write_file(self.connman_dhcp, str(auto_connman_dhcp)) return if not FileHelper.is_readable(self.origin_resolv_cfg): logger.info( f'Backup System DNS config file to [{self.origin_resolv_cfg}]...' ) FileHelper.backup(DNSResolver.DNS_SYSTEM_FILE, self.origin_resolv_cfg, remove=False) if not FileHelper.is_readable(self.origin_resolv_cfg): logger.error( f'Not found origin DNS config file [{self.origin_resolv_cfg}]') sys.exit(ErrorCode.FILE_CORRUPTED) if not FileHelper.is_readable(self.vpn_hook_cfg): FileHelper.touch(self.vpn_hook_cfg, 0o0644) self._resolver().setup(vpn_service, self.origin_resolv_cfg, self.vpn_resolv_cfg, self.vpn_hook_cfg) self._resolver().restart(_all=True)
def __validate_nameservers(self, reason: DHCPReason, new_ns: str = None, old_ns: str = None) -> Optional[list]: if reason.is_ignore(): return None if reason is DHCPReason.RENEW and new_ns == old_ns and FileHelper.is_readable( self.vpn_hook_cfg): return None nameservers = old_ns if reason.is_unreachable() else new_ns return [ns for ns in nameservers.split(',') if ns][0:2] if nameservers else None
def cleanup_config(self, vpn_service: str, keep_dnsmasq=True): if self.is_connman(): return resolver = self._resolver() if keep_dnsmasq: resolver.reset_hook(self.vpn_hook_cfg) elif FileHelper.is_readable(self.origin_resolv_cfg): logger.info(f'Restore System DNS config file...') FileHelper.backup(self.origin_resolv_cfg, DNSResolver.DNS_SYSTEM_FILE) FileHelper.rm(self.vpn_resolv_cfg) resolver.restore_config(vpn_service, keep_dnsmasq) resolver.restart(_all=not keep_dnsmasq, keep_dnsmasq=keep_dnsmasq)
def adapt_dnsmasq(self, origin_resolv_conf: Path, vpn_service: str) -> Optional[Path]: return FileHelper.get_target_link(origin_resolv_conf) or self.config.runtime_resolv if \ FileHelper.is_readable(self.config.runtime_resolv) else origin_resolv_conf