2019-09-11 18:30:01 +02:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2020-10-15 02:59:12 +02:00
|
|
|
"github.com/lbryio/reflector.go/internal/metrics"
|
2021-01-09 05:08:20 +01:00
|
|
|
"github.com/lbryio/reflector.go/shared"
|
2019-09-27 20:38:49 +02:00
|
|
|
"github.com/lbryio/reflector.go/store"
|
|
|
|
|
2019-11-14 01:11:35 +01:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/stream"
|
2019-09-11 18:30:01 +02:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client is an instance of a client connected to a server.
|
|
|
|
type Client struct {
|
|
|
|
Timeout time.Duration
|
|
|
|
conn net.Conn
|
|
|
|
buf *bufio.Reader
|
|
|
|
connected bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect connects to a specific clients and errors if it cannot be contacted.
|
|
|
|
func (c *Client) Connect(address string) error {
|
|
|
|
var err error
|
|
|
|
if c.Timeout == 0 {
|
|
|
|
c.Timeout = 5 * time.Second
|
|
|
|
}
|
|
|
|
c.conn, err = net.Dial("tcp4", address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.connected = true
|
|
|
|
c.buf = bufio.NewReader(c.conn)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the connection with the client.
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
c.connected = false
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStream gets a stream
|
2019-09-27 20:38:49 +02:00
|
|
|
func (c *Client) GetStream(sdHash string, blobCache store.BlobStore) (stream.Stream, error) {
|
2019-09-11 18:30:01 +02:00
|
|
|
if !c.connected {
|
|
|
|
return nil, errors.Err("not connected")
|
|
|
|
}
|
|
|
|
|
|
|
|
var sd stream.SDBlob
|
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
b, trace, err := c.GetBlob(sdHash)
|
2019-09-11 18:30:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-09 05:08:20 +01:00
|
|
|
log.Debug(trace.String())
|
2019-09-11 18:30:01 +02:00
|
|
|
|
|
|
|
err = sd.FromBlob(b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := make(stream.Stream, len(sd.BlobInfos)+1-1) // +1 for sd blob, -1 for last null blob
|
|
|
|
s[0] = b
|
|
|
|
|
|
|
|
for i := 0; i < len(sd.BlobInfos)-1; i++ {
|
2021-01-09 05:08:20 +01:00
|
|
|
s[i+1], trace, err = c.GetBlob(hex.EncodeToString(sd.BlobInfos[i].BlobHash))
|
2019-09-11 18:30:01 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-09 05:08:20 +01:00
|
|
|
log.Debug(trace.String())
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2019-10-03 22:13:08 +02:00
|
|
|
// HasBlob checks if the blob is available
|
|
|
|
func (c *Client) HasBlob(hash string) (bool, error) {
|
|
|
|
if !c.connected {
|
|
|
|
return false, errors.Err("not connected")
|
2019-09-27 20:38:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:13:08 +02:00
|
|
|
sendRequest, err := json.Marshal(availabilityRequest{
|
|
|
|
RequestedBlobs: []string{hash},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2019-09-27 20:38:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:13:08 +02:00
|
|
|
err = c.write(sendRequest)
|
2019-09-27 20:38:49 +02:00
|
|
|
if err != nil {
|
2019-10-03 22:13:08 +02:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp availabilityResponse
|
|
|
|
err = c.read(&resp)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
2019-09-27 20:38:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-03 22:13:08 +02:00
|
|
|
for _, h := range resp.AvailableBlobs {
|
|
|
|
if h == hash {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 20:38:49 +02:00
|
|
|
|
2019-10-03 22:13:08 +02:00
|
|
|
return false, nil
|
2019-09-27 20:38:49 +02:00
|
|
|
}
|
|
|
|
|
2019-09-11 18:30:01 +02:00
|
|
|
// GetBlob gets a blob
|
2021-01-09 05:08:20 +01:00
|
|
|
func (c *Client) GetBlob(hash string) (stream.Blob, shared.BlobTrace, error) {
|
|
|
|
start := time.Now()
|
2019-09-11 18:30:01 +02:00
|
|
|
if !c.connected {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, shared.NewBlobTrace(time.Since(start), "tcp"), errors.Err("not connected")
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sendRequest, err := json.Marshal(blobRequest{
|
2019-09-27 20:38:49 +02:00
|
|
|
RequestedBlob: hash,
|
2019-09-11 18:30:01 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, shared.NewBlobTrace(time.Since(start), "tcp"), err
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = c.write(sendRequest)
|
|
|
|
if err != nil {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, shared.NewBlobTrace(time.Since(start), "tcp"), err
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var resp blobResponse
|
|
|
|
err = c.read(&resp)
|
|
|
|
if err != nil {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, shared.NewBlobTrace(time.Since(start), "tcp"), err
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
|
2021-01-09 05:08:20 +01:00
|
|
|
trace := shared.NewBlobTrace(time.Since(start), "tcp")
|
|
|
|
if resp.RequestTrace != nil {
|
|
|
|
trace = *resp.RequestTrace
|
|
|
|
}
|
2019-09-11 18:30:01 +02:00
|
|
|
if resp.IncomingBlob.Error != "" {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, trace, errors.Prefix(hash[:8], resp.IncomingBlob.Error)
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
2019-09-27 20:38:49 +02:00
|
|
|
if resp.IncomingBlob.BlobHash != hash {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, trace.Stack(time.Since(start), "tcp"), errors.Prefix(hash[:8], "blob hash in response does not match requested hash")
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
if resp.IncomingBlob.Length <= 0 {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, trace, errors.Prefix(hash[:8], "length reported as <= 0")
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
|
2019-12-16 15:52:51 +01:00
|
|
|
log.Debugf("receiving blob %s from %s", hash[:8], c.conn.RemoteAddr())
|
2019-09-11 18:30:01 +02:00
|
|
|
|
|
|
|
blob, err := c.readRawBlob(resp.IncomingBlob.Length)
|
|
|
|
if err != nil {
|
2021-01-09 05:08:20 +01:00
|
|
|
return nil, (*resp.RequestTrace).Stack(time.Since(start), "tcp"), err
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
2020-10-15 02:59:12 +02:00
|
|
|
metrics.MtrInBytesTcp.Add(float64(len(blob)))
|
2021-01-09 05:08:20 +01:00
|
|
|
return blob, trace.Stack(time.Since(start), "tcp"), nil
|
2019-09-11 18:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) read(v interface{}) error {
|
|
|
|
err := c.conn.SetReadDeadline(time.Now().Add(c.Timeout))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := readNextMessage(c.buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-16 15:52:51 +01:00
|
|
|
log.Debugf("read %d bytes from %s", len(m), c.conn.RemoteAddr())
|
2019-10-03 22:13:08 +02:00
|
|
|
|
2019-09-11 18:30:01 +02:00
|
|
|
err = json.Unmarshal(m, v)
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) readRawBlob(blobSize int) ([]byte, error) {
|
|
|
|
err := c.conn.SetReadDeadline(time.Now().Add(c.Timeout))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
blob := make([]byte, blobSize)
|
2019-10-03 22:13:08 +02:00
|
|
|
n, err := io.ReadFull(c.buf, blob)
|
2019-12-16 15:52:51 +01:00
|
|
|
log.Debugf("read %d bytes from %s", n, c.conn.RemoteAddr())
|
2019-09-11 18:30:01 +02:00
|
|
|
return blob, errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) write(b []byte) error {
|
|
|
|
err := c.conn.SetWriteDeadline(time.Now().Add(c.Timeout))
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
|
2019-12-16 15:52:51 +01:00
|
|
|
log.Debugf("writing %d bytes to %s", len(b), c.conn.RemoteAddr())
|
2019-09-11 18:30:01 +02:00
|
|
|
|
|
|
|
n, err := c.conn.Write(b)
|
|
|
|
if err == nil && n != len(b) {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
}
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|