def shake_hand(self): address = self.config['address'] if ('ip' not in self.config or self.config['ip'] == '') and not utils.is_valid_ipv4_address(address): self.config['ip'] = utils.get_ip_address( self.client_object, address, self.client_object.bootstrap_dns_object)
def load_hosts(self): hosts = self.dns_config['hosts'] if self.dns_config['force_safe_search']: hosts.update(utils.load_hosts_from_file('filter_lists/safe_search.txt')) if self.dns_config['block_ads']: hosts.update(utils.load_hosts_from_file('filter_lists/ads.txt')) for name in hosts: if utils.is_valid_ipv4_address(hosts[name]): hosts[name] = [hosts[name], 'A'] else: hosts[name] = [hosts[name], 'CNAME'] return hosts
def load_hosts(self): hosts = self.dns_config['hosts'] current_dir = os.path.dirname(os.path.abspath(__file__)).rstrip('/').rstrip('server') if self.dns_config['force_safe_search']: hosts.update(utils.load_hosts_from_file(current_dir + 'filter_lists/safe_search.txt')) if self.dns_config['block_ads']: hosts.update(utils.load_hosts_from_file(current_dir + 'filter_lists/ads.txt')) for name in hosts: if utils.is_valid_ipv4_address(hosts[name]): hosts[name] = [hosts[name], 'A'] else: hosts[name] = [hosts[name], 'CNAME'] return hosts
def check_config(self): bootstrap_dns_address = self.dns_config['bootstrap_dns_address'][ 'address'] bootstrap_dns_port = self.dns_config['bootstrap_dns_address']['port'] for item in self.dns_config['upstream_dns']: protocol = item['protocol'] address = item['address'] if protocol == 'https' or protocol == 'tls': if not utils.is_valid_ipv4_address(address): if 'ip' not in item or item['ip'] == '': item['ip'] = self.get_ip_address( address, bootstrap_dns_address, bootstrap_dns_port, self.dns_config['upstream_timeout']) self.upstream_object[protocol][address] = self.shake_hand(item)
def load_hosts(self): hosts = self.dns_config['hosts'] hosts_safe_search = { "www.google.com": "forcesafesearch.google.com", "www.bing.com": "strict.bing.com", "www.youtube.com": "restrictmoderate.youtube.com", "m.youtube.com": "restrictmoderate.youtube.com", "youtubei.googleapis.com": "restrictmoderate.youtube.com", "youtube.googleapis.com": "restrictmoderate.youtube.com", "www.youtube-nocookie.com": "restrictmoderate.youtube.com" } if self.dns_config['force_safe_search']: hosts.update(hosts_safe_search) for name in hosts: if utils.is_valid_ipv4_address(hosts[name]): hosts[name] = [hosts[name], 'A'] else: hosts[name] = [hosts[name], 'CNAME'] return hosts
def start(self): try: while True: recv_data, recv_address = self.server.recvfrom(512) recv_header = parse.ParseHeader.parse_header(recv_data) if self.enable_log: self.logger.write_log('recv_data:' + str(recv_data)) transaction_id = recv_header['transaction_id'] if self.enable_log: self.logger.write_log('transaction_id:' + str(transaction_id)) if recv_header['flags']['QR'] == '0': self.dns_map[transaction_id] = [recv_address, 0] query_thread = threading.Thread(target=self.handle_query, args=(transaction_id, recv_data,)) query_thread.daemon = True query_thread.start() elif recv_header['flags']['QR'] == '1' and transaction_id in self.dns_map: response = self.handle_response(recv_data) sendback_address = self.dns_map[transaction_id][0] if self.dns_bypass_china and response[2]: if len(response[2]) > 1: ip_address = response[2][-1]['record'] else: ip_address = response[2][0]['record'] if self.dns_map[transaction_id][1] == 1 or ( utils.is_valid_ipv4_address(ip_address) and utils.is_subnet_address( 'filter_lists/chnroute.txt', ip_address) ): self.server.sendto(recv_data, sendback_address) self.dns_map.pop(transaction_id) elif self.dns_map[transaction_id][1] == 0: self.dns_map[transaction_id][1] = 1 continue else: self.server.sendto(recv_data, sendback_address) self.dns_map.pop(transaction_id) if self.enable_cache and response[2]: response_name = utils.get_domain_name_string(response[1]['QNAME']) if response_name != '': response_type = response[1]['QTYPE'] response_ttl = response[2][0]['ttl'] if response_name not in self.cache: self.cache[response_name] = {} self.cache[response_name][response_type] = [recv_data, int(time.time()), response_ttl] except socket.timeout as exc: print('[Error]', str(exc)) self.start() except KeyboardInterrupt: print('Stop Encrypted-DNS Resolver') exit() except Exception as exc: print('[Error]', str(exc)) self.start()