From 9f1319850bb100d60456e7b5926a414a983965c9 Mon Sep 17 00:00:00 2001 From: Emran Billah Date: Thu, 5 Sep 2024 10:11:34 -0300 Subject: [PATCH] Refactored environment vars into docker compose instead of using env vars, working on ssh and bridging wifi to xbnet, reviewing with Giovanni --- Dockerfile | 4 +++ docker-compose.yml | 17 ++++++++++-- scripts/debug/host-setup-docker.sh | 44 ++++++++++++++++++++++++++++++ scripts/debug/test.py | 18 ++++++++++++ scripts/entrypoint.sh | 14 ++++++---- serial | 0 subprocess | 0 7 files changed, 89 insertions(+), 8 deletions(-) create mode 100755 scripts/debug/host-setup-docker.sh create mode 100755 scripts/debug/test.py create mode 100644 serial create mode 100644 subprocess diff --git a/Dockerfile b/Dockerfile index fbcaeff..8e38399 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,5 +38,9 @@ RUN chmod +x /set-env-vars.sh COPY ./scripts/health-check.sh /health-check.sh RUN chmod +x /health-check.sh +# TEST +COPY ./scripts/debug/host-setup-docker.sh /host-setup-docker.sh +RUN chmod +x /host-setup-docker.sh + # Add healthcheck HEALTHCHECK CMD /health-check.sh || exit 1 diff --git a/docker-compose.yml b/docker-compose.yml index e18ddf3..ea5ad1b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,7 +9,18 @@ services: - XBEE_INDEX=1 - XBEE_BAUDRATE=230400 - XBEE_PORT=/dev/ttyUSB0 - - XBNET_BASE_SUBNET=2.2.2 + - XBNET_BASE_SUBNET=172.18.0 - XBNET_INTERFACE_TYPE=router - - XBNET_PROTO=tap - command: bash /entrypoint.sh \ No newline at end of file + - XBNET_PROTO=tun + command: bash /entrypoint.sh + networks: + eth0: + ipv4_address: 172.18.0.100 + +networks: + # Assigning a static IP to eth0 interface, so that it stays the same everytime the container is started + eth0: + ipam: + config: + - subnet: 172.18.0.0/24 + gateway: 172.18.0.1 \ No newline at end of file diff --git a/scripts/debug/host-setup-docker.sh b/scripts/debug/host-setup-docker.sh new file mode 100755 index 0000000..dc9c2b0 --- /dev/null +++ b/scripts/debug/host-setup-docker.sh @@ -0,0 +1,44 @@ +#!/bin/sh -e +# IP forwarding and NAT rules + +# Default host IP +HOST_IP="" + +# Parse command line argument for --host-ip +for arg in "$@"; do + case $arg in + --host-ip=*) + HOST_IP="${arg#*=}" + shift + ;; + *) + echo "Unknown argument: $arg" + exit 1 + ;; + esac +done + +# If HOST_IP is not set, fetch the IP address of the eth0 interface +if [ -z "$HOST_IP" ]; then + HOST_IP=$(ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}') + if [ -z "$HOST_IP" ]; then + echo "Error: Could not retrieve IP from eth0" + exit 1 + fi + echo "No --host-ip provided. Using eth0 IP: $HOST_IP" +else + echo "Using provided --host-ip: $HOST_IP" +fi + +# Enable IP forwarding +echo 1 > /proc/sys/net/ipv4/ip_forward + +# Set iptables rules for NAT and forwarding +iptables -t nat -A POSTROUTING -s 2.2.2.0/24 -o eth0 -j MASQUERADE +iptables -A FORWARD -i xbnet_router_1 -o eth0 -j ACCEPT +iptables -A FORWARD -i eth0 -o xbnet_router_1 -m state --state RELATED,ESTABLISHED -j ACCEPT + +# Ensure the xbnet_router_1 interface has a default route through eth0 +ip route add default via $(ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}') dev xbnet_router_1 + +exit 0 diff --git a/scripts/debug/test.py b/scripts/debug/test.py new file mode 100755 index 0000000..44f54cc --- /dev/null +++ b/scripts/debug/test.py @@ -0,0 +1,18 @@ +import serial +import subprocess + +# Set up serial connection (adjust serial port to match your configuration) +ser = serial.Serial('/dev/ttyUSB0', 230400) + +# Function to send command to XBee +def send_command(command): + ser.write((command + '\r\n').encode()) + +# Function to ping from the Raspberry Pi +def ping_address(address): + response = subprocess.run(['ping', '-c', '5', address], capture_output=True) + return response.stdout.decode() + +# Example usage +send_command('Hello XBee') # Sending data to XBee +print(ping_address('8.8.8.8')) # Pinging Google DNS from Pi \ No newline at end of file diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index 18b346b..a91b923 100755 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -36,7 +36,7 @@ loop() { fi # Log messages sent and received over xbnet - # log_xbnet_messages + log_xbnet_messages sleep 0.5 done @@ -72,11 +72,14 @@ create_tap_interface() { done # Create and bring up xbnet interface + ip route add default via 172.18.0.1 dev $XBNET_INTERFACE_NAME ip addr add $XBNET_IP/24 dev $XBNET_INTERFACE_NAME ip link set $XBNET_INTERFACE_NAME up + ip link show $XBNET_INTERFACE_NAME + ip addr show $XBNET_INTERFACE_NAME echo "---------------------------------------------------------" - echo "***************** Tap interface created *****************" + echo "************ ${XBNET_PROTO} interface created ************" echo "---------------------------------------------------------" echo "XBEE_INDEX : ${XBEE_INDEX}" echo "XBEE_BAUDRATE : ${XBEE_BAUDRATE}" @@ -84,6 +87,7 @@ create_tap_interface() { echo "XBNET_BASE_SUBNET : ${XBNET_BASE_SUBNET}" echo "XBNET_IP : ${XBNET_IP}" echo "XBNET_INTERFACE_NAME : ${XBNET_INTERFACE_NAME}" + echo "XBNET_PROTO : ${XBNET_PROTO}" echo "XBNET_DEFAULT_GATEWAY : ${XBNET_DEFAULT_GATEWAY}" echo "XBNET_DEFAULT_IPVLAN_IP : ${XBNET_DEFAULT_IPVLAN_IP}" echo "XBNET_DEFAULT_MACVLAN_IP : ${XBNET_DEFAULT_MACVLAN_IP}" @@ -154,9 +158,9 @@ log_packet_info() { length=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/statistics/tx_bytes) echo "Length: ${length} bytes" - # Placeholder for payload (not feasible to extract without raw packet inspection) - echo "Payload: [Payload extraction requires raw packet inspection tools]" - echo "******************************************************************" + # # Placeholder for payload (not feasible to extract without raw packet inspection) + # echo "Payload: [Payload extraction requires raw packet inspection tools]" + # echo "******************************************************************" } diff --git a/serial b/serial new file mode 100644 index 0000000..e69de29 diff --git a/subprocess b/subprocess new file mode 100644 index 0000000..e69de29