bittorrent: add AddressField to ScrapeRequest

This commit is contained in:
Leo Balduf 2017-01-20 20:34:39 +01:00
parent 3c098c0703
commit 3ae3843944
4 changed files with 11 additions and 24 deletions

View file

@ -94,8 +94,9 @@ type AnnounceResponse struct {
// ScrapeRequest represents the parsed parameters from a scrape request.
type ScrapeRequest struct {
InfoHashes []InfoHash
Params Params
AddressFamily AddressFamily
InfoHashes []InfoHash
Params Params
}
// ScrapeResponse represents the parameters used to create a scrape response.

View file

@ -15,7 +15,6 @@ import (
"github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/frontend"
"github.com/chihaya/chihaya/middleware"
)
func init() {
@ -177,20 +176,17 @@ func (t *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprou
}
reqIP := net.ParseIP(host)
af := bittorrent.IPv4
if reqIP.To4() != nil {
af = bittorrent.IPv4
req.AddressFamily = bittorrent.IPv4
} else if len(reqIP) == net.IPv6len { // implies reqIP.To4() == nil
af = bittorrent.IPv6
req.AddressFamily = bittorrent.IPv6
} else {
log.Errorln("http: invalid IP: neither v4 nor v6, RemoteAddr was", r.RemoteAddr)
WriteError(w, ErrInvalidIP)
return
}
ctx := context.WithValue(context.Background(), middleware.ScrapeIsIPv6Key, af == bittorrent.IPv6)
resp, err := t.logic.HandleScrape(ctx, req)
resp, err := t.logic.HandleScrape(context.Background(), req)
if err != nil {
WriteError(w, err)
return

View file

@ -16,7 +16,6 @@ import (
"github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/frontend"
"github.com/chihaya/chihaya/frontend/udp/bytepool"
"github.com/chihaya/chihaya/middleware"
)
func init() {
@ -232,21 +231,18 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
return
}
af := bittorrent.IPv4
if r.IP.To4() != nil {
af = bittorrent.IPv4
req.AddressFamily = bittorrent.IPv4
} else if len(r.IP) == net.IPv6len { // implies r.IP.To4() == nil
af = bittorrent.IPv6
req.AddressFamily = bittorrent.IPv6
} else {
log.Errorln("http: invalid IP: neither v4 nor v6, IP was", r.IP)
log.Errorln("udp: invalid IP: neither v4 nor v6, IP was", r.IP)
WriteError(w, txID, ErrInvalidIP)
return
}
ctx := context.WithValue(context.Background(), middleware.ScrapeIsIPv6Key, af == bittorrent.IPv6)
var resp *bittorrent.ScrapeResponse
resp, err = t.logic.HandleScrape(ctx, req)
resp, err = t.logic.HandleScrape(context.Background(), req)
if err != nil {
WriteError(w, txID, err)
return

View file

@ -182,14 +182,8 @@ func (h *responseHook) HandleScrape(ctx context.Context, req *bittorrent.ScrapeR
return ctx, nil
}
v6, _ := ctx.Value(ScrapeIsIPv6Key).(bool)
for _, infoHash := range req.InfoHashes {
if v6 {
resp.Files[infoHash] = h.store.ScrapeSwarm(infoHash, bittorrent.IPv6)
} else {
resp.Files[infoHash] = h.store.ScrapeSwarm(infoHash, bittorrent.IPv4)
}
resp.Files[infoHash] = h.store.ScrapeSwarm(infoHash, req.AddressFamily)
}
return ctx, nil