From 3f23ff5c0874ebdaed0fe28e8e51604835fe7a74 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 2 Feb 2023 19:38:24 +0100 Subject: [PATCH] Mostly get rid of ioutil --- dnscrypt-proxy/common.go | 3 +-- dnscrypt-proxy/local-doh.go | 3 +-- dnscrypt-proxy/sources.go | 5 ++--- dnscrypt-proxy/sources_test.go | 6 +++--- dnscrypt-proxy/xtransport.go | 8 ++++---- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/dnscrypt-proxy/common.go b/dnscrypt-proxy/common.go index 18ee350e..0eed4efd 100644 --- a/dnscrypt-proxy/common.go +++ b/dnscrypt-proxy/common.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/binary" "errors" - "io/ioutil" "net" "os" "strconv" @@ -161,7 +160,7 @@ func ExtractHostAndPort(str string, defaultPort int) (host string, port int) { } func ReadTextFile(filename string) (string, error) { - bin, err := ioutil.ReadFile(filename) + bin, err := os.ReadFile(filename) if err != nil { return "", err } diff --git a/dnscrypt-proxy/local-doh.go b/dnscrypt-proxy/local-doh.go index 3c1ee23a..ac6c7672 100644 --- a/dnscrypt-proxy/local-doh.go +++ b/dnscrypt-proxy/local-doh.go @@ -4,7 +4,6 @@ import ( "encoding/base64" "fmt" "io" - "io/ioutil" "net" "net/http" "strings" @@ -36,7 +35,7 @@ func (handler localDoHHandler) ServeHTTP(writer http.ResponseWriter, request *ht start := time.Now() if request.Method == "POST" && request.Header.Get("Content-Type") == dataType { - packet, err = ioutil.ReadAll(io.LimitReader(request.Body, int64(MaxDNSPacketSize))) + packet, err = io.ReadAll(io.LimitReader(request.Body, int64(MaxDNSPacketSize))) if err != nil { dlog.Warnf("No body in a local DoH query") return diff --git a/dnscrypt-proxy/sources.go b/dnscrypt-proxy/sources.go index 811704ac..05ff352f 100644 --- a/dnscrypt-proxy/sources.go +++ b/dnscrypt-proxy/sources.go @@ -3,7 +3,6 @@ package main import ( "bytes" "fmt" - "io/ioutil" "math/rand" "net/url" "os" @@ -54,10 +53,10 @@ var timeNow = time.Now func (source *Source) fetchFromCache(now time.Time) (delay time.Duration, err error) { var bin, sig []byte - if bin, err = ioutil.ReadFile(source.cacheFile); err != nil { + if bin, err = os.ReadFile(source.cacheFile); err != nil { return } - if sig, err = ioutil.ReadFile(source.cacheFile + ".minisig"); err != nil { + if sig, err = os.ReadFile(source.cacheFile + ".minisig"); err != nil { return } if err = source.checkSignature(bin, sig); err != nil { diff --git a/dnscrypt-proxy/sources_test.go b/dnscrypt-proxy/sources_test.go index fbbc0fef..fc604013 100644 --- a/dnscrypt-proxy/sources_test.go +++ b/dnscrypt-proxy/sources_test.go @@ -69,7 +69,7 @@ type SourceTestExpect struct { } func readFixture(t *testing.T, name string) []byte { - bin, err := ioutil.ReadFile(filepath.Join("testdata", name)) + bin, err := os.ReadFile(filepath.Join("testdata", name)) if err != nil { t.Fatalf("Unable to read test fixture %s: %v", name, err) } @@ -86,7 +86,7 @@ func writeSourceCache(t *testing.T, e *SourceTestExpect) { if perms == 0 { perms = 0644 } - if err := ioutil.WriteFile(path, f.content, perms); err != nil { + if err := os.WriteFile(path, f.content, perms); err != nil { t.Fatalf("Unable to write cache file %s: %v", path, err) } if err := acl.Chmod(path, perms); err != nil { @@ -109,7 +109,7 @@ func checkSourceCache(c *check.C, e *SourceTestExpect) { for _, f := range e.cache { path := e.cachePath + f.suffix _ = acl.Chmod(path, 0644) // don't worry if this fails, reading it will catch the same problem - got, err := ioutil.ReadFile(path) + got, err := os.ReadFile(path) c.DeepEqual(got, f.content, "Unexpected content for cache file '%s', err %v", path, err) if f.suffix != "" { continue diff --git a/dnscrypt-proxy/xtransport.go b/dnscrypt-proxy/xtransport.go index e365fa27..21bc707e 100644 --- a/dnscrypt-proxy/xtransport.go +++ b/dnscrypt-proxy/xtransport.go @@ -10,11 +10,11 @@ import ( "encoding/hex" "errors" "io" - "io/ioutil" "math/rand" "net" "net/http" "net/url" + "os" "strconv" "strings" "sync" @@ -179,7 +179,7 @@ func (xTransport *XTransport) rebuildTransport() { if certPool == nil { dlog.Fatalf("Additional CAs not supported on this platform: %v", certPoolErr) } - additionalCaCert, err := ioutil.ReadFile(clientCreds.rootCA) + additionalCaCert, err := os.ReadFile(clientCreds.rootCA) if err != nil { dlog.Fatal(err) } @@ -470,7 +470,7 @@ func (xTransport *XTransport) Fetch( } if body != nil { req.ContentLength = int64(len(*body)) - req.Body = ioutil.NopCloser(bytes.NewReader(*body)) + req.Body = io.NopCloser(bytes.NewReader(*body)) } start := time.Now() resp, err := client.Do(req) @@ -528,7 +528,7 @@ func (xTransport *XTransport) Fetch( } } tls := resp.TLS - bin, err := ioutil.ReadAll(io.LimitReader(resp.Body, MaxHTTPBodyLength)) + bin, err := io.ReadAll(io.LimitReader(resp.Body, MaxHTTPBodyLength)) if err != nil { return nil, statusCode, tls, rtt, err }