From 081d3752d85b69dc94234ef18a5fedcb3d980276 Mon Sep 17 00:00:00 2001 From: "Lawrence, Rendall" Date: Mon, 25 Apr 2022 16:05:19 +0300 Subject: [PATCH] (tested) fix static check warnings * UDP writer: defer buffer close * HTTP writer: remove duplicated compact4/6 --- frontend/http/writer.go | 35 +++++++++++++---------------------- frontend/udp/writer.go | 10 +++++----- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/frontend/http/writer.go b/frontend/http/writer.go index b88fade..79a785e 100644 --- a/frontend/http/writer.go +++ b/frontend/http/writer.go @@ -25,7 +25,7 @@ func WriteError(w http.ResponseWriter, err error) { if err = bencode.NewEncoder(w).Encode(map[string]any{ "failure reason": message, }); err != nil { - log.Error("unable to encode string", log.Err(err)) + log.Error("unable to encode message", log.Err(err)) } } @@ -50,21 +50,21 @@ func WriteAnnounceResponse(w http.ResponseWriter, resp *bittorrent.AnnounceRespo // Add the peers to the dictionary in the compact format. if resp.Compact { // Add the IPv4 peers to the dictionary. - ipv4CompactDict := make([]byte, 0, (net.IPv4len+2)*len(resp.IPv4Peers)) + compactAddresses := make([]byte, 0, (net.IPv4len+2)*len(resp.IPv4Peers)) for _, peer := range resp.IPv4Peers { - ipv4CompactDict = append(ipv4CompactDict, compact4(peer)...) + compactAddresses = append(compactAddresses, compactAddress(peer)...) } - if len(ipv4CompactDict) > 0 { - bdict["peers"] = ipv4CompactDict + if len(compactAddresses) > 0 { + bdict["peers"] = compactAddresses } // Add the IPv6 peers to the dictionary. - ipv6CompactDict := make([]byte, 0, (net.IPv6len+2)*len(resp.IPv6Peers)) // IP + port + compactAddresses = make([]byte, 0, (net.IPv6len+2)*len(resp.IPv6Peers)) // IP + port for _, peer := range resp.IPv6Peers { - ipv6CompactDict = append(ipv6CompactDict, compact6(peer)...) + compactAddresses = append(compactAddresses, compactAddress(peer)...) } - if len(ipv6CompactDict) > 0 { - bdict["peers6"] = ipv6CompactDict + if len(compactAddresses) > 0 { + bdict["peers6"] = compactAddresses } } else { // Add the peers to the dictionary. @@ -97,25 +97,16 @@ func WriteScrapeResponse(w http.ResponseWriter, resp *bittorrent.ScrapeResponse) }) } -func compact4(peer bittorrent.Peer) (buf []byte) { - ip := peer.Addr().As4() - buf = append(buf, ip[:]...) +func compactAddress(peer bittorrent.Peer) (buf []byte) { + buf = append(buf, peer.Addr().AsSlice()...) port := peer.Port() - buf = append(buf, byte(port>>8), byte(port&0xff)) - return -} - -func compact6(peer bittorrent.Peer) (buf []byte) { - ip := peer.Addr().As16() - buf = append(buf, ip[:]...) - port := peer.Port() - buf = append(buf, byte(port>>8), byte(port&0xff)) + buf = append(buf, byte(port>>8), byte(port)) return } func dict(peer bittorrent.Peer) map[string]any { return map[string]any{ - "peer id": string(peer.ID[:]), + "peer id": peer.ID.RawString(), "ip": peer.Addr(), "port": peer.Port(), } diff --git a/frontend/udp/writer.go b/frontend/udp/writer.go index aa65e46..fe860d3 100644 --- a/frontend/udp/writer.go +++ b/frontend/udp/writer.go @@ -14,16 +14,16 @@ import ( func WriteError(w io.Writer, txID []byte, err error) { // If the client wasn't at fault, acknowledge it. var clientErr bittorrent.ClientError - if !errors.As(err, &clientErr) { + if !errors.Is(err, &clientErr) { err = fmt.Errorf("internal error occurred: %w", err) } buf := newBuffer() + defer buf.free() writeHeader(buf, txID, errorActionID) _, _ = buf.WriteString(err.Error()) _, _ = buf.WriteRune('\000') _, _ = w.Write(buf.Bytes()) - buf.free() } // WriteAnnounce encodes an announce response according to BEP 15. @@ -33,6 +33,7 @@ func WriteError(w io.Writer, txID []byte, err error) { // https://web.archive.org/web/20170503181830/http://opentracker.blog.h3q.com/2007/12/28/the-ipv6-situation/ func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse, v6Action, v6Peers bool) { buf := newBuffer() + defer buf.free() if v6Action { writeHeader(buf, txID, announceV6ActionID) @@ -54,12 +55,12 @@ func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse, } _, _ = w.Write(buf.Bytes()) - buf.free() } // WriteScrape encodes a scrape response according to BEP 15. func WriteScrape(w io.Writer, txID []byte, resp *bittorrent.ScrapeResponse) { buf := newBuffer() + defer buf.free() writeHeader(buf, txID, scrapeActionID) @@ -70,18 +71,17 @@ func WriteScrape(w io.Writer, txID []byte, resp *bittorrent.ScrapeResponse) { } _, _ = w.Write(buf.Bytes()) - buf.free() } // WriteConnectionID encodes a new connection response according to BEP 15. func WriteConnectionID(w io.Writer, txID, connID []byte) { buf := newBuffer() + defer buf.free() writeHeader(buf, txID, connectActionID) _, _ = buf.Write(connID) _, _ = w.Write(buf.Bytes()) - buf.free() } // writeHeader writes the action and transaction ID to the provided response