diff --git a/cmd/sendblob.go b/cmd/sendblob.go new file mode 100644 index 0000000..9668e92 --- /dev/null +++ b/cmd/sendblob.go @@ -0,0 +1,41 @@ +package cmd + +import ( + "crypto/rand" + + "github.com/lbryio/reflector.go/reflector" + + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func init() { + var cmd = &cobra.Command{ + Use: "sendblob ADDRESS:PORT", + Short: "Send a random blob to a reflector server", + Args: cobra.ExactArgs(1), + Run: sendBlobCmd, + } + rootCmd.AddCommand(cmd) +} + +func sendBlobCmd(cmd *cobra.Command, args []string) { + addr := args[0] + + c := reflector.Client{} + err := c.Connect(addr) + if err != nil { + log.Fatal("error connecting client to server", err) + } + + blob := make([]byte, 1024) + _, err = rand.Read(blob) + if err != nil { + log.Fatal("failed to make random blob", err) + } + + err = c.SendBlob(blob) + if err != nil { + log.Error(err) + } +} diff --git a/reflector/client.go b/reflector/client.go index 6ea6e55..3fe6cc9 100644 --- a/reflector/client.go +++ b/reflector/client.go @@ -47,7 +47,7 @@ func (c *Client) SendBlob(blob []byte) error { return errors.Err("blob is empty") } - blobHash := getBlobHash(blob) + blobHash := BlobHash(blob) sendRequest, err := json.Marshal(sendBlobRequest{ BlobSize: len(blob), BlobHash: blobHash, diff --git a/reflector/server.go b/reflector/server.go index 05f2fa2..ef7e621 100644 --- a/reflector/server.go +++ b/reflector/server.go @@ -191,16 +191,16 @@ func (s *Server) receiveBlob(conn net.Conn) error { blob, err := s.readRawBlob(conn, blobSize) if err != nil { - return err + return errors.Prefix("error reading blob "+blobHash[:8], err) } - receivedBlobHash := getBlobHash(blob) + receivedBlobHash := BlobHash(blob) if blobHash != receivedBlobHash { return errors.Err("hash of received blob data does not match hash from send request") // this can also happen if the blob size is wrong, because the server will read the wrong number of bytes from the stream } - log.Debugln("Got blob " + blobHash[:8]) + log.Infoln("Got blob " + blobHash[:8]) if isSdBlob { err = s.store.PutSD(blobHash, blob) @@ -337,7 +337,7 @@ func (s *Server) quitting() bool { } } -func getBlobHash(blob []byte) string { +func BlobHash(blob []byte) string { hashBytes := sha512.Sum384(blob) return hex.EncodeToString(hashBytes[:]) }