From 3ae384394466990fbe0f78064a65fd68b76fdada Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Fri, 20 Jan 2017 20:34:39 +0100 Subject: [PATCH] bittorrent: add AddressField to ScrapeRequest --- bittorrent/bittorrent.go | 5 +++-- frontend/http/frontend.go | 10 +++------- frontend/udp/frontend.go | 12 ++++-------- middleware/hooks.go | 8 +------- 4 files changed, 11 insertions(+), 24 deletions(-) diff --git a/bittorrent/bittorrent.go b/bittorrent/bittorrent.go index c7a7b0b..14971d3 100644 --- a/bittorrent/bittorrent.go +++ b/bittorrent/bittorrent.go @@ -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. diff --git a/frontend/http/frontend.go b/frontend/http/frontend.go index a2f0bdd..f4f0d34 100644 --- a/frontend/http/frontend.go +++ b/frontend/http/frontend.go @@ -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 diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go index 69975b8..419e36d 100644 --- a/frontend/udp/frontend.go +++ b/frontend/udp/frontend.go @@ -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 diff --git a/middleware/hooks.go b/middleware/hooks.go index bba0aa0..5ac7525 100644 --- a/middleware/hooks.go +++ b/middleware/hooks.go @@ -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