dns_server.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env python2.7
  2. # Copyright 2015 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Starts a local DNS server for use in tests"""
  16. import argparse
  17. import os
  18. import platform
  19. import signal
  20. import sys
  21. import threading
  22. import time
  23. import twisted
  24. import twisted.internet
  25. import twisted.internet.defer
  26. import twisted.internet.protocol
  27. import twisted.internet.reactor
  28. import twisted.internet.threads
  29. import twisted.names
  30. from twisted.names import authority
  31. from twisted.names import client
  32. from twisted.names import common
  33. from twisted.names import dns
  34. from twisted.names import server
  35. import twisted.names.client
  36. import twisted.names.dns
  37. import twisted.names.server
  38. import yaml
  39. _SERVER_HEALTH_CHECK_RECORD_NAME = 'health-check-local-dns-server-is-alive.resolver-tests.grpctestingexp' # missing end '.' for twisted syntax
  40. _SERVER_HEALTH_CHECK_RECORD_DATA = '123.123.123.123'
  41. class NoFileAuthority(authority.FileAuthority):
  42. def __init__(self, soa, records):
  43. # skip FileAuthority
  44. common.ResolverBase.__init__(self)
  45. self.soa = soa
  46. self.records = records
  47. def start_local_dns_server(args):
  48. all_records = {}
  49. def _push_record(name, r):
  50. name = name.encode('ascii')
  51. print('pushing record: |%s|' % name)
  52. if all_records.get(name) is not None:
  53. all_records[name].append(r)
  54. return
  55. all_records[name] = [r]
  56. def _maybe_split_up_txt_data(name, txt_data, r_ttl):
  57. txt_data = txt_data.encode('ascii')
  58. start = 0
  59. txt_data_list = []
  60. while len(txt_data[start:]) > 0:
  61. next_read = len(txt_data[start:])
  62. if next_read > 255:
  63. next_read = 255
  64. txt_data_list.append(txt_data[start:start + next_read])
  65. start += next_read
  66. _push_record(name, dns.Record_TXT(*txt_data_list, ttl=r_ttl))
  67. with open(args.records_config_path) as config:
  68. test_records_config = yaml.load(config)
  69. common_zone_name = test_records_config['resolver_tests_common_zone_name']
  70. for group in test_records_config['resolver_component_tests']:
  71. for name in group['records'].keys():
  72. for record in group['records'][name]:
  73. r_type = record['type']
  74. r_data = record['data']
  75. r_ttl = int(record['TTL'])
  76. record_full_name = '%s.%s' % (name, common_zone_name)
  77. assert record_full_name[-1] == '.'
  78. record_full_name = record_full_name[:-1]
  79. if r_type == 'A':
  80. _push_record(record_full_name,
  81. dns.Record_A(r_data, ttl=r_ttl))
  82. if r_type == 'AAAA':
  83. _push_record(record_full_name,
  84. dns.Record_AAAA(r_data, ttl=r_ttl))
  85. if r_type == 'SRV':
  86. p, w, port, target = r_data.split(' ')
  87. p = int(p)
  88. w = int(w)
  89. port = int(port)
  90. target_full_name = (
  91. '%s.%s' % (target, common_zone_name)).encode('ascii')
  92. _push_record(
  93. record_full_name,
  94. dns.Record_SRV(p, w, port, target_full_name, ttl=r_ttl))
  95. if r_type == 'TXT':
  96. _maybe_split_up_txt_data(record_full_name, r_data, r_ttl)
  97. # Add an optional IPv4 record is specified
  98. if args.add_a_record:
  99. extra_host, extra_host_ipv4 = args.add_a_record.split(':')
  100. _push_record(extra_host, dns.Record_A(extra_host_ipv4, ttl=0))
  101. # Server health check record
  102. _push_record(_SERVER_HEALTH_CHECK_RECORD_NAME,
  103. dns.Record_A(_SERVER_HEALTH_CHECK_RECORD_DATA, ttl=0))
  104. soa_record = dns.Record_SOA(mname=common_zone_name.encode('ascii'))
  105. test_domain_com = NoFileAuthority(
  106. soa=(common_zone_name.encode('ascii'), soa_record),
  107. records=all_records,
  108. )
  109. server = twisted.names.server.DNSServerFactory(
  110. authorities=[test_domain_com], verbose=2)
  111. server.noisy = 2
  112. twisted.internet.reactor.listenTCP(args.port, server)
  113. dns_proto = twisted.names.dns.DNSDatagramProtocol(server)
  114. dns_proto.noisy = 2
  115. twisted.internet.reactor.listenUDP(args.port, dns_proto)
  116. print('starting local dns server on 127.0.0.1:%s' % args.port)
  117. print('starting twisted.internet.reactor')
  118. twisted.internet.reactor.suggestThreadPoolSize(1)
  119. twisted.internet.reactor.run()
  120. def _quit_on_signal(signum, _frame):
  121. print('Received SIGNAL %d. Quitting with exit code 0' % signum)
  122. twisted.internet.reactor.stop()
  123. sys.stdout.flush()
  124. sys.exit(0)
  125. def flush_stdout_loop():
  126. num_timeouts_so_far = 0
  127. sleep_time = 1
  128. # Prevent zombies. Tests that use this server are short-lived.
  129. max_timeouts = 60 * 10
  130. while num_timeouts_so_far < max_timeouts:
  131. sys.stdout.flush()
  132. time.sleep(sleep_time)
  133. num_timeouts_so_far += 1
  134. print('Process timeout reached, or cancelled. Exitting 0.')
  135. os.kill(os.getpid(), signal.SIGTERM)
  136. def main():
  137. argp = argparse.ArgumentParser(
  138. description='Local DNS Server for resolver tests')
  139. argp.add_argument('-p',
  140. '--port',
  141. default=None,
  142. type=int,
  143. help='Port for DNS server to listen on for TCP and UDP.')
  144. argp.add_argument(
  145. '-r',
  146. '--records_config_path',
  147. default=None,
  148. type=str,
  149. help=('Directory of resolver_test_record_groups.yaml file. '
  150. 'Defaults to path needed when the test is invoked as part '
  151. 'of run_tests.py.'))
  152. argp.add_argument(
  153. '--add_a_record',
  154. default=None,
  155. type=str,
  156. help=('Add an A record via the command line. Useful for when we '
  157. 'need to serve a one-off A record that is under a '
  158. 'different domain then the rest the records configured in '
  159. '--records_config_path (which all need to be under the '
  160. 'same domain). Format: <name>:<ipv4 address>'))
  161. args = argp.parse_args()
  162. signal.signal(signal.SIGTERM, _quit_on_signal)
  163. signal.signal(signal.SIGINT, _quit_on_signal)
  164. output_flush_thread = threading.Thread(target=flush_stdout_loop)
  165. output_flush_thread.setDaemon(True)
  166. output_flush_thread.start()
  167. start_local_dns_server(args)
  168. if __name__ == '__main__':
  169. main()