From 3620d33f7f1cdf0fae281895ac321f1133dbc270 Mon Sep 17 00:00:00 2001 From: gzi01 Date: Tue, 18 Mar 2025 21:16:12 +0100 Subject: [PATCH] feat: add optional whitelist identifiying users to be allowed access to the bbs --- config_init.py | 6 +++++- example_config.ini | 10 ++++++++++ message_processing.py | 7 +++++-- server.py | 3 ++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/config_init.py b/config_init.py index e8a8e7f..5dac88a 100644 --- a/config_init.py +++ b/config_init.py @@ -117,6 +117,9 @@ def initialize_config(config_file: str = None) -> dict[str, Any]: allowed_nodes = config.get('allow_list', 'allowed_nodes', fallback='').split(',') if allowed_nodes == ['']: allowed_nodes = [] + whitelist = config.get('whitelist', 'allowed_users', fallback='').split(',') + if whitelist == ['']: + whitelist = [] print(f"Nodes with Urgent board permissions: {allowed_nodes}") @@ -127,7 +130,8 @@ def initialize_config(config_file: str = None) -> dict[str, Any]: 'port': port, 'bbs_nodes': bbs_nodes, 'allowed_nodes': allowed_nodes, - 'mqtt_topic': 'meshtastic.receive' + 'mqtt_topic': 'meshtastic.receive', + 'whitelist': whitelist } diff --git a/example_config.ini b/example_config.ini index 3066b36..e4d759c 100644 --- a/example_config.ini +++ b/example_config.ini @@ -100,3 +100,13 @@ utilities_menu_items = S, F, W, X # js8groups = @GRP1,@GRP2,@GRP3 # store_messages = True # js8urgent = @URGNT + +############################ +#### Allowed User IDs #### +############################ +# Provide a list of user IDs that are allowed to use the bbs. +# If allowed users is commented out anyone can use the bbs +# Example: +#[whitelist] +#allowed_users = 123456789,123456780 +# diff --git a/message_processing.py b/message_processing.py index 41bd7d7..f46a561 100644 --- a/message_processing.py +++ b/message_processing.py @@ -176,7 +176,8 @@ def process_message(sender_id, message, interface, is_sync_message=False): handle_help_command(sender_id, interface) -def on_receive(packet, interface): +def on_receive(packet, interface, whitelist=None): + whitelist = whitelist or [] try: if 'decoded' in packet and packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP': message_bytes = packet['decoded']['payload'] @@ -184,7 +185,9 @@ def on_receive(packet, interface): sender_id = packet['from'] to_id = packet.get('to') sender_node_id = packet['fromId'] - + if whitelist and str(sender_id) not in whitelist: + logging.info(f'loaded whitelist {whitelist} does not contain {sender_id}') + return sender_short_name = get_node_short_name(sender_node_id, interface) receiver_short_name = get_node_short_name(get_node_id_from_num(to_id, interface), interface) if to_id else "Group Chat" diff --git a/server.py b/server.py index 177f82f..d6bf1f6 100644 --- a/server.py +++ b/server.py @@ -66,9 +66,10 @@ def main(): logging.info(f"TC²-BBS is running on {system_config['interface_type']} interface...") initialize_database() + whitelist = system_config['whitelist'] def receive_packet(packet, interface): - on_receive(packet, interface) + on_receive(packet, interface, whitelist) pub.subscribe(receive_packet, system_config['mqtt_topic'])