2023-05-09 12:25:20 -06:00
|
|
|
# Copyright 2023 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# 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
|
2023-05-23 07:05:30 -06:00
|
|
|
from typing import Dict, Optional
|
2023-05-09 12:25:20 -06:00
|
|
|
|
|
|
|
from zope.interface import implementer
|
|
|
|
|
|
|
|
from twisted.internet import defer
|
2023-07-11 12:08:06 -06:00
|
|
|
from twisted.internet.endpoints import (
|
|
|
|
HostnameEndpoint,
|
|
|
|
UNIXClientEndpoint,
|
|
|
|
wrapClientTLS,
|
|
|
|
)
|
2023-05-09 12:25:20 -06:00
|
|
|
from twisted.internet.interfaces import IStreamClientEndpoint
|
|
|
|
from twisted.python.failure import Failure
|
|
|
|
from twisted.web.client import URI, HTTPConnectionPool, _AgentBase
|
|
|
|
from twisted.web.error import SchemeNotSupported
|
|
|
|
from twisted.web.http_headers import Headers
|
|
|
|
from twisted.web.iweb import (
|
|
|
|
IAgent,
|
|
|
|
IAgentEndpointFactory,
|
|
|
|
IBodyProducer,
|
|
|
|
IPolicyForHTTPS,
|
|
|
|
IResponse,
|
|
|
|
)
|
|
|
|
|
2023-07-11 12:08:06 -06:00
|
|
|
from synapse.config.workers import (
|
|
|
|
InstanceLocationConfig,
|
|
|
|
InstanceTcpLocationConfig,
|
|
|
|
InstanceUnixLocationConfig,
|
|
|
|
)
|
2023-05-09 12:25:20 -06:00
|
|
|
from synapse.types import ISynapseReactor
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@implementer(IAgentEndpointFactory)
|
|
|
|
class ReplicationEndpointFactory:
|
2023-07-11 12:08:06 -06:00
|
|
|
"""Connect to a given TCP or UNIX socket"""
|
2023-05-09 12:25:20 -06:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
reactor: ISynapseReactor,
|
2023-05-23 07:05:30 -06:00
|
|
|
instance_map: Dict[str, InstanceLocationConfig],
|
2023-05-09 12:25:20 -06:00
|
|
|
context_factory: IPolicyForHTTPS,
|
|
|
|
) -> None:
|
|
|
|
self.reactor = reactor
|
2023-05-23 07:05:30 -06:00
|
|
|
self.instance_map = instance_map
|
2023-05-09 12:25:20 -06:00
|
|
|
self.context_factory = context_factory
|
|
|
|
|
|
|
|
def endpointForURI(self, uri: URI) -> IStreamClientEndpoint:
|
|
|
|
"""
|
|
|
|
This part of the factory decides what kind of endpoint is being connected to.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
uri: The pre-parsed URI object containing all the uri data
|
|
|
|
|
|
|
|
Returns: The correct client endpoint object
|
|
|
|
"""
|
2023-05-23 07:05:30 -06:00
|
|
|
# The given URI has a special scheme and includes the worker name. The
|
|
|
|
# actual connection details are pulled from the instance map.
|
|
|
|
worker_name = uri.netloc.decode("utf-8")
|
2023-07-11 12:08:06 -06:00
|
|
|
location_config = self.instance_map[worker_name]
|
|
|
|
scheme = location_config.scheme()
|
2023-05-23 07:05:30 -06:00
|
|
|
|
2023-07-11 12:08:06 -06:00
|
|
|
if isinstance(location_config, InstanceTcpLocationConfig):
|
2023-05-23 07:05:30 -06:00
|
|
|
endpoint = HostnameEndpoint(
|
|
|
|
self.reactor,
|
2023-07-11 12:08:06 -06:00
|
|
|
location_config.host,
|
|
|
|
location_config.port,
|
2023-05-23 07:05:30 -06:00
|
|
|
)
|
|
|
|
if scheme == "https":
|
2023-05-09 12:25:20 -06:00
|
|
|
endpoint = wrapClientTLS(
|
2023-05-23 07:05:30 -06:00
|
|
|
# The 'port' argument below isn't actually used by the function
|
|
|
|
self.context_factory.creatorForNetloc(
|
2023-07-11 12:08:06 -06:00
|
|
|
location_config.host.encode("utf-8"),
|
|
|
|
location_config.port,
|
2023-05-23 07:05:30 -06:00
|
|
|
),
|
|
|
|
endpoint,
|
2023-05-09 12:25:20 -06:00
|
|
|
)
|
|
|
|
return endpoint
|
2023-07-11 12:08:06 -06:00
|
|
|
elif isinstance(location_config, InstanceUnixLocationConfig):
|
|
|
|
return UNIXClientEndpoint(self.reactor, location_config.path)
|
2023-05-09 12:25:20 -06:00
|
|
|
else:
|
2023-05-23 07:05:30 -06:00
|
|
|
raise SchemeNotSupported(f"Unsupported scheme: {scheme}")
|
2023-05-09 12:25:20 -06:00
|
|
|
|
|
|
|
|
|
|
|
@implementer(IAgent)
|
|
|
|
class ReplicationAgent(_AgentBase):
|
|
|
|
"""
|
|
|
|
Client for connecting to replication endpoints via HTTP and HTTPS.
|
|
|
|
|
|
|
|
Much of this code is copied from Twisted's twisted.web.client.Agent.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
reactor: ISynapseReactor,
|
2023-05-23 07:05:30 -06:00
|
|
|
instance_map: Dict[str, InstanceLocationConfig],
|
2023-05-09 12:25:20 -06:00
|
|
|
contextFactory: IPolicyForHTTPS,
|
|
|
|
connectTimeout: Optional[float] = None,
|
|
|
|
bindAddress: Optional[bytes] = None,
|
|
|
|
pool: Optional[HTTPConnectionPool] = None,
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Create a ReplicationAgent.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
reactor: A reactor for this Agent to place outgoing connections.
|
|
|
|
contextFactory: A factory for TLS contexts, to control the
|
|
|
|
verification parameters of OpenSSL. The default is to use a
|
|
|
|
BrowserLikePolicyForHTTPS, so unless you have special
|
|
|
|
requirements you can leave this as-is.
|
|
|
|
connectTimeout: The amount of time that this Agent will wait
|
|
|
|
for the peer to accept a connection.
|
|
|
|
bindAddress: The local address for client sockets to bind to.
|
|
|
|
pool: An HTTPConnectionPool instance, or None, in which
|
|
|
|
case a non-persistent HTTPConnectionPool instance will be
|
|
|
|
created.
|
|
|
|
"""
|
|
|
|
_AgentBase.__init__(self, reactor, pool)
|
2023-05-23 07:05:30 -06:00
|
|
|
endpoint_factory = ReplicationEndpointFactory(
|
|
|
|
reactor, instance_map, contextFactory
|
|
|
|
)
|
2023-05-09 12:25:20 -06:00
|
|
|
self._endpointFactory = endpoint_factory
|
|
|
|
|
|
|
|
def request(
|
|
|
|
self,
|
|
|
|
method: bytes,
|
|
|
|
uri: bytes,
|
|
|
|
headers: Optional[Headers] = None,
|
|
|
|
bodyProducer: Optional[IBodyProducer] = None,
|
|
|
|
) -> "defer.Deferred[IResponse]":
|
|
|
|
"""
|
|
|
|
Issue a request to the server indicated by the given uri.
|
|
|
|
|
|
|
|
An existing connection from the connection pool may be used or a new
|
|
|
|
one may be created.
|
|
|
|
|
2023-07-11 12:08:06 -06:00
|
|
|
Currently, HTTP, HTTPS and UNIX schemes are supported in uri.
|
2023-05-09 12:25:20 -06:00
|
|
|
|
|
|
|
This is copied from twisted.web.client.Agent, except:
|
|
|
|
|
2023-07-11 12:08:06 -06:00
|
|
|
* It uses a different pool key (combining the scheme with either host & port or
|
|
|
|
socket path).
|
|
|
|
* It does not call _ensureValidURI(...) as the strictness of IDNA2008 is not
|
|
|
|
required when using a worker's name as a 'hostname' for Synapse HTTP
|
|
|
|
Replication machinery. Specifically, this allows a range of ascii characters
|
|
|
|
such as '+' and '_' in hostnames/worker's names.
|
2023-05-09 12:25:20 -06:00
|
|
|
|
|
|
|
See: twisted.web.iweb.IAgent.request
|
|
|
|
"""
|
|
|
|
parsedURI = URI.fromBytes(uri)
|
|
|
|
try:
|
|
|
|
endpoint = self._endpointFactory.endpointForURI(parsedURI)
|
|
|
|
except SchemeNotSupported:
|
|
|
|
return defer.fail(Failure())
|
|
|
|
|
2023-07-11 12:08:06 -06:00
|
|
|
worker_name = parsedURI.netloc.decode("utf-8")
|
|
|
|
key_scheme = self._endpointFactory.instance_map[worker_name].scheme()
|
|
|
|
key_netloc = self._endpointFactory.instance_map[worker_name].netloc()
|
2023-05-09 12:25:20 -06:00
|
|
|
# This sets the Pool key to be:
|
2023-07-11 12:08:06 -06:00
|
|
|
# (http(s), <host:port>) or (unix, <socket_path>)
|
|
|
|
key = (key_scheme, key_netloc)
|
2023-05-09 12:25:20 -06:00
|
|
|
|
|
|
|
# _requestWithEndpoint comes from _AgentBase class
|
|
|
|
return self._requestWithEndpoint(
|
|
|
|
key,
|
|
|
|
endpoint,
|
|
|
|
method,
|
|
|
|
parsedURI,
|
|
|
|
headers,
|
|
|
|
bodyProducer,
|
|
|
|
parsedURI.originForm,
|
|
|
|
)
|