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>
This commit is contained in:
Silke Hofstra 2017-09-02 17:26:40 +02:00 committed by Silke
parent 3e59143ba8
commit 37d1a90025
2 changed files with 61 additions and 42 deletions

View file

@ -58,6 +58,7 @@ from twisted.internet import defer, reactor
from twisted.web.resource import EncodingResourceWrapper, Resource from twisted.web.resource import EncodingResourceWrapper, Resource
from twisted.web.server import GzipEncoderFactory from twisted.web.server import GzipEncoderFactory
from twisted.web.static import File from twisted.web.static import File
from twisted.internet import error
logger = logging.getLogger("synapse.app.homeserver") logger = logging.getLogger("synapse.app.homeserver")
@ -131,6 +132,7 @@ class SynapseHomeServer(HomeServer):
if tls: if tls:
for address in bind_addresses: for address in bind_addresses:
try:
reactor.listenSSL( reactor.listenSSL(
port, port,
SynapseSite( SynapseSite(
@ -142,8 +144,12 @@ class SynapseHomeServer(HomeServer):
self.tls_server_context_factory, self.tls_server_context_factory,
interface=address interface=address
) )
except error.CannotListenError as e:
check_bind_error(e, address, bind_addresses)
else: else:
for address in bind_addresses: for address in bind_addresses:
try:
reactor.listenTCP( reactor.listenTCP(
port, port,
SynapseSite( SynapseSite(
@ -154,6 +160,8 @@ class SynapseHomeServer(HomeServer):
), ),
interface=address interface=address
) )
except error.CannotListenError as e:
check_bind_error(e, address, bind_addresses)
logger.info("Synapse now listening on port %d", port) logger.info("Synapse now listening on port %d", port)
def _configure_named_resource(self, name, compress=False): def _configure_named_resource(self, name, compress=False):
@ -232,6 +240,7 @@ class SynapseHomeServer(HomeServer):
bind_addresses = listener["bind_addresses"] bind_addresses = listener["bind_addresses"]
for address in bind_addresses: for address in bind_addresses:
try:
reactor.listenTCP( reactor.listenTCP(
listener["port"], listener["port"],
manhole( manhole(
@ -241,9 +250,12 @@ class SynapseHomeServer(HomeServer):
), ),
interface=address interface=address
) )
except error.CannotListenError as e:
check_bind_error(e, address, bind_addresses)
elif listener["type"] == "replication": elif listener["type"] == "replication":
bind_addresses = listener["bind_addresses"] bind_addresses = listener["bind_addresses"]
for address in bind_addresses: for address in bind_addresses:
try:
factory = ReplicationStreamProtocolFactory(self) factory = ReplicationStreamProtocolFactory(self)
server_listener = reactor.listenTCP( server_listener = reactor.listenTCP(
listener["port"], factory, interface=address listener["port"], factory, interface=address
@ -251,6 +263,8 @@ class SynapseHomeServer(HomeServer):
reactor.addSystemEventTrigger( reactor.addSystemEventTrigger(
"before", "shutdown", server_listener.stopListening, "before", "shutdown", server_listener.stopListening,
) )
except error.CannotListenError as e:
check_bind_error(e, address, bind_addresses)
else: else:
logger.warn("Unrecognized listener type: %s", listener["type"]) logger.warn("Unrecognized listener type: %s", listener["type"])
@ -284,6 +298,13 @@ class SynapseHomeServer(HomeServer):
return db_conn 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): def setup(config_options):
""" """
Args: Args:

View file

@ -220,14 +220,12 @@ class ServerConfig(Config):
port: %(bind_port)s port: %(bind_port)s
# Local addresses to listen on. # Local addresses to listen on.
# On Linux and Mac OS, this will listen on all IPv4 and IPv6 # On Linux and Mac OS, `::` will listen on all IPv4 and IPv6
# addresses by default. For most other OSes, this will only listen # addresses by default. For most other OSes, this will only listen
# on IPv6. # on IPv6.
bind_addresses: bind_addresses:
- '::' - '::'
# For systems other than Linux or Mac OS, uncomment the next line - '0.0.0.0'
# to also listen on IPv4.
#- '0.0.0.0'
# This is a 'http' listener, allows us to specify 'resources'. # This is a 'http' listener, allows us to specify 'resources'.
type: http type: http
@ -265,7 +263,7 @@ class ServerConfig(Config):
# For when matrix traffic passes through loadbalancer that unwraps TLS. # For when matrix traffic passes through loadbalancer that unwraps TLS.
- port: %(unsecure_port)s - port: %(unsecure_port)s
tls: false tls: false
bind_addresses: ['::'] bind_addresses: ['::', '0.0.0.0']
type: http type: http
x_forwarded: false x_forwarded: false