diff --git a/src/wgfrontend/config.py b/src/wgfrontend/config.py index 56def25..da4f85a 100644 --- a/src/wgfrontend/config.py +++ b/src/wgfrontend/config.py @@ -33,11 +33,15 @@ class Configuration(): logger.warning('Config file [{0}] could not be read [{1}], using defaults'.format(self.filename, str(e))) self._config = dict() - def write_config(self, wg_configfile='', user='', users={}): + def write_config(self, wg_configfile='', socket_host='0.0.0.0', socket_port=8080, user='', users={}): """Writes a new config file with the given attributes""" # Set default values if not wg_configfile.strip(): wg_configfile = '/etc/wireguard/wg_rw.conf' + if not socket_host.strip(): + socket_host = '0.0.0.0' + if not str(socket_port).strip(): + socket_port = 8080 if not user.strip(): user = 'wgfrontend' users = { username if username.strip() else 'admin': password for username, password in users.items() } @@ -48,19 +52,23 @@ class Configuration(): ### Config file of the Towalink WireGuard Frontend ### [general] # The WireGuard config file to read and write + # wg_configfile = /etc/wireguard/wg_rw.conf wg_configfile = {wg_configfile} # The command to be executed when the WireGuard config has changed # on_change_command = - # Example: on_change_command = "sudo /etc/init.d/wgfrontend_interface restart" + # Example: on_change_command = "sudo /etc/init.d/wgfrontend_interface restart" - # The interface to bind to for the web server + # The interface the web server shall bind to # socket_host = 0.0.0.0 + socket_host = {socket_host} - # The port to bind to for the web server + # The port the web server shall bind to # socket_port = 8080 + socket_port = {socket_port} # The system user to be used for the frontend + # user = wgfrontend user = {user} [users] @@ -115,7 +123,9 @@ class Configuration(): @property def on_change_command(self): """The command to be executed on config changes""" - return self.config.get('on_change_command') + cmd = self.config.get('on_change_command') + cmd = cmd.strip('"\'') + return cmd @property def socket_host(self): diff --git a/src/wgfrontend/setupenv.py b/src/wgfrontend/setupenv.py index 02f6895..fa6d8cb 100644 --- a/src/wgfrontend/setupenv.py +++ b/src/wgfrontend/setupenv.py @@ -107,20 +107,34 @@ def setup_environment(): user = input(f'2b) Please specify the system user for the web frontend [wgfrontend]: ') ok = False while not ok: - username = input(f'2c) Please specify the username for your web frontend user [admin]: ') + socket_host = input(f'2c) Please specify the listening interface for the web server [0.0.0.0]: ') + if check_validcharacters(socket_host, string.hexdigits + '.:'): + ok = True + else: + print(' Invalid characters entered. Please enter anew.') + ok = False + while not ok: + socket_port = input(f'2d) Please specify the listening port for the web server [8080]: ') + if (not socket_port.strip()) or socket_port.isdigit(): + ok = True + else: + print(' You need to provide a port number. Please enter anew.') + ok = False + while not ok: + username = input(f'2e) Please specify the username for your web frontend user [admin]: ') if check_validcharacters(username, string.ascii_letters + '_'): ok = True else: print(' Username must only contain letters and underscores. Please enter anew.') ok = False while not ok: - password = input(f'2d) Please specify the password for your web frontend user: ') + password = input(f'2f) Please specify the password for your web frontend user: ') if len(password) >= 8: ok = True else: print(' Password must have at least eight characters. Please enter anew.') touch_file(cfg.filename, perm=0o640) # create without world read permissions - cfg.write_config(wg_configfile=wg_configfile, user=user, users={username: password}) + cfg.write_config(wg_configfile=wg_configfile, socket_host=socket_host, socket_port=socket_port, user=user, users={username: password}) print(' Config file written. Ok.') print(f'3) Ensuring that system user "{cfg.user}" exists.') ensure_user(cfg.user)