Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Handle IP literals explicitly
Browse files Browse the repository at this point in the history
We don't want to be doing .well-known lookups on these guys.
  • Loading branch information
richvdh committed Jan 28, 2019
1 parent 51958df commit 0fd5b3b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
19 changes: 19 additions & 0 deletions synapse/http/federation/matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import logging

import attr
from netaddr import IPAddress
from zope.interface import implementer

from twisted.internet import defer
Expand Down Expand Up @@ -137,6 +138,24 @@ def _route_matrix_uri(self, parsed_uri):
Returns:
Deferred[_RoutingResult]
"""
# check for an IP literal
try:
ip_address = IPAddress(parsed_uri.host.decode("ascii"))
except Exception:
# not an IP address
ip_address = None

if ip_address:
port = parsed_uri.port
if port == -1:
port = 8448
defer.returnValue(_RoutingResult(
host_header=parsed_uri.netloc,
tls_server_name=parsed_uri.host,
target_host=parsed_uri.host,
target_port=port,
))

if parsed_uri.port != -1:
# there is an explicit port
defer.returnValue(_RoutingResult(
Expand Down
19 changes: 2 additions & 17 deletions tests/http/federation/test_matrix_federation_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,14 @@ def test_get_ip_address(self):
"""
Test the behaviour when the server name contains an explicit IP (with no port)
"""

# the SRV lookup will return an empty list (XXX: why do we even do an SRV lookup?)
self.mock_resolver.resolve_service.side_effect = lambda _: []

# then there will be a getaddrinfo on the IP
# there will be a getaddrinfo on the IP
self.reactor.lookups["1.2.3.4"] = "1.2.3.4"

test_d = self._make_get_request(b"matrix://1.2.3.4/foo/bar")

# Nothing happened yet
self.assertNoResult(test_d)

self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.1.2.3.4",
)

# Make sure treq is trying to connect
clients = self.reactor.tcpClients
self.assertEqual(len(clients), 1)
Expand Down Expand Up @@ -215,21 +207,14 @@ def test_get_ipv6_address(self):
(with no port)
"""

# the SRV lookup will return an empty list (XXX: why do we even do an SRV lookup?)
self.mock_resolver.resolve_service.side_effect = lambda _: []

# then there will be a getaddrinfo on the IP
# there will be a getaddrinfo on the IP
self.reactor.lookups["::1"] = "::1"

test_d = self._make_get_request(b"matrix://[::1]/foo/bar")

# Nothing happened yet
self.assertNoResult(test_d)

self.mock_resolver.resolve_service.assert_called_once_with(
b"_matrix._tcp.::1",
)

# Make sure treq is trying to connect
clients = self.reactor.tcpClients
self.assertEqual(len(clients), 1)
Expand Down

0 comments on commit 0fd5b3b

Please sign in to comment.