A text-based peer-to-peer chat built in Rust, showcasing P2P networking, cryptographic messaging, and threshold security.
- Automatic Peer Discovery: LAN peers detected automatically via UDP broadcast
- Real-time Messaging: Instant text delivery over TCP
- Decentralized: No central server required
- Simple CLI: Easy-to-use command line interface
- Heartbeat System: Tracks active peers
- Threshold Signatures: M-of-N voting for enabling secure-only messaging
- Cryptographic Security: Ed25519 message signing & verification
- Secure-Only Mode: Reject unsigned messages once enabled
Make sure you have Rust installed, then clone and build:
git clone https://github.com/miladtsx/p2p_lan_chat
cd p2p
cargo build
cargo run -- start --name "Alice" --port 8080
Default: name="Anonymous"
, port=8080
.
Command | Description | |
---|---|---|
Type message | Broadcast a signed message | |
/msg <msg> |
Send a signed message | |
/unsigned <msg> |
Send unsigned message | |
/propose <desc> |
Propose secure-only messaging | |
/vote <id> <vote> |
Vote on a proposal | |
/proposals |
List proposals | |
/status |
Show security & proposal status | |
/list |
List discovered peers | |
/crypto |
Show your cryptographic identity | |
/quit |
Exit |
# Terminal 1
cargo run -- start --name "Alice" --port 8080
# Terminal 2
cargo run -- start --name "Bob" --port 8081
# Terminal 3
cargo run -- start --name "Charlie" --port 8082
Wait for peer discovery, then type messages to broadcast! Each instance will automatically discover the others and you can send messages between them!
- Discovery: UDP
255.255.255.255:9999
broadcast - Messaging: TCP
8080+
, JSONNetworkMessage
- Async Rust: Concurrent networking with
tokio
- Cryptography: Ed25519 signatures for authenticity & integrity
- Threshold Voting: M-of-N approval for secure mode
- Command Pattern: Message handling uses a trait-based command dispatch for extensibility and clean code.
To add a new message type:
- Define your variant in
NetworkMessage
. - Implement the
NetworkCommand
trait for your type insrc/network/command.rs
. - Update the factory function to return your new command. This design makes it easy to add new features and keep message handling modular.
- Peer Discovery: Broadcasts info every 5s, updates peer list with heartbeats
- Message Broadcasting: TCP delivery, JSON format
- Threshold Signature System: Peer proposals → votes → automatic enforcement of secure-only messaging
- Cryptographic Identity: Auto-generated Ed25519 keypair per peer
- Messages signed automatically with private key
- Public key attached for verification
- Unsigned messages allowed for testing/backward compatibility
- Replay prevention using timestamps
- Public keys cached for efficiency
Symbol | Meaning |
---|---|
🔐 | Verified signature |
Invalid signature | |
❓ | Verification failed |
📝 | Unsigned |
# Send signed message
Hello world!
# Propose secure-only mode
/propose Enable secure-only messaging
# Vote
/vote <proposal_id> approve|reject
- Peers not discovering: Check LAN, firewall, ports
- Messages not sending: Ensure peers discovered, TCP open
- Threshold issues: Verify proposal ID, check /status
- Rust crates:
tokio
,serde
,serde_json
,clap
,uuid
,local-ip-address
,ed25519-dalek
- Message Format (JSON):
{
"from_id": "uuid",
"from_name": "Alice",
"content": "Hello",
"timestamp": 1234567890,
"signature": "base64",
"public_key": "base64"
}
- Private keys never transmitted; public keys exchanged automatically
- Minimal cryptographic overhead for real-time messaging