17
17
18
18
import logging
19
19
import os .path
20
+ import re
21
+ from textwrap import indent
20
22
21
23
import attr
24
+ import yaml
22
25
from netaddr import IPSet
23
26
24
27
from synapse .api .room_versions import KNOWN_ROOM_VERSIONS
@@ -352,7 +355,7 @@ def has_tls_listener(self):
352
355
return any (l ["tls" ] for l in self .listeners )
353
356
354
357
def generate_config_section (
355
- self , server_name , data_dir_path , open_private_ports , ** kwargs
358
+ self , server_name , data_dir_path , open_private_ports , listeners , ** kwargs
356
359
):
357
360
_ , bind_port = parse_and_validate_server_name (server_name )
358
361
if bind_port is not None :
@@ -366,11 +369,68 @@ def generate_config_section(
366
369
# Bring DEFAULT_ROOM_VERSION into the local-scope for use in the
367
370
# default config string
368
371
default_room_version = DEFAULT_ROOM_VERSION
372
+ secure_listeners = []
373
+ unsecure_listeners = []
374
+ private_addresses = ["::1" , "127.0.0.1" ]
375
+ if listeners :
376
+ for listener in listeners :
377
+ if listener ["tls" ]:
378
+ secure_listeners .append (listener )
379
+ else :
380
+ # If we don't want open ports we need to bind the listeners
381
+ # to some address other than 0.0.0.0. Here we chose to use
382
+ # localhost.
383
+ # If the addresses are already bound we won't overwrite them
384
+ # however.
385
+ if not open_private_ports :
386
+ listener .setdefault ("bind_addresses" , private_addresses )
387
+
388
+ unsecure_listeners .append (listener )
389
+
390
+ secure_http_bindings = indent (
391
+ yaml .dump (secure_listeners ), " " * 10
392
+ ).lstrip ()
393
+
394
+ unsecure_http_bindings = indent (
395
+ yaml .dump (unsecure_listeners ), " " * 10
396
+ ).lstrip ()
397
+
398
+ if not unsecure_listeners :
399
+ unsecure_http_bindings = (
400
+ """- port: %(unsecure_port)s
401
+ tls: false
402
+ type: http
403
+ x_forwarded: true"""
404
+ % locals ()
405
+ )
406
+
407
+ if not open_private_ports :
408
+ unsecure_http_bindings += (
409
+ "\n bind_addresses: ['::1', '127.0.0.1']"
410
+ )
411
+
412
+ unsecure_http_bindings += """
413
+
414
+ resources:
415
+ - names: [client, federation]
416
+ compress: false"""
417
+
418
+ if listeners :
419
+ # comment out this block
420
+ unsecure_http_bindings = "#" + re .sub (
421
+ "\n {10}" ,
422
+ lambda match : match .group (0 ) + "#" ,
423
+ unsecure_http_bindings ,
424
+ )
369
425
370
- unsecure_http_binding = "port: %i\n tls: false" % (unsecure_port ,)
371
- if not open_private_ports :
372
- unsecure_http_binding += (
373
- "\n bind_addresses: ['::1', '127.0.0.1']"
426
+ if not secure_listeners :
427
+ secure_http_bindings = (
428
+ """#- port: %(bind_port)s
429
+ # type: http
430
+ # tls: true
431
+ # resources:
432
+ # - names: [client, federation]"""
433
+ % locals ()
374
434
)
375
435
376
436
return (
@@ -556,25 +616,15 @@ def generate_config_section(
556
616
# will also need to give Synapse a TLS key and certificate: see the TLS section
557
617
# below.)
558
618
#
559
- #- port: %(bind_port)s
560
- # type: http
561
- # tls: true
562
- # resources:
563
- # - names: [client, federation]
619
+ %(secure_http_bindings)s
564
620
565
621
# Unsecure HTTP listener: for when matrix traffic passes through a reverse proxy
566
622
# that unwraps TLS.
567
623
#
568
624
# If you plan to use a reverse proxy, please see
569
625
# https://github.com/matrix-org/synapse/blob/master/docs/reverse_proxy.rst.
570
626
#
571
- - %(unsecure_http_binding)s
572
- type: http
573
- x_forwarded: true
574
-
575
- resources:
576
- - names: [client, federation]
577
- compress: false
627
+ %(unsecure_http_bindings)s
578
628
579
629
# example additional_resources:
580
630
#
0 commit comments