summary refs log tree commit diff
path: root/synapse/app
diff options
context:
space:
mode:
authorSilke Hofstra <silke@slxh.eu>2017-09-02 17:26:40 +0200
committerSilke <silke@slxh.eu>2017-12-17 13:10:31 +0100
commit37d1a90025c7dd3d87487f8cc0e6794e29becb93 (patch)
tree154fa915805b137b4ea7406646bfb43d598ac426 /synapse/app
parentAdapt the default config to bind on IPv6. (diff)
downloadsynapse-37d1a90025c7dd3d87487f8cc0e6794e29becb93.tar.xz
Allow binds to both :: and 0.0.0.0
Binding on 0.0.0.0 when :: is specified in the bind_addresses is now allowed.
This causes a warning explaining the behaviour.
Configuration changed to match.

See #2232

Signed-off-by: Silke Hofstra <silke@slxh.eu>
Diffstat (limited to 'synapse/app')
-rwxr-xr-xsynapse/app/homeserver.py95
1 files changed, 58 insertions, 37 deletions
diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py
index 6b8875afb4..82a4e18c67 100755
--- a/synapse/app/homeserver.py
+++ b/synapse/app/homeserver.py
@@ -58,6 +58,7 @@ from twisted.internet import defer, reactor
 from twisted.web.resource import EncodingResourceWrapper, Resource
 from twisted.web.server import GzipEncoderFactory
 from twisted.web.static import File
+from twisted.internet import error
 
 logger = logging.getLogger("synapse.app.homeserver")
 
@@ -131,29 +132,36 @@ class SynapseHomeServer(HomeServer):
 
         if tls:
             for address in bind_addresses:
-                reactor.listenSSL(
-                    port,
-                    SynapseSite(
-                        "synapse.access.https.%s" % (site_tag,),
-                        site_tag,
-                        listener_config,
-                        root_resource,
-                    ),
-                    self.tls_server_context_factory,
-                    interface=address
-                )
+                try:
+                    reactor.listenSSL(
+                        port,
+                        SynapseSite(
+                            "synapse.access.https.%s" % (site_tag,),
+                            site_tag,
+                            listener_config,
+                            root_resource,
+                        ),
+                        self.tls_server_context_factory,
+                        interface=address
+                    )
+                except error.CannotListenError as e:
+                    check_bind_error(e, address, bind_addresses)
+
         else:
             for address in bind_addresses:
-                reactor.listenTCP(
-                    port,
-                    SynapseSite(
-                        "synapse.access.http.%s" % (site_tag,),
-                        site_tag,
-                        listener_config,
-                        root_resource,
-                    ),
-                    interface=address
-                )
+                try:
+                    reactor.listenTCP(
+                        port,
+                        SynapseSite(
+                            "synapse.access.http.%s" % (site_tag,),
+                            site_tag,
+                            listener_config,
+                            root_resource,
+                        ),
+                        interface=address
+                    )
+                except error.CannotListenError as e:
+                    check_bind_error(e, address, bind_addresses)
         logger.info("Synapse now listening on port %d", port)
 
     def _configure_named_resource(self, name, compress=False):
@@ -232,25 +240,31 @@ class SynapseHomeServer(HomeServer):
                 bind_addresses = listener["bind_addresses"]
 
                 for address in bind_addresses:
-                    reactor.listenTCP(
-                        listener["port"],
-                        manhole(
-                            username="matrix",
-                            password="rabbithole",
-                            globals={"hs": self},
-                        ),
-                        interface=address
-                    )
+                    try:
+                        reactor.listenTCP(
+                            listener["port"],
+                            manhole(
+                                username="matrix",
+                                password="rabbithole",
+                                globals={"hs": self},
+                            ),
+                            interface=address
+                        )
+                    except error.CannotListenError as e:
+                        check_bind_error(e, address, bind_addresses)
             elif listener["type"] == "replication":
                 bind_addresses = listener["bind_addresses"]
                 for address in bind_addresses:
-                    factory = ReplicationStreamProtocolFactory(self)
-                    server_listener = reactor.listenTCP(
-                        listener["port"], factory, interface=address
-                    )
-                    reactor.addSystemEventTrigger(
-                        "before", "shutdown", server_listener.stopListening,
-                    )
+                    try:
+                        factory = ReplicationStreamProtocolFactory(self)
+                        server_listener = reactor.listenTCP(
+                            listener["port"], factory, interface=address
+                        )
+                        reactor.addSystemEventTrigger(
+                            "before", "shutdown", server_listener.stopListening,
+                        )
+                    except error.CannotListenError as e:
+                        check_bind_error(e, address, bind_addresses)
             else:
                 logger.warn("Unrecognized listener type: %s", listener["type"])
 
@@ -284,6 +298,13 @@ class SynapseHomeServer(HomeServer):
         return db_conn
 
 
+def check_bind_error(e, address, bind_addresses):
+    if address == '0.0.0.0' and '::' in bind_addresses:
+        logger.warn('Failed to listen on 0.0.0.0, continuing because listening on [::]')
+    else:
+        raise e
+
+
 def setup(config_options):
     """
     Args: