-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
408 lines (347 loc) · 10.1 KB
/
server.go
File metadata and controls
408 lines (347 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package main
import (
"bytes"
"encoding/binary"
"encoding/gob"
"fmt"
"io"
"log"
"sync"
"time"
"github.com/ShivankSharma070/SwarmStore/p2p"
)
func init() {
gob.Register(MessageStoreFile{})
gob.Register(MessageGetFile{})
gob.Register(MessageDeleteFile{})
}
// FileServerOpts holds configuration options for creating a new FileServer
type FileServerOpts struct {
// ID is the unique identifier for this file server node
ID string
// EncryptionKey is the key used for encrypting files when
// storing/retrieving on other connected peers
EncryptionKey []byte
StorageRoot string
PathTransformFunc PathTransformFunc
Transport p2p.Transport
// bootstrapNodes is a list of addresses to connect to on startup
bootstrapNodes []string
}
// FileServer is the main server that handles file storage and retrieval
// across a P2P network. It manages local storage, peer connections,
// and message handling for file operations
type FileServer struct {
FileServerOpts
// peerLock protects the peers map during concurrent access
peerLock sync.Mutex
// peers is a map of remote peer addresses to their connection
peers map[string]p2p.Peer
store *Store
// quitCh is used to signal the server to stop
quitCh chan struct{}
}
// NewFileServer creates and returns a new FileServer instance with the
// provided options. If no ID is provided, one will be generated automatically
func NewFileServer(opts FileServerOpts) *FileServer {
serverOpts := StoreOpts{
PathTransformFunc: opts.PathTransformFunc,
root: opts.StorageRoot,
}
if len(opts.ID) == 0 {
opts.ID = generateID()
}
return &FileServer{
store: NewStore(serverOpts),
FileServerOpts: opts,
quitCh: make(chan struct{}),
peers: make(map[string]p2p.Peer),
}
}
// broadcast sends a message to all connected peers in the network
func (s *FileServer) broadcast(msg *Message) error {
buff := new(bytes.Buffer)
if err := gob.NewEncoder(buff).Encode(msg); err != nil {
return err
}
for _, peer := range s.peers {
peer.Send([]byte{p2p.IncomingMessage})
if err := peer.Send(buff.Bytes()); err != nil {
return err
}
}
return nil
}
// Message is the generic message structure used for P2P communication.
// The Payload field contains the actual message data (e.g., MessageStoreFile,
// MessageGetFile, or MessageDeleteFile)
type Message struct {
Payload any
}
// MessageDeleteFile is the payload for requesting file deletion across peers
type MessageDeleteFile struct {
ID string
Key string
}
// MessageStoreFile is the payload for announcing a new file storage to peers
type MessageStoreFile struct {
ID string
Key string
Size int64
}
// MessageGetFile is the payload for requesting a file from another peer
type MessageGetFile struct {
ID string
Key string
}
// Delete removes the file with the given key from the local store and
// broadcasts the deletion request to all connected peers
func (s *FileServer) Delete(key string) error {
if !s.store.Has(s.ID, key) {
return fmt.Errorf("[%s] Don't have file %s locally, not checking on other peers", s.Transport.Addr(), key)
}
err := s.store.Delete(s.ID, key)
if err != nil {
return err
}
log.Printf("[%s] file %s deleted succesfullly, requesting other peers", s.Transport.Addr(), key)
msg := &Message{
Payload: MessageDeleteFile{
Key: hashKey(key),
ID: s.ID,
},
}
if err := s.broadcast(msg); err != nil {
return err
}
return nil
}
// Get retrieves a file with the given key. First checks local storage,
// and if not found, broadcasts a request to peers and waits for response
func (s *FileServer) Get(key string) (io.Reader, error) {
if s.store.Has(s.ID, key) {
_, buf, err := s.store.Read(s.ID, key)
if err != nil {
return nil, err
}
return buf, nil
}
log.Printf(
"[%s] Dont have file locally, fetching from network",
s.Transport.Addr())
msg := &Message{
Payload: MessageGetFile{
Key: hashKey(key),
ID: s.ID,
},
}
if err := s.broadcast(msg); err != nil {
return nil, err
}
time.Sleep(time.Millisecond * 100)
for _, peer := range s.peers {
var fSize int64
binary.Read(peer, binary.LittleEndian, &fSize)
n, err := s.store.writeDecrypt(s.EncryptionKey, s.ID, key, io.LimitReader(peer, fSize))
if err != nil {
return nil, err
}
fmt.Printf(
"[%s] Received %d bytes over the network.\n",
s.Transport.Addr(),
n)
peer.CloseStream()
}
_, buf, err := s.store.Read(s.ID, key)
return buf, err
}
// Store writes the file content from the provided io.Reader to local storage
// and broadcasts it to all connected peers
func (s *FileServer) Store(key string, r io.Reader) error {
var (
fileBuffer = new(bytes.Buffer)
tee = io.TeeReader(r, fileBuffer)
)
size, err := s.store.Write(s.ID, key, tee)
if err != nil {
return err
}
msg := &Message{
Payload: MessageStoreFile{
Key: hashKey(key),
Size: size + 16,
ID: s.ID,
},
}
if err := s.broadcast(msg); err != nil {
return err
}
time.Sleep(5 * time.Millisecond)
peers := []io.Writer{}
for _, peer := range s.peers {
peers = append(peers, peer)
}
// Mulitwriter enables writting to mulitple peers, for loop cannot be used
// here as reader becomes unreadable once it is read
mw := io.MultiWriter(peers...)
mw.Write([]byte{p2p.IncomingStream})
n, err := copyEncrypt(s.EncryptionKey, fileBuffer, mw)
if err != nil {
return err
}
fmt.Printf("[%s] Broadcasted %d bytes \n", s.Transport.Addr(), n)
return nil
}
// OnPeer is called when a new peer connects. It adds the peer to the
// peers map and logs the connection
func (s *FileServer) OnPeer(p p2p.Peer) error {
s.peerLock.Lock()
defer s.peerLock.Unlock()
s.peers[p.RemoteAddr().String()] = p
if !p.(*p2p.TCPPeer).Outbound {
log.Printf(
"Connected with remote(server - %s): %s\n",
s.Transport.(*p2p.TCPTransport).ListenAddr,
p.RemoteAddr())
}
return nil
}
// Start begins the file server by starting the transport layer to listen
// for incoming connections, bootstraps the network with known nodes,
// and starts the main message handling loop, it is a blocking function,
// it will return only when there is a error
func (s *FileServer) Start() error {
if err := s.Transport.ListenAndAccept(); err != nil {
return err
}
if err := s.BootStrapNetwork(); err != nil {
return err
}
s.loop()
return nil
}
// Quit signals the server to stop by closing the quit channel
func (s *FileServer) Quit() {
close(s.quitCh)
}
// BootStrapNetwork attempts to connect to all configured bootstrap nodes
// in separate goroutines. Each connection is attempted asynchronously
func (s *FileServer) BootStrapNetwork() error {
for _, addr := range s.bootstrapNodes {
if len(addr) == 0 {
continue
}
go func(addr string) {
log.Printf(
"Server %s is trying to connect to %s\n",
s.Transport.(*p2p.TCPTransport).ListenAddr,
addr)
err := s.Transport.Dial(addr)
if err != nil {
log.Printf("Dial Error: %s\n", err)
}
}(addr)
}
return nil
}
// handleMessage routes incoming messages to the appropriate handler
// based on the payload type (MessageStoreFile, MessageGetFile, or MessageDeleteFile)
func (s *FileServer) handleMessage(from string, msg *Message) error {
switch v := msg.Payload.(type) {
case MessageStoreFile:
return s.handleMessageStoreFile(from, v)
case MessageGetFile:
return s.handleMessageGetFile(from, v)
case MessageDeleteFile:
return s.handleMessageDeleteFile(from, v)
}
return nil
}
// handleMessageDeleteFile processes a file deletion request from a peer.
// It deletes the file from local storage if it exists
func (s *FileServer) handleMessageDeleteFile(from string, msg MessageDeleteFile) error {
if !s.store.Has(msg.ID, msg.Key) {
return fmt.Errorf(
"[%s] need to delete file %s, but it does not exists on disk",
s.Transport.Addr(),
msg.Key,
)
}
err := s.store.Delete(msg.ID, msg.Key)
if err != nil {
return fmt.Errorf("[%s] delete error: %s", s.Transport.Addr(), err)
}
log.Printf("[%s] file %s deleted succesfullly", s.Transport.Addr(), msg.Key)
return nil
}
// handleMessageGetFile processes a file retrieval request from a peer.
// If the file exists locally, it sends the file data to the requesting peer
func (s *FileServer) handleMessageGetFile(from string, msg MessageGetFile) error {
if !s.store.Has(msg.ID, msg.Key) {
return fmt.Errorf(
"[%s] need to serve file (%s), but it does not exists on disk",
s.Transport.Addr(),
msg.Key)
}
peer, ok := s.peers[from]
if !ok {
return fmt.Errorf("Peer %s could not be found in peer list", from)
}
fmt.Printf("[%s] Serve file %s over the network\n", s.Transport.Addr(), msg.Key)
fSize, r, err := s.store.Read(msg.ID, msg.Key)
if err != nil {
return err
}
rc, ok := r.(io.ReadCloser)
if ok {
defer func(rc io.ReadCloser) {
fmt.Println("Closing reader")
rc.Close()
}(rc)
}
peer.Send([]byte{p2p.IncomingStream})
binary.Write(peer, binary.LittleEndian, fSize)
n, err := io.Copy(peer, r)
if err != nil {
return err
}
log.Printf("[%s] Written %d bytes, to the network %s", s.Transport.Addr(), n, from)
return nil
}
// handleMessageStoreFile processes a file storage request from a peer.
// It reads the file data from the peer and writes it to local storage
func (s *FileServer) handleMessageStoreFile(from string, msg MessageStoreFile) error {
peer, ok := s.peers[from]
if !ok {
return fmt.Errorf("Peer %s could not be found in peer list", from)
}
n, err := s.store.Write(msg.ID, msg.Key, io.LimitReader(peer, msg.Size))
if err != nil {
return err
}
log.Printf("(%s) -> Written (%d) bytes to disk\n", s.Transport.Addr(), n)
peer.CloseStream()
return nil
}
// loop is the main event loop that continuously listens for incoming
// messages from the transport layer and dispatches them for handling
func (s *FileServer) loop() {
defer func() {
log.Println("File server stopped due to error or user action.")
s.Transport.Close()
}()
for {
select {
case <-s.quitCh:
return
case rpc := <-s.Transport.Consume():
var msg Message
if err := gob.NewDecoder(bytes.NewReader(rpc.Payload)).Decode(&msg); err != nil {
log.Println("Decoding error: ", err)
}
if err := s.handleMessage(rpc.From, &msg); err != nil {
log.Println("Handle message error: ", err)
}
}
}
}