def reset(self): zone_file = open("test_data/IS0401/dns_base.zone").read() template = Template(zone_file) self.base_zone_data = template.render(ip_address=self.default_ip) self.resolver = ZoneResolver(self.base_zone_data) self.stop() self.start()
def setUp(self): zone_file = ''' crate.internal. 600 IN SOA localhost localhost ( 2007120710 1d 2h 4w 1h ) crate.internal. 400 IN NS localhost crate.internal. 600 IN A 127.0.0.1''' transport_ports = [bind_port() for _ in range(self.num_nodes)] for port in transport_ports: zone_file += ''' _test._srv.crate.internal. 600 IN SRV 1 10 {port} 127.0.0.1.'''.format( port=port) dns_port = bind_port() self.dns_server = DNSServer(ZoneResolver(zone_file), port=dns_port) self.dns_server.start_thread() self.nodes = nodes = [] for i in range(self.num_nodes): settings = { 'node.name': f'node-{i}', 'cluster.name': 'crate-dns-discovery', 'psql.port': 0, 'transport.tcp.port': transport_ports[i], "discovery.seed_providers": "srv", "discovery.srv.query": "_test._srv.crate.internal.", "discovery.srv.resolver": "127.0.0.1:" + str(dns_port) } if i is 0: settings['cluster.initial_master_nodes'] = f'node-{i}' node = CrateNode(crate_dir=crate_path(), version=(4, 0, 0), settings=settings, env={'CRATE_HEAP_SIZE': '256M'}) node.start() nodes.append(node)
def load_zone(self, api_version): zone_file = open("test_data/IS0401/dns_records.zone").read() template = Template(zone_file) zone_data = template.render(ip_address=self.default_ip, api_ver=api_version) self.resolver = ZoneResolver(self.base_zone_data + zone_data) self.stop() self.start()
def test_discover(self, srv_address: str): server_info = ServerInfo() lock: Lock = Lock() Thread(target=start_tcp_server, args=(server_info, lock)).start() sleep(0.01) # wait for thread # waiting for the TCP server emulator thread is coming up lock.acquire() lock.release() name_servers: List[str] = ['127.0.0.1'] # the list of nameservers # configures the DNS Server zone_resolver: ZoneResolver = ZoneResolver(zone=zone_file.format( domain=domain, srv_address=srv_address, port=server_info.port), ) # port=0 means that the DNS Server will choose a free UDP Port dns_server: DNSServer = DNSServer(resolver=zone_resolver, port=0, tcp=False) dns_server.start_thread( ) # start the DNS Server in a separate python thread sleep(0.1) # waiting for the DNS Server thread is coming up port: int = dns_server.server.server_address[ 1] # gets the number of the UDP Port # discover for domain controllers dc_list: DCList = DCList(domain=domain, nameservers=name_servers, port=port) self.assertIsNotNone(dc_list.get_dc_list(), 'Could not get a list of Domain Controllers') # try to get an available domain controller dc: str = dc_list.get_available_dc_ip() self.assertIsNotNone(dc, "Could not get an available Domain Controller") # stop DNS Server dns_server.server.server_close() dns_server.stop()
def resolve(self, request, handler): qtype = request.q.qtype qname = str(request.q.qname) try: self.watching[qtype][qname].set() except (KeyError, AttributeError): pass return ZoneResolver.resolve(self, request, handler)
def __init__(self, zone: str = ""): LOGGER.debug(f"DNS Server received zones: \n {zone}") self.resolver = ZoneResolver(textwrap.dedent(zone)) self.dns_logger = DNSLogger(prefix=False) self.server = DNSServer(self.resolver, port=10053, address="0.0.0.0", logger=self.dns_logger)
def reset(self): zone_file = open("test_data/IS0401/dns_base.zone").read() template = Template(zone_file) self.base_zone_data = template.render(ip_address=self.default_ip, domain=DNS_DOMAIN) self.resolver = ZoneResolver(self.base_zone_data) self.stop() print(" * Loading DNS zone base file") self.start()
def update_zone(self, new_zone: str): LOGGER.debug(f"Updating DNS with new zone:\n {new_zone}") self.stop() self.resolver = ZoneResolver(textwrap.dedent(new_zone)) self.server = DNSServer(self.resolver, port=10053, address="0.0.0.0", logger=self.dns_logger) self.start()
def load_zone(self, api_version, api_protocol): zone_file = open("test_data/IS0401/dns_records.zone").read() template = Template(zone_file) zone_data = template.render(ip_address=self.default_ip, api_ver=api_version, api_proto=api_protocol) self.resolver = ZoneResolver(self.base_zone_data + zone_data) self.stop() print(" * Loading DNS zone file with api_ver={}".format(api_version)) self.start()
def main(): num_nodes = 3 node0_http_port = bind_port() dns_port = bind_port() transport_ports = [] zone_file = ''' crate.internal. 600 IN SOA localhost localhost ( 2007120710 1d 2h 4w 1h ) crate.internal. 400 IN NS localhost crate.internal. 600 IN A 127.0.0.1''' for i in range(0, num_nodes): port = bind_port() transport_ports.append(port) zone_file += ''' _test._srv.crate.internal. 600 IN SRV 1 10 {port} 127.0.0.1.'''.format( port=port) dns_server = DNSServer(ZoneResolver(zone_file), port=dns_port) dns_server.start_thread() crate_layers = [] for i in range(0, num_nodes): crate_layer = CrateLayer( 'node-' + str(i), cluster_name='crate-dns-discovery', crate_home=crate_path(), port=node0_http_port if i == 0 else bind_port(), transport_port=transport_ports[i], settings={ 'psql.port': bind_port(), "discovery.zen.hosts_provider": "srv", "discovery.srv.query": "_test._srv.crate.internal.", "discovery.srv.resolver": "127.0.0.1:" + str(dns_port) }) crate_layers.append(crate_layer) crate_layer.start() try: conn = connect('localhost:{}'.format(node0_http_port)) c = conn.cursor() c.execute('''select count() from sys.nodes''') result = c.fetchone() if result[0] != num_nodes: raise AssertionError( "Nodes could not join, expected number of nodes: " + str(num_nodes) + ", found: " + str(result[0])) finally: for crate_layer in crate_layers: crate_layer.stop() dns_server.stop()
def start_bind(self): """ 启动监听,udp监听同一个端口时只有第一个有用 :return: """ self.logger.info("\n" + self.zone) resolver = ZoneResolver(textwrap.dedent(self.zone), True) dns_logger = DnsLogger() self.udp_server = DNSServer(resolver, port=self.port, address=self.host, logger=dns_logger) return True
def resolve(self, request, handler): reply = ZoneResolver.resolve(self, request, handler) if reply.header.rcode == RCODE.NXDOMAIN and self.upstream: try: if handler.protocol == 'udp': proxy_r = request.send(self.upstream, self.upstream_port, timeout=self.timeout) else: proxy_r = request.send(self.upstream, self.upstream_port, tcp=True, timeout=self.timeout) reply = DNSRecord.parse(proxy_r) except socket.timeout: reply.header.rcode = RCODE.SERVFAIL return reply
def __enter__(self): zones = [] for (domain, txt) in self.records.items(): n = 255 frags = [("'" + txt[i:i+n] + "'") for i in range(0, len(txt), n)] zones.append(domain + " IN TXT " + " ".join(frags)) resolver = ZoneResolver("\n".join(zones)) if self.verbose: logger = DNSLogger() else: logger = NullLogger() self.server = DNSServer(resolver, port=self.port, logger=logger) self.server.start_thread()
def __init__(self, zonedir, glob=False): self.georeader = geolite2.reader() self.zones = {} """ Initialise resolver from zone file directory. GEO location information is extracted from file name suffix e.g: "oversea.ceair.com-CN", this is a zone file for all CN client ip """ zone_files = [f for f in listdir(zonedir) if isfile(join(zonedir, f))] for file in zone_files: location = self._location_from_suffix(file) full_path = join(zonedir, file) print("Processing zone file: %s" % full_path) zone = open(full_path) self.zones[location] = ZoneResolver(zone, glob) self.glob = glob self.eq = 'matchGlob' if glob else '__eq__'
def __init__(self, zone, glob=False): ZoneResolver.__init__(self, zone, glob) self.watching = {}
def __init__(self, zone, glob=False): ZoneResolver.__init__(self, zone, glob) self.upstream = CONFIG.DNS_UPSTREAM_IP self.upstream_port = 53 self.timeout = None