mirror of
https://github.com/element-hq/synapse.git
synced 2024-12-15 17:51:10 +00:00
Create a SynapseReactor type which incorporates the necessary reactor interfaces. (#9528)
This helps fix some type hints when running with Twisted 21.2.0.
This commit is contained in:
parent
0fc4eb103a
commit
58114f8a17
8 changed files with 32 additions and 12 deletions
1
changelog.d/9528.misc
Normal file
1
changelog.d/9528.misc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Fix incorrect type hints.
|
|
@ -73,7 +73,9 @@ class AcmeHandler:
|
||||||
"Listening for ACME requests on %s:%i", host, self.hs.config.acme_port
|
"Listening for ACME requests on %s:%i", host, self.hs.config.acme_port
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
self.reactor.listenTCP(self.hs.config.acme_port, srv, interface=host)
|
self.reactor.listenTCP(
|
||||||
|
self.hs.config.acme_port, srv, backlog=50, interface=host
|
||||||
|
)
|
||||||
except twisted.internet.error.CannotListenError as e:
|
except twisted.internet.error.CannotListenError as e:
|
||||||
check_bind_error(e, host, bind_addresses)
|
check_bind_error(e, host, bind_addresses)
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,7 @@ from synapse.http import QuieterFileBodyProducer, RequestTimedOutError, redact_u
|
||||||
from synapse.http.proxyagent import ProxyAgent
|
from synapse.http.proxyagent import ProxyAgent
|
||||||
from synapse.logging.context import make_deferred_yieldable
|
from synapse.logging.context import make_deferred_yieldable
|
||||||
from synapse.logging.opentracing import set_tag, start_active_span, tags
|
from synapse.logging.opentracing import set_tag, start_active_span, tags
|
||||||
|
from synapse.types import ISynapseReactor
|
||||||
from synapse.util import json_decoder
|
from synapse.util import json_decoder
|
||||||
from synapse.util.async_helpers import timeout_deferred
|
from synapse.util.async_helpers import timeout_deferred
|
||||||
|
|
||||||
|
@ -199,7 +200,7 @@ class _IPBlacklistingResolver:
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
@implementer(IReactorPluggableNameResolver)
|
@implementer(ISynapseReactor)
|
||||||
class BlacklistingReactorWrapper:
|
class BlacklistingReactorWrapper:
|
||||||
"""
|
"""
|
||||||
A Reactor wrapper which will prevent DNS resolution to blacklisted IP
|
A Reactor wrapper which will prevent DNS resolution to blacklisted IP
|
||||||
|
@ -324,7 +325,7 @@ class SimpleHttpClient:
|
||||||
# filters out blacklisted IP addresses, to prevent DNS rebinding.
|
# filters out blacklisted IP addresses, to prevent DNS rebinding.
|
||||||
self.reactor = BlacklistingReactorWrapper(
|
self.reactor = BlacklistingReactorWrapper(
|
||||||
hs.get_reactor(), self._ip_whitelist, self._ip_blacklist
|
hs.get_reactor(), self._ip_whitelist, self._ip_blacklist
|
||||||
)
|
) # type: ISynapseReactor
|
||||||
else:
|
else:
|
||||||
self.reactor = hs.get_reactor()
|
self.reactor = hs.get_reactor()
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@ from synapse.http.client import BlacklistingAgentWrapper
|
||||||
from synapse.http.federation.srv_resolver import Server, SrvResolver
|
from synapse.http.federation.srv_resolver import Server, SrvResolver
|
||||||
from synapse.http.federation.well_known_resolver import WellKnownResolver
|
from synapse.http.federation.well_known_resolver import WellKnownResolver
|
||||||
from synapse.logging.context import make_deferred_yieldable, run_in_background
|
from synapse.logging.context import make_deferred_yieldable, run_in_background
|
||||||
|
from synapse.types import ISynapseReactor
|
||||||
from synapse.util import Clock
|
from synapse.util import Clock
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -68,7 +69,7 @@ class MatrixFederationAgent:
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
reactor: IReactorCore,
|
reactor: ISynapseReactor,
|
||||||
tls_client_options_factory: Optional[FederationPolicyForHTTPS],
|
tls_client_options_factory: Optional[FederationPolicyForHTTPS],
|
||||||
user_agent: bytes,
|
user_agent: bytes,
|
||||||
ip_blacklist: IPSet,
|
ip_blacklist: IPSet,
|
||||||
|
|
|
@ -59,7 +59,7 @@ from synapse.logging.opentracing import (
|
||||||
start_active_span,
|
start_active_span,
|
||||||
tags,
|
tags,
|
||||||
)
|
)
|
||||||
from synapse.types import JsonDict
|
from synapse.types import ISynapseReactor, JsonDict
|
||||||
from synapse.util import json_decoder
|
from synapse.util import json_decoder
|
||||||
from synapse.util.async_helpers import timeout_deferred
|
from synapse.util.async_helpers import timeout_deferred
|
||||||
from synapse.util.metrics import Measure
|
from synapse.util.metrics import Measure
|
||||||
|
@ -237,14 +237,14 @@ class MatrixFederationHttpClient:
|
||||||
# addresses, to prevent DNS rebinding.
|
# addresses, to prevent DNS rebinding.
|
||||||
self.reactor = BlacklistingReactorWrapper(
|
self.reactor = BlacklistingReactorWrapper(
|
||||||
hs.get_reactor(), None, hs.config.federation_ip_range_blacklist
|
hs.get_reactor(), None, hs.config.federation_ip_range_blacklist
|
||||||
)
|
) # type: ISynapseReactor
|
||||||
|
|
||||||
user_agent = hs.version_string
|
user_agent = hs.version_string
|
||||||
if hs.config.user_agent_suffix:
|
if hs.config.user_agent_suffix:
|
||||||
user_agent = "%s %s" % (user_agent, hs.config.user_agent_suffix)
|
user_agent = "%s %s" % (user_agent, hs.config.user_agent_suffix)
|
||||||
user_agent = user_agent.encode("ascii")
|
user_agent = user_agent.encode("ascii")
|
||||||
|
|
||||||
self.agent = MatrixFederationAgent(
|
federation_agent = MatrixFederationAgent(
|
||||||
self.reactor,
|
self.reactor,
|
||||||
tls_client_options_factory,
|
tls_client_options_factory,
|
||||||
user_agent,
|
user_agent,
|
||||||
|
@ -254,7 +254,7 @@ class MatrixFederationHttpClient:
|
||||||
# Use a BlacklistingAgentWrapper to prevent circumventing the IP
|
# Use a BlacklistingAgentWrapper to prevent circumventing the IP
|
||||||
# blacklist via IP literals in server names
|
# blacklist via IP literals in server names
|
||||||
self.agent = BlacklistingAgentWrapper(
|
self.agent = BlacklistingAgentWrapper(
|
||||||
self.agent,
|
federation_agent,
|
||||||
ip_blacklist=hs.config.federation_ip_range_blacklist,
|
ip_blacklist=hs.config.federation_ip_range_blacklist,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -328,6 +328,6 @@ def lazyConnection(
|
||||||
factory.continueTrying = reconnect
|
factory.continueTrying = reconnect
|
||||||
|
|
||||||
reactor = hs.get_reactor()
|
reactor = hs.get_reactor()
|
||||||
reactor.connectTCP(host, port, factory, 30)
|
reactor.connectTCP(host, port, factory, timeout=30, bindAddress=None)
|
||||||
|
|
||||||
return factory.handler
|
return factory.handler
|
||||||
|
|
|
@ -36,7 +36,6 @@ from typing import (
|
||||||
cast,
|
cast,
|
||||||
)
|
)
|
||||||
|
|
||||||
import twisted.internet.base
|
|
||||||
import twisted.internet.tcp
|
import twisted.internet.tcp
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
from twisted.mail.smtp import sendmail
|
from twisted.mail.smtp import sendmail
|
||||||
|
@ -130,7 +129,7 @@ from synapse.server_notices.worker_server_notices_sender import (
|
||||||
from synapse.state import StateHandler, StateResolutionHandler
|
from synapse.state import StateHandler, StateResolutionHandler
|
||||||
from synapse.storage import Databases, DataStore, Storage
|
from synapse.storage import Databases, DataStore, Storage
|
||||||
from synapse.streams.events import EventSources
|
from synapse.streams.events import EventSources
|
||||||
from synapse.types import DomainSpecificString
|
from synapse.types import DomainSpecificString, ISynapseReactor
|
||||||
from synapse.util import Clock
|
from synapse.util import Clock
|
||||||
from synapse.util.distributor import Distributor
|
from synapse.util.distributor import Distributor
|
||||||
from synapse.util.ratelimitutils import FederationRateLimiter
|
from synapse.util.ratelimitutils import FederationRateLimiter
|
||||||
|
@ -291,7 +290,7 @@ class HomeServer(metaclass=abc.ABCMeta):
|
||||||
for i in self.REQUIRED_ON_BACKGROUND_TASK_STARTUP:
|
for i in self.REQUIRED_ON_BACKGROUND_TASK_STARTUP:
|
||||||
getattr(self, "get_" + i + "_handler")()
|
getattr(self, "get_" + i + "_handler")()
|
||||||
|
|
||||||
def get_reactor(self) -> twisted.internet.base.ReactorBase:
|
def get_reactor(self) -> ISynapseReactor:
|
||||||
"""
|
"""
|
||||||
Fetch the Twisted reactor in use by this HomeServer.
|
Fetch the Twisted reactor in use by this HomeServer.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -35,6 +35,14 @@ from typing import (
|
||||||
import attr
|
import attr
|
||||||
from signedjson.key import decode_verify_key_bytes
|
from signedjson.key import decode_verify_key_bytes
|
||||||
from unpaddedbase64 import decode_base64
|
from unpaddedbase64 import decode_base64
|
||||||
|
from zope.interface import Interface
|
||||||
|
|
||||||
|
from twisted.internet.interfaces import (
|
||||||
|
IReactorCore,
|
||||||
|
IReactorPluggableNameResolver,
|
||||||
|
IReactorTCP,
|
||||||
|
IReactorTime,
|
||||||
|
)
|
||||||
|
|
||||||
from synapse.api.errors import Codes, SynapseError
|
from synapse.api.errors import Codes, SynapseError
|
||||||
from synapse.util.stringutils import parse_and_validate_server_name
|
from synapse.util.stringutils import parse_and_validate_server_name
|
||||||
|
@ -67,6 +75,14 @@ MutableStateMap = MutableMapping[StateKey, T]
|
||||||
JsonDict = Dict[str, Any]
|
JsonDict = Dict[str, Any]
|
||||||
|
|
||||||
|
|
||||||
|
# Note that this seems to require inheriting *directly* from Interface in order
|
||||||
|
# for mypy-zope to realize it is an interface.
|
||||||
|
class ISynapseReactor(
|
||||||
|
IReactorTCP, IReactorPluggableNameResolver, IReactorTime, IReactorCore, Interface
|
||||||
|
):
|
||||||
|
"""The interfaces necessary for Synapse to function."""
|
||||||
|
|
||||||
|
|
||||||
class Requester(
|
class Requester(
|
||||||
namedtuple(
|
namedtuple(
|
||||||
"Requester",
|
"Requester",
|
||||||
|
|
Loading…
Reference in a new issue