diff options
author | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2023-02-05 05:04:18 +0100 |
---|---|---|
committer | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2023-02-14 10:37:18 +0100 |
commit | ff1fcdc60e8968ee770b8720b4f3f9667af69127 (patch) | |
tree | b0a310b70494eeb75848fd5782e2c128741b73bc /recording | |
parent | c43840900af9f5388529ef03cdd9ab1a1308ef3d (diff) |
Add module to load the configuration
"server.conf.in" is provided as an example file with a description of
all the configurations. Some descriptions were copied from
https://github.com/strukturag/nextcloud-spreed-signaling/blob/v1.0.0/server.conf.in,
which is also licensed under the GNU AGPLv3.
Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
Diffstat (limited to 'recording')
-rw-r--r-- | recording/server.conf.in | 67 | ||||
-rw-r--r-- | recording/src/nextcloud/talk/recording/Config.py | 160 | ||||
-rw-r--r-- | recording/src/nextcloud/talk/recording/__main__.py | 31 |
3 files changed, 258 insertions, 0 deletions
diff --git a/recording/server.conf.in b/recording/server.conf.in new file mode 100644 index 000000000..111916235 --- /dev/null +++ b/recording/server.conf.in @@ -0,0 +1,67 @@ +[logs] +# Log level based on numeric values of Python logging levels: +# - Critical: 50 +# - Error: 40 +# - Warning: 30 +# - Info: 20 +# - Debug: 10 +# - Not set: 0 +#level = 20 + +[http] +# IP and port to listen on for HTTP requests. +#listen = 127.0.0.1:8000 + +[backend] +# Allow any hostname as backend endpoint. This is extremely insecure and should +# only be used during development. +#allowall = false + +# Common shared secret for requests from and to the backend servers if +# "allowall" is enabled. This must be the same value as configured in the +# Nextcloud admin ui. +#secret = the-shared-secret + +# Comma-separated list of backend ids allowed to connect. +#backends = backend-id, another-backend + +# If set to "true", certificate validation of backend endpoints will be skipped. +# This should only be enabled during development, e.g. to work with self-signed +# certificates. +# Overridable by backend. +#skipverify = false + +# Maximum allowed size in bytes for messages sent by the backend. +# Overridable by backend. +#maxmessagesize = 1024 + +# Width for recorded videos. +# Overridable by backend. +#videowidth = 1920 + +# Height for recorded videos. +# Overridable by backend. +#videoheight = 1080 + +# Temporary directory used to store recordings until uploaded. It must be +# writable by the user running the recording server. +# Overridable by backend. +#directory = /tmp + +# Backend configurations as defined in the "[backend]" section above. The +# section names must match the ids used in "backends" above. +#[backend-id] +# URL of the Nextcloud instance +#url = https://cloud.domain.invalid + +# Shared secret for requests from and to the backend servers. This must be the +# same value as configured in the Nextcloud admin ui. +#secret = the-shared-secret + +#[another-backend] +# URL of the Nextcloud instance +#url = https://cloud.otherdomain.invalid + +# Shared secret for requests from and to the backend servers. This must be the +# same value as configured in the Nextcloud admin ui. +#secret = the-shared-secret diff --git a/recording/src/nextcloud/talk/recording/Config.py b/recording/src/nextcloud/talk/recording/Config.py new file mode 100644 index 000000000..9e177dedb --- /dev/null +++ b/recording/src/nextcloud/talk/recording/Config.py @@ -0,0 +1,160 @@ +# +# @copyright Copyright (c) 2023, Daniel Calviño Sánchez (danxuliu@gmail.com) +# +# @license GNU AGPL version 3 or any later version +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +""" +Module for getting the configuration. + +Other modules are expected to import the shared "config" object, which will be +loaded with the configuration file at startup. +""" + +import logging +import os + +from configparser import ConfigParser + +class Config: + def __init__(self): + self._logger = logging.getLogger(__name__) + + self._configParser = ConfigParser() + + self._backendIdsByBackendUrl = {} + + def load(self, fileName): + fileName = os.path.abspath(fileName) + + if not os.path.exists(fileName): + self._logger.warning(f"Configuration file not found: {fileName}") + else: + self._logger.info(f"Loading {fileName}") + + self._configParser.read(fileName) + + self._loadBackends() + + def _loadBackends(self): + self._backendIdsByBackendUrl = {} + + if 'backend' not in self._configParser or 'backends' not in self._configParser['backend']: + self._logger.warning(f"No configured backends") + + return + + backendIds = self._configParser.get('backend', 'backends') + backendIds = [backendId.strip() for backendId in backendIds.split(',')] + + for backendId in backendIds: + if 'url' not in self._configParser[backendId]: + self._logger.error(f"Missing 'url' property for backend {backendId}") + continue + + if 'secret' not in self._configParser[backendId]: + self._logger.error(f"Missing 'secret' property for backend {backendId}") + continue + + backendUrl = self._configParser[backendId]['url'].rstrip('/') + self._backendIdsByBackendUrl[backendUrl] = backendId + + def getLogLevel(self): + """ + Returns the log level. + + Defaults to INFO (20). + """ + return int(self._configParser.get('logs', 'level', fallback=logging.INFO)) + + def getListen(self): + """ + Returns the IP and port to listen on for HTTP requests. + + Defaults to "127.0.0.1:8000". + """ + return self._configParser.get('http', 'listen', fallback='127.0.0.1:8000') + + def getBackendSecret(self, backendUrl): + """ + Returns the shared secret for requests from and to the backend servers. + + Defaults to None. + """ + if self._configParser.get('backend', 'allowall', fallback=None): + return self._configParser.get('backend', 'secret') + + backendUrl = backendUrl.rstrip('/') + if backendUrl in self._backendIdsByBackendUrl: + backendId = self._backendIdsByBackendUrl[backendUrl] + + return self._configParser.get(backendId, 'secret', fallback=None) + + return None + + def getBackendSkipVerify(self, backendUrl): + """ + Returns whether the certificate validation of backend endpoints should + be skipped or not. + + Defaults to False. + """ + return self._getBackendValue(backendUrl, 'skipverify', False) == 'true' + + def getBackendMaximumMessageSize(self, backendUrl): + """ + Returns the maximum allowed size in bytes for messages sent by the + backend. + + Defaults to 1024. + """ + return int(self._getBackendValue(backendUrl, 'maxmessagesize', 1024)) + + def getBackendVideoWidth(self, backendUrl): + """ + Returns the width for recorded videos. + + Defaults to 1920. + """ + return int(self._getBackendValue(backendUrl, 'videowidth', 1920)) + + def getBackendVideoHeight(self, backendUrl): + """ + Returns the height for recorded videos. + + Defaults to 1080. + """ + return int(self._getBackendValue(backendUrl, 'videoheight', 1080)) + + def getBackendDirectory(self, backendUrl): + """ + Returns the temporary directory used to store recordings until uploaded. + + Defaults to False. + """ + return self._getBackendValue(backendUrl, 'directory', '/tmp') + + def _getBackendValue(self, backendUrl, key, default): + backendUrl = backendUrl.rstrip('/') + if backendUrl in self._backendIdsByBackendUrl: + backendId = self._backendIdsByBackendUrl[backendUrl] + + if self._configParser.get(backendId, key, fallback=None): + return self._configParser.get(backendId, key) + + return self._configParser.get('backend', key, fallback=default) + +config = Config() diff --git a/recording/src/nextcloud/talk/recording/__main__.py b/recording/src/nextcloud/talk/recording/__main__.py new file mode 100644 index 000000000..358fc91bb --- /dev/null +++ b/recording/src/nextcloud/talk/recording/__main__.py @@ -0,0 +1,31 @@ +# +# @copyright Copyright (c) 2023, Daniel Calviño Sánchez (danxuliu@gmail.com) +# +# @license GNU AGPL version 3 or any later version +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +import argparse +import logging + +from .Config import config + +parser = argparse.ArgumentParser() +parser.add_argument("-c", "--config", help="path to configuration file", default="server.conf") +args = parser.parse_args() + +config.load(args.config) + +logging.basicConfig(level=config.getLogLevel()) |