NexNet is a peer-to-peer distributed file storage system built in Go that demonstrates advanced systems programming concepts including cryptography, concurrent networking, and distributed consensus. The system automatically replicates encrypted files across multiple nodes with content-addressable storage.
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β NexNet P2P Network β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Node A βββββΊβ Node B βββββΊβ Node C β β
β β :3000 β β :4000 β β :5000 β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Storage β β Storage β β Storage β β
β β Layer β β Layer β β Layer β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β TCP Transportβ β Cryptography β β Storage β
β β’ Peers β β β’ AES-CTR β β β’ CAS β
β β’ Handshakeβ β β’ SHA-1 β β β’ PathKey β
β β’ Streamingβ β β’ Random IVβ β β’ Chunking β
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
- TCP-based peer-to-peer communication
- Concurrent connection handling with goroutines
- Custom RPC protocol with message/stream differentiation
- Peer lifecycle management with proper cleanup
- AES-256-CTR encryption for all file data
Why?
Key Benefits:
- β Stream encryption (no buffering entire file)
- β Random access (can decrypt any part without decrypting whole file)
- β Parallel processing (multiple chunks simultaneously)
- β No padding required (unlike CBC mode)
- Random IV generation for each encryption operation
- 32KB streaming chunks for memory-efficient processing
- XORKeyStream for real-time encrypt/decrypt
- SHA-1 hash-based file organization
- Hierarchical directory structure (5-char blocks)
- Collision-resistant addressing
- Efficient file deduplication
- Multi-node replication with automatic discovery
- Network-wide file retrieval when not available locally
- Concurrent file operations using Go channels
- Bootstrap node connectivity
// Concurrent peer handling
go t.handleConn(conn, false)
// Bootstrap network connections
go func(addr string) {
if err := s.Transport.Dial(addr); err != nil {
log.Println("Dial error: ", err)
}
}(addr)
// TeeReader for simultaneous read/write
tee := io.TeeReader(r, fileBuffer)
// MultiWriter for broadcasting to multiple peers
peers := []io.Writer{}
for _, peer := range s.peers {
peers = append(peers, peer)
}
mw := io.MultiWriter(peers...)
Concept
Traditional Systems: Think of it like a photocopier workflow
- Read document β Make copy 1 β Put away original β Get original again β Make copy 2...
- Each operation requires getting the original document again
NexNet's Approach: Think of it like a water pipe with multiple outlets
- Water flows ONCE through the main pipe
- Multiple taps can draw from the same flow simultaneously
- No need to "re-flow" the water for each tap
Technical Magic:
TeeReader
= The "pipe splitter" (one input β multiple outputs)MultiWriter
= The "broadcast valve" (one write β multiple destinations)- Result = Single data read feeds ALL operations simultaneously
This is why your approach is genuinely sophisticated - you're eliminating the fundamental inefficiency of traditional file distribution systems!
// 32KB chunked processing prevents memory overflow
buf := make([]byte, 32*1024)
for {
n, err := src.Read(buf)
if n > 0 {
stream.XORKeyStream(buf, buf[:n]) // In-place encryption
dst.Write(buf[:n])
}
}
func CASPathTransformFunc(key string) PathKey {
hash := sha1.Sum([]byte(key))
hashStr := hex.EncodeToString(hash[:])
// Create hierarchical path: ab/cd/ef/gh/ij/abcdefghij...
blocksize := 5
sliceLen := len(hashStr) / blocksize
paths := make([]string, sliceLen)
for i := 0; i < sliceLen; i++ {
from, to := i*blocksize, i*blocksize+blocksize
paths[i] = hashStr[from:to]
}
return PathKey{
PathName: strings.Join(paths, "/"),
Filename: hashStr,
}
}
- Counter Mode: Enables streaming encryption without padding
- Random IV: 16-byte initialization vector for each file
- Key Derivation: 32-byte random keys for AES-256
- Stream Cipher: XORKeyStream for real-time processing
- Memory Efficiency: Prevents loading entire files into RAM
- Network Optimization: Optimal TCP packet sizing
- Concurrent Processing: Enables pipeline processing
- Cache Friendly: Fits in CPU L2/L3 cache
- Mesh Network: Each node can connect to multiple peers
- Bootstrap Discovery: New nodes discover network through bootstrap nodes
- Automatic Replication: Files are automatically replicated across connected peers
- Fault Tolerance: System continues operating with node failures
- Store: Encrypt and replicate files across network
- Retrieve: Fetch files from any node in the network
- Delete: Coordinate file deletion across all nodes
- Deduplication: Same content stored only once per node
# Build the binary
make build
# Run the demo
make run
# Run tests
make test
The main.go demonstrates a 3-node network:
s := makeServer(":3000", "") // Bootstrap node
s1 := makeServer(":4000", ":3000") // Connects to 3000
s2 := makeServer(":5000", ":4000", ":3000") // Connects to both
.
βββ bin/ # Compiled binaries
βββ cryptography/ # Encryption/decryption logic
β βββ crypto.go # AES-CTR implementation
β βββ crypto_test.go
βββ p2p/ # Peer-to-peer networking
β βββ encoding.go # Message encoding/decoding
β βββ handshake.go # Peer handshake logic
β βββ message.go # RPC message definitions
β βββ tcp_transport.go # TCP transport implementation
β βββ transport.go # Transport interface
βββ server/ # Distributed file server
β βββ server.go # Main server logic
β βββ server_test.go
βββ storage/ # Content-addressable storage
β βββ store.go # Storage implementation
β βββ store_test.go
βββ main.go # Demo application
βββ Makefile # Build configuration
βββ README.md
- Channel-based communication for RPC handling
- WaitGroup synchronization for stream operations
- Mutex protection for shared peer state
- Graceful shutdown with context cancellation
- CTR mode encryption for parallel processing
- IV prepending for secure key reuse
- In-place XOR operations for memory efficiency
- Binary encoding for network transmission
- Collision-resistant hashing with SHA-1
- Hierarchical file organization for filesystem efficiency
- Automatic deduplication through hash-based naming
- Scalable directory structure preventing single-directory limits
- Gossip-style replication for file distribution
- Network-wide search for file retrieval
- Coordinated deletion across all nodes
- Bootstrap-based discovery for network joining
- Real-time file streaming with encryption
- Automatic peer discovery and connection management
- Fault-tolerant file retrieval from multiple sources
- Memory-efficient processing of large files
- Hierarchical storage organization for scalability
This project demonstrates mastery of:
- Advanced Go concurrency patterns
- Cryptographic protocol implementation
- Distributed systems design
- Network programming with TCP
- Content-addressable storage systems
- Streaming I/O operations
- P2P network architecture
NexNet showcases production-ready distributed systems engineering with Go, emphasizing security, scalability, and concurrent processing.