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

Commit 88f7d31

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 88f7d31

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

changelog.d/13616.bugfix

+1
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

+51-6
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,74 @@ 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+
server_url = _find_client_listener(config)
234+
if not server_url:
235+
server_url = _DEFAULT_SERVER_URL
236+
print(
237+
"Unable to find a suitable HTTP listener in the configuration file. "
238+
f"Trying {server_url} as a last resort.",
239+
file=sys.stderr,
240+
)
220241
else:
221-
secret = args.shared_secret
242+
server_url = _DEFAULT_SERVER_URL
243+
print(
244+
f"No server url or configuration file given. Defaulting to {server_url}.",
245+
file=sys.stderr,
246+
)
222247

223248
admin = None
224249
if args.admin or args.no_admin:
225250
admin = args.admin
226251

227252
register_new_user(
228-
args.user, args.password, args.server_url, secret, admin, args.user_type
253+
args.user, args.password, server_url, secret, admin, args.user_type
229254
)
230255

231256

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

0 commit comments

Comments
 (0)