reflector.go/peer/server.go

220 lines
4.4 KiB
Go
Raw Normal View History

2018-01-29 20:37:26 +01:00
package peer
import (
"bufio"
2018-01-29 20:37:26 +01:00
"crypto/sha512"
"encoding/hex"
"encoding/json"
"io"
"net"
"strings"
"time"
2018-01-29 20:37:26 +01:00
"github.com/davecgh/go-spew/spew"
2018-01-29 20:37:26 +01:00
"github.com/lbryio/reflector.go/store"
log "github.com/sirupsen/logrus"
)
const (
2018-02-07 21:21:20 +01:00
DefaultPort = 3333
LbrycrdAddress = "bJxKvpD96kaJLriqVajZ7SaQTsWWyrGQct"
2018-01-29 20:37:26 +01:00
)
type Server struct {
store store.BlobStore
}
func NewServer(store store.BlobStore) *Server {
return &Server{
store: store,
}
}
func (s *Server) ListenAndServe(address string) error {
log.Println("Listening on " + address)
l, err := net.Listen("tcp", address)
if err != nil {
return err
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Error(err)
} else {
go s.handleConnection(conn)
2018-01-29 20:37:26 +01:00
}
}
}
func (s *Server) handleConnection(conn net.Conn) {
2018-01-29 20:37:26 +01:00
defer conn.Close()
timeoutDuration := 5 * time.Second
2018-01-29 20:37:26 +01:00
for {
var request []byte
var response []byte
var err error
conn.SetReadDeadline(time.Now().Add(timeoutDuration))
request, err = readNextRequest(conn)
2018-01-29 20:37:26 +01:00
if err != nil {
if err != io.EOF {
log.Errorln(err)
2018-01-29 20:37:26 +01:00
}
return
}
conn.SetReadDeadline(time.Time{})
if strings.Contains(string(request), `"requested_blobs"`) {
log.Debugln("received availability request")
response, err = s.handleAvailabilityRequest(request)
} else if strings.Contains(string(request), `"blob_data_payment_rate"`) {
log.Debugln("received rate negotiation request")
response, err = s.handlePaymentRateNegotiation(request)
} else if strings.Contains(string(request), `"requested_blob"`) {
log.Debugln("received blob request")
response, err = s.handleBlobRequest(request)
} else {
log.Errorln("invalid request")
spew.Dump(request)
return
}
if err != nil {
log.Error(err)
return
}
n, err := conn.Write(response)
if err != nil {
log.Errorln(err)
return
} else if n != len(response) {
log.Errorln(io.ErrShortWrite)
return
}
2018-01-29 20:37:26 +01:00
}
}
func (s *Server) handleAvailabilityRequest(data []byte) ([]byte, error) {
2018-01-29 20:37:26 +01:00
var request availabilityRequest
err := json.Unmarshal(data, &request)
2018-01-29 20:37:26 +01:00
if err != nil {
return []byte{}, err
2018-01-29 20:37:26 +01:00
}
availableBlobs := []string{}
for _, blobHash := range request.RequestedBlobs {
exists, err := s.store.Has(blobHash)
if err != nil {
return []byte{}, err
2018-01-29 20:37:26 +01:00
}
if exists {
availableBlobs = append(availableBlobs, blobHash)
}
}
2018-02-07 21:21:20 +01:00
return json.Marshal(availabilityResponse{LbrycrdAddress: LbrycrdAddress, AvailableBlobs: availableBlobs})
2018-01-29 20:37:26 +01:00
}
func (s *Server) handlePaymentRateNegotiation(data []byte) ([]byte, error) {
2018-01-29 20:37:26 +01:00
var request paymentRateRequest
err := json.Unmarshal(data, &request)
2018-01-29 20:37:26 +01:00
if err != nil {
return []byte{}, err
2018-01-29 20:37:26 +01:00
}
offerReply := paymentRateAccepted
if request.BlobDataPaymentRate < 0 {
offerReply = paymentRateTooLow
}
return json.Marshal(paymentRateResponse{BlobDataPaymentRate: offerReply})
2018-01-29 20:37:26 +01:00
}
func (s *Server) handleBlobRequest(data []byte) ([]byte, error) {
2018-01-29 20:37:26 +01:00
var request blobRequest
err := json.Unmarshal(data, &request)
2018-01-29 20:37:26 +01:00
if err != nil {
return []byte{}, err
2018-01-29 20:37:26 +01:00
}
log.Println("Sending blob " + request.RequestedBlob[:8])
blob, err := s.store.Get(request.RequestedBlob)
if err != nil {
return []byte{}, err
2018-01-29 20:37:26 +01:00
}
response, err := json.Marshal(blobResponse{IncomingBlob: incomingBlob{
BlobHash: getBlobHash(blob),
Length: len(blob),
}})
if err != nil {
return []byte{}, err
2018-01-29 20:37:26 +01:00
}
return append(response, blob...), nil
2018-01-29 20:37:26 +01:00
}
func readNextRequest(conn net.Conn) ([]byte, error) {
request := make([]byte, 0)
eof := false
buf := bufio.NewReader(conn)
2018-01-29 20:37:26 +01:00
for {
chunk, err := buf.ReadBytes('}')
2018-01-29 20:37:26 +01:00
if err != nil {
if err != io.EOF {
log.Errorln("read error:", err)
return request, err
}
eof = true
}
//log.Debugln("got", len(chunk), "bytes.")
//spew.Dump(chunk)
if len(chunk) > 0 {
request = append(request, chunk...)
if len(request) > maxRequestSize {
return request, errRequestTooLarge
}
// yes, this is how the peer protocol knows when the request finishes
if isValidJSON(request) {
break
2018-01-29 20:37:26 +01:00
}
}
if eof {
2018-01-29 20:37:26 +01:00
break
}
}
//log.Debugln("total size:", len(request))
//if len(request) > 0 {
// spew.Dump(request)
//}
if len(request) == 0 && eof {
return []byte{}, io.EOF
2018-01-29 20:37:26 +01:00
}
return request, nil
}
func isValidJSON(b []byte) bool {
var r json.RawMessage
return json.Unmarshal(b, &r) == nil
2018-01-29 20:37:26 +01:00
}
func getBlobHash(blob []byte) string {
hashBytes := sha512.Sum384(blob)
return hex.EncodeToString(hashBytes[:])
}