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.

secondary.py 7.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. # -*- test-case-name: twisted.names.test.test_names -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. __all__ = ["SecondaryAuthority", "SecondaryAuthorityService"]
  5. from twisted.application import service
  6. from twisted.internet import defer, task
  7. from twisted.names import client, common, dns, resolve
  8. from twisted.names.authority import FileAuthority
  9. from twisted.python import failure, log
  10. from twisted.python.compat import nativeString
  11. class SecondaryAuthorityService(service.Service):
  12. """
  13. A service that keeps one or more authorities up to date by doing hourly
  14. zone transfers from a master.
  15. @ivar primary: IP address of the master.
  16. @type primary: L{str}
  17. @ivar domains: An authority for each domain mirrored from the master.
  18. @type domains: L{list} of L{SecondaryAuthority}
  19. """
  20. calls = None
  21. _port = 53
  22. def __init__(self, primary, domains):
  23. """
  24. @param primary: The IP address of the server from which to perform
  25. zone transfers.
  26. @type primary: L{str}
  27. @param domains: A sequence of domain names for which to perform
  28. zone transfers.
  29. @type domains: L{list} of L{bytes}
  30. """
  31. self.primary = nativeString(primary)
  32. self.domains = [SecondaryAuthority(primary, d) for d in domains]
  33. @classmethod
  34. def fromServerAddressAndDomains(cls, serverAddress, domains):
  35. """
  36. Construct a new L{SecondaryAuthorityService} from a tuple giving a
  37. server address and a C{str} giving the name of a domain for which this
  38. is an authority.
  39. @param serverAddress: A two-tuple, the first element of which is a
  40. C{str} giving an IP address and the second element of which is a
  41. C{int} giving a port number. Together, these define where zone
  42. transfers will be attempted from.
  43. @param domains: Domain names for which to perform zone transfers.
  44. @type domains: sequence of L{bytes}
  45. @return: A new instance of L{SecondaryAuthorityService}.
  46. """
  47. primary, port = serverAddress
  48. service = cls(primary, [])
  49. service._port = port
  50. service.domains = [
  51. SecondaryAuthority.fromServerAddressAndDomain(serverAddress, d)
  52. for d in domains
  53. ]
  54. return service
  55. def getAuthority(self):
  56. """
  57. Get a resolver for the transferred domains.
  58. @rtype: L{ResolverChain}
  59. """
  60. return resolve.ResolverChain(self.domains)
  61. def startService(self):
  62. service.Service.startService(self)
  63. self.calls = [task.LoopingCall(d.transfer) for d in self.domains]
  64. i = 0
  65. from twisted.internet import reactor
  66. for c in self.calls:
  67. # XXX Add errbacks, respect proper timeouts
  68. reactor.callLater(i, c.start, 60 * 60)
  69. i += 1
  70. def stopService(self):
  71. service.Service.stopService(self)
  72. for c in self.calls:
  73. c.stop()
  74. class SecondaryAuthority(FileAuthority):
  75. """
  76. An Authority that keeps itself updated by performing zone transfers.
  77. @ivar primary: The IP address of the server from which zone transfers will
  78. be attempted.
  79. @type primary: L{str}
  80. @ivar _port: The port number of the server from which zone transfers will
  81. be attempted.
  82. @type _port: L{int}
  83. @ivar domain: The domain for which this is the secondary authority.
  84. @type domain: L{bytes}
  85. @ivar _reactor: The reactor to use to perform the zone transfers, or
  86. L{None} to use the global reactor.
  87. """
  88. transferring = False
  89. soa = records = None
  90. _port = 53
  91. _reactor = None
  92. def __init__(self, primaryIP, domain):
  93. """
  94. @param domain: The domain for which this will be the secondary
  95. authority.
  96. @type domain: L{bytes} or L{str}
  97. """
  98. # Yep. Skip over FileAuthority.__init__. This is a hack until we have
  99. # a good composition-based API for the complicated DNS record lookup
  100. # logic we want to share.
  101. common.ResolverBase.__init__(self)
  102. self.primary = nativeString(primaryIP)
  103. self.domain = dns.domainString(domain)
  104. @classmethod
  105. def fromServerAddressAndDomain(cls, serverAddress, domain):
  106. """
  107. Construct a new L{SecondaryAuthority} from a tuple giving a server
  108. address and a C{bytes} giving the name of a domain for which this is an
  109. authority.
  110. @param serverAddress: A two-tuple, the first element of which is a
  111. C{str} giving an IP address and the second element of which is a
  112. C{int} giving a port number. Together, these define where zone
  113. transfers will be attempted from.
  114. @param domain: A C{bytes} giving the domain to transfer.
  115. @type domain: L{bytes}
  116. @return: A new instance of L{SecondaryAuthority}.
  117. """
  118. primary, port = serverAddress
  119. secondary = cls(primary, domain)
  120. secondary._port = port
  121. return secondary
  122. def transfer(self):
  123. """
  124. Attempt a zone transfer.
  125. @returns: A L{Deferred} that fires with L{None} when attempted zone
  126. transfer has completed.
  127. """
  128. # FIXME: This logic doesn't avoid duplicate transfers
  129. # https://twistedmatrix.com/trac/ticket/9754
  130. if self.transferring: # <-- never true
  131. return
  132. self.transfering = True # <-- speling
  133. reactor = self._reactor
  134. if reactor is None:
  135. from twisted.internet import reactor
  136. resolver = client.Resolver(
  137. servers=[(self.primary, self._port)], reactor=reactor
  138. )
  139. return (
  140. resolver.lookupZone(self.domain)
  141. .addCallback(self._cbZone)
  142. .addErrback(self._ebZone)
  143. )
  144. def _lookup(self, name, cls, type, timeout=None):
  145. if not self.soa or not self.records:
  146. # No transfer has occurred yet. Fail non-authoritatively so that
  147. # the caller can try elsewhere.
  148. return defer.fail(failure.Failure(dns.DomainError(name)))
  149. return FileAuthority._lookup(self, name, cls, type, timeout)
  150. def _cbZone(self, zone):
  151. ans, _, _ = zone
  152. self.records = r = {}
  153. for rec in ans:
  154. if not self.soa and rec.type == dns.SOA:
  155. self.soa = (rec.name.name.lower(), rec.payload)
  156. else:
  157. r.setdefault(rec.name.name.lower(), []).append(rec.payload)
  158. def _ebZone(self, failure):
  159. log.msg(
  160. "Updating %s from %s failed during zone transfer"
  161. % (self.domain, self.primary)
  162. )
  163. log.err(failure)
  164. def update(self):
  165. self.transfer().addCallbacks(self._cbTransferred, self._ebTransferred)
  166. def _cbTransferred(self, result):
  167. self.transferring = False
  168. def _ebTransferred(self, failure):
  169. self.transferred = False
  170. log.msg(
  171. "Transferring %s from %s failed after zone transfer"
  172. % (self.domain, self.primary)
  173. )
  174. log.err(failure)