Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit d8114e0

Browse files
committed
register_new_matrix_user: read server url from config
Fixes #3672: `https://localhost:8448` is virtually never right.
1 parent 371db86 commit d8114e0

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

changelog.d/13616.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a longstanding bug in `register_new_matrix_user` which meant it was always necessary to explicitly give a server URL.

synapse/_scripts/register_new_matrix_user.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import hmac
2121
import logging
2222
import sys
23-
from typing import Callable, Optional
23+
from typing import Any, Callable, Dict, Optional
2424

2525
import requests
2626
import yaml
2727

28+
_DEFAULT_SERVER_URL = "http://localhost:8008"
29+
2830

2931
def request_registration(
3032
user: str,
@@ -203,31 +205,76 @@ def main() -> None:
203205

204206
parser.add_argument(
205207
"server_url",
206-
default="https://localhost:8448",
207208
nargs="?",
208-
help="URL to use to talk to the homeserver. Defaults to "
209-
" 'https://localhost:8448'.",
209+
help="URL to use to talk to the homeserver. By default, tries to find a "
210+
"suitable URL from the configuration file. Otherwise, defaults to "
211+
f"'{_DEFAULT_SERVER_URL}'.",
210212
)
211213

212214
args = parser.parse_args()
213215

214216
if "config" in args and args.config:
215217
config = yaml.safe_load(args.config)
218+
219+
if args.shared_secret:
220+
secret = args.shared_secret
221+
else:
222+
# argparse should check that we have either config or shared secret
223+
assert config
224+
216225
secret = config.get("registration_shared_secret", None)
217226
if not secret:
218227
print("No 'registration_shared_secret' defined in config.")
219228
sys.exit(1)
229+
230+
if args.server_url:
231+
server_url = args.server_url
232+
elif config:
233+
listening_port = _find_client_listener(config)
234+
if listening_port:
235+
server_url = f"http://{listening_port}"
236+
else:
237+
server_url = _DEFAULT_SERVER_URL
238+
print(
239+
"Unable to find a suitable HTTP listener in the configuration file. "
240+
f"Trying {server_url} as a last resort.",
241+
file=sys.stderr,
242+
)
220243
else:
221-
secret = args.shared_secret
244+
server_url = _DEFAULT_SERVER_URL
245+
print(
246+
f"No server url or configuration file given. Defaulting to {server_url}.",
247+
file=sys.stderr,
248+
)
222249

223250
admin = None
224251
if args.admin or args.no_admin:
225252
admin = args.admin
226253

227254
register_new_user(
228-
args.user, args.password, args.server_url, secret, admin, args.user_type
255+
args.user, args.password, server_url, secret, admin, args.user_type
229256
)
230257

231258

259+
def _find_client_listener(config: Dict[str, Any]) -> Optional[str]:
260+
# try to find a listener in the config. Returns a host:port pair
261+
for listener in config.get("listeners", []):
262+
if listener.get("type") != "http" or listener.get("tls", False):
263+
continue
264+
265+
if not any(
266+
name == "client"
267+
for resource in listener.get("resources", [])
268+
for name in resource.get("names", [])
269+
):
270+
continue
271+
272+
# TODO: consider bind_addresses
273+
return f"localhost:{listener['port']}"
274+
275+
# no suitable listeners?
276+
return None
277+
278+
232279
if __name__ == "__main__":
233280
main()

0 commit comments

Comments
 (0)