Refactored environment vars into docker compose instead of using env vars, working on ssh and bridging wifi to xbnet, reviewing with Giovanni

This commit is contained in:
Emran Billah 2024-09-05 10:11:34 -03:00
parent 96c6f641b8
commit 9f1319850b
7 changed files with 89 additions and 8 deletions

View File

@ -38,5 +38,9 @@ RUN chmod +x /set-env-vars.sh
COPY ./scripts/health-check.sh /health-check.sh COPY ./scripts/health-check.sh /health-check.sh
RUN chmod +x /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 # Add healthcheck
HEALTHCHECK CMD /health-check.sh || exit 1 HEALTHCHECK CMD /health-check.sh || exit 1

View File

@ -9,7 +9,18 @@ services:
- XBEE_INDEX=1 - XBEE_INDEX=1
- XBEE_BAUDRATE=230400 - XBEE_BAUDRATE=230400
- XBEE_PORT=/dev/ttyUSB0 - XBEE_PORT=/dev/ttyUSB0
- XBNET_BASE_SUBNET=2.2.2 - XBNET_BASE_SUBNET=172.18.0
- XBNET_INTERFACE_TYPE=router - XBNET_INTERFACE_TYPE=router
- XBNET_PROTO=tap - XBNET_PROTO=tun
command: bash /entrypoint.sh 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

View File

@ -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

18
scripts/debug/test.py Executable file
View File

@ -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

View File

@ -36,7 +36,7 @@ loop() {
fi fi
# Log messages sent and received over xbnet # Log messages sent and received over xbnet
# log_xbnet_messages log_xbnet_messages
sleep 0.5 sleep 0.5
done done
@ -72,11 +72,14 @@ create_tap_interface() {
done done
# Create and bring up xbnet interface # 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 addr add $XBNET_IP/24 dev $XBNET_INTERFACE_NAME
ip link set $XBNET_INTERFACE_NAME up ip link set $XBNET_INTERFACE_NAME up
ip link show $XBNET_INTERFACE_NAME
ip addr show $XBNET_INTERFACE_NAME
echo "---------------------------------------------------------" echo "---------------------------------------------------------"
echo "***************** Tap interface created *****************" echo "************ ${XBNET_PROTO} interface created ************"
echo "---------------------------------------------------------" echo "---------------------------------------------------------"
echo "XBEE_INDEX : ${XBEE_INDEX}" echo "XBEE_INDEX : ${XBEE_INDEX}"
echo "XBEE_BAUDRATE : ${XBEE_BAUDRATE}" echo "XBEE_BAUDRATE : ${XBEE_BAUDRATE}"
@ -84,6 +87,7 @@ create_tap_interface() {
echo "XBNET_BASE_SUBNET : ${XBNET_BASE_SUBNET}" echo "XBNET_BASE_SUBNET : ${XBNET_BASE_SUBNET}"
echo "XBNET_IP : ${XBNET_IP}" echo "XBNET_IP : ${XBNET_IP}"
echo "XBNET_INTERFACE_NAME : ${XBNET_INTERFACE_NAME}" echo "XBNET_INTERFACE_NAME : ${XBNET_INTERFACE_NAME}"
echo "XBNET_PROTO : ${XBNET_PROTO}"
echo "XBNET_DEFAULT_GATEWAY : ${XBNET_DEFAULT_GATEWAY}" echo "XBNET_DEFAULT_GATEWAY : ${XBNET_DEFAULT_GATEWAY}"
echo "XBNET_DEFAULT_IPVLAN_IP : ${XBNET_DEFAULT_IPVLAN_IP}" echo "XBNET_DEFAULT_IPVLAN_IP : ${XBNET_DEFAULT_IPVLAN_IP}"
echo "XBNET_DEFAULT_MACVLAN_IP : ${XBNET_DEFAULT_MACVLAN_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) length=$(cat /sys/class/net/$XBNET_INTERFACE_NAME/statistics/tx_bytes)
echo "Length: ${length} bytes" echo "Length: ${length} bytes"
# Placeholder for payload (not feasible to extract without raw packet inspection) # # Placeholder for payload (not feasible to extract without raw packet inspection)
echo "Payload: [Payload extraction requires raw packet inspection tools]" # echo "Payload: [Payload extraction requires raw packet inspection tools]"
echo "******************************************************************" # echo "******************************************************************"
} }

0
serial Normal file
View File

0
subprocess Normal file
View File