From f27813f219d03a91adcba075a4f38307b47e9269 Mon Sep 17 00:00:00 2001 From: guangwu Date: Thu, 19 Oct 2023 19:58:31 +0800 Subject: [PATCH] chore: remove refs to deprecated io/ioutil (#459) thanks @testwill --- client/client.go | 4 ++-- main.go | 3 +-- server/server_listen.go | 5 ++--- share/cio/stdio.go | 3 +-- share/settings/users.go | 4 ++-- share/tunnel/tunnel.go | 4 ++-- test/bench/main.go | 3 +-- test/e2e/cert_utils_test.go | 15 +++++++-------- test/e2e/setup_test.go | 6 +++--- 9 files changed, 21 insertions(+), 26 deletions(-) diff --git a/client/client.go b/client/client.go index c96edde6..49b62e65 100644 --- a/client/client.go +++ b/client/client.go @@ -8,10 +8,10 @@ import ( "encoding/base64" "errors" "fmt" - "io/ioutil" "net" "net/http" "net/url" + "os" "regexp" "strings" "time" @@ -118,7 +118,7 @@ func NewClient(c *Config) (*Client, error) { tc.InsecureSkipVerify = true } else if c.TLS.CA != "" { rootCAs := x509.NewCertPool() - if b, err := ioutil.ReadFile(c.TLS.CA); err != nil { + if b, err := os.ReadFile(c.TLS.CA); err != nil { return nil, fmt.Errorf("Failed to load file: %s", c.TLS.CA) } else if ok := rootCAs.AppendCertsFromPEM(b); !ok { return nil, fmt.Errorf("Failed to decode PEM: %s", c.TLS.CA) diff --git a/main.go b/main.go index f78ee156..525a2c7d 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "io/ioutil" "log" "net/http" "os" @@ -89,7 +88,7 @@ var commonHelp = ` func generatePidFile() { pid := []byte(strconv.Itoa(os.Getpid())) - if err := ioutil.WriteFile("chisel.pid", pid, 0644); err != nil { + if err := os.WriteFile("chisel.pid", pid, 0644); err != nil { log.Fatal(err) } } diff --git a/server/server_listen.go b/server/server_listen.go index a7dcfe84..f6eb1ffa 100644 --- a/server/server_listen.go +++ b/server/server_listen.go @@ -4,7 +4,6 @@ import ( "crypto/tls" "crypto/x509" "errors" - "io/ioutil" "net" "os" "os/user" @@ -116,7 +115,7 @@ func addCA(ca string, c *tls.Config) error { clientCAPool := x509.NewCertPool() if fileInfo.IsDir() { //this is a directory holding CA bundle files - files, err := ioutil.ReadDir(ca) + files, err := os.ReadDir(ca) if err != nil { return err } @@ -140,7 +139,7 @@ func addCA(ca string, c *tls.Config) error { } func addPEMFile(path string, pool *x509.CertPool) error { - content, err := ioutil.ReadFile(path) + content, err := os.ReadFile(path) if err != nil { return err } diff --git a/share/cio/stdio.go b/share/cio/stdio.go index 24798def..16327989 100644 --- a/share/cio/stdio.go +++ b/share/cio/stdio.go @@ -2,7 +2,6 @@ package cio import ( "io" - "io/ioutil" "os" ) @@ -11,6 +10,6 @@ var Stdio = &struct { io.ReadCloser io.Writer }{ - ioutil.NopCloser(os.Stdin), + io.NopCloser(os.Stdin), os.Stdout, } diff --git a/share/settings/users.go b/share/settings/users.go index ea57f280..a6f0a093 100644 --- a/share/settings/users.go +++ b/share/settings/users.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "os" "regexp" "sync" @@ -125,7 +125,7 @@ func (u *UserIndex) loadUserIndex() error { if u.configFile == "" { return errors.New("configuration file not set") } - b, err := ioutil.ReadFile(u.configFile) + b, err := os.ReadFile(u.configFile) if err != nil { return fmt.Errorf("Failed to read auth file: %s, error: %s", u.configFile, err) } diff --git a/share/tunnel/tunnel.go b/share/tunnel/tunnel.go index 1d80bd5b..7493a5ac 100644 --- a/share/tunnel/tunnel.go +++ b/share/tunnel/tunnel.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "errors" - "io/ioutil" + "io" "log" "os" "sync" @@ -57,7 +57,7 @@ func New(c Config) *Tunnel { //setup socks server (not listening on any port!) extra := "" if c.Socks { - sl := log.New(ioutil.Discard, "", 0) + sl := log.New(io.Discard, "", 0) if t.Logger.Debug { sl = log.New(os.Stdout, "[socks]", log.Ldate|log.Ltime) } diff --git a/test/bench/main.go b/test/bench/main.go index 091f2d5d..f47b10c1 100644 --- a/test/bench/main.go +++ b/test/bench/main.go @@ -17,7 +17,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -86,7 +85,7 @@ func testTunnel(port string, size int) { fatal(err) } - n, err := io.Copy(ioutil.Discard, resp.Body) + n, err := io.Copy(io.Discard, resp.Body) if err != nil { fatal(err) } diff --git a/test/e2e/cert_utils_test.go b/test/e2e/cert_utils_test.go index 379e282b..f71a13dd 100644 --- a/test/e2e/cert_utils_test.go +++ b/test/e2e/cert_utils_test.go @@ -11,7 +11,6 @@ import ( "crypto/x509/pkix" "encoding/pem" "fmt" - "io/ioutil" "math/big" "net" "os" @@ -53,7 +52,7 @@ func newTestTLSConfig() (*tlsConfig, error) { return nil, err } - tlsConfig.tmpDir, err = ioutil.TempDir("", "") + tlsConfig.tmpDir, err = os.MkdirTemp("", "") if err != nil { return nil, err } @@ -63,7 +62,7 @@ func newTestTLSConfig() (*tlsConfig, error) { return nil, err } pathServerCACrt := path.Join(dirServerCA, "client.crt") - if err := ioutil.WriteFile(pathServerCACrt, clientCertPEM, 0666); err != nil { + if err := os.WriteFile(pathServerCACrt, clientCertPEM, 0666); err != nil { return nil, err } @@ -72,7 +71,7 @@ func newTestTLSConfig() (*tlsConfig, error) { return nil, err } pathClientCACrt := path.Join(dirClientCA, "server.crt") - if err := ioutil.WriteFile(pathClientCACrt, serverCertPEM, 0666); err != nil { + if err := os.WriteFile(pathClientCACrt, serverCertPEM, 0666); err != nil { return nil, err } @@ -81,11 +80,11 @@ func newTestTLSConfig() (*tlsConfig, error) { return nil, err } pathServerCrtCrt := path.Join(dirServerCrt, "server.crt") - if err := ioutil.WriteFile(pathServerCrtCrt, serverCertPEM, 0666); err != nil { + if err := os.WriteFile(pathServerCrtCrt, serverCertPEM, 0666); err != nil { return nil, err } pathServerCrtKey := path.Join(dirServerCrt, "server.key") - if err := ioutil.WriteFile(pathServerCrtKey, serverKeyPEM, 0666); err != nil { + if err := os.WriteFile(pathServerCrtKey, serverKeyPEM, 0666); err != nil { return nil, err } @@ -94,11 +93,11 @@ func newTestTLSConfig() (*tlsConfig, error) { return nil, err } pathClientCrtCrt := path.Join(dirClientCrt, "client.crt") - if err := ioutil.WriteFile(pathClientCrtCrt, clientCertPEM, 0666); err != nil { + if err := os.WriteFile(pathClientCrtCrt, clientCertPEM, 0666); err != nil { return nil, err } pathClientCrtKey := path.Join(dirClientCrt, "client.key") - if err := ioutil.WriteFile(pathClientCrtKey, clientKeyPEM, 0666); err != nil { + if err := os.WriteFile(pathClientCrtKey, clientKeyPEM, 0666); err != nil { return nil, err } diff --git a/test/e2e/setup_test.go b/test/e2e/setup_test.go index c6228757..c1611bfb 100644 --- a/test/e2e/setup_test.go +++ b/test/e2e/setup_test.go @@ -2,7 +2,7 @@ package e2e_test import ( "context" - "io/ioutil" + "io" "log" "net" "net/http" @@ -36,7 +36,7 @@ func (tl *testLayout) setup(t *testing.T) (server *chserver.Server, client *chcl fileAddr := "127.0.0.1:" + filePort f := http.Server{ Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - b, _ := ioutil.ReadAll(r.Body) + b, _ := io.ReadAll(r.Body) w.Write(append(b, '!')) }), } @@ -133,7 +133,7 @@ func post(url, body string) (string, error) { if err != nil { return "", err } - b, err := ioutil.ReadAll(resp.Body) + b, err := io.ReadAll(resp.Body) if err != nil { return "", err }