From 249dba391d9ed5dafcc361346dbe73faeb35f06c Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 25 Apr 2024 12:43:29 +0200 Subject: [PATCH] Support gzip compression to fetch source files --- dnscrypt-proxy/sources.go | 2 +- dnscrypt-proxy/xtransport.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/dnscrypt-proxy/sources.go b/dnscrypt-proxy/sources.go index 30bd3cb4..2bf83415 100644 --- a/dnscrypt-proxy/sources.go +++ b/dnscrypt-proxy/sources.go @@ -131,7 +131,7 @@ func (source *Source) parseURLs(urls []string) { } func fetchFromURL(xTransport *XTransport, u *url.URL) ([]byte, error) { - bin, _, _, _, err := xTransport.Get(u, "", DefaultTimeout) + bin, _, _, _, err := xTransport.GetWithCompression(u, "", DefaultTimeout) return bin, err } diff --git a/dnscrypt-proxy/xtransport.go b/dnscrypt-proxy/xtransport.go index e0efd780..249a514e 100644 --- a/dnscrypt-proxy/xtransport.go +++ b/dnscrypt-proxy/xtransport.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "compress/gzip" "context" "crypto/sha512" "crypto/tls" @@ -482,6 +483,7 @@ func (xTransport *XTransport) Fetch( contentType string, body *[]byte, timeout time.Duration, + compress bool, ) ([]byte, int, *tls.ConnectionState, time.Duration, error) { if timeout <= 0 { timeout = xTransport.timeout @@ -530,6 +532,9 @@ func (xTransport *XTransport) Fetch( ) return nil, 0, nil, 0, err } + if compress && body == nil { + header["Accept-Encoding"] = []string{"gzip"} + } req := &http.Request{ Method: method, URL: url, @@ -596,7 +601,17 @@ func (xTransport *XTransport) Fetch( } } tls := resp.TLS - bin, err := io.ReadAll(io.LimitReader(resp.Body, MaxHTTPBodyLength)) + + var bodyReader io.ReadCloser = resp.Body + if compress && resp.Header.Get("Content-Encoding") == "gzip" { + bodyReader, err = gzip.NewReader(io.LimitReader(resp.Body, MaxHTTPBodyLength)) + if err != nil { + return nil, statusCode, tls, rtt, err + } + defer bodyReader.Close() + } + + bin, err := io.ReadAll(io.LimitReader(bodyReader, MaxHTTPBodyLength)) if err != nil { return nil, statusCode, tls, rtt, err } @@ -604,12 +619,20 @@ func (xTransport *XTransport) Fetch( return bin, statusCode, tls, rtt, err } +func (xTransport *XTransport) GetWithCompression( + url *url.URL, + accept string, + timeout time.Duration, +) ([]byte, int, *tls.ConnectionState, time.Duration, error) { + return xTransport.Fetch("GET", url, accept, "", nil, timeout, true) +} + func (xTransport *XTransport) Get( url *url.URL, accept string, timeout time.Duration, ) ([]byte, int, *tls.ConnectionState, time.Duration, error) { - return xTransport.Fetch("GET", url, accept, "", nil, timeout) + return xTransport.Fetch("GET", url, accept, "", nil, timeout, false) } func (xTransport *XTransport) Post( @@ -619,7 +642,7 @@ func (xTransport *XTransport) Post( body *[]byte, timeout time.Duration, ) ([]byte, int, *tls.ConnectionState, time.Duration, error) { - return xTransport.Fetch("POST", url, accept, contentType, body, timeout) + return xTransport.Fetch("POST", url, accept, contentType, body, timeout, false) } func (xTransport *XTransport) dohLikeQuery(