1+ #! /bin/bash
2+ # Comprehensive P2P test: local machine + Digital Ocean droplet (default) or dual droplets (CI)
3+
4+ set -e
5+
6+ # Colors for output
7+ RED=' \033[0;31m'
8+ GREEN=' \033[0;32m'
9+ YELLOW=' \033[1;33m'
10+ BLUE=' \033[0;34m'
11+ PURPLE=' \033[0;35m'
12+ NC=' \033[0m' # No Color
13+
14+ # Configuration
15+ MODE=" single" # Default: local + 1 droplet
16+ TEST_ID=$( date +%s) _$$
17+ LOCAL_HOME=" /tmp/fastn-local-${TEST_ID} "
18+ DROPLET_SIZE=" s-1vcpu-1gb"
19+ DROPLET_REGION=" nyc3"
20+ DROPLET_IMAGE=" ubuntu-22-04-x64"
21+
22+ # Parse arguments
23+ while [[ $# -gt 0 ]]; do
24+ case $1 in
25+ --dual)
26+ MODE=" dual"
27+ shift
28+ ;;
29+ --single)
30+ MODE=" single"
31+ shift
32+ ;;
33+ * )
34+ echo " Usage: $0 [--single|--dual]"
35+ echo " --single: Local machine + 1 Digital Ocean droplet (default)"
36+ echo " --dual: 2 Digital Ocean droplets (for CI)"
37+ exit 1
38+ ;;
39+ esac
40+ done
41+
42+ cleanup () {
43+ echo -e " ${YELLOW} π§Ή Cleaning up test environment...${NC} "
44+
45+ # Kill local processes
46+ pkill -f " fastn-p2p daemon" || true
47+ pkill -f " request_response" || true
48+
49+ # Clean up local directories
50+ rm -rf " $LOCAL_HOME "
51+
52+ # Clean up droplets
53+ if [ -n " $ALICE_DROPLET_ID " ]; then
54+ echo -e " ${YELLOW} ποΈ Destroying alice droplet: $ALICE_DROPLET_ID ${NC} "
55+ doctl compute droplet delete " $ALICE_DROPLET_ID " --force || true
56+ fi
57+
58+ if [ -n " $BOB_DROPLET_ID " ]; then
59+ echo -e " ${YELLOW} ποΈ Destroying bob droplet: $BOB_DROPLET_ID ${NC} "
60+ doctl compute droplet delete " $BOB_DROPLET_ID " --force || true
61+ fi
62+
63+ echo -e " ${GREEN} β
Cleanup complete${NC} "
64+ }
65+
66+ # Set up cleanup trap
67+ trap cleanup EXIT
68+
69+ echo -e " ${GREEN} π Starting P2P test in ${MODE} mode${NC} "
70+
71+ # Build binaries
72+ echo -e " ${YELLOW} π¦ Building binaries...${NC} "
73+ cargo build --release --bin fastn-p2p
74+ cargo build --release --bin request_response
75+
76+ if [ " $MODE " = " single" ]; then
77+ echo -e " ${BLUE} π Single mode: Local machine (bob) + Digital Ocean droplet (alice)${NC} "
78+
79+ # Set up local environment for bob
80+ mkdir -p " $LOCAL_HOME "
81+
82+ echo -e " ${YELLOW} π§ Starting local daemon (bob)...${NC} "
83+ FASTN_HOME=" $LOCAL_HOME " ./target/release/fastn-p2p daemon &
84+ LOCAL_DAEMON_PID=$!
85+ sleep 3
86+
87+ # Create bob identity locally
88+ FASTN_HOME=" $LOCAL_HOME " ./target/release/fastn-p2p create-identity bob
89+ FASTN_HOME=" $LOCAL_HOME " ./target/release/fastn-p2p identity-online bob
90+ echo -e " ${GREEN} β
Bob identity created locally${NC} "
91+
92+ # Create alice droplet (server)
93+ echo -e " ${YELLOW} π Creating alice droplet (server)...${NC} "
94+ ALICE_DROPLET_ID=$( doctl compute droplet create \
95+ fastn-alice-test-${TEST_ID} \
96+ --size $DROPLET_SIZE \
97+ --region $DROPLET_REGION \
98+ --image $DROPLET_IMAGE \
99+ --ssh-keys $( doctl compute ssh-key list --format FingerPrint --no-header | head -1) \
100+ --wait \
101+ --format ID \
102+ --no-header)
103+
104+ ALICE_IP=$( doctl compute droplet get $ALICE_DROPLET_ID --format PublicIPv4 --no-header)
105+ echo -e " ${GREEN} β
Alice droplet created: $ALICE_DROPLET_ID ($ALICE_IP )${NC} "
106+
107+ # Wait for droplet to be ready
108+ echo -e " ${YELLOW} β³ Waiting for alice droplet to be ready...${NC} "
109+ sleep 60
110+
111+ # Deploy to alice droplet
112+ echo -e " ${YELLOW} π¦ Deploying to alice droplet...${NC} "
113+ scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
114+ target/release/fastn-p2p target/release/request_response \
115+ root@$ALICE_IP :/root/
116+
117+ # Setup alice server
118+ ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
119+ root@$ALICE_IP << 'EOF '
120+ # Install dependencies
121+ apt-get update
122+ apt-get install -y build-essential
123+
124+ # Start alice's daemon
125+ export FASTN_HOME=/root/alice
126+ ./fastn-p2p daemon &
127+ sleep 5
128+
129+ # Create alice identity and setup Echo server
130+ ./fastn-p2p create-identity alice
131+ ./fastn-p2p add-protocol alice --protocol Echo --config '{"max_message_length": 1000}'
132+ ./fastn-p2p identity-online alice
133+
134+ # Get alice's peer ID
135+ ALICE_ID52=$(./fastn-p2p status | grep -o 'alice (ONLINE) - [a-z0-9]*' | grep -o '[a-z0-9]*$')
136+ echo "ALICE_PEER_ID=$ALICE_ID52" > alice_peer_id.txt
137+
138+ # Start Echo server
139+ ./request_response server alice &
140+
141+ echo "π’ Alice server ready, peer ID: $ALICE_ID52"
142+ EOF
143+
144+ # Get alice's peer ID
145+ ALICE_PEER_ID=$( ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
146+ root@$ALICE_IP " cat alice_peer_id.txt | cut -d'=' -f2" )
147+ echo -e " ${BLUE} π Alice's peer ID: $ALICE_PEER_ID ${NC} "
148+
149+ # Test P2P communication: local bob β remote alice
150+ echo -e " ${PURPLE} π Testing local β remote P2P communication...${NC} "
151+ echo -e " ${BLUE} Local bob will send request to remote alice via real internet P2P${NC} "
152+
153+ FASTN_HOME=" $LOCAL_HOME " ./target/release/request_response client " $ALICE_PEER_ID " " Hello Alice from local Bob via real internet P2P!"
154+
155+ echo -e " ${GREEN} π Single-mode P2P test completed successfully!${NC} "
156+ echo -e " ${GREEN} - Local daemon (bob) β Remote droplet (alice)${NC} "
157+ echo -e " ${GREEN} - Real internet P2P communication validated${NC} "
158+
159+ else
160+ echo -e " ${BLUE} βοΈ Dual mode: Two Digital Ocean droplets (alice + bob)${NC} "
161+
162+ # Create both droplets for CI mode
163+ echo -e " ${YELLOW} π Creating alice droplet (server)...${NC} "
164+ ALICE_DROPLET_ID=$( doctl compute droplet create \
165+ fastn-alice-test-${TEST_ID} \
166+ --size $DROPLET_SIZE \
167+ --region $DROPLET_REGION \
168+ --image $DROPLET_IMAGE \
169+ --ssh-keys $( doctl compute ssh-key list --format FingerPrint --no-header | head -1) \
170+ --wait \
171+ --format ID \
172+ --no-header)
173+
174+ echo -e " ${YELLOW} π Creating bob droplet (client)...${NC} "
175+ BOB_DROPLET_ID=$( doctl compute droplet create \
176+ fastn-bob-test-${TEST_ID} \
177+ --size $DROPLET_SIZE \
178+ --region $DROPLET_REGION \
179+ --image $DROPLET_IMAGE \
180+ --ssh-keys $( doctl compute ssh-key list --format FingerPrint --no-header | head -1) \
181+ --wait \
182+ --format ID \
183+ --no-header)
184+
185+ ALICE_IP=$( doctl compute droplet get $ALICE_DROPLET_ID --format PublicIPv4 --no-header)
186+ BOB_IP=$( doctl compute droplet get $BOB_DROPLET_ID --format PublicIPv4 --no-header)
187+
188+ echo -e " ${GREEN} β
Droplets created:${NC} "
189+ echo -e " ${GREEN} Alice: $ALICE_DROPLET_ID ($ALICE_IP )${NC} "
190+ echo -e " ${GREEN} Bob: $BOB_DROPLET_ID ($BOB_IP )${NC} "
191+
192+ # Continue with dual-droplet setup...
193+ echo -e " ${PURPLE} π Dual-droplet P2P testing not fully implemented yet${NC} "
194+ echo -e " ${BLUE} Use --single mode for now${NC} "
195+ fi
0 commit comments