Exemple #1
0
// newScrape parses an HTTP request and generates a models.Scrape.
func (s *Server) newScrape(r *http.Request, p httprouter.Params) (*models.Scrape, error) {
	q, err := query.New(r.URL.RawQuery)
	if err != nil {
		return nil, err
	}

	if q.Infohashes == nil {
		if _, exists := q.Params["info_hash"]; !exists {
			// There aren't any infohashes.
			return nil, models.ErrMalformedRequest
		}
		q.Infohashes = []string{q.Params["info_hash"]}
	}

	return &models.Scrape{
		Config:     s.config,
		Infohashes: q.Infohashes,
	}, nil
}
Exemple #2
0
// newAnnounce parses an HTTP request and generates a models.Announce.
func (s *Server) newAnnounce(r *http.Request, p httprouter.Params) (*models.Announce, error) {
	q, err := query.New(r.URL.RawQuery)
	if err != nil {
		return nil, err
	}

	compact := q.Params["compact"] != "0"
	event, _ := q.Params["event"]
	numWant := requestedPeerCount(q, s.config.NumWantFallback)

	infohash, exists := q.Params["info_hash"]
	if !exists {
		return nil, models.ErrMalformedRequest
	}

	peerID, exists := q.Params["peer_id"]
	if !exists {
		return nil, models.ErrMalformedRequest
	}

	port, err := q.Uint64("port")
	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	left, err := q.Uint64("left")
	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	ipv4, ipv6, err := requestedIP(q, r, &s.config.NetConfig)
	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	ipv4Endpoint := models.Endpoint{ipv4, uint16(port)}
	ipv6Endpoint := models.Endpoint{ipv6, uint16(port)}

	downloaded, err := q.Uint64("downloaded")
	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	uploaded, err := q.Uint64("uploaded")
	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	return &models.Announce{
		Config:     s.config,
		Compact:    compact,
		Downloaded: downloaded,
		Event:      event,
		IPv4:       ipv4Endpoint,
		IPv6:       ipv6Endpoint,
		Infohash:   infohash,
		Left:       left,
		NumWant:    numWant,
		PeerID:     peerID,
		Uploaded:   uploaded,
	}, nil
}
Exemple #3
0
// newAnnounce parses an HTTP request and generates a models.Announce.
func (s *Server) newAnnounce(r *http.Request, p httprouter.Params) (*models.Announce, error) {
	q, err := query.New(r.URL.RawQuery)
	if err != nil {
		return nil, err
	}

	compact := q.Params["compact"] != "0"
	event, _ := q.Params["event"]
	numWant := requestedPeerCount(q, s.config.NumWantFallback)

	infohash, exists := q.Params["info_hash"]
	if !exists {
		return nil, models.ErrMalformedRequest
	}

	peerID, exists := q.Params["peer_id"]
	if !exists {
		return nil, models.ErrMalformedRequest
	}

	port, err := q.Uint64("port")
	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	left, err := q.Uint64("left")

	ipv4, ipv6, err := requestedEndpoint(q, r, &s.config.NetConfig)
	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	var ipv4Endpoint, ipv6Endpoint models.Endpoint
	if ipv4 != nil {
		ipv4Endpoint = *ipv4
		// If the port we couldn't get the port before, fallback to the port param.
		if ipv4Endpoint.Port == uint16(0) {
			ipv4Endpoint.Port = uint16(port)
		}
	}
	if ipv6 != nil {
		ipv6Endpoint = *ipv6
		// If the port we couldn't get the port before, fallback to the port param.
		if ipv6Endpoint.Port == uint16(0) {
			ipv6Endpoint.Port = uint16(port)
		}
	}

	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	downloaded, err := q.Uint64("downloaded")
	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	uploaded, err := q.Uint64("uploaded")
	if err != nil {
		return nil, models.ErrMalformedRequest
	}

	return &models.Announce{
		Config:     s.config,
		Compact:    compact,
		Downloaded: downloaded,
		Event:      event,
		IPv4:       ipv4Endpoint,
		IPv6:       ipv6Endpoint,
		Infohash:   infohash,
		Left:       left,
		NumWant:    numWant,
		Passkey:    p.ByName("passkey"),
		PeerID:     peerID,
		Uploaded:   uploaded,
	}, nil
}