reflector.go/reflector/blocklist.go

155 lines
3.1 KiB
Go
Raw Normal View History

2018-09-01 01:50:22 +02:00
package reflector
import (
"encoding/hex"
"encoding/json"
"net/http"
"strconv"
"strings"
2018-09-20 17:24:36 +02:00
"time"
2018-09-01 01:50:22 +02:00
2018-09-20 17:24:36 +02:00
"github.com/lbryio/reflector.go/store"
2018-09-01 01:50:22 +02:00
"github.com/lbryio/reflector.go/wallet"
2019-01-09 23:52:30 +01:00
"github.com/lbryio/lbry.go/extras/errors"
2018-09-01 01:50:22 +02:00
types "github.com/lbryio/types/go"
"github.com/golang/protobuf/proto"
2018-09-20 17:24:36 +02:00
log "github.com/sirupsen/logrus"
2018-09-01 01:50:22 +02:00
)
const blocklistURL = "https://api.lbry.io/file/list_blocked"
2018-09-20 17:24:36 +02:00
func (s *Server) enableBlocklist(b store.Blocklister) {
updateBlocklist(b)
t := time.NewTicker(12 * time.Hour)
for {
select {
case <-s.grp.Ch():
return
case <-t.C:
updateBlocklist(b)
}
}
2018-09-01 01:50:22 +02:00
}
2018-09-20 17:24:36 +02:00
func updateBlocklist(b store.Blocklister) {
values, err := blockedSdHashes()
if err != nil {
log.Error(err)
return
}
for _, v := range values {
if v.Err != nil {
continue
}
err = b.Block(v.Value)
if err != nil {
log.Error(err)
}
}
}
2018-09-20 20:24:30 +02:00
func blockedSdHashes() (map[string]valOrErr, error) {
2018-09-01 01:50:22 +02:00
resp, err := http.Get(blocklistURL)
if err != nil {
return nil, errors.Err(err)
}
2018-09-24 16:31:14 +02:00
defer func() {
err := resp.Body.Close()
if err != nil {
log.Errorln(err)
}
}()
2018-09-01 01:50:22 +02:00
2018-09-20 17:24:36 +02:00
var r struct {
Success bool `json:"success"`
Error string `json:"error"`
Data struct {
Outpoints []string `json:"outpoints"`
} `json:"data"`
}
2018-09-01 01:50:22 +02:00
if err = json.NewDecoder(resp.Body).Decode(&r); err != nil {
return nil, errors.Err(err)
}
if !r.Success {
return nil, errors.Prefix("list_blocked API call", r.Error)
}
2018-09-11 13:41:29 +02:00
return sdHashesForOutpoints(r.Data.Outpoints)
}
2018-09-01 01:50:22 +02:00
2018-09-20 20:24:30 +02:00
type valOrErr struct {
2018-09-11 13:41:29 +02:00
Value string
Err error
2018-09-01 01:50:22 +02:00
}
2018-09-11 13:41:29 +02:00
// sdHashesForOutpoints queries wallet server for the sd hashes in a given outpoints
2018-09-20 20:24:30 +02:00
func sdHashesForOutpoints(outpoints []string) (map[string]valOrErr, error) {
values := make(map[string]valOrErr)
2018-09-01 01:50:22 +02:00
node := wallet.NewNode()
2018-09-20 17:24:36 +02:00
err := node.Connect([]string{
"victor.lbry.tech:50001",
//"lbryumx1.lbry.io:50001", // cant use real servers until victor pushes bugfix
//"lbryumx2.lbry.io:50001",
}, nil)
2018-09-01 01:50:22 +02:00
if err != nil {
return nil, err
}
2018-09-11 13:41:29 +02:00
for _, outpoint := range outpoints {
parts := strings.Split(outpoint, ":")
if len(parts) != 2 {
2018-09-20 20:24:30 +02:00
values[outpoint] = valOrErr{Err: errors.Err("invalid outpoint format")}
2018-09-11 13:41:29 +02:00
continue
}
nout, err := strconv.Atoi(parts[1])
if err != nil {
2018-09-20 20:24:30 +02:00
values[outpoint] = valOrErr{Err: errors.Prefix("invalid nout", err)}
2018-09-11 13:41:29 +02:00
continue
}
resp, err := node.GetClaimsInTx(parts[0])
if err != nil {
2018-09-20 20:24:30 +02:00
values[outpoint] = valOrErr{Err: err}
2018-09-11 13:41:29 +02:00
continue
}
var value []byte
for _, tx := range resp.Result {
if tx.Nout != nout {
continue
}
2018-09-01 01:50:22 +02:00
2018-09-11 13:41:29 +02:00
value, err = hex.DecodeString(tx.Value)
break
2018-09-01 01:50:22 +02:00
}
2018-09-11 13:41:29 +02:00
if err != nil {
2018-09-20 20:24:30 +02:00
values[outpoint] = valOrErr{Err: err}
2018-09-11 13:41:29 +02:00
continue
}
claim := &types.Claim{}
err = proto.Unmarshal(value, claim)
if err != nil {
2018-09-20 20:24:30 +02:00
values[outpoint] = valOrErr{Err: err}
2018-09-11 13:41:29 +02:00
continue
}
if claim.GetStream().GetSource().GetSourceType() != types.Source_lbry_sd_hash {
2018-09-20 20:24:30 +02:00
values[outpoint] = valOrErr{Err: errors.Err("source is nil or source type is not lbry_sd_hash")}
2018-09-11 13:41:29 +02:00
continue
}
2018-09-20 20:24:30 +02:00
values[outpoint] = valOrErr{Value: hex.EncodeToString(claim.GetStream().GetSource().GetSource())}
2018-09-01 01:50:22 +02:00
}
2018-09-11 13:41:29 +02:00
return values, nil
2018-09-01 01:50:22 +02:00
}