Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tap.py 4.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # -*- test-case-name: twisted.names.test.test_tap -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Domain Name Server
  6. """
  7. import os
  8. import traceback
  9. from twisted.application import internet, service
  10. from twisted.names import authority, dns, secondary, server
  11. from twisted.python import usage
  12. class Options(usage.Options):
  13. optParameters = [
  14. ["interface", "i", "", "The interface to which to bind"],
  15. ["port", "p", "53", "The port on which to listen"],
  16. [
  17. "resolv-conf",
  18. None,
  19. None,
  20. "Override location of resolv.conf (implies --recursive)",
  21. ],
  22. ["hosts-file", None, None, "Perform lookups with a hosts file"],
  23. ]
  24. optFlags = [
  25. ["cache", "c", "Enable record caching"],
  26. ["recursive", "r", "Perform recursive lookups"],
  27. ["verbose", "v", "Log verbosely"],
  28. ]
  29. compData = usage.Completions(
  30. optActions={"interface": usage.CompleteNetInterfaces()}
  31. )
  32. zones = None
  33. zonefiles = None
  34. def __init__(self):
  35. usage.Options.__init__(self)
  36. self["verbose"] = 0
  37. self.bindfiles = []
  38. self.zonefiles = []
  39. self.secondaries = []
  40. def opt_pyzone(self, filename):
  41. """Specify the filename of a Python syntax zone definition"""
  42. if not os.path.exists(filename):
  43. raise usage.UsageError(filename + ": No such file")
  44. self.zonefiles.append(filename)
  45. def opt_bindzone(self, filename):
  46. """Specify the filename of a BIND9 syntax zone definition"""
  47. if not os.path.exists(filename):
  48. raise usage.UsageError(filename + ": No such file")
  49. self.bindfiles.append(filename)
  50. def opt_secondary(self, ip_domain):
  51. """Act as secondary for the specified domain, performing
  52. zone transfers from the specified IP (IP/domain)
  53. """
  54. args = ip_domain.split("/", 1)
  55. if len(args) != 2:
  56. raise usage.UsageError("Argument must be of the form IP[:port]/domain")
  57. address = args[0].split(":")
  58. if len(address) == 1:
  59. address = (address[0], dns.PORT)
  60. else:
  61. try:
  62. port = int(address[1])
  63. except ValueError:
  64. raise usage.UsageError(
  65. f"Specify an integer port number, not {address[1]!r}"
  66. )
  67. address = (address[0], port)
  68. self.secondaries.append((address, [args[1]]))
  69. def opt_verbose(self):
  70. """Increment verbosity level"""
  71. self["verbose"] += 1
  72. def postOptions(self):
  73. if self["resolv-conf"]:
  74. self["recursive"] = True
  75. self.svcs = []
  76. self.zones = []
  77. for f in self.zonefiles:
  78. try:
  79. self.zones.append(authority.PySourceAuthority(f))
  80. except Exception:
  81. traceback.print_exc()
  82. raise usage.UsageError("Invalid syntax in " + f)
  83. for f in self.bindfiles:
  84. try:
  85. self.zones.append(authority.BindAuthority(f))
  86. except Exception:
  87. traceback.print_exc()
  88. raise usage.UsageError("Invalid syntax in " + f)
  89. for f in self.secondaries:
  90. svc = secondary.SecondaryAuthorityService.fromServerAddressAndDomains(*f)
  91. self.svcs.append(svc)
  92. self.zones.append(self.svcs[-1].getAuthority())
  93. try:
  94. self["port"] = int(self["port"])
  95. except ValueError:
  96. raise usage.UsageError("Invalid port: {!r}".format(self["port"]))
  97. def _buildResolvers(config):
  98. """
  99. Build DNS resolver instances in an order which leaves recursive
  100. resolving as a last resort.
  101. @type config: L{Options} instance
  102. @param config: Parsed command-line configuration
  103. @return: Two-item tuple of a list of cache resovers and a list of client
  104. resolvers
  105. """
  106. from twisted.names import cache, client, hosts
  107. ca, cl = [], []
  108. if config["cache"]:
  109. ca.append(cache.CacheResolver(verbose=config["verbose"]))
  110. if config["hosts-file"]:
  111. cl.append(hosts.Resolver(file=config["hosts-file"]))
  112. if config["recursive"]:
  113. cl.append(client.createResolver(resolvconf=config["resolv-conf"]))
  114. return ca, cl
  115. def makeService(config):
  116. ca, cl = _buildResolvers(config)
  117. f = server.DNSServerFactory(config.zones, ca, cl, config["verbose"])
  118. p = dns.DNSDatagramProtocol(f)
  119. f.noisy = 0
  120. ret = service.MultiService()
  121. for (klass, arg) in [(internet.TCPServer, f), (internet.UDPServer, p)]:
  122. s = klass(config["port"], arg, interface=config["interface"])
  123. s.setServiceParent(ret)
  124. for svc in config.svcs:
  125. svc.setServiceParent(ret)
  126. return ret