Don't return error while converting hash to v1

Mark clientapproval as broken because of incorrect client_id parsing
This commit is contained in:
Širhoe Biazhkovič
2021-10-27 16:48:28 +03:00
committed by Lawrence, Rendall
parent 2f092bad45
commit af1cbc543c
6 changed files with 46 additions and 48 deletions

View File

@@ -0,0 +1,27 @@
// Package clientapproval XXX: implementation is broken, client ID is NOT 6 static bytes
// refer:
// - https://wiki.theory.org/BitTorrentSpecification#peer_id
// - https://github.com/webtorrent/bittorrent-peerid/blob/master/lib/utils.js
package clientapproval
import "github.com/chihaya/chihaya/bittorrent"
// ClientID represents the part of a PeerID that identifies a Peer's client
// software.
type ClientID [6]byte
// NewClientID parses a ClientID from a PeerID.
func NewClientID(pid bittorrent.PeerID) ClientID {
var cid ClientID
if pid[0] == '-' {
copy(cid[:], pid[1:7])
} else {
copy(cid[:], pid[:6])
}
return cid
}
func (cid ClientID) String() string {
return string(cid[:])
}

View File

@@ -0,0 +1,56 @@
package clientapproval
import (
"github.com/chihaya/chihaya/bittorrent"
"testing"
)
func TestClientID(t *testing.T) {
var clientTable = []struct{ peerID, clientID string }{
{"-AZ3034-6wfG2wk6wWLc", "AZ3034"},
{"-AZ3042-6ozMq5q6Q3NX", "AZ3042"},
{"-BS5820-oy4La2MWGEFj", "BS5820"},
{"-AR6360-6oZyyMWoOOBe", "AR6360"},
{"-AG2083-s1hiF8vGAAg0", "AG2083"},
{"-AG3003-lEl2Mm4NEO4n", "AG3003"},
{"-MR1100-00HS~T7*65rm", "MR1100"},
{"-LK0140-ATIV~nbEQAMr", "LK0140"},
{"-KT2210-347143496631", "KT2210"},
{"-TR0960-6ep6svaa61r4", "TR0960"},
{"-XX1150-dv220cotgj4d", "XX1150"},
{"-AZ2504-192gwethivju", "AZ2504"},
{"-KT4310-3L4UvarKuqIu", "KT4310"},
{"-AZ2060-0xJQ02d4309O", "AZ2060"},
{"-BD0300-2nkdf08Jd890", "BD0300"},
{"-A~0010-a9mn9DFkj39J", "A~0010"},
{"-UT2300-MNu93JKnm930", "UT2300"},
{"-UT2300-KT4310KT4301", "UT2300"},
{"T03A0----f089kjsdf6e", "T03A0-"},
{"S58B-----nKl34GoNb75", "S58B--"},
{"M4-4-0--9aa757Efd5Bl", "M4-4-0"},
{"AZ2500BTeYUzyabAfo6U", "AZ2500"}, // BitTyrant
{"exbc0JdSklm834kj9Udf", "exbc0J"}, // Old BitComet
{"FUTB0L84j542mVc84jkd", "FUTB0L"}, // Alt BitComet
{"XBT054d-8602Jn83NnF9", "XBT054"}, // XBT
{"OP1011affbecbfabeefb", "OP1011"}, // Opera
{"-ML2.7.2-kgjjfkd9762", "ML2.7."}, // MLDonkey
{"-BOWA0C-SDLFJWEIORNM", "BOWA0C"}, // Bits on Wheels
{"Q1-0-0--dsn34DFn9083", "Q1-0-0"}, // Queen Bee
{"Q1-10-0-Yoiumn39BDfO", "Q1-10-"}, // Queen Bee Alt
{"346------SDFknl33408", "346---"}, // TorreTopia
{"QVOD0054ABFFEDCCDEDB", "QVOD00"}, // Qvod
}
for _, tt := range clientTable {
t.Run(tt.peerID, func(t *testing.T) {
var clientID ClientID
copy(clientID[:], tt.clientID)
parsedID := NewClientID(bittorrent.PeerIDFromString(tt.peerID))
if parsedID != clientID {
t.Error("Incorrectly parsed peer ID", tt.peerID, "as", parsedID)
}
})
}
}

View File

@@ -44,15 +44,15 @@ type Config struct {
}
type hook struct {
approved map[bittorrent.ClientID]struct{}
unapproved map[bittorrent.ClientID]struct{}
approved map[ClientID]struct{}
unapproved map[ClientID]struct{}
}
// NewHook returns an instance of the client approval middleware.
func NewHook(cfg Config) (middleware.Hook, error) {
h := &hook{
approved: make(map[bittorrent.ClientID]struct{}),
unapproved: make(map[bittorrent.ClientID]struct{}),
approved: make(map[ClientID]struct{}),
unapproved: make(map[ClientID]struct{}),
}
if len(cfg.Whitelist) > 0 && len(cfg.Blacklist) > 0 {
@@ -64,7 +64,7 @@ func NewHook(cfg Config) (middleware.Hook, error) {
if len(cidBytes) != 6 {
return nil, errors.New("client ID " + cidString + " must be 6 bytes")
}
var cid bittorrent.ClientID
var cid ClientID
copy(cid[:], cidBytes)
h.approved[cid] = struct{}{}
}
@@ -74,7 +74,7 @@ func NewHook(cfg Config) (middleware.Hook, error) {
if len(cidBytes) != 6 {
return nil, errors.New("client ID " + cidString + " must be 6 bytes")
}
var cid bittorrent.ClientID
var cid ClientID
copy(cid[:], cidBytes)
h.unapproved[cid] = struct{}{}
}
@@ -83,7 +83,7 @@ func NewHook(cfg Config) (middleware.Hook, error) {
}
func (h *hook) HandleAnnounce(ctx context.Context, req *bittorrent.AnnounceRequest, resp *bittorrent.AnnounceResponse) (context.Context, error) {
clientID := bittorrent.NewClientID(req.Peer.ID)
clientID := NewClientID(req.Peer.ID)
if len(h.approved) > 0 {
if _, found := h.approved[clientID]; !found {