Esempio n. 1
0
func (r *httpResponse) UnmarshalPeers() (ret []Peer, err error) {
	s, ok := r.Peers.(string)
	if !ok {
		err = fmt.Errorf("unsupported peers value type: %T", r.Peers)
		return
	}
	cp, err := util.UnmarshalIPv4CompactPeers([]byte(s))
	if err != nil {
		return
	}
	ret = make([]Peer, 0, len(cp))
	for _, p := range cp {
		ret = append(ret, Peer{net.IP(p.IP[:]), int(p.Port)})
	}
	return
}
Esempio n. 2
0
func (c *udpClient) Announce(req *AnnounceRequest) (res AnnounceResponse, err error) {
	if !c.connected() {
		err = ErrNotConnected
		return
	}
	reqURI := c.url.RequestURI()
	// Clearly this limits the request URI to 255 bytes. BEP 41 supports
	// longer but I'm not fussed.
	options := append([]byte{optionTypeURLData, byte(len(reqURI))}, []byte(reqURI)...)
	b, err := c.request(Announce, req, options)
	if err != nil {
		return
	}
	var h AnnounceResponseHeader
	err = readBody(b, &h)
	if err != nil {
		if err == io.EOF {
			err = io.ErrUnexpectedEOF
		}
		err = fmt.Errorf("error parsing announce response: %s", err)
		return
	}
	res.Interval = h.Interval
	res.Leechers = h.Leechers
	res.Seeders = h.Seeders
	cps, err := util.UnmarshalIPv4CompactPeers(b.Bytes())
	if err != nil {
		return
	}
	for _, cp := range cps {
		res.Peers = append(res.Peers, Peer{
			IP:   cp.IP[:],
			Port: int(cp.Port),
		})
	}
	return
}