From 7716aa828a13dba57342837fdf81c3fbaa1cc16f Mon Sep 17 00:00:00 2001 From: "Lawrence, Rendall" Date: Sat, 16 Apr 2022 21:22:41 +0300 Subject: [PATCH] (minor) Merge commits 7a4c2ee..77b3bf9 from https://github.com/jzelinskie/chihaya --- cmd/mochi/e2e.go | 5 ++--- frontend/http/writer.go | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cmd/mochi/e2e.go b/cmd/mochi/e2e.go index 20955d3..19c5c91 100644 --- a/cmd/mochi/e2e.go +++ b/cmd/mochi/e2e.go @@ -9,7 +9,6 @@ import ( "time" "github.com/anacrolix/torrent/tracker" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/sot-tech/mochi/bittorrent" @@ -97,7 +96,7 @@ func testWithInfohash(infoHash bittorrent.InfoHash, url string, delay time.Durat UserAgent: "mochi-e2e", }.Do() if err != nil { - return errors.Wrap(err, "announce failed") + return fmt.Errorf("announce failed: %w", err) } if len(resp.Peers) != 1 { @@ -125,7 +124,7 @@ func testWithInfohash(infoHash bittorrent.InfoHash, url string, delay time.Durat UserAgent: "mochi-e2e", }.Do() if err != nil { - return errors.Wrap(err, "announce failed") + return fmt.Errorf("announce failed: %w", err) } if len(resp.Peers) != 1 { diff --git a/frontend/http/writer.go b/frontend/http/writer.go index ae1bac2..bc452ae 100644 --- a/frontend/http/writer.go +++ b/frontend/http/writer.go @@ -2,6 +2,7 @@ package http import ( "errors" + "net" "net/http" "time" @@ -49,26 +50,26 @@ func WriteAnnounceResponse(w http.ResponseWriter, resp *bittorrent.AnnounceRespo // Add the peers to the dictionary in the compact format. if resp.Compact { - var IPv4CompactDict, IPv6CompactDict []byte - // Add the IPv4 peers to the dictionary. + ipv4CompactDict := make([]byte, 0, (net.IPv4len+2)*len(resp.IPv4Peers)) for _, peer := range resp.IPv4Peers { - IPv4CompactDict = append(IPv4CompactDict, compact4(peer)...) + ipv4CompactDict = append(ipv4CompactDict, compact4(peer)...) } - if len(IPv4CompactDict) > 0 { - bdict["peers"] = IPv4CompactDict + if len(ipv4CompactDict) > 0 { + bdict["peers"] = ipv4CompactDict } // Add the IPv6 peers to the dictionary. + ipv6CompactDict := make([]byte, 0, (net.IPv6len+2)*len(resp.IPv6Peers)) // IP + port for _, peer := range resp.IPv6Peers { - IPv6CompactDict = append(IPv6CompactDict, compact6(peer)...) + ipv6CompactDict = append(ipv6CompactDict, compact6(peer)...) } - if len(IPv6CompactDict) > 0 { - bdict["peers6"] = IPv6CompactDict + if len(ipv6CompactDict) > 0 { + bdict["peers6"] = ipv6CompactDict } } else { // Add the peers to the dictionary. - peers := make([]map[string]any, 0, len(resp.IPv4Peers)+len(resp.IPv6Peers)) + peers := make([]map[string]any, 0, len(resp.IPv4Peers)+len(resp.IPv6Peers)) // IP + port for _, peer := range resp.IPv4Peers { peers = append(peers, dict(peer)) }