Support marshalling http tracker response peers

This commit is contained in:
Matt Joiner 2022-12-05 17:53:36 +11:00
parent b81423c101
commit 98d5a19d50
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
4 changed files with 41 additions and 16 deletions

View File

@ -133,10 +133,10 @@ func (cl Client) Announce(ctx context.Context, ar AnnounceRequest, opt AnnounceO
ret.Interval = trackerResponse.Interval
ret.Leechers = trackerResponse.Incomplete
ret.Seeders = trackerResponse.Complete
if len(trackerResponse.Peers) != 0 {
if len(trackerResponse.Peers.List) != 0 {
vars.Add("http responses with nonempty peers key", 1)
}
ret.Peers = trackerResponse.Peers
ret.Peers = trackerResponse.Peers.List
if len(trackerResponse.Peers6) != 0 {
vars.Add("http responses with nonempty peers6 key", 1)
}

View File

@ -22,12 +22,12 @@ func TestUnmarshalHTTPResponsePeerDicts(t *testing.T) {
"e"),
&hr))
require.Len(t, hr.Peers, 2)
assert.Equal(t, []byte("thisisthe20bytepeeri"), hr.Peers[0].ID)
assert.EqualValues(t, 9999, hr.Peers[0].Port)
assert.EqualValues(t, 9998, hr.Peers[1].Port)
assert.NotNil(t, hr.Peers[0].IP)
assert.NotNil(t, hr.Peers[1].IP)
require.Len(t, hr.Peers.List, 2)
assert.Equal(t, []byte("thisisthe20bytepeeri"), hr.Peers.List[0].ID)
assert.EqualValues(t, 9999, hr.Peers.List[0].Port)
assert.EqualValues(t, 9998, hr.Peers.List[1].Port)
assert.NotNil(t, hr.Peers.List[0].IP)
assert.NotNil(t, hr.Peers.List[1].IP)
assert.Len(t, hr.Peers6, 1)
assert.EqualValues(t, "1234123412341234", hr.Peers6[0].IP)
@ -40,7 +40,7 @@ func TestUnmarshalHttpResponseNoPeers(t *testing.T) {
[]byte("d6:peers618:123412341234123456e"),
&hr,
))
require.Len(t, hr.Peers, 0)
require.Len(t, hr.Peers.List, 0)
assert.Len(t, hr.Peers6, 1)
}

View File

@ -8,9 +8,9 @@ import (
)
type Peer struct {
IP net.IP
Port int
ID []byte
IP net.IP `bencode:"ip"`
Port int `bencode:"port"`
ID []byte `bencode:"peer id"`
}
func (p Peer) String() string {

View File

@ -19,7 +19,30 @@ type HttpResponse struct {
Peers6 krpc.CompactIPv6NodeAddrs `bencode:"peers6"`
}
type Peers []Peer
type Peers struct {
List []Peer
Compact bool
}
func (me Peers) MarshalBencode() ([]byte, error) {
if me.Compact {
cnas := make([]krpc.NodeAddr, 0, len(me.List))
for _, peer := range me.List {
cnas = append(cnas, krpc.NodeAddr{
IP: peer.IP,
Port: peer.Port,
})
}
return krpc.CompactIPv4NodeAddrs(cnas).MarshalBencode()
} else {
return bencode.Marshal(me.List)
}
}
var (
_ bencode.Unmarshaler = (*Peers)(nil)
_ bencode.Marshaler = Peers{}
)
func (me *Peers) UnmarshalBencode(b []byte) (err error) {
var _v interface{}
@ -35,19 +58,21 @@ func (me *Peers) UnmarshalBencode(b []byte) (err error) {
if err != nil {
return
}
me.Compact = true
for _, cp := range cnas {
*me = append(*me, Peer{
me.List = append(me.List, Peer{
IP: cp.IP[:],
Port: int(cp.Port),
Port: cp.Port,
})
}
return
case []interface{}:
vars.Add("http responses with list peers", 1)
me.Compact = false
for _, i := range v {
var p Peer
p.FromDictInterface(i.(map[string]interface{}))
*me = append(*me, p)
me.List = append(me.List, p)
}
return
default: