From b1f6c324a697f1da346e61b4f1fd6e4bbf52fe85 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Mon, 27 Sep 2021 00:36:34 +0800 Subject: [PATCH] refactor: move from io/ioutil to io and os package The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun --- app/dns/nameserver_doh.go | 5 ++--- common/buf/multi_buffer_test.go | 3 +-- common/common.go | 6 +++--- common/drain/drainer.go | 3 +-- common/log/logger_test.go | 3 +-- infra/conf/geodata/memconservative/cache.go | 6 +++--- infra/control/certchainhash.go | 4 ++-- infra/control/config.go | 5 ++--- infra/vprotogen/main.go | 3 +-- main/confloader/external/external.go | 5 ++--- main/main.go | 3 +-- testing/scenarios/common.go | 4 ++-- testing/scenarios/feature_test.go | 4 ++-- testing/scenarios/http_test.go | 7 +++---- 14 files changed, 26 insertions(+), 35 deletions(-) diff --git a/app/dns/nameserver_doh.go b/app/dns/nameserver_doh.go index ec86884a1d9..e8e0d59e7e4 100644 --- a/app/dns/nameserver_doh.go +++ b/app/dns/nameserver_doh.go @@ -8,7 +8,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "net/url" "sync" @@ -283,11 +282,11 @@ func (s *DoHNameServer) dohHTTPSContext(ctx context.Context, b []byte) ([]byte, defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - io.Copy(ioutil.Discard, resp.Body) // flush resp.Body so that the conn is reusable + io.Copy(io.Discard, resp.Body) // flush resp.Body so that the conn is reusable return nil, fmt.Errorf("DOH server returned code %d", resp.StatusCode) } - return ioutil.ReadAll(resp.Body) + return io.ReadAll(resp.Body) } func (s *DoHNameServer) findIPsForDomain(domain string, option dns_feature.IPOption) ([]net.IP, error) { diff --git a/common/buf/multi_buffer_test.go b/common/buf/multi_buffer_test.go index cfd35523505..866462c4ff8 100644 --- a/common/buf/multi_buffer_test.go +++ b/common/buf/multi_buffer_test.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/rand" "io" - "io/ioutil" "os" "testing" @@ -120,7 +119,7 @@ func TestMultiBufferReadAllToByte(t *testing.T) { common.Must(err) f.Close() - cnt, err := ioutil.ReadFile(dat) + cnt, err := os.ReadFile(dat) common.Must(err) if d := cmp.Diff(buf2, cnt); d != "" { diff --git a/common/common.go b/common/common.go index c73d8f14527..edb9e74a6d7 100644 --- a/common/common.go +++ b/common/common.go @@ -5,7 +5,7 @@ package common import ( "fmt" "go/build" - "io/ioutil" + "io" "net/http" "net/url" "os" @@ -70,7 +70,7 @@ func GetRuntimeEnv(key string) (string, error) { } var data []byte var runtimeEnv string - data, readErr := ioutil.ReadFile(file) + data, readErr := os.ReadFile(file) if readErr != nil { return "", readErr } @@ -153,7 +153,7 @@ func FetchHTTPContent(target string) ([]byte, error) { return nil, newError("unexpected HTTP status code: ", resp.StatusCode) } - content, err := ioutil.ReadAll(resp.Body) + content, err := io.ReadAll(resp.Body) if err != nil { return nil, newError("failed to read HTTP response").Base(err) } diff --git a/common/drain/drainer.go b/common/drain/drainer.go index 5ed887abc6b..e92c4363d12 100644 --- a/common/drain/drainer.go +++ b/common/drain/drainer.go @@ -2,7 +2,6 @@ package drain import ( "io" - "io/ioutil" "github.com/v2fly/v2ray-core/v4/common/dice" ) @@ -36,7 +35,7 @@ func (d *BehaviorSeedLimitedDrainer) Drain(reader io.Reader) error { } func drainReadN(reader io.Reader, n int) error { - _, err := io.CopyN(ioutil.Discard, reader, int64(n)) + _, err := io.CopyN(io.Discard, reader, int64(n)) return err } diff --git a/common/log/logger_test.go b/common/log/logger_test.go index fcb91ad6c5d..87b08006c08 100644 --- a/common/log/logger_test.go +++ b/common/log/logger_test.go @@ -1,7 +1,6 @@ package log_test import ( - "io/ioutil" "os" "strings" "testing" @@ -13,7 +12,7 @@ import ( ) func TestFileLogger(t *testing.T) { - f, err := ioutil.TempFile("", "vtest") + f, err := os.CreateTemp("", "vtest") common.Must(err) path := f.Name() common.Must(f.Close()) diff --git a/infra/conf/geodata/memconservative/cache.go b/infra/conf/geodata/memconservative/cache.go index 0632c0ad401..5fd7fb35a28 100644 --- a/infra/conf/geodata/memconservative/cache.go +++ b/infra/conf/geodata/memconservative/cache.go @@ -1,7 +1,7 @@ package memconservative import ( - "io/ioutil" + "os" "strings" "google.golang.org/protobuf/proto" @@ -53,7 +53,7 @@ func (g GeoIPCache) Unmarshal(filename, code string) (*router.GeoIP, error) { case errFailedToReadBytes, errFailedToReadExpectedLenBytes, errInvalidGeodataFile, errInvalidGeodataVarintLength: newError("failed to decode geoip file: ", filename, ", fallback to the original ReadFile method") - geoipBytes, err = ioutil.ReadFile(asset) + geoipBytes, err = os.ReadFile(asset) if err != nil { return nil, err } @@ -118,7 +118,7 @@ func (g GeoSiteCache) Unmarshal(filename, code string) (*router.GeoSite, error) case errFailedToReadBytes, errFailedToReadExpectedLenBytes, errInvalidGeodataFile, errInvalidGeodataVarintLength: newError("failed to decode geoip file: ", filename, ", fallback to the original ReadFile method") - geositeBytes, err = ioutil.ReadFile(asset) + geositeBytes, err = os.ReadFile(asset) if err != nil { return nil, err } diff --git a/infra/control/certchainhash.go b/infra/control/certchainhash.go index 1726ecc2841..b195455ad74 100644 --- a/infra/control/certchainhash.go +++ b/infra/control/certchainhash.go @@ -3,7 +3,7 @@ package control import ( "flag" "fmt" - "io/ioutil" + "os" v2tls "github.com/v2fly/v2ray-core/v4/transport/internet/tls" ) @@ -30,7 +30,7 @@ func (c CertificateChainHashCommand) Execute(args []string) error { if err := fs.Parse(args); err != nil { return err } - certContent, err := ioutil.ReadFile(*cert) + certContent, err := os.ReadFile(*cert) if err != nil { return err } diff --git a/infra/control/config.go b/infra/control/config.go index 6a1f9b50d0f..395bda8fcaf 100644 --- a/infra/control/config.go +++ b/infra/control/config.go @@ -3,7 +3,6 @@ package control import ( "bytes" "io" - "io/ioutil" "os" "strings" @@ -73,10 +72,10 @@ func (c *ConfigCommand) LoadArg(arg string) (out io.Reader, err error) { data, err = FetchHTTPContent(arg) case arg == "stdin:": - data, err = ioutil.ReadAll(os.Stdin) + data, err = io.ReadAll(os.Stdin) default: - data, err = ioutil.ReadFile(arg) + data, err = os.ReadFile(arg) } if err != nil { diff --git a/infra/vprotogen/main.go b/infra/vprotogen/main.go index 08ea8569092..04437b5c0c3 100644 --- a/infra/vprotogen/main.go +++ b/infra/vprotogen/main.go @@ -4,7 +4,6 @@ import ( "fmt" "go/build" "io" - "io/ioutil" "net/http" "os" "os/exec" @@ -46,7 +45,7 @@ func GetRuntimeEnv(key string) (string, error) { } var data []byte var runtimeEnv string - data, readErr := ioutil.ReadFile(file) + data, readErr := os.ReadFile(file) if readErr != nil { return "", readErr } diff --git a/main/confloader/external/external.go b/main/confloader/external/external.go index 7e543b1e276..246a32e2d26 100644 --- a/main/confloader/external/external.go +++ b/main/confloader/external/external.go @@ -5,7 +5,6 @@ package external import ( "bytes" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -24,10 +23,10 @@ func ConfigLoader(arg string) (out io.Reader, err error) { data, err = FetchHTTPContent(arg) case arg == "stdin:": - data, err = ioutil.ReadAll(os.Stdin) + data, err = io.ReadAll(os.Stdin) default: - data, err = ioutil.ReadFile(arg) + data, err = os.ReadFile(arg) } if err != nil { diff --git a/main/main.go b/main/main.go index c2ff3d3fc62..b33827996e0 100644 --- a/main/main.go +++ b/main/main.go @@ -5,7 +5,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" "os" "os/signal" @@ -54,7 +53,7 @@ func dirExists(file string) bool { } func readConfDir(dirPath string) { - confs, err := ioutil.ReadDir(dirPath) + confs, err := os.ReadDir(dirPath) if err != nil { log.Fatalln(err) } diff --git a/testing/scenarios/common.go b/testing/scenarios/common.go index 66bf9e37fab..af3f40dc205 100644 --- a/testing/scenarios/common.go +++ b/testing/scenarios/common.go @@ -5,7 +5,7 @@ import ( "crypto/rand" "fmt" "io" - "io/ioutil" + "os" "os/exec" "path/filepath" "runtime" @@ -102,7 +102,7 @@ func genTestBinaryPath() { testBinaryPathGen.Do(func() { var tempDir string common.Must(retry.Timed(5, 100).On(func() error { - dir, err := ioutil.TempDir("", "v2ray") + dir, err := os.MkdirTemp("", "v2ray") if err != nil { return err } diff --git a/testing/scenarios/feature_test.go b/testing/scenarios/feature_test.go index 2e49df3ebd7..5e822bd3deb 100644 --- a/testing/scenarios/feature_test.go +++ b/testing/scenarios/feature_test.go @@ -2,7 +2,7 @@ package scenarios import ( "context" - "io/ioutil" + "io" "net/http" "net/url" "testing" @@ -643,7 +643,7 @@ func TestDomainSniffing(t *testing.T) { if resp.StatusCode != 200 { t.Error("unexpected status code: ", resp.StatusCode) } - common.Must(resp.Write(ioutil.Discard)) + common.Must(resp.Write(io.Discard)) } } diff --git a/testing/scenarios/http_test.go b/testing/scenarios/http_test.go index 99af84210bc..d2f4f3bb0ad 100644 --- a/testing/scenarios/http_test.go +++ b/testing/scenarios/http_test.go @@ -5,7 +5,6 @@ import ( "context" "crypto/rand" "io" - "io/ioutil" "net/http" "net/url" "testing" @@ -75,7 +74,7 @@ func TestHttpConformance(t *testing.T) { t.Fatal("status: ", resp.StatusCode) } - content, err := ioutil.ReadAll(resp.Body) + content, err := io.ReadAll(resp.Body) common.Must(err) if string(content) != "Home" { t.Fatal("body: ", string(content)) @@ -271,7 +270,7 @@ func TestHttpPost(t *testing.T) { t.Fatal("status: ", resp.StatusCode) } - content, err := ioutil.ReadAll(resp.Body) + content, err := io.ReadAll(resp.Body) common.Must(err) if r := cmp.Diff(content, xor(payload)); r != "" { t.Fatal(r) @@ -368,7 +367,7 @@ func TestHttpBasicAuth(t *testing.T) { t.Fatal("status: ", resp.StatusCode) } - content, err := ioutil.ReadAll(resp.Body) + content, err := io.ReadAll(resp.Body) common.Must(err) if string(content) != "Home" { t.Fatal("body: ", string(content))