Update federation routing logic to check .well-known before SRV

pull/4539/head
Richard van der Hoff 2019-01-31 23:13:44 +00:00
parent 85129d7068
commit d428b46346
3 changed files with 27 additions and 35 deletions

1
changelog.d/4539.misc Normal file
View File

@ -0,0 +1 @@
Update federation routing logic to check .well-known before SRV

View File

@ -210,11 +210,7 @@ class MatrixFederationAgent(object):
target_port=parsed_uri.port, target_port=parsed_uri.port,
)) ))
# try a SRV lookup if lookup_well_known:
service_name = b"_matrix._tcp.%s" % (parsed_uri.host,)
server_list = yield self._srv_resolver.resolve_service(service_name)
if not server_list and lookup_well_known:
# try a .well-known lookup # try a .well-known lookup
well_known_server = yield self._get_well_known(parsed_uri.host) well_known_server = yield self._get_well_known(parsed_uri.host)
@ -250,6 +246,10 @@ class MatrixFederationAgent(object):
res = yield self._route_matrix_uri(new_uri, lookup_well_known=False) res = yield self._route_matrix_uri(new_uri, lookup_well_known=False)
defer.returnValue(res) defer.returnValue(res)
# try a SRV lookup
service_name = b"_matrix._tcp.%s" % (parsed_uri.host,)
server_list = yield self._srv_resolver.resolve_service(service_name)
if not server_list: if not server_list:
target_host = parsed_uri.host target_host = parsed_uri.host
port = 8448 port = 8448

View File

@ -358,9 +358,8 @@ class MatrixFederationAgentTests(TestCase):
# Nothing happened yet # Nothing happened yet
self.assertNoResult(test_d) self.assertNoResult(test_d)
self.mock_resolver.resolve_service.assert_called_once_with( # No SRV record lookup yet
b"_matrix._tcp.testserv", self.mock_resolver.resolve_service.assert_not_called()
)
# there should be an attempt to connect on port 443 for the .well-known # there should be an attempt to connect on port 443 for the .well-known
clients = self.reactor.tcpClients clients = self.reactor.tcpClients
@ -376,6 +375,11 @@ class MatrixFederationAgentTests(TestCase):
# .well-known request fails. # .well-known request fails.
self.reactor.pump((0.4,)) self.reactor.pump((0.4,))
# now there should be a SRV lookup
self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.testserv",
)
# we should fall back to a direct connection # we should fall back to a direct connection
self.assertEqual(len(clients), 2) self.assertEqual(len(clients), 2)
(host, port, client_factory, _timeout, _bindAddress) = clients[1] (host, port, client_factory, _timeout, _bindAddress) = clients[1]
@ -403,8 +407,7 @@ class MatrixFederationAgentTests(TestCase):
self.successResultOf(test_d) self.successResultOf(test_d)
def test_get_well_known(self): def test_get_well_known(self):
"""Test the behaviour when the server name has no port and no SRV record, but """Test the behaviour when the .well-known redirects elsewhere
the .well-known redirects elsewhere
""" """
self.mock_resolver.resolve_service.side_effect = lambda _: [] self.mock_resolver.resolve_service.side_effect = lambda _: []
@ -416,11 +419,6 @@ class MatrixFederationAgentTests(TestCase):
# Nothing happened yet # Nothing happened yet
self.assertNoResult(test_d) self.assertNoResult(test_d)
self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.testserv",
)
self.mock_resolver.resolve_service.reset_mock()
# there should be an attempt to connect on port 443 for the .well-known # there should be an attempt to connect on port 443 for the .well-known
clients = self.reactor.tcpClients clients = self.reactor.tcpClients
self.assertEqual(len(clients), 1) self.assertEqual(len(clients), 1)
@ -432,7 +430,7 @@ class MatrixFederationAgentTests(TestCase):
client_factory, expected_sni=b"testserv", target_server=b"target-server", client_factory, expected_sni=b"testserv", target_server=b"target-server",
) )
# there should be another SRV lookup # there should be a SRV lookup
self.mock_resolver.resolve_service.assert_called_once_with( self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.target-server", b"_matrix._tcp.target-server",
) )
@ -483,11 +481,6 @@ class MatrixFederationAgentTests(TestCase):
# Nothing happened yet # Nothing happened yet
self.assertNoResult(test_d) self.assertNoResult(test_d)
self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.testserv",
)
self.mock_resolver.resolve_service.reset_mock()
# there should be an attempt to connect on port 443 for the .well-known # there should be an attempt to connect on port 443 for the .well-known
clients = self.reactor.tcpClients clients = self.reactor.tcpClients
self.assertEqual(len(clients), 1) self.assertEqual(len(clients), 1)
@ -529,7 +522,7 @@ class MatrixFederationAgentTests(TestCase):
self.reactor.pump((0.1, )) self.reactor.pump((0.1, ))
# there should be another SRV lookup # there should be a SRV lookup
self.mock_resolver.resolve_service.assert_called_once_with( self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.target-server", b"_matrix._tcp.target-server",
) )
@ -581,6 +574,7 @@ class MatrixFederationAgentTests(TestCase):
# Nothing happened yet # Nothing happened yet
self.assertNoResult(test_d) self.assertNoResult(test_d)
# the request for a .well-known will have failed with a DNS lookup error.
self.mock_resolver.resolve_service.assert_called_once_with( self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.testserv", b"_matrix._tcp.testserv",
) )
@ -613,11 +607,9 @@ class MatrixFederationAgentTests(TestCase):
self.successResultOf(test_d) self.successResultOf(test_d)
def test_get_well_known_srv(self): def test_get_well_known_srv(self):
"""Test the behaviour when the server name has no port and no SRV record, but """Test the behaviour when the .well-known redirects to a place where there
the .well-known redirects to a place where there is a SRV. is a SRV.
""" """
self.mock_resolver.resolve_service.side_effect = lambda _: []
self.reactor.lookups["testserv"] = "1.2.3.4" self.reactor.lookups["testserv"] = "1.2.3.4"
self.reactor.lookups["srvtarget"] = "5.6.7.8" self.reactor.lookups["srvtarget"] = "5.6.7.8"
@ -626,11 +618,6 @@ class MatrixFederationAgentTests(TestCase):
# Nothing happened yet # Nothing happened yet
self.assertNoResult(test_d) self.assertNoResult(test_d)
self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.testserv",
)
self.mock_resolver.resolve_service.reset_mock()
# there should be an attempt to connect on port 443 for the .well-known # there should be an attempt to connect on port 443 for the .well-known
clients = self.reactor.tcpClients clients = self.reactor.tcpClients
self.assertEqual(len(clients), 1) self.assertEqual(len(clients), 1)
@ -646,7 +633,7 @@ class MatrixFederationAgentTests(TestCase):
client_factory, expected_sni=b"testserv", target_server=b"target-server", client_factory, expected_sni=b"testserv", target_server=b"target-server",
) )
# there should be another SRV lookup # there should be a SRV lookup
self.mock_resolver.resolve_service.assert_called_once_with( self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.target-server", b"_matrix._tcp.target-server",
) )
@ -691,9 +678,8 @@ class MatrixFederationAgentTests(TestCase):
# Nothing happened yet # Nothing happened yet
self.assertNoResult(test_d) self.assertNoResult(test_d)
self.mock_resolver.resolve_service.assert_called_once_with( # No SRV record lookup yet
b"_matrix._tcp.xn--bcher-kva.com", self.mock_resolver.resolve_service.assert_not_called()
)
# there should be an attempt to connect on port 443 for the .well-known # there should be an attempt to connect on port 443 for the .well-known
clients = self.reactor.tcpClients clients = self.reactor.tcpClients
@ -709,6 +695,11 @@ class MatrixFederationAgentTests(TestCase):
# .well-known request fails. # .well-known request fails.
self.reactor.pump((0.4,)) self.reactor.pump((0.4,))
# now there should have been a SRV lookup
self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.xn--bcher-kva.com",
)
# We should fall back to port 8448 # We should fall back to port 8448
clients = self.reactor.tcpClients clients = self.reactor.tcpClients
self.assertEqual(len(clients), 2) self.assertEqual(len(clients), 2)