268 lines
6.6 KiB
Bash
268 lines
6.6 KiB
Bash
#!/bin/bash
|
|
#
|
|
# Local Network P2P Test using Loopback Aliases
|
|
#
|
|
# This creates multiple "virtual" IP addresses on the loopback interface
|
|
# to simulate cross-machine P2P testing without Docker.
|
|
#
|
|
# Usage:
|
|
# sudo ./local-network-test.sh # Run test
|
|
# sudo ./local-network-test.sh --clean # Clean up aliases
|
|
|
|
set -e
|
|
|
|
# Colors
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
# Configuration
|
|
SEEDER_IP="127.0.0.2"
|
|
LEECHER1_IP="127.0.0.3"
|
|
LEECHER2_IP="127.0.0.4"
|
|
SEEDER_PORT=9001
|
|
LEECHER1_PORT=9002
|
|
LEECHER2_PORT=9003
|
|
|
|
print_header() {
|
|
echo ""
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo -e "${BLUE} $1${NC}"
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo ""
|
|
}
|
|
|
|
print_status() {
|
|
echo -e "${GREEN}[✓]${NC} $1"
|
|
}
|
|
|
|
print_warning() {
|
|
echo -e "${YELLOW}[!]${NC} $1"
|
|
}
|
|
|
|
print_error() {
|
|
echo -e "${RED}[✗]${NC} $1"
|
|
}
|
|
|
|
# Check if running as root
|
|
check_root() {
|
|
if [ "$EUID" -ne 0 ]; then
|
|
print_error "This script requires root privileges for loopback aliases"
|
|
echo "Please run: sudo $0"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Create loopback aliases
|
|
setup_aliases() {
|
|
print_header "Setting Up Loopback Aliases"
|
|
|
|
# Check if aliases already exist
|
|
if ifconfig lo0 | grep -q "$SEEDER_IP"; then
|
|
print_warning "Aliases already exist"
|
|
return
|
|
fi
|
|
|
|
# Create aliases
|
|
ifconfig lo0 alias $SEEDER_IP
|
|
print_status "Created alias $SEEDER_IP"
|
|
|
|
ifconfig lo0 alias $LEECHER1_IP
|
|
print_status "Created alias $LEECHER1_IP"
|
|
|
|
ifconfig lo0 alias $LEECHER2_IP
|
|
print_status "Created alias $LEECHER2_IP"
|
|
|
|
# Verify
|
|
echo ""
|
|
echo "Active loopback addresses:"
|
|
ifconfig lo0 | grep "inet " | awk '{print " " $2}'
|
|
}
|
|
|
|
# Remove loopback aliases
|
|
cleanup_aliases() {
|
|
print_header "Cleaning Up Loopback Aliases"
|
|
|
|
ifconfig lo0 -alias $SEEDER_IP 2>/dev/null && print_status "Removed $SEEDER_IP" || true
|
|
ifconfig lo0 -alias $LEECHER1_IP 2>/dev/null && print_status "Removed $LEECHER1_IP" || true
|
|
ifconfig lo0 -alias $LEECHER2_IP 2>/dev/null && print_status "Removed $LEECHER2_IP" || true
|
|
}
|
|
|
|
# Clean up on exit
|
|
cleanup() {
|
|
print_header "Cleaning Up"
|
|
|
|
# Kill background processes
|
|
if [ ! -z "$SEEDER_PID" ]; then
|
|
kill $SEEDER_PID 2>/dev/null || true
|
|
print_status "Stopped seeder (PID $SEEDER_PID)"
|
|
fi
|
|
|
|
if [ ! -z "$LEECHER1_PID" ]; then
|
|
kill $LEECHER1_PID 2>/dev/null || true
|
|
print_status "Stopped leecher 1 (PID $LEECHER1_PID)"
|
|
fi
|
|
|
|
if [ ! -z "$LEECHER2_PID" ]; then
|
|
kill $LEECHER2_PID 2>/dev/null || true
|
|
print_status "Stopped leecher 2 (PID $LEECHER2_PID)"
|
|
fi
|
|
|
|
# Clean up test data
|
|
rm -rf /tmp/p2p-test-* 2>/dev/null || true
|
|
|
|
# Remove aliases
|
|
cleanup_aliases
|
|
}
|
|
|
|
# Build the test binary
|
|
build_binary() {
|
|
print_header "Building Test Binary"
|
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
if ! zig build docker-peer 2>/dev/null; then
|
|
# docker-peer isn't a separate step, just build all
|
|
zig build
|
|
fi
|
|
|
|
if [ -f "zig-out/bin/docker-peer" ]; then
|
|
print_status "Binary built: zig-out/bin/docker-peer"
|
|
else
|
|
print_error "Failed to build docker-peer"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Run the test
|
|
run_test() {
|
|
print_header "P2P Local Network Test"
|
|
|
|
echo "Network topology:"
|
|
echo " Seeder: $SEEDER_IP:$SEEDER_PORT"
|
|
echo " Leecher1: $LEECHER1_IP:$LEECHER1_PORT"
|
|
echo " Leecher2: $LEECHER2_IP:$LEECHER2_PORT"
|
|
echo ""
|
|
|
|
# Create test directories
|
|
mkdir -p /tmp/p2p-test-seeder
|
|
mkdir -p /tmp/p2p-test-leecher1
|
|
mkdir -p /tmp/p2p-test-leecher2
|
|
|
|
cd "$(dirname "$0")/.."
|
|
BINARY="./zig-out/bin/docker-peer"
|
|
|
|
# Start seeder
|
|
print_status "Starting seeder..."
|
|
PEER_MODE=seeder \
|
|
PEER_PORT=$SEEDER_PORT \
|
|
FILE_SIZE_MB=5 \
|
|
$BINARY > /tmp/p2p-test-seeder/log.txt 2>&1 &
|
|
SEEDER_PID=$!
|
|
print_status "Seeder started (PID $SEEDER_PID)"
|
|
|
|
# Wait for seeder to initialize
|
|
sleep 3
|
|
|
|
# Check if seeder is still running
|
|
if ! kill -0 $SEEDER_PID 2>/dev/null; then
|
|
print_error "Seeder crashed!"
|
|
cat /tmp/p2p-test-seeder/log.txt
|
|
exit 1
|
|
fi
|
|
|
|
# Start leecher 1
|
|
print_status "Starting leecher 1..."
|
|
PEER_MODE=leecher \
|
|
PEER_PORT=$LEECHER1_PORT \
|
|
SEEDER_HOST=$SEEDER_IP \
|
|
SEEDER_PORT=$SEEDER_PORT \
|
|
FILE_SIZE_MB=5 \
|
|
$BINARY > /tmp/p2p-test-leecher1/log.txt 2>&1 &
|
|
LEECHER1_PID=$!
|
|
print_status "Leecher 1 started (PID $LEECHER1_PID)"
|
|
|
|
# Start leecher 2
|
|
print_status "Starting leecher 2..."
|
|
PEER_MODE=leecher \
|
|
PEER_PORT=$LEECHER2_PORT \
|
|
SEEDER_HOST=$SEEDER_IP \
|
|
SEEDER_PORT=$SEEDER_PORT \
|
|
FILE_SIZE_MB=5 \
|
|
$BINARY > /tmp/p2p-test-leecher2/log.txt 2>&1 &
|
|
LEECHER2_PID=$!
|
|
print_status "Leecher 2 started (PID $LEECHER2_PID)"
|
|
|
|
# Monitor for 30 seconds
|
|
print_header "Monitoring Test (30 seconds)"
|
|
|
|
for i in {1..6}; do
|
|
sleep 5
|
|
echo "--- Progress at ${i}0 seconds ---"
|
|
|
|
# Check if processes are still running
|
|
if kill -0 $SEEDER_PID 2>/dev/null; then
|
|
echo "Seeder: Running"
|
|
tail -3 /tmp/p2p-test-seeder/log.txt 2>/dev/null | sed 's/^/ /'
|
|
else
|
|
echo "Seeder: Stopped"
|
|
fi
|
|
|
|
if kill -0 $LEECHER1_PID 2>/dev/null; then
|
|
echo "Leecher 1: Running"
|
|
tail -3 /tmp/p2p-test-leecher1/log.txt 2>/dev/null | sed 's/^/ /'
|
|
else
|
|
echo "Leecher 1: Stopped"
|
|
fi
|
|
|
|
if kill -0 $LEECHER2_PID 2>/dev/null; then
|
|
echo "Leecher 2: Running"
|
|
tail -3 /tmp/p2p-test-leecher2/log.txt 2>/dev/null | sed 's/^/ /'
|
|
else
|
|
echo "Leecher 2: Stopped"
|
|
fi
|
|
echo ""
|
|
done
|
|
|
|
print_header "Test Complete"
|
|
|
|
# Show final logs
|
|
echo "=== Seeder Final Log ==="
|
|
tail -20 /tmp/p2p-test-seeder/log.txt 2>/dev/null
|
|
echo ""
|
|
echo "=== Leecher 1 Final Log ==="
|
|
tail -20 /tmp/p2p-test-leecher1/log.txt 2>/dev/null
|
|
echo ""
|
|
echo "=== Leecher 2 Final Log ==="
|
|
tail -20 /tmp/p2p-test-leecher2/log.txt 2>/dev/null
|
|
}
|
|
|
|
# Main
|
|
case "${1:-}" in
|
|
--clean)
|
|
check_root
|
|
cleanup_aliases
|
|
;;
|
|
--help|-h)
|
|
echo "Local Network P2P Test"
|
|
echo ""
|
|
echo "Usage: sudo $0 [OPTIONS]"
|
|
echo ""
|
|
echo "Options:"
|
|
echo " --clean Remove loopback aliases only"
|
|
echo " --help Show this help"
|
|
echo ""
|
|
echo "This script creates loopback aliases to simulate multiple"
|
|
echo "machines on a single host for P2P testing."
|
|
;;
|
|
*)
|
|
check_root
|
|
trap cleanup EXIT
|
|
build_binary
|
|
setup_aliases
|
|
run_test
|
|
;;
|
|
esac
|