srv_resolver.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. # Copyright 2019 New Vector Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import logging
  17. import time
  18. import attr
  19. from twisted.internet import defer
  20. from twisted.internet.error import ConnectError
  21. from twisted.names import client, dns
  22. from twisted.names.error import DNSNameError, DomainError
  23. from synapse.util.logcontext import make_deferred_yieldable
  24. logger = logging.getLogger(__name__)
  25. SERVER_CACHE = {}
  26. @attr.s
  27. class Server(object):
  28. """
  29. Our record of an individual server which can be tried to reach a destination.
  30. Attributes:
  31. host (bytes): target hostname
  32. port (int):
  33. priority (int):
  34. weight (int):
  35. expires (int): when the cache should expire this record - in *seconds* since
  36. the epoch
  37. """
  38. host = attr.ib()
  39. port = attr.ib()
  40. priority = attr.ib(default=0)
  41. weight = attr.ib(default=0)
  42. expires = attr.ib(default=0)
  43. @defer.inlineCallbacks
  44. def resolve_service(service_name, dns_client=client, cache=SERVER_CACHE, clock=time):
  45. """Look up a SRV record, with caching
  46. The default resolver in twisted.names doesn't do any caching (it has a CacheResolver,
  47. but the cache never gets populated), so we add our own caching layer here.
  48. Args:
  49. service_name (unicode|bytes): record to look up
  50. dns_client (twisted.internet.interfaces.IResolver): twisted resolver impl
  51. cache (dict): cache object
  52. clock (object): clock implementation. must provide a time() method.
  53. Returns:
  54. Deferred[list[Server]]: a list of the SRV records, or an empty list if none found
  55. """
  56. # TODO: the dns client handles both unicode names (encoding via idna) and pre-encoded
  57. # byteses; however they will obviously end up as separate entries in the cache. We
  58. # should pick one form and stick with it.
  59. cache_entry = cache.get(service_name, None)
  60. if cache_entry:
  61. if all(s.expires > int(clock.time()) for s in cache_entry):
  62. servers = list(cache_entry)
  63. defer.returnValue(servers)
  64. try:
  65. answers, _, _ = yield make_deferred_yieldable(
  66. dns_client.lookupService(service_name),
  67. )
  68. except DNSNameError:
  69. # TODO: cache this. We can get the SOA out of the exception, and use
  70. # the negative-TTL value.
  71. defer.returnValue([])
  72. except DomainError as e:
  73. # We failed to resolve the name (other than a NameError)
  74. # Try something in the cache, else rereaise
  75. cache_entry = cache.get(service_name, None)
  76. if cache_entry:
  77. logger.warn(
  78. "Failed to resolve %r, falling back to cache. %r",
  79. service_name, e
  80. )
  81. defer.returnValue(list(cache_entry))
  82. else:
  83. raise e
  84. if (len(answers) == 1
  85. and answers[0].type == dns.SRV
  86. and answers[0].payload
  87. and answers[0].payload.target == dns.Name(b'.')):
  88. raise ConnectError("Service %s unavailable" % service_name)
  89. servers = []
  90. for answer in answers:
  91. if answer.type != dns.SRV or not answer.payload:
  92. continue
  93. payload = answer.payload
  94. servers.append(Server(
  95. host=payload.target.name,
  96. port=payload.port,
  97. priority=payload.priority,
  98. weight=payload.weight,
  99. expires=int(clock.time()) + answer.ttl,
  100. ))
  101. servers.sort() # FIXME: get rid of this (it's broken by the attrs change)
  102. cache[service_name] = list(servers)
  103. defer.returnValue(servers)