From a8643b166eb70e80e30dadedf579617383e0c707 Mon Sep 17 00:00:00 2001 From: Geoff Whittington Date: Wed, 17 Aug 2022 21:55:12 -0400 Subject: [PATCH 1/6] update --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 71198cf..632d897 100644 --- a/main.py +++ b/main.py @@ -31,7 +31,7 @@ SUPPORTED_MESSAGES = [ 'TEXT_MESSAGE_APP' ] -SUPPORTED_BRIDGE_DISTANCE_KM = 5 +SUPPORTED_BRIDGE_DISTANCE_KM = int(os.environ['BRIDGE_DISTANCE_KM']) if 'BRIDGE_DISTANCE_KM' in os.environ else 0 CHANNEL_INDEX = 0 NODE_PROXY = {BROADCAST_ADDR: BROADCAST_ADDR} From 2a84414ddcb42e66c0b55fed3f058f43e1f40835 Mon Sep 17 00:00:00 2001 From: Geoff Whittington Date: Wed, 17 Aug 2022 22:03:07 -0400 Subject: [PATCH 2/6] fixes --- main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 632d897..f3e71e1 100644 --- a/main.py +++ b/main.py @@ -12,12 +12,12 @@ from pubsub import pub local_interface = None remote_interface = None -local_node_addr = os.environ['LOCAL_NODE_ADDR'] +local_node_addr = os.environ['LOCAL_NODE_ADDR'] if 'LOCAL_NODE_ADDR' in os.environ else None remote_node_addr = os.environ['REMOTE_NODE_ADDR'] -if '/' in local_node_addr: +if local_node_addr and '/' in local_node_addr: local_interface = meshtastic.serial_interface.SerialInterface(devPath=local_node_addr) -elif '.' in local_node_addr: +elif local_node_addr and '.' in local_node_addr: local_interface = meshtastic.tcp_interface.TCPInterface(hostname=local_node_addr) else: local_interface = meshtastic.serial_interface.SerialInterface() From 2f1d14ae4e4ac57f2ffc7777810f687d8e66e98e Mon Sep 17 00:00:00 2001 From: geoffwhittington Date: Wed, 17 Aug 2022 22:04:07 -0400 Subject: [PATCH 3/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 027a366..b570a57 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Connect two distinct radio [Meshtastic](https://meshtastic.org) networks using T - Python3 - Two Meshtastic devices: - Local `LOCAL_NODE_ADDR` The IP address or Serial devPath (micro USB cable needed for serial access) of a local Meshtastic device - - Remote `REMOTE_NODE_ARR` The IP address of a remote Meshtastic device + - Remote `REMOTE_NODE_ADDR` The IP address of a remote Meshtastic device Refer to to configure a Meshtastic device to use wifi and expose a TCP address From 45e3323ebcdd0328cfb9d38680f1663bd9e5cc9a Mon Sep 17 00:00:00 2001 From: geoffwhittington Date: Wed, 17 Aug 2022 22:10:42 -0400 Subject: [PATCH 4/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b570a57..c93e1e3 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ $ pip install -r requirements.txt ## Turn on the Bridge -In the `meshtastic-bridge` directory run the following: +In the `meshtastic-bridge` directory run the following - replacing `BRIDGE_DISTANCE_KM`, `LOCAL_NODE_ADDR` and `REMOTE_NODE_ADDR` with the proper values: ``` BRIDGE_DISTANCE_KM=0 LOCAL_NODE_ADDR=/dev/ttyUSB0 REMOTE_NODE_ADDR=182.168.86.123 python main.py From 7815931e6f4dd9bc9cacc934cadf995ac96a2cd6 Mon Sep 17 00:00:00 2001 From: geoffwhittington Date: Wed, 17 Aug 2022 22:11:54 -0400 Subject: [PATCH 5/6] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c93e1e3..31569d0 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ Connect two distinct radio [Meshtastic](https://meshtastic.org) networks using TCP. +WARNING: Traffic is sent insecure using TCP. Use a VPN to secure traffic between the nodes + ## Requirements - Python3 From 3276b8f8281ea2552a5880964fe715d4bf83ab9c Mon Sep 17 00:00:00 2001 From: Geoff Whittington Date: Thu, 18 Aug 2022 23:10:40 -0400 Subject: [PATCH 6/6] add logging --- main.py | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index f3e71e1..c177f59 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import logging import meshtastic import meshtastic.serial_interface import meshtastic.tcp_interface @@ -11,19 +12,39 @@ from pubsub import pub local_interface = None remote_interface = None +bridge_logger = logging.getLogger(name="meshtastic.bridge") + +BRIDGE_LOG = os.environ['BRIDGE_LOG'] if 'BRIDGE_LOG' in os.environ else 'INFO' +NODE_LOG = os.environ['NODE_LOG'] if 'NODE_LOG' in os.environ else 'INFO' + +if BRIDGE_LOG == 'DEBUG': + bridge_logger.setLevel(logging.DEBUG) +elif BRIDGE_LOG == 'INFO': + bridge_logger.setLevel(logging.INFO) + +if NODE_LOG == 'DEBUG': + logging.basicConfig(level=logging.DEBUG) +elif NODE_LOG == 'INFO': + logging.basicConfig(level=logging.INFO) local_node_addr = os.environ['LOCAL_NODE_ADDR'] if 'LOCAL_NODE_ADDR' in os.environ else None remote_node_addr = os.environ['REMOTE_NODE_ADDR'] if local_node_addr and '/' in local_node_addr: + bridge_logger.debug(f"Connecting to local node via serial port: {local_node_addr} ...") local_interface = meshtastic.serial_interface.SerialInterface(devPath=local_node_addr) -elif local_node_addr and '.' in local_node_addr: +elif local_node_addr: + bridge_logger.debug(f"Connecting to local node via TCP: {local_node_addr} ...") local_interface = meshtastic.tcp_interface.TCPInterface(hostname=local_node_addr) else: + bridge_logger.debug(f"Connecting to local node via serial port ...") local_interface = meshtastic.serial_interface.SerialInterface() -remote_interface = meshtastic.tcp_interface.TCPInterface(hostname=remote_node_addr) +bridge_logger.info(f"Connected to local node") +bridge_logger.debug(f"Connecting to remote node via TCP: {remote_node_addr} ...") +remote_interface = meshtastic.tcp_interface.TCPInterface(hostname=remote_node_addr) +bridge_logger.info(f"Connected to remote node") ourNode = local_interface.getNode('^local') SUPPORTED_MESSAGES = [ @@ -37,8 +58,10 @@ NODE_PROXY = {BROADCAST_ADDR: BROADCAST_ADDR} def onReceive(packet, interface): # called when a packet arrives + bridge_logger.debug(f"Packet received: {packet}") + if packet['decoded']['portnum'] not in SUPPORTED_MESSAGES: - print(f"Dropping {packet['decoded']['portnum']}") + bridge_logger.debug(f"Dropping {packet['decoded']['portnum']}") return message_source_position = None @@ -53,8 +76,12 @@ def onReceive(packet, interface): # called when a packet arrives current_local_position = (nodeInfo['position']['latitude'], nodeInfo['position']['longitude']) if message_source_position and current_local_position: + + distance_km = haversine(message_source_position, current_local_position) + # message originates from too far a distance - if SUPPORTED_BRIDGE_DISTANCE_KM > 0 and haversine(message_source_position, current_local_position) > SUPPORTED_BRIDGE_DISTANCE_KM: + if SUPPORTED_BRIDGE_DISTANCE_KM > 0 and distance_km > SUPPORTED_BRIDGE_DISTANCE_KM: + bridge_logger.debug(f"Packet from too far: {distance_km} > {SUPPORTED_BRIDGE_DISTANCE_KM}") return if 'to' in packet: @@ -74,7 +101,7 @@ def onReceive(packet, interface): # called when a packet arrives meshPacket.id = remote_interface._generatePacketId() if destinationId == BROADCAST_ADDR or destinationId in remote_interface.nodes: - print(f"Sending {meshPacket} to TCP server") + bridge_logger.debug(f"Sending packet {meshPacket.id} to TCP server") remote_interface._sendPacket(meshPacket=meshPacket, destinationId=destinationId) def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect to the radio