A peer-to-peer distributed file storage system built in Go that enables multiple nodes to store, retrieve, and manage files across a decentralized network with automatic replication and encryption.
- Peer-to-Peer Network: TCP-based communication between nodes
- File Encryption: AES encryption for all stored files
- Content Addressable Storage (CAS): Files are stored using SHA-1 hashing for deduplication
- Automatic Replication: Files are automatically replicated across network nodes
- Network Discovery: Bootstrap nodes for network formation
- File Operations: Store, retrieve, and delete files across the network
- Decentralized Architecture: No single point of failure
- Transport Interface: Abstraction for network communication protocols
- TCP Transport: TCP-based implementation for reliable peer communication
- Peer Management: Handles connections, handshakes, and message routing
- Message Encoding: Gob-based message serialization
- Content Addressable Storage: Files stored with SHA-1 hash-based paths
- Path Transformation: Hierarchical directory structure for efficient storage
- Encryption/Decryption: Transparent file encryption using AES
- File Operations: Read, write, delete, and existence checks
- File Server: Main orchestrator combining P2P and storage layers
- Message Handling: Processes store, get, and delete requests
- Network Operations: Broadcasts operations to peer nodes
- Peer Discovery: Bootstrap and maintain peer connections
- Key Generation: Secure random key generation for encryption
- File Hashing: MD5 and SHA-1 hashing utilities
- AES Encryption: Stream encryption/decryption for file content
βββ main.go # Application entry point and demo
βββ go.mod # Go module definition
βββ Makefile # Build and test commands
βββ p2p/ # Peer-to-peer networking
β βββ transport.go # Transport interface
β βββ tcp_transport.go # TCP implementation
β βββ message.go # Message types and constants
β βββ encoding.go # Message encoding/decoding
β βββ handshake.go # Peer handshake logic
βββ server/ # File server implementation
β βββ server.go # Main server logic
βββ store/ # Storage management
β βββ store.go # Storage interface and CAS
β βββ store_test.go # Storage tests
βββ crypto/ # Cryptographic utilities
βββ crypto.go # Encryption and hashing
- Nodes connect to bootstrap peers to join the network
- TCP connections are established between peers
- Handshake protocol ensures secure peer authentication
Client β Store File β Local Storage + Network Broadcast β Peer Replication
- File is encrypted using AES with a random key
- File is stored locally using CAS (content-addressable storage)
- Store message is broadcast to all connected peers
- Peers receive and store the encrypted file locally
Client β Request File β Check Local β Network Query β Stream Response
- Check if file exists in local storage
- If not found locally, broadcast get request to peers
- Peer with the file streams it back encrypted
- File is decrypted and returned to client
Client β Delete Request β Network Broadcast β Peer Deletion β Local Cleanup
- Broadcast delete message to all peers first
- Peers delete their local copies
- Finally delete from local storage
Files are stored in a hierarchical structure based on SHA-1 hash:
/storage_root/
βββ node_id/
βββ c08bf/
βββ 03b70/
βββ 1e356/
βββ 06cf8/
βββ ef77d12e1a44fc5e31071a750e3ea90d9872352a7a13
- Go 1.24+ installed
- Network ports 3000, 4000, 5000 available for demo
# Clone the repository
git clone https://github.com/ArditZubaku/distributed-file-storage.git
cd distributed-file-storage
# Install dependencies
go mod tidy
# Build the project
make build# Run the demonstration
make run
# Or run directly
go run main.gomake testThe main.go demonstrates a complete workflow:
// Create a 3-node network
s1 := makeServer("3000", "") // Bootstrap node
s2 := makeServer("4000", ":3000") // Connects to s1
s3 := makeServer("5000", ":3000", ":4000") // Connects to s1 and s2
// Store a file (automatically replicated)
data := bytes.NewReader([]byte("my big data file"))
s3.StoreFile("picture.png", data)
// Retrieve file from network
reader, err := s3.Get("picture.png")
// Delete from all nodes
s3.DeleteFile("picture.png")fileServerOpts := server.FileServerOpts{
ID: "unique-node-id", // Auto-generated if empty
EncKey: crypto.NewEncryptionKey(), // AES encryption key
StorageRoot: "storage_directory", // Local storage path
PathTransformFunc: store.CASPathTransformFunc, // CAS path transform
Transport: tcpTransport, // P2P transport
BootstrapNodes: []string{":3000"}, // Bootstrap peers
}tcpTransportOpts := p2p.TCPTransportOpts{
ListenAddr: ":3000", // Listening address
HandShakeFunc: p2p.NOPHandShakeFunc, // Handshake function
Decoder: p2p.DefaultDecoder{}, // Message decoder
OnPeer: server.OnPeer, // Peer connection handler
}- File Encryption: All files encrypted with AES before storage
- Content Integrity: SHA-1 hashing ensures file integrity
- Secure Communication: TCP with handshake protocol
- Random Key Generation: Cryptographically secure random keys
The project includes comprehensive tests:
- Unit tests for storage operations
- P2P transport testing
- Integration tests for file operations
# Run all tests
go test ./... -v
# Test specific package
go test ./store -v
go test ./p2p -v- MessageStoreFile: Replicates file to peers
- MessageGetFile: Requests file from network
- MessageDeleteFile: Removes file from all nodes
[Node A] β Store Message β [Node B, Node C]
[Node A] β File Stream β [Node B] (if has file)
[Node A] β Delete Message β [Node B, Node C]
- Web API interface for file operations
- Node discovery via DHT (Distributed Hash Table)
- Configurable replication factor
- File versioning and conflict resolution
- Performance metrics and monitoring
- Support for large file streaming
- Network partition tolerance
- Authentication and authorization