| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
|
| 8 |
"github.com/pkg/errors" |
| 9 |
) |
| 10 |
|
| 11 |
// ProtocolServer serves up chunks from a local store using the casync protocol |
| 12 |
type ProtocolServer struct { |
| 13 |
p *Protocol |
| 14 |
store Store |
| 15 |
} |
| 16 |
|
| 17 |
// NewProtocolServer returns an initialized server that can serve chunks from |
| 18 |
// a chunk store via the casync protocol |
| 19 |
func NewProtocolServer(r io.Reader, w io.Writer, s Store) *ProtocolServer { |
| 20 |
return &ProtocolServer{ |
| 21 |
p: NewProtocol(r, w), |
| 22 |
store: s, |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
// Serve starts the protocol server. Blocks unless an error is encountered |
| 27 |
func (s *ProtocolServer) Serve(ctx context.Context) error { |
| 28 |
flags, err := s.p.Initialize(CaProtocolReadableStore) |
| 29 |
if err != nil { |
| 30 |
return errors.Wrap(err, "failed to perform protocol handshake") |
| 31 |
} |
| 32 |
if flags&CaProtocolPullChunks == 0 { |
| 33 |
return fmt.Errorf("client is not requesting chunks, provided flags %x", flags) |
| 34 |
} |
| 35 |
for { |
| 36 |
// See if we're meant to stop |
| 37 |
select { |
| 38 |
case <-ctx.Done(): |
| 39 |
return nil |
| 40 |
default: |
| 41 |
} |
| 42 |
m, err := s.p.ReadMessage() |
| 43 |
if err != nil { |
| 44 |
return errors.Wrap(err, "failed to read protocol message from client") |
| 45 |
} |
| 46 |
switch m.Type { |
| 47 |
case CaProtocolRequest: |
| 48 |
if len(m.Body) < 40 { |
| 49 |
return errors.New("protocol request too small") |
| 50 |
} |
| 51 |
id, err := ChunkIDFromSlice(m.Body[8:40]) |
| 52 |
if err != nil { |
| 53 |
return errors.Wrap(err, "unable to decode requested chunk id") |
| 54 |
} |
| 55 |
chunk, err := s.store.GetChunk(id) |
| 56 |
if err != nil { |
| 57 |
if _, ok := err.(ChunkMissing); !ok { |
| 58 |
return errors.Wrap(err, "unable to read chunk from store") |
| 59 |
} |
| 60 |
if err = s.p.SendMissing(id); err != nil { |
| 61 |
return errors.Wrap(err, "failed to send to client") |
| 62 |
} |
| 63 |
continue |
| 64 |
} |
| 65 |
b, err := chunk.Storage([]converter{Compressor{}}) |
| 66 |
if err != nil { |
| 67 |
// The chunk is in the store but can't be converted to the |
| 68 |
// wire format, perhaps corrupt or encrypted with a different |
| 69 |
// key. Report it missing so the client can deal with the one |
| 70 |
// chunk instead of tearing down the whole session. |
| 71 |
Log.WithField("chunk", id).WithError(err).Error("unable to convert chunk to wire format") |
| 72 |
if err = s.p.SendMissing(id); err != nil { |
| 73 |
return errors.Wrap(err, "failed to send to client") |
| 74 |
} |
| 75 |
continue |
| 76 |
} |
| 77 |
if err := s.p.SendProtocolChunk(chunk.ID(), CaProtocolChunkCompressed, b); err != nil { |
| 78 |
return errors.Wrap(err, "failed to send chunk data") |
| 79 |
} |
| 80 |
case CaProtocolAbort: |
| 81 |
return errors.New("client aborted connection") |
| 82 |
case CaProtocolGoodbye: |
| 83 |
return nil |
| 84 |
default: |
| 85 |
return fmt.Errorf("unexpected command (%x) from client", m.Type) |
| 86 |
} |
| 87 |
} |
| 88 |
} |