"net/url"
"time"
+ "github.com/anacrolix/missinggo"
+
"github.com/anacrolix/torrent/util"
)
Scrape
Error
+ connectRequestConnectionId = 0x41727101980
+
// BEP 41
optionTypeEndOfOptions = 0
optionTypeNOP = 1
res.Interval = h.Interval
res.Leechers = h.Leechers
res.Seeders = h.Seeders
- for {
- var p util.CompactPeer
- err = binary.Read(b, binary.BigEndian, &p)
- switch err {
- case nil:
- case io.EOF:
- err = nil
- fallthrough
- default:
- return
- }
+ cps, err := util.UnmarshalIPv4CompactPeers(b.Bytes())
+ if err != nil {
+ return
+ }
+ for _, cp := range cps {
res.Peers = append(res.Peers, Peer{
- IP: p.IP[:],
- Port: int(p.Port),
+ IP: cp.IP[:],
+ Port: int(cp.Port),
})
}
+ return
}
// body is the binary serializable request body. trailer is optional data
// following it, such as for BEP 41.
func (c *udpClient) write(h *RequestHeader, body interface{}, trailer []byte) (err error) {
- buf := &bytes.Buffer{}
- err = binary.Write(buf, binary.BigEndian, h)
+ var buf bytes.Buffer
+ err = binary.Write(&buf, binary.BigEndian, h)
if err != nil {
panic(err)
}
if body != nil {
- err = binary.Write(buf, binary.BigEndian, body)
+ err = binary.Write(&buf, binary.BigEndian, body)
if err != nil {
panic(err)
}
// args is the binary serializable request body. trailer is optional data
// following it, such as for BEP 41.
-func (c *udpClient) request(action Action, args interface{}, options []byte) (responseBody *bytes.Reader, err error) {
+func (c *udpClient) request(action Action, args interface{}, options []byte) (responseBody *bytes.Buffer, err error) {
tid := newTransactionId()
err = c.write(&RequestHeader{
ConnectionId: c.connectionId,
if h.Action == Error {
err = errors.New(buf.String())
}
- responseBody = bytes.NewReader(buf.Bytes())
+ responseBody = buf
return
}
}
-func readBody(r *bytes.Reader, data ...interface{}) (err error) {
+func readBody(r io.Reader, data ...interface{}) (err error) {
for _, datum := range data {
err = binary.Read(r, binary.BigEndian, datum)
if err != nil {
if c.connected() {
return nil
}
- c.connectionId = 0x41727101980
+ c.connectionId = connectRequestConnectionId
if c.socket == nil {
- c.socket, err = net.Dial("udp", c.url.Host)
+ hmp := missinggo.SplitHostPort(c.url.Host)
+ if hmp.NoPort {
+ hmp.NoPort = false
+ hmp.Port = 80
+ }
+ c.socket, err = net.Dial("udp", hmp.String())
if err != nil {
return
}