2018-01-29 20:37:26 +01:00
|
|
|
package reflector
|
2017-08-11 00:25:42 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2018-01-24 17:45:18 +01:00
|
|
|
|
2018-02-22 19:48:46 +01:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
2018-06-07 05:48:07 +02:00
|
|
|
"github.com/lbryio/lbry.go/stopOnce"
|
|
|
|
"github.com/lbryio/reflector.go/store"
|
2018-01-24 17:45:18 +01:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2017-08-11 00:25:42 +02:00
|
|
|
)
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Server is and instance of the reflector server. It houses the blob store and listener.
|
2017-08-11 00:25:42 +02:00
|
|
|
type Server struct {
|
2018-05-29 23:19:40 +02:00
|
|
|
store store.BlobStore
|
|
|
|
closed bool
|
2018-06-07 05:48:07 +02:00
|
|
|
|
|
|
|
stop *stopOnce.Stopper
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// NewServer returns an initialized reflector server pointer.
|
2018-01-29 20:37:26 +01:00
|
|
|
func NewServer(store store.BlobStore) *Server {
|
2017-08-11 00:25:42 +02:00
|
|
|
return &Server{
|
2018-01-29 20:37:26 +01:00
|
|
|
store: store,
|
2018-06-07 05:48:07 +02:00
|
|
|
stop: stopOnce.New(),
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
// Shutdown shuts down the reflector server gracefully.
|
2018-05-29 23:19:40 +02:00
|
|
|
func (s *Server) Shutdown() {
|
2018-06-07 05:48:07 +02:00
|
|
|
log.Debug("shutting down reflector server...")
|
|
|
|
s.stop.StopAndWait()
|
2018-06-15 04:30:37 +02:00
|
|
|
log.Debug("reflector server stopped")
|
2018-05-29 23:19:40 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 05:48:07 +02:00
|
|
|
//Start starts the server listener to handle connections.
|
|
|
|
func (s *Server) Start(address string) error {
|
2018-05-30 03:38:55 +02:00
|
|
|
//ToDo - We should make this DRY as it is the same code in both servers.
|
2018-06-07 05:48:07 +02:00
|
|
|
log.Println("reflector listening on " + address)
|
2017-08-11 00:25:42 +02:00
|
|
|
l, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-30 03:38:55 +02:00
|
|
|
|
2018-06-07 05:48:07 +02:00
|
|
|
go s.listenForShutdown(l)
|
|
|
|
|
|
|
|
s.stop.Add(1)
|
|
|
|
go func() {
|
|
|
|
s.listenAndServe(l)
|
2018-06-15 04:30:37 +02:00
|
|
|
s.stop.Done()
|
2018-06-07 05:48:07 +02:00
|
|
|
}()
|
2017-08-11 00:25:42 +02:00
|
|
|
|
2018-06-07 05:48:07 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) listenForShutdown(listener net.Listener) {
|
|
|
|
<-s.stop.Ch()
|
|
|
|
s.closed = true
|
2018-06-15 04:30:37 +02:00
|
|
|
err := listener.Close()
|
|
|
|
if err != nil {
|
2018-06-07 05:48:07 +02:00
|
|
|
log.Error("error closing listener for peer server - ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) listenAndServe(listener net.Listener) {
|
2017-08-11 00:25:42 +02:00
|
|
|
for {
|
2018-06-07 05:48:07 +02:00
|
|
|
conn, err := listener.Accept()
|
2017-08-11 00:25:42 +02:00
|
|
|
if err != nil {
|
2018-05-29 23:19:40 +02:00
|
|
|
if s.closed {
|
2018-06-07 05:48:07 +02:00
|
|
|
return
|
2018-05-29 23:19:40 +02:00
|
|
|
}
|
2018-01-29 20:37:26 +01:00
|
|
|
log.Error(err)
|
|
|
|
} else {
|
2018-06-07 05:48:07 +02:00
|
|
|
s.stop.Add(1)
|
2018-06-15 04:30:37 +02:00
|
|
|
go func() {
|
|
|
|
s.handleConn(conn)
|
|
|
|
s.stop.Done()
|
|
|
|
}()
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleConn(conn net.Conn) {
|
2018-06-15 04:30:37 +02:00
|
|
|
defer func() {
|
|
|
|
if err := conn.Close(); err != nil {
|
|
|
|
log.Error(errors.Prefix("closing peer conn", err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-11 00:25:42 +02:00
|
|
|
// TODO: connection should time out eventually
|
2018-06-15 04:30:37 +02:00
|
|
|
|
2017-08-11 00:25:42 +02:00
|
|
|
err := s.doHandshake(conn)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
2018-06-15 04:30:37 +02:00
|
|
|
err := s.doError(conn, err)
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
log.Error("error sending error response to reflector client connection - ", err)
|
|
|
|
}
|
2017-08-11 00:25:42 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
err = s.receiveBlob(conn)
|
|
|
|
if err != nil {
|
2018-01-24 17:45:18 +01:00
|
|
|
if err != io.EOF {
|
2018-06-15 04:30:37 +02:00
|
|
|
err := s.doError(conn, err)
|
|
|
|
if err != nil {
|
2018-05-30 03:38:55 +02:00
|
|
|
log.Error("error sending error response for receiving a blob to reflector client connection - ", err)
|
|
|
|
}
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:45:18 +01:00
|
|
|
func (s *Server) doError(conn net.Conn, err error) error {
|
|
|
|
log.Errorln(err)
|
|
|
|
if e2, ok := err.(*json.SyntaxError); ok {
|
2017-08-11 00:25:42 +02:00
|
|
|
log.Printf("syntax error at byte offset %d", e2.Offset)
|
|
|
|
}
|
2018-01-24 17:45:18 +01:00
|
|
|
resp, err := json.Marshal(errorResponse{Error: err.Error()})
|
2017-08-11 00:25:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = conn.Write(resp)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) receiveBlob(conn net.Conn) error {
|
2017-08-15 22:02:18 +02:00
|
|
|
blobSize, blobHash, isSdBlob, err := s.readBlobRequest(conn)
|
2017-08-11 00:25:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-11 00:41:39 +02:00
|
|
|
blobExists := false
|
2018-01-29 20:37:26 +01:00
|
|
|
if !isSdBlob {
|
|
|
|
// we have to say sd blobs are missing because if we say we have it, they wont try to send any content blobs
|
|
|
|
has, err := s.store.Has(blobHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-08-15 22:02:18 +02:00
|
|
|
}
|
2018-01-29 20:37:26 +01:00
|
|
|
blobExists = has
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:18 +02:00
|
|
|
err = s.sendBlobResponse(conn, blobExists, isSdBlob)
|
2017-08-11 00:25:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-11 00:41:39 +02:00
|
|
|
if blobExists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
blob := make([]byte, blobSize)
|
2017-08-11 00:25:42 +02:00
|
|
|
_, err = io.ReadFull(bufio.NewReader(conn), blob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-11 00:41:39 +02:00
|
|
|
receivedBlobHash := getBlobHash(blob)
|
|
|
|
if blobHash != receivedBlobHash {
|
2018-01-24 17:45:18 +01:00
|
|
|
return errors.Err("hash of received blob data does not match hash from send request")
|
2017-08-15 22:02:18 +02:00
|
|
|
// this can also happen if the blob size is wrong, because the server will read the wrong number of bytes from the stream
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
2018-02-02 22:49:20 +01:00
|
|
|
|
2017-08-11 00:25:42 +02:00
|
|
|
log.Println("Got blob " + blobHash[:8])
|
|
|
|
|
2018-02-02 22:49:20 +01:00
|
|
|
if isSdBlob {
|
|
|
|
err = s.store.PutSD(blobHash, blob)
|
|
|
|
} else {
|
|
|
|
err = s.store.Put(blobHash, blob)
|
|
|
|
}
|
2017-08-11 00:25:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:18 +02:00
|
|
|
return s.sendTransferResponse(conn, true, isSdBlob)
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) doHandshake(conn net.Conn) error {
|
|
|
|
var handshake handshakeRequestResponse
|
2018-01-24 17:45:18 +01:00
|
|
|
err := json.NewDecoder(conn).Decode(&handshake)
|
2017-08-11 00:25:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-08-15 22:02:18 +02:00
|
|
|
} else if handshake.Version != protocolVersion1 && handshake.Version != protocolVersion2 {
|
2018-01-24 17:45:18 +01:00
|
|
|
return errors.Err("protocol version not supported")
|
2017-08-11 00:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := json.Marshal(handshakeRequestResponse{Version: handshake.Version})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = conn.Write(resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:18 +02:00
|
|
|
func (s *Server) readBlobRequest(conn net.Conn) (int, string, bool, error) {
|
2017-08-11 00:41:39 +02:00
|
|
|
var sendRequest sendBlobRequest
|
2018-01-24 17:45:18 +01:00
|
|
|
err := json.NewDecoder(conn).Decode(&sendRequest)
|
2017-08-11 00:41:39 +02:00
|
|
|
if err != nil {
|
2017-08-15 22:02:18 +02:00
|
|
|
return 0, "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sendRequest.SdBlobHash != "" && sendRequest.BlobHash != "" {
|
2018-01-24 17:45:18 +01:00
|
|
|
return 0, "", false, errors.Err("invalid request")
|
2017-08-15 22:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var blobHash string
|
|
|
|
var blobSize int
|
|
|
|
isSdBlob := sendRequest.SdBlobHash != ""
|
|
|
|
|
2018-01-29 20:37:26 +01:00
|
|
|
if blobSize > maxBlobSize {
|
|
|
|
return 0, "", isSdBlob, errors.Err("blob cannot be more than " + strconv.Itoa(maxBlobSize) + " bytes")
|
2018-01-24 17:45:18 +01:00
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:18 +02:00
|
|
|
if isSdBlob {
|
|
|
|
blobSize = sendRequest.SdBlobSize
|
|
|
|
blobHash = sendRequest.SdBlobHash
|
|
|
|
} else {
|
|
|
|
blobSize = sendRequest.BlobSize
|
|
|
|
blobHash = sendRequest.BlobHash
|
2017-08-11 00:41:39 +02:00
|
|
|
}
|
2017-08-15 22:02:18 +02:00
|
|
|
|
|
|
|
return blobSize, blobHash, isSdBlob, nil
|
2017-08-11 00:41:39 +02:00
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:18 +02:00
|
|
|
func (s *Server) sendBlobResponse(conn net.Conn, blobExists, isSdBlob bool) error {
|
|
|
|
var response []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if isSdBlob {
|
|
|
|
response, err = json.Marshal(sendSdBlobResponse{SendSdBlob: !blobExists})
|
|
|
|
} else {
|
|
|
|
response, err = json.Marshal(sendBlobResponse{SendBlob: !blobExists})
|
|
|
|
}
|
2017-08-11 00:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-15 22:02:18 +02:00
|
|
|
|
|
|
|
_, err = conn.Write(response)
|
2017-08-11 00:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-15 22:02:18 +02:00
|
|
|
func (s *Server) sendTransferResponse(conn net.Conn, receivedBlob, isSdBlob bool) error {
|
|
|
|
var response []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if isSdBlob {
|
|
|
|
response, err = json.Marshal(sdBlobTransferResponse{ReceivedSdBlob: receivedBlob})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
response, err = json.Marshal(blobTransferResponse{ReceivedBlob: receivedBlob})
|
|
|
|
}
|
2017-08-11 00:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-15 22:02:18 +02:00
|
|
|
|
|
|
|
_, err = conn.Write(response)
|
2017-08-11 00:41:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|