Safely load config file for go2rtc

This commit is contained in:
Nicolas Mowen 2024-05-22 16:02:04 -06:00
parent 9652ea9e96
commit 2802a484a2

View File

@ -32,6 +32,7 @@ config_file_yaml = config_file.replace(".yml", ".yaml")
if os.path.isfile(config_file_yaml): if os.path.isfile(config_file_yaml):
config_file = config_file_yaml config_file = config_file_yaml
try:
with open(config_file) as f: with open(config_file) as f:
raw_config = f.read() raw_config = f.read()
@ -39,6 +40,8 @@ if config_file.endswith((".yaml", ".yml")):
config: dict[str, any] = yaml.safe_load(raw_config) config: dict[str, any] = yaml.safe_load(raw_config)
elif config_file.endswith(".json"): elif config_file.endswith(".json"):
config: dict[str, any] = json.loads(raw_config) config: dict[str, any] = json.loads(raw_config)
except FileNotFoundError:
config: dict[str, any] = {}
go2rtc_config: dict[str, any] = config.get("go2rtc", {}) go2rtc_config: dict[str, any] = config.get("go2rtc", {})