2019-01-21 18:27:57 -05:00
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
import logging
|
2020-11-25 07:07:21 -05:00
|
|
|
import urllib.parse
|
2021-03-03 15:47:38 -05:00
|
|
|
from typing import Any, Generator, List, Optional
|
2021-08-11 10:34:59 -04:00
|
|
|
from urllib.request import ( # type: ignore[attr-defined]
|
|
|
|
getproxies_environment,
|
|
|
|
proxy_bypass_environment,
|
|
|
|
)
|
2019-01-21 18:27:57 -05:00
|
|
|
|
2020-12-02 11:09:24 -05:00
|
|
|
from netaddr import AddrFormatError, IPAddress, IPSet
|
2019-01-21 18:27:57 -05:00
|
|
|
from zope.interface import implementer
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
|
|
|
from twisted.internet.endpoints import HostnameEndpoint, wrapClientTLS
|
2020-11-25 07:07:21 -05:00
|
|
|
from twisted.internet.interfaces import (
|
2021-12-14 12:35:28 -05:00
|
|
|
IProtocol,
|
2020-11-25 07:07:21 -05:00
|
|
|
IProtocolFactory,
|
|
|
|
IReactorCore,
|
|
|
|
IStreamClientEndpoint,
|
|
|
|
)
|
|
|
|
from twisted.web.client import URI, Agent, HTTPConnectionPool
|
2019-01-25 07:38:16 -05:00
|
|
|
from twisted.web.http_headers import Headers
|
2021-07-22 07:00:16 -04:00
|
|
|
from twisted.web.iweb import IAgent, IAgentEndpointFactory, IBodyProducer, IResponse
|
2019-01-21 18:27:57 -05:00
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
from synapse.crypto.context_factory import FederationPolicyForHTTPS
|
2021-08-11 10:34:59 -04:00
|
|
|
from synapse.http import proxyagent
|
2023-05-19 08:25:25 -04:00
|
|
|
from synapse.http.client import BlocklistingAgentWrapper, BlocklistingReactorWrapper
|
2021-08-11 10:34:59 -04:00
|
|
|
from synapse.http.connectproxyclient import HTTPConnectProxyEndpoint
|
2019-08-08 10:30:04 -04:00
|
|
|
from synapse.http.federation.srv_resolver import Server, SrvResolver
|
2019-08-07 10:36:38 -04:00
|
|
|
from synapse.http.federation.well_known_resolver import WellKnownResolver
|
2021-08-11 10:34:59 -04:00
|
|
|
from synapse.http.proxyagent import ProxyAgent
|
2019-08-23 10:05:56 -04:00
|
|
|
from synapse.logging.context import make_deferred_yieldable, run_in_background
|
2021-03-08 08:25:43 -05:00
|
|
|
from synapse.types import ISynapseReactor
|
2019-01-31 19:36:24 -05:00
|
|
|
from synapse.util import Clock
|
2019-08-06 11:27:46 -04:00
|
|
|
|
2019-01-21 18:27:57 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@implementer(IAgent)
|
2020-09-04 06:54:56 -04:00
|
|
|
class MatrixFederationAgent:
|
2019-08-08 10:30:04 -04:00
|
|
|
"""An Agent-like thing which provides a `request` method which correctly
|
2023-06-20 05:05:31 -04:00
|
|
|
handles resolving matrix server names when using `matrix-federation://`. Handles
|
|
|
|
standard https URIs as normal. The `matrix-federation://` scheme is internal to
|
|
|
|
Synapse and we purposely want to avoid colliding with the `matrix://` URL scheme
|
|
|
|
which is now specced.
|
2019-01-21 18:27:57 -05:00
|
|
|
|
|
|
|
Doesn't implement any retries. (Those are done in MatrixFederationHttpClient.)
|
|
|
|
|
|
|
|
Args:
|
2020-11-25 07:07:21 -05:00
|
|
|
reactor: twisted reactor to use for underlying requests
|
2019-01-22 12:42:26 -05:00
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
tls_client_options_factory:
|
2019-01-21 18:27:57 -05:00
|
|
|
factory to use for fetching client tls options, or none to disable TLS.
|
2019-01-22 12:42:26 -05:00
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
user_agent:
|
2020-06-16 10:43:29 -04:00
|
|
|
The user agent header to use for federation requests.
|
|
|
|
|
2023-05-19 08:25:25 -04:00
|
|
|
ip_allowlist: Allowed IP addresses.
|
2021-08-11 10:34:59 -04:00
|
|
|
|
2023-05-19 08:25:25 -04:00
|
|
|
ip_blocklist: Disallowed IP addresses.
|
2021-08-11 10:34:59 -04:00
|
|
|
|
|
|
|
proxy_reactor: twisted reactor to use for connections to the proxy server
|
2023-05-19 08:25:25 -04:00
|
|
|
reactor might have some blocking applied (i.e. for DNS queries),
|
2021-08-11 10:34:59 -04:00
|
|
|
but we need unblocked access to the proxy.
|
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
_srv_resolver:
|
|
|
|
SrvResolver implementation to use for looking up SRV records. None
|
|
|
|
to use a default implementation.
|
2019-02-28 11:24:01 -05:00
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
_well_known_resolver:
|
2019-08-13 13:04:46 -04:00
|
|
|
WellKnownResolver to use to perform well-known lookups. None to use a
|
|
|
|
default implementation.
|
2019-01-21 18:27:57 -05:00
|
|
|
"""
|
|
|
|
|
2019-01-22 12:42:26 -05:00
|
|
|
def __init__(
|
2019-01-29 08:53:02 -05:00
|
|
|
self,
|
2021-03-08 08:25:43 -05:00
|
|
|
reactor: ISynapseReactor,
|
2020-11-25 07:07:21 -05:00
|
|
|
tls_client_options_factory: Optional[FederationPolicyForHTTPS],
|
|
|
|
user_agent: bytes,
|
2023-05-19 08:25:25 -04:00
|
|
|
ip_allowlist: Optional[IPSet],
|
|
|
|
ip_blocklist: IPSet,
|
2020-11-25 07:07:21 -05:00
|
|
|
_srv_resolver: Optional[SrvResolver] = None,
|
|
|
|
_well_known_resolver: Optional[WellKnownResolver] = None,
|
2019-01-22 12:42:26 -05:00
|
|
|
):
|
2023-05-19 08:25:25 -04:00
|
|
|
# proxy_reactor is not blocklisting reactor
|
2021-08-11 10:34:59 -04:00
|
|
|
proxy_reactor = reactor
|
|
|
|
|
2023-05-19 08:25:25 -04:00
|
|
|
# We need to use a DNS resolver which filters out blocked IP
|
2021-08-11 10:34:59 -04:00
|
|
|
# addresses, to prevent DNS rebinding.
|
2023-05-19 08:25:25 -04:00
|
|
|
reactor = BlocklistingReactorWrapper(reactor, ip_allowlist, ip_blocklist)
|
2021-08-11 10:34:59 -04:00
|
|
|
|
2019-01-31 19:36:24 -05:00
|
|
|
self._clock = Clock(reactor)
|
2019-01-21 18:27:57 -05:00
|
|
|
self._pool = HTTPConnectionPool(reactor)
|
|
|
|
self._pool.retryAutomatically = False
|
|
|
|
self._pool.maxPersistentPerHost = 5
|
|
|
|
self._pool.cachedConnectionTimeout = 2 * 60
|
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
self._agent = Agent.usingEndpointFactory(
|
2021-08-11 10:34:59 -04:00
|
|
|
reactor,
|
2019-08-08 10:30:04 -04:00
|
|
|
MatrixHostnameEndpointFactory(
|
2021-08-11 10:34:59 -04:00
|
|
|
reactor,
|
|
|
|
proxy_reactor,
|
|
|
|
tls_client_options_factory,
|
|
|
|
_srv_resolver,
|
2019-08-08 10:30:04 -04:00
|
|
|
),
|
|
|
|
pool=self._pool,
|
|
|
|
)
|
2020-06-16 10:43:29 -04:00
|
|
|
self.user_agent = user_agent
|
2019-08-08 10:30:04 -04:00
|
|
|
|
2019-08-13 13:04:46 -04:00
|
|
|
if _well_known_resolver is None:
|
|
|
|
_well_known_resolver = WellKnownResolver(
|
2021-08-11 10:34:59 -04:00
|
|
|
reactor,
|
2023-05-19 08:25:25 -04:00
|
|
|
agent=BlocklistingAgentWrapper(
|
2021-08-11 10:34:59 -04:00
|
|
|
ProxyAgent(
|
|
|
|
reactor,
|
|
|
|
proxy_reactor,
|
2020-12-02 11:09:24 -05:00
|
|
|
pool=self._pool,
|
|
|
|
contextFactory=tls_client_options_factory,
|
2021-08-11 10:34:59 -04:00
|
|
|
use_proxy=True,
|
2020-12-02 11:09:24 -05:00
|
|
|
),
|
2023-05-19 08:25:25 -04:00
|
|
|
ip_blocklist=ip_blocklist,
|
2019-08-13 13:04:46 -04:00
|
|
|
),
|
2020-06-16 10:43:29 -04:00
|
|
|
user_agent=self.user_agent,
|
2019-08-13 13:04:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
self._well_known_resolver = _well_known_resolver
|
2019-01-30 05:55:25 -05:00
|
|
|
|
2019-01-21 18:27:57 -05:00
|
|
|
@defer.inlineCallbacks
|
2020-11-25 07:07:21 -05:00
|
|
|
def request(
|
|
|
|
self,
|
|
|
|
method: bytes,
|
|
|
|
uri: bytes,
|
|
|
|
headers: Optional[Headers] = None,
|
|
|
|
bodyProducer: Optional[IBodyProducer] = None,
|
2021-07-22 07:00:16 -04:00
|
|
|
) -> Generator[defer.Deferred, Any, IResponse]:
|
2019-01-21 18:27:57 -05:00
|
|
|
"""
|
|
|
|
Args:
|
2020-11-25 07:07:21 -05:00
|
|
|
method: HTTP method: GET/POST/etc
|
|
|
|
uri: Absolute URI to be retrieved
|
|
|
|
headers:
|
|
|
|
HTTP headers to send with the request, or None to send no extra headers.
|
|
|
|
bodyProducer:
|
2019-01-21 18:27:57 -05:00
|
|
|
An object which can generate bytes to make up the
|
|
|
|
body of this request (for example, the properly encoded contents of
|
|
|
|
a file for a file upload). Or None if the request is to have
|
|
|
|
no body.
|
|
|
|
Returns:
|
2022-11-16 10:25:24 -05:00
|
|
|
A deferred which fires when the header of the response has been received
|
|
|
|
(regardless of the response status code). Fails if there is any problem
|
|
|
|
which prevents that response from being received (including problems that
|
|
|
|
prevent the request from being sent).
|
2019-01-21 18:27:57 -05:00
|
|
|
"""
|
2019-08-08 10:30:04 -04:00
|
|
|
# We use urlparse as that will set `port` to None if there is no
|
|
|
|
# explicit port.
|
|
|
|
parsed_uri = urllib.parse.urlparse(uri)
|
2019-01-21 18:27:57 -05:00
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
# There must be a valid hostname.
|
|
|
|
assert parsed_uri.hostname
|
|
|
|
|
2023-06-20 05:05:31 -04:00
|
|
|
# If this is a matrix-federation:// URI check if the server has delegated matrix
|
2019-08-08 10:30:04 -04:00
|
|
|
# traffic using well-known delegation.
|
2019-01-27 18:24:17 -05:00
|
|
|
#
|
2019-08-08 10:30:04 -04:00
|
|
|
# We have to do this here and not in the endpoint as we need to rewrite
|
|
|
|
# the host header with the delegated server name.
|
|
|
|
delegated_server = None
|
|
|
|
if (
|
2023-06-20 05:05:31 -04:00
|
|
|
parsed_uri.scheme == b"matrix-federation"
|
2019-08-08 10:30:04 -04:00
|
|
|
and not _is_ip_literal(parsed_uri.hostname)
|
|
|
|
and not parsed_uri.port
|
|
|
|
):
|
2020-09-01 09:15:22 -04:00
|
|
|
well_known_result = yield defer.ensureDeferred(
|
|
|
|
self._well_known_resolver.get_well_known(parsed_uri.hostname)
|
2019-08-08 10:30:04 -04:00
|
|
|
)
|
|
|
|
delegated_server = well_known_result.delegated_server
|
|
|
|
|
|
|
|
if delegated_server:
|
|
|
|
# Ok, the server has delegated matrix traffic to somewhere else, so
|
|
|
|
# lets rewrite the URL to replace the server with the delegated
|
|
|
|
# server name.
|
|
|
|
uri = urllib.parse.urlunparse(
|
|
|
|
(
|
|
|
|
parsed_uri.scheme,
|
|
|
|
delegated_server,
|
|
|
|
parsed_uri.path,
|
|
|
|
parsed_uri.params,
|
|
|
|
parsed_uri.query,
|
|
|
|
parsed_uri.fragment,
|
|
|
|
)
|
2019-01-28 04:56:59 -05:00
|
|
|
)
|
2019-08-08 10:30:04 -04:00
|
|
|
parsed_uri = urllib.parse.urlparse(uri)
|
2019-01-21 18:27:57 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
# We need to make sure the host header is set to the netloc of the
|
2020-06-16 10:43:29 -04:00
|
|
|
# server and that a user-agent is provided.
|
2019-01-25 07:38:16 -05:00
|
|
|
if headers is None:
|
2021-03-03 15:47:38 -05:00
|
|
|
request_headers = Headers()
|
2019-01-25 07:38:16 -05:00
|
|
|
else:
|
2021-03-03 15:47:38 -05:00
|
|
|
request_headers = headers.copy()
|
2019-01-25 07:38:16 -05:00
|
|
|
|
2021-03-03 15:47:38 -05:00
|
|
|
if not request_headers.hasHeader(b"host"):
|
|
|
|
request_headers.addRawHeader(b"host", parsed_uri.netloc)
|
|
|
|
if not request_headers.hasHeader(b"user-agent"):
|
|
|
|
request_headers.addRawHeader(b"user-agent", self.user_agent)
|
2019-01-25 07:38:16 -05:00
|
|
|
|
2019-08-23 10:05:56 -04:00
|
|
|
res = yield make_deferred_yieldable(
|
2021-03-03 15:47:38 -05:00
|
|
|
self._agent.request(method, uri, request_headers, bodyProducer)
|
2019-08-23 10:05:56 -04:00
|
|
|
)
|
2019-01-21 18:27:57 -05:00
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return res
|
2019-01-27 18:24:17 -05:00
|
|
|
|
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
@implementer(IAgentEndpointFactory)
|
2020-09-04 06:54:56 -04:00
|
|
|
class MatrixHostnameEndpointFactory:
|
2019-08-08 10:30:04 -04:00
|
|
|
"""Factory for MatrixHostnameEndpoint for parsing to an Agent."""
|
2019-01-27 18:24:17 -05:00
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
reactor: IReactorCore,
|
2021-08-11 10:34:59 -04:00
|
|
|
proxy_reactor: IReactorCore,
|
2020-11-25 07:07:21 -05:00
|
|
|
tls_client_options_factory: Optional[FederationPolicyForHTTPS],
|
|
|
|
srv_resolver: Optional[SrvResolver],
|
|
|
|
):
|
2019-08-08 10:30:04 -04:00
|
|
|
self._reactor = reactor
|
2021-08-11 10:34:59 -04:00
|
|
|
self._proxy_reactor = proxy_reactor
|
2019-08-08 10:30:04 -04:00
|
|
|
self._tls_client_options_factory = tls_client_options_factory
|
2019-01-29 08:53:02 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
if srv_resolver is None:
|
|
|
|
srv_resolver = SrvResolver()
|
2019-01-26 16:48:50 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
self._srv_resolver = srv_resolver
|
2019-01-27 18:24:17 -05:00
|
|
|
|
2022-05-12 10:33:50 -04:00
|
|
|
def endpointForURI(self, parsed_uri: URI) -> "MatrixHostnameEndpoint":
|
2019-08-08 10:30:04 -04:00
|
|
|
return MatrixHostnameEndpoint(
|
|
|
|
self._reactor,
|
2021-08-11 10:34:59 -04:00
|
|
|
self._proxy_reactor,
|
2019-08-08 10:30:04 -04:00
|
|
|
self._tls_client_options_factory,
|
|
|
|
self._srv_resolver,
|
|
|
|
parsed_uri,
|
|
|
|
)
|
2019-01-29 08:53:02 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
|
|
|
|
@implementer(IStreamClientEndpoint)
|
2020-09-04 06:54:56 -04:00
|
|
|
class MatrixHostnameEndpoint:
|
2023-06-20 05:05:31 -04:00
|
|
|
"""An endpoint that resolves matrix-federation:// URLs using Matrix server name
|
2019-08-08 10:30:04 -04:00
|
|
|
resolution (i.e. via SRV). Does not check for well-known delegation.
|
2019-08-23 10:09:08 -04:00
|
|
|
|
|
|
|
Args:
|
2020-11-25 07:07:21 -05:00
|
|
|
reactor: twisted reactor to use for underlying requests
|
2021-08-11 10:34:59 -04:00
|
|
|
proxy_reactor: twisted reactor to use for connections to the proxy server.
|
2023-05-19 08:25:25 -04:00
|
|
|
'reactor' might have some blocking applied (i.e. for DNS queries),
|
2021-08-11 10:34:59 -04:00
|
|
|
but we need unblocked access to the proxy.
|
2020-11-25 07:07:21 -05:00
|
|
|
tls_client_options_factory:
|
2019-08-23 10:09:08 -04:00
|
|
|
factory to use for fetching client tls options, or none to disable TLS.
|
2020-11-25 07:07:21 -05:00
|
|
|
srv_resolver: The SRV resolver to use
|
|
|
|
parsed_uri: The parsed URI that we're wanting to connect to.
|
2021-08-11 10:34:59 -04:00
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError if the environment variables contain an invalid proxy specification.
|
|
|
|
RuntimeError if no tls_options_factory is given for a https connection
|
2019-08-08 10:30:04 -04:00
|
|
|
"""
|
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
reactor: IReactorCore,
|
2021-08-11 10:34:59 -04:00
|
|
|
proxy_reactor: IReactorCore,
|
2020-11-25 07:07:21 -05:00
|
|
|
tls_client_options_factory: Optional[FederationPolicyForHTTPS],
|
|
|
|
srv_resolver: SrvResolver,
|
|
|
|
parsed_uri: URI,
|
|
|
|
):
|
2019-08-08 10:30:04 -04:00
|
|
|
self._reactor = reactor
|
|
|
|
self._parsed_uri = parsed_uri
|
|
|
|
|
2021-08-11 10:34:59 -04:00
|
|
|
# http_proxy is not needed because federation is always over TLS
|
|
|
|
proxies = getproxies_environment()
|
|
|
|
https_proxy = proxies["https"].encode() if "https" in proxies else None
|
|
|
|
self.no_proxy = proxies["no"] if "no" in proxies else None
|
|
|
|
|
|
|
|
# endpoint and credentials to use to connect to the outbound https proxy, if any.
|
|
|
|
(
|
|
|
|
self._https_proxy_endpoint,
|
|
|
|
self._https_proxy_creds,
|
|
|
|
) = proxyagent.http_proxy_endpoint(
|
|
|
|
https_proxy,
|
|
|
|
proxy_reactor,
|
|
|
|
tls_client_options_factory,
|
|
|
|
)
|
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
# set up the TLS connection params
|
|
|
|
#
|
|
|
|
# XXX disabling TLS is really only supported here for the benefit of the
|
|
|
|
# unit tests. We should make the UTs cope with TLS rather than having to make
|
|
|
|
# the code support the unit tests.
|
|
|
|
|
|
|
|
if tls_client_options_factory is None:
|
|
|
|
self._tls_options = None
|
2019-01-27 18:24:17 -05:00
|
|
|
else:
|
2019-08-08 10:30:04 -04:00
|
|
|
self._tls_options = tls_client_options_factory.get_options(
|
2019-09-13 14:58:38 -04:00
|
|
|
self._parsed_uri.host
|
2019-01-27 18:24:17 -05:00
|
|
|
)
|
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
self._srv_resolver = srv_resolver
|
2019-01-27 18:24:17 -05:00
|
|
|
|
2021-12-14 12:35:28 -05:00
|
|
|
def connect(
|
|
|
|
self, protocol_factory: IProtocolFactory
|
|
|
|
) -> "defer.Deferred[IProtocol]":
|
2019-08-08 10:30:04 -04:00
|
|
|
"""Implements IStreamClientEndpoint interface"""
|
2019-01-30 05:55:25 -05:00
|
|
|
|
2019-08-23 10:05:56 -04:00
|
|
|
return run_in_background(self._do_connect, protocol_factory)
|
|
|
|
|
2021-12-14 12:35:28 -05:00
|
|
|
async def _do_connect(self, protocol_factory: IProtocolFactory) -> IProtocol:
|
2019-08-08 10:30:04 -04:00
|
|
|
first_exception = None
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-07-23 07:05:57 -04:00
|
|
|
server_list = await self._resolve_server()
|
2019-01-31 18:18:20 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
for server in server_list:
|
|
|
|
host = server.host
|
|
|
|
port = server.port
|
2019-01-31 18:18:20 -05:00
|
|
|
|
2021-08-11 10:34:59 -04:00
|
|
|
should_skip_proxy = False
|
|
|
|
if self.no_proxy is not None:
|
|
|
|
should_skip_proxy = proxy_bypass_environment(
|
|
|
|
host.decode(),
|
|
|
|
proxies={"no": self.no_proxy},
|
|
|
|
)
|
|
|
|
|
|
|
|
endpoint: IStreamClientEndpoint
|
2019-08-08 10:30:04 -04:00
|
|
|
try:
|
2021-08-11 10:34:59 -04:00
|
|
|
if self._https_proxy_endpoint and not should_skip_proxy:
|
|
|
|
logger.debug(
|
|
|
|
"Connecting to %s:%i via %s",
|
|
|
|
host.decode("ascii"),
|
|
|
|
port,
|
|
|
|
self._https_proxy_endpoint,
|
|
|
|
)
|
|
|
|
endpoint = HTTPConnectProxyEndpoint(
|
|
|
|
self._reactor,
|
|
|
|
self._https_proxy_endpoint,
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
proxy_creds=self._https_proxy_creds,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
logger.debug("Connecting to %s:%i", host.decode("ascii"), port)
|
|
|
|
# not using a proxy
|
|
|
|
endpoint = HostnameEndpoint(self._reactor, host, port)
|
2019-08-08 10:30:04 -04:00
|
|
|
if self._tls_options:
|
|
|
|
endpoint = wrapClientTLS(self._tls_options, endpoint)
|
2020-07-23 07:05:57 -04:00
|
|
|
result = await make_deferred_yieldable(
|
2019-08-08 10:30:04 -04:00
|
|
|
endpoint.connect(protocol_factory)
|
|
|
|
)
|
2019-01-31 18:18:20 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
return result
|
|
|
|
except Exception as e:
|
|
|
|
logger.info(
|
|
|
|
"Failed to connect to %s:%i: %s", host.decode("ascii"), port, e
|
|
|
|
)
|
|
|
|
if not first_exception:
|
|
|
|
first_exception = e
|
2019-01-28 07:43:09 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
# We return the first failure because that's probably the most interesting.
|
|
|
|
if first_exception:
|
|
|
|
raise first_exception
|
2019-01-28 07:43:09 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
# This shouldn't happen as we should always have at least one host/port
|
|
|
|
# to try and if that doesn't work then we'll have an exception.
|
|
|
|
raise Exception("Failed to resolve server %r" % (self._parsed_uri.netloc,))
|
2019-01-28 07:43:09 -05:00
|
|
|
|
2020-07-23 07:05:57 -04:00
|
|
|
async def _resolve_server(self) -> List[Server]:
|
2019-08-08 10:30:04 -04:00
|
|
|
"""Resolves the server name to a list of hosts and ports to attempt to
|
|
|
|
connect to.
|
|
|
|
"""
|
2019-01-28 07:43:09 -05:00
|
|
|
|
2023-06-20 05:05:31 -04:00
|
|
|
if self._parsed_uri.scheme != b"matrix-federation":
|
2019-08-08 10:30:04 -04:00
|
|
|
return [Server(host=self._parsed_uri.host, port=self._parsed_uri.port)]
|
2019-01-28 07:43:09 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
# Note: We don't do well-known lookup as that needs to have happened
|
|
|
|
# before now, due to needing to rewrite the Host header of the HTTP
|
|
|
|
# request.
|
2019-01-28 07:43:09 -05:00
|
|
|
|
2019-08-23 10:09:08 -04:00
|
|
|
# We reparse the URI so that defaultPort is -1 rather than 80
|
2019-08-08 10:30:04 -04:00
|
|
|
parsed_uri = urllib.parse.urlparse(self._parsed_uri.toBytes())
|
2019-01-28 07:43:09 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
host = parsed_uri.hostname
|
|
|
|
port = parsed_uri.port
|
2019-01-28 07:43:09 -05:00
|
|
|
|
2019-08-08 10:30:04 -04:00
|
|
|
# If there is an explicit port or the host is an IP address we bypass
|
|
|
|
# SRV lookups and just use the given host/port.
|
|
|
|
if port or _is_ip_literal(host):
|
|
|
|
return [Server(host, port or 8448)]
|
2019-01-28 07:43:09 -05:00
|
|
|
|
2021-02-03 16:47:30 -05:00
|
|
|
logger.debug("Looking up SRV record for %s", host.decode(errors="replace"))
|
2020-07-23 07:05:57 -04:00
|
|
|
server_list = await self._srv_resolver.resolve_service(b"_matrix._tcp." + host)
|
2019-08-08 10:30:04 -04:00
|
|
|
|
|
|
|
if server_list:
|
2021-02-03 16:47:30 -05:00
|
|
|
logger.debug(
|
|
|
|
"Got %s from SRV lookup for %s",
|
|
|
|
", ".join(map(str, server_list)),
|
|
|
|
host.decode(errors="replace"),
|
|
|
|
)
|
2019-08-08 10:30:04 -04:00
|
|
|
return server_list
|
|
|
|
|
|
|
|
# No SRV records, so we fallback to host and 8448
|
2021-02-03 16:47:30 -05:00
|
|
|
logger.debug("No SRV records for %s", host.decode(errors="replace"))
|
2019-08-08 10:30:04 -04:00
|
|
|
return [Server(host, 8448)]
|
|
|
|
|
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
def _is_ip_literal(host: bytes) -> bool:
|
2019-08-08 10:30:04 -04:00
|
|
|
"""Test if the given host name is either an IPv4 or IPv6 literal.
|
|
|
|
|
|
|
|
Args:
|
2020-11-25 07:07:21 -05:00
|
|
|
host: The host name to check
|
2019-08-08 10:30:04 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-11-25 07:07:21 -05:00
|
|
|
True if the hostname is an IP address literal.
|
2019-01-28 07:43:09 -05:00
|
|
|
"""
|
2019-08-08 10:30:04 -04:00
|
|
|
|
2020-11-25 07:07:21 -05:00
|
|
|
host_str = host.decode("ascii")
|
2019-08-08 10:30:04 -04:00
|
|
|
|
|
|
|
try:
|
2020-11-25 07:07:21 -05:00
|
|
|
IPAddress(host_str)
|
2019-08-08 10:30:04 -04:00
|
|
|
return True
|
|
|
|
except AddrFormatError:
|
|
|
|
return False
|