| Crates.io | hanzo-libp2p |
| lib.rs | hanzo-libp2p |
| version | 1.1.12 |
| created_at | 2025-11-30 06:59:43.027457+00 |
| updated_at | 2026-01-07 22:44:44.707591+00 |
| description | Libp2P Relayer for Hanzo AI platform |
| homepage | https://hanzo.ai |
| repository | https://github.com/hanzoai/hanzo-node |
| max_upload_size | |
| id | 1957968 |
| size | 293,616 |
A LibP2P relay server implementation for the Hanzo network with automatic external IP detection for cloud deployments.
When deploying LibP2P relay servers on Google Cloud Platform using Container-Optimized OS with --network="host", the container cannot automatically detect the VM's public IP address. This prevents external peers from connecting to the relay server.
This implementation solves the problem by:
# Dockerfile for Google Cloud deployment
FROM rust:1.75 as builder
WORKDIR /app
COPY . .
RUN cargo build --release --bin hanzo-libp2p-relayer
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/hanzo-libp2p-relayer /usr/local/bin/
EXPOSE 9090
CMD ["hanzo-libp2p-relayer"]
# Deploy to Google Cloud with host networking
docker run -d \
--name hanzo-relay \
--network="host" \
-e RELAY_PORT=9090 \
-e NODE_NAME="@@my-relay.sep-hanzo" \
your-relay-image:latest
use hanzo_libp2p_relayer::RelayManager;
use ed25519_dalek::SigningKey;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let identity_secret_key = SigningKey::generate(&mut rand::rngs::OsRng);
let mut relay_manager = RelayManager::new(
9090, // Listen port
"@@my-relay.sep-hanzo".to_string(),
identity_secret_key,
).await?;
// Check if external IP was detected
if let Some(external_ip) = relay_manager.get_external_ip() {
println!("External IP detected: {}", external_ip);
// Get external addresses for advertising
let addresses = relay_manager.get_external_addresses(9090);
for addr in addresses {
println!("External address: {}", addr);
}
}
// Start the relay manager
relay_manager.run().await?;
Ok(())
}
RELAY_PORT: Port to listen on (default: 9090)NODE_NAME: Identity name for the relay serverIDENTITY_SECRET_KEY: Ed25519 private key for node identityThe relay manager automatically detects external IP addresses using multiple services for reliability:
┌─────────────────────────────────────────────────────────┐
│ Google Cloud VM │
│ ┌─────────────────────────────────────────────────────┐│
│ │ Container (--network=host) ││
│ │ ┌─────────────────────────────────────────────────┐││
│ │ │ LibP2P Relay Manager │││
│ │ │ │││
│ │ │ • Binds to 0.0.0.0:9090 (all interfaces) │││
│ │ │ • Detects external IP: 203.0.113.42 │││
│ │ │ • Advertises: /ip4/203.0.113.42/tcp/9090 │││
│ │ │ /ip4/203.0.113.42/udp/9090/quic │││
│ │ └─────────────────────────────────────────────────┘││
│ └─────────────────────────────────────────────────────┘│
│ │
│ Internal IP: 10.128.0.42 │
│ External IP: 203.0.113.42 │
└─────────────────────────────────────────────────────────┘
│
│ Firewall allows :9090
│
▼
┌─────────────────────────────────────────────────────────┐
│ Internet Peers │
│ │
│ Connect to: /ip4/203.0.113.42/tcp/9090 │
│ /ip4/203.0.113.42/udp/9090/quic-v1 │
└─────────────────────────────────────────────────────────┘
The relay manager provides detailed logging for monitoring:
🌐 External address confirmed and advertised: /ip4/203.0.113.42/tcp/9090
📍 Connection established with peer: 12D3KooW...
⚠️ External address expired: /ip4/203.0.113.42/tcp/9090
Key log messages:
If external IP detection fails:
Firewall Rules: Ensure the relay port is open:
gcloud compute firewall-rules create allow-hanzo-relay \
--allow tcp:9090,udp:9090 \
--source-ranges 0.0.0.0/0
Container-Optimized OS: Use --network="host" for direct IP access
External IP Assignment: Ensure the VM has an external IP assigned
libp2p 0.55.0+ - Core LibP2P networkingreqwest 0.11+ - HTTP client for IP detectiontokio - Async runtimeserde_json - JSON parsing for IP detection servicesThis project is licensed under the same terms as the main Hanzo Node project.